Skip to content

Instantly share code, notes, and snippets.

@DanBrink91
Created January 14, 2014 23:35
Show Gist options
  • Select an option

  • Save DanBrink91/8428104 to your computer and use it in GitHub Desktop.

Select an option

Save DanBrink91/8428104 to your computer and use it in GitHub Desktop.
projecteuler.net problems
print sum([x for x in xrange(1000) if x%3==0 or x%5==0])
def primes(limit):
is_prime = [False] * 2 + [True] * (limit - 1)
for n in xrange(limit+1):
if is_prime[n]:
yield n
for i in xrange(n*2, limit+1, n):
is_prime[i] = False
print sum(primes(2000000))
fibs = {1: 1, 2:2}
def fib(n):
if n in fibs:
return fibs[n]
fibs[n] = fib(n-1) + fib(n-2)
return fibs[n]
num = 1
total = 0
limit = 4000000
next = 0
while next <= limit:
next = fib(num)
if next %2 ==0:
total += next
num+=1
print total
#include <stdio.h>
int isPrime(long long n)
{
long long d = 2;
while(d < n)
{
if(n%d==0)
{
return 0;
}
d++;
}
return 1;
}
long long largestPrimeFactor(long long n)
{
long long d = 3;
while(d < n)
{
if(n%d==0)
{
if(isPrime(n/d))
{
return n/d;
}
}
d += 2;
}
return 0;
}
int main()
{
printf("%lld \n", largestPrimeFactor(600851475143));
return 0;
}
def is_prime(n):
d = 2
while d < n:
if n%d==0:
return False
d+=1
return True
def largest_prime_factor(n):
potential_factor = 3
while potential_factor < n:
if n%potential_factor==0:
if is_prime(n/potential_factor):
return n/potential_factor
potential_factor += 2
return None
print largest_prime_factor(600851475143)
# Largest palindrome made from product of two 3-digit
def is_palindrome(n):
s = str(n)
if len(s) < 2:
return False
for x in xrange(len(s)/2):
if s[x] != s[-(x+1)]:
return False
return True
pals = []
for n in xrange(100, 999):
for j in xrange(100, 999):
val = n * j
if is_palindrome(val):
pals.append(val)
print max(pals)
# Smallest positive number that is evenly disible by all numbers 1-20
def is_div_20(n):
for x in xrange(2, 20):
if n%x != 0:
return False
return True
n = 2
while is_div_20(n)==False:
n += 2
print n
'''
difference between the sum of the squares of the first
one hundred natural numbers and the square of the sum
'''
print abs(sum([x**2 for x in xrange(1, 101)])-(sum([x for x in xrange(1, 101)])**2))
def is_prime(n):
d = 2
while d < n:
if n%d==0:
return False
d+=1
return True
primes = 2
counter = 3
while primes < 10001:
counter += 2
if is_prime(counter):
primes += 1
print counter
# Largest product of 5 consecute digits
nums = map(lambda x: int(x), list("3167176531330624919225119674426574742355349194934\
96983520312774506326239578318016984801869478851843\
85861560789112949495459501737958331952853208805511\
12540698747158523863050715693290963295227443043557\
66896648950445244523161731856403098711121722383113\
62229893423380308135336276614282806444486645238749\
30358907296290491560440772390713810515859307960866\
70172427121883998797908792274921901699720888093776\
65727333001053367881220235421809751254540594752243\
52584907711670556013604839586446706324415722155397\
53697817977846174064955149290862569321978468622482\
83972241375657056057490261407972968652414535100474\
82166370484403199890008895243450658541227588666881\
16427171479924442928230863465674813919123162824586\
17866458359124566529476545682848912883142607690042\
24219022671055626321111109370544217506941658960408\
07198403850962455444362981230987879927244284909188\
84580156166097919133875499200524063689912560717606\
05886116467109405077541002256983155200055935729725\
71636269561882670428252483600823257530420752963450"))
products = [nums[i]*nums[i+1]*nums[i+2]*nums[i+3]*nums[i+4] for i in xrange(len(nums) - 5)]
print max(products)
def find_triplet():
for a in xrange(1, 1000):
for b in xrange(1, 1000):
for c in xrange(1, 1000):
if a+b+c == 1000:
if a**2 + b**2 == c**2:
return a*b*c
print find_triplet()
# Largest prime factor of...
def prime_factors(n):
factors = []
d =2
while n > 1:
while n%d == 0:
factors.append(d)
n /= d
d += 1
return factors
print max(prime_factors(600851475143))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment