Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Wednesday, December 29, 2010
( Euler )

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Finding ‘Triangle numbers’ can be done by either:

[TestMethod]
public void Get6thTriangenumber()
{
int result = GetTriangleNumber(6);
Assert.AreEqual(21, result);
}

[TestMethod]
public void Get7thTriangenumber()
{
int result = GetTriangleNumber(7);
Assert.AreEqual(28, result);
}

 

public int GetTriangleNumber(int nThNumberInTriangleSequence)
{
int sumOfNumbers = 0;
for (int i = 1; i <= nThNumberInTriangleSequence; i++)
sumOfNumbers += i;

return sumOfNumbers;
}

or we could use the summation formula (faster).. this sums up n numbers in a sequence eg 1,2,3,4,5,6 (see first test above)

public int GetTriangleNumber(int nThNumberInTriangleSequence)
{
int sumOfNumbers = nThNumberInTriangleSequence * (nThNumberInTriangleSequence + 1) / 2;
return sumOfNumbers+1;
}

Factorising – What integers will it divide by with no remainder?

Let us list the factors of the first seven triangle numbers:

1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28

We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?

http://www.mathsisfun.com/numbers/factors-all-tool.html

[TestMethod]
public void GetFactorCount36() // an exact square
{
int result = GetFactorCount(36);
Assert.AreEqual(9, result);
}

[TestMethod]
public void GetFactorCount45()
{
int result = GetFactorCount(45);
Assert.AreEqual(6, result);
}

[TestMethod]
public void GetFactorCount55()
{
int result = GetFactorCount(55);
Assert.AreEqual(4, result);
}

[TestMethod]
public void GetFactorCount30() // this isn't triangular
{
int result = GetFactorCount(30);
Assert.AreEqual(8, result);
}

Really Simple Factorisation to Get How Many Factors
public int GetFactorCount(int numberToCheck)
{
// we know 1 is a factor and the numberToCheck
int factorCount = 2;
// start from 2 as we know 1 is a factor, and less than as numberToCheck is a factor
for (int i = 2; i < numberToCheck; i++)
{
if (numberToCheck % i == 0)
factorCount++;
}
return factorCount;
}

However this isn’t fast enough.
Factorisation using Square Root

find square root of the number eg for 30 it is 5.477.  Then find all factors below that sqrt, then double.  if number is an exact square then add one.

eg 30

1*30 = 30
2*15 = 30
3*10 = 30
5*6 = 30
sqrt of 30 = 5.477
6*5 = 30
10*3 = 30
15*2 = 30
30*1 = 30

 
public int GetFactorCount(int numberToCheck)
{
int factorCount = 0;
int sqrt = (int)Math.Ceiling(Math.Sqrt(numberToCheck));

// Start from 1 as we want our method to also work when numberToCheck is 0 or 1.
for (int i = 1; i < sqrt; i++)
{
if (numberToCheck % i == 0)
factorCount += 2; // We found a pair of factors.
}

// Check if our number is an exact square.
if (sqrt * sqrt == numberToCheck)
factorCount++;

return factorCount;
}

 

All comments require the approval of the site owner before being displayed.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview