Skip to content

Instantly share code, notes, and snippets.

@bencoveney
Last active August 29, 2015 14:04
Show Gist options
  • Save bencoveney/3bb44e973ffd29ea1775 to your computer and use it in GitHub Desktop.
Save bencoveney/3bb44e973ffd29ea1775 to your computer and use it in GitHub Desktop.
Highly divisible triangular number
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EulerProject
{
class Program
{
static void Main(string[] args)
{
// Constant Values
const int factorsToFind = 500;
int lastNumberAdded = 2;
int runningTotal = 3;
int mostFactorsFound = 0;
while (mostFactorsFound <= factorsToFind)
{
// Find the number of factors
int factors = getNumberOfFactors(runningTotal);
// If the number of factors is the new highest
if (factors > mostFactorsFound)
{
Console.WriteLine("{0} found to have {1} factors", runningTotal, factors);
mostFactorsFound = factors;
}
Console.WriteLine(lastNumberAdded);
Console.CursorTop--;
// Increment the iterator
lastNumberAdded++;
// Add the iterator to the running total
runningTotal += lastNumberAdded;
}
Console.WriteLine(runningTotal); // 76576500!
Console.ReadLine();
}
private static int getNumberOfFactors(int number)
{
int numberOfFactors = 0;
for (int i = 1; i <= (number / 2); i++)
{
if (number % i == 0) numberOfFactors++;
}
return numberOfFactors;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment