Skip to content

Instantly share code, notes, and snippets.

@jakedobkin
Created December 2, 2011 18:14
Show Gist options
  • Select an option

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

Select an option

Save jakedobkin/1424240 to your computer and use it in GitHub Desktop.
Euler 29 a
#!/usr/local/bin/node
// prime sieve from problem 9 (we only need primes through 11, since 11^2=121)
n=100;
myPrimes = new Array();
for (i=2;i<=n;i++)
{
myPrimes[i]=true;
}
for (i=2;i<=n;i++)
{
if (myPrimes[i])
{
for (j=2*i;j<=n;j+=i)
{
myPrimes[j]=false;
}
}
}
// for every combination a^b, we'll store a unique fingerprint as a string
storage = new Array();
count = 0;
for(a=2;a<=5;a++)
{
for(b=2;b<=5;b++)
{
fingerprint = "";
for (j=2;j<=a;j++)
{
if(myPrimes[j])
{
if (a%j == 0)
{
k=1;
power = 1;
while(Math.pow(j,k)<=a)
{
power = k*b;
k++
}
fingerprint = fingerprint.concat(j+"^"+power+");
}
}
}
// console.log(a + "^" + b + " has fingerprint: " + fingerprint);
count +=1;
storage[count]=fingerprint;
}
}
// sort and de-duplicate storage array, print storage array
storage = storage.sort();
console.log(storage);
console.log("total fingerprints: " + (storage.length-1));
count=0;
for (l=0;l<=storage.length-1;l++)
{
if(storage[l]==storage[l+1] && storage[l]!=undefined)
{
count+=1;
console.log("duplicate: " + storage[l]);
}
}
console.log("duplicate fingerprints: " + count);
console.log("unique fingerprints: " + ((storage.length-1)-count));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment