E-mail
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;}
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);}
public int GetFactorCount(int numberToCheck){// we know 1 is a factor and the numberToCheckint factorCount = 2;// start from 2 as we know 1 is a factor, and less than as numberToCheck is a factorfor (int i = 2; i < numberToCheck; i++){ if (numberToCheck % i == 0) factorCount++;}return factorCount;}
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;}
Remember Me
a@href@title, strike