Skip to content

Instantly share code, notes, and snippets.

@jakedobkin
Created November 25, 2011 19:14
Show Gist options
  • Select an option

  • Save jakedobkin/1394238 to your computer and use it in GitHub Desktop.

Select an option

Save jakedobkin/1394238 to your computer and use it in GitHub Desktop.
Euler 21
#!/usr/local/bin/node
// declare an array to hold the sums of factors for all numbers below 10,000
sums = new Array();
// fill array with sums of all "proper divisors" at each i
for (i=1;i<=10000;i++)
{
sums[i]=0;
for (j=1;j<i;j++)
{
if (i%j==0)
{
sums[i]+=j;
}
}
}
// check each i to see if it has an amicable partner
// you need to make sure the partner is larger than k (otherwise you get bad pairs)
amicablesum=0;
for (k=1;k<=10000;k++)
{
partner = sums[k];
if (partner > k)
{
if (sums[partner]==k)
{
console.log(k + " and " + partner + " form an amicable pair");
amicablesum+=(k + partner);
}
}
}
console.log("sum of all pairs is " + amicablesum);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment