Friday, September 13, 2013

Problem 7 - 1001st Prime

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?

_________________________________________________________________________________

import math

def prime(n):
 if n==1:
  return False
 else:
  i=2
  k = 1 + math.floor(math.sqrt(n))
  while i<k and n%i != 0:
   i = i+1
  if i == k:
   return True
  else:
   return False

j=2
i=1
while i<10002:
 if prime(j) == True:
  i=i+1
 j=j+1


print j-1

Problems 5, 6, 8, 11

Did not use programming. Relied on basic math/observation.

Problem 10 - Summation of Primes

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
_________________________________________________________________________________

import math

def prime(n):
 i=2
 k = 1+math.floor(math.sqrt(n))
 while i<k and n%i != 0:
    i = i+1
 if i == k:
  return True
 else:
  return False

i=2
j=0
while i<2000000:
 if prime(i)==True:
  j=j+i
 i=i+1

print j


Problem 4 - Largest Palindrome Product

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 ×99.
Find the largest palindrome made from the product of two 3-digit numbers.
_________________________________________________________________________________

def palin(n):
 if n == reverse(n):
  return True
 else:
  return False

def reverse(n):
 j=0
 i=10
 while 10*n>i:
  if n%i > i/10:
   j=10*j + first(n-(n/i)*i)
  else:
   j =10*j
  i=10*i
 return j

def first(n):
 j=1
 i=1
 while n > i:
  j=n/i
  i = 10*i
 return j 

k=1
for i in range(900,999):
 for j in range(900,999):
  if palin(i*j) == True:
    print i,j,i*j

Problem 3 - Largest Prime Factor

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
_________________________________________________________________________________

j=600851475143 
i=1
while j>1:
 if j%i==0:
  j=j/i
  print i
 i=i+1

Problem 2 - Even Fibonacci Numbers

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
_________________________________________________________________________________

fib=[1,2]
i=1
j=2
l=0
while i<4000000 and j<4000000:
 i=i+j
 j=i+j
 fib.append(i)
 fib.append(j)
 if i%2 == 0 and i<4000000:
  l=l+i
 if j%2 == 0 and j<4000000:
  l=l+j

print l+2

Problem 1 - Multiples of 2 and 3

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
_________________________________________________________________________________

j = 0
for i in range(1,1000):
 if i%3 == 0 or i%5 == 0:
  j = j + i

print j