Skip to content

Instantly share code, notes, and snippets.

@jakedobkin
jakedobkin / gist:1424931
Created December 2, 2011 21:35
Euler 30
#!/usr/local/bin/node
/* the question is a little unclear- b/c a naive reading might assume
you only need to check the five digit numbers, since the example given is 4 digits
this would be a special class of numbers called "narcissistic"- but here we're looking
for a larger case- ANY number of digits that, when ^5 and added, give the number, starting
from two (since the problem says don't count 1). harder is to figure out upper limit.
at first, i just let it run to 1MM, hoping that would be enough. it was. then i read
up and found someone had figured out the exact limit with this logic:
assume each digit is a 9: 9^5~~ 60K-- so every digit we add gives us another 60K or so.
@jakedobkin
jakedobkin / gist:1427499
Created December 3, 2011 16:25
Euler 31
#!/usr/local/bin/node
// our basic method here is to start with the largest coin and count down to the smallest
// each time you use up a coin (ie. that loop gets to zero) you're still checking the next
// smallest coin, down to the smallest one. there is probably a way to write this recursively
// instead of copying out the loop 8 times, but i don't know recursive functions yet.
// remove the comments if you want an actual enumeration of each set.
goal = 200;
count = 0;
@jakedobkin
jakedobkin / gist:1430566
Created December 4, 2011 16:14
Euler 32
# first we'll need a set to store results in- that means we won't need to de-dupe
h=set()
total=0;
# now, we'lll make the strings-
# 1 digits x 4 digits will be 4 or 5 digits (9-10 total) and 2 digits x 3 digits will by 4 or 5 digits (9-10 total) so try up to 9999x99
for a in range(1,9999):
for b in range(1,99):
product = str(a)+"x"+str(b)+"x"+str(a*b)
@jakedobkin
jakedobkin / gist:1431792
Created December 5, 2011 00:34
Euler 33
# first we'll need a set to store results in- that means we won't need to de-dupe
h=set()
total=0;
# now, we'lll make the fractions 10 to 99 in numerator and denominator
for a in range(10,100):
for b in range(10,100):
if (a!=b):
numerator = str(a)
denominator = str(b)
@jakedobkin
jakedobkin / gist:1431857
Created December 5, 2011 01:00
Euler 34
#!/usr/local/bin/node
// these are the values of the factorials of each digit from 0-9- from earlier exercise
factorial = [1,1,2,6,24,120,720,5040,40320,362880]
sum = 0;
// you could use advanced math to figure out the upper bound- i just tried up to 10^7
for (a=3;a<=100000;a++)
@jakedobkin
jakedobkin / gist:1434559
Created December 5, 2011 17:53
Euler 35
#!/usr/local/bin/node
// first, prep prime seive up to 1MM
n=1000000;
myPrimes = new Array();
for (i=2;i<=n;i++)
{
myPrimes[i]=true;
}
@jakedobkin
jakedobkin / gist:1435312
Created December 5, 2011 20:58
Euler 36
#!/usr/local/bin/node
function reverse(s){
return s.split("").reverse().join("");
}
function ispali(a)
{
a = a.toString();
for (i=0;i<=a.length-1;i++)
@jakedobkin
jakedobkin / gist:1436576
Created December 6, 2011 03:29
Euler 37
#!/usr/local/bin/node
// first, prep prime seive up to 1MM
n=1000000;
myPrimes = new Array();
for (i=2;i<=n;i++)
{
myPrimes[i]=true;
}
@jakedobkin
jakedobkin / gist:1439174
Created December 6, 2011 17:54
Euler 38 Python
# first we'll need a set to store results in- that means we won't need to de-dupe
h=set()
total=0;
# obviously i would be less than < 5 digits long, since it's at least n = 2 (number cat number*2)
# and b is less than 7- because with a > 2, that would be 10 digits
for a in range(1,9999):
product = ""
for b in range(1,7):
product += str(a*b)
@jakedobkin
jakedobkin / gist:1439641
Created December 6, 2011 19:45
Euler 39 Python
# initialize an array to hold results
l=[0]*1001
# brute force to calculate a,b,c
for a in range(1,1000):
for b in range(1,1000):
c = (a**2 + b**2)**0.5
# verify c is a perfect square and p is < 1000, if it is, increment count
if (a+b+c<=1000 and c-round(c,0)==0):