Saturday, September 14, 2013

Problem 18 - Maximum Path Sum I

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
3
7 4
4 6
8 5 9 3
That is, 3 + 7 + 4 + 9 = 23.
Find the maximum total from top to bottom of the triangle below:
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
_________________________________________________________________________________

def level(n):
 i=1
 while i*(i+1)<2*(n+1):
  i=i+1
 return i

def neigh1(n):
 return n+level(n)

def neigh2(n):
 return n+level(n)+1

def suma(li,n):
 if level(n)<level(len(li)-1):
  ans=li[n]+max(suma(li,neigh1(n)),suma(li,neigh2(n)))
 else:
  ans=li[n]
 return ans

l=[75,95,64,17,47,82,18,35,87,10,20,04,82,47,65,19,01,23,75,03,34,88,02,77,73,07,63,67,99,65,04,28,06,16,70,92,41,41,26,56,83,40,80,70,33,41,48,72,33,47,32,37,16,94,29,53,71,44,65,25,43,91,52,97,51,14,70,11,33,28,77,73,17,78,39,68,17,57,91,71,52,38,17,14,91,43,58,50,27,29,48,63,66,04,68,89,53,67,30,73,16,69,87,40,31,04,62,98,27,23,9,70,98,73,93,38,53,60,04,23]

print suma(l,0)

Problem 16 - Power Digit Sum

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 21000?
_________________________________________________________________________________

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

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

print sumd(2**1000)

Problem 15 (Lattice Path) and Problem 17 (Number Letter Counts)

Use simple combinatorics. No programming required or used.

Problem 14 - Longest Collatz sequence

The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even)
n → 3n + 1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.
Which starting number, under one million, produces the longest chain?
NOTE: Once the chain starts the terms are allowed to go above one million.
_________________________________________________________________________________

def coll(n):
 i=1
 while n>1:
  if n%2==0:
   n=n/2
  else:
    n=1+3*n
  i=i+1
 return i

i=1
j=0
while i<1000000:
 if coll(i)>coll(j):
  j=i
 i=i+1
print j

A small library of basic prime number algorithms


  • prime(n) tests if a number is prime or not and returns True/False.
  • primeseiveupto(n) returns a list of all prime numbers upto n.
  • numprimelessthan(n) returns the number of primes less than n.
  • primeseiventh(n) returns a list of the first n prime numbers.
  • nthprime(n) returns the nth prime number.
  • primefac(n) lists the prime factors of n in increasing order with repetition, as a list.
  • primefactors(n) return a list of pairs, the first coordinate being the prime factor, and the second coordinate being the corresponding power.
  • numfac(n) returns the number of factors of n, while numpfac(n) returns the number of prime factors of n.
________________________________________________________________________


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

def primeseiveupto(n): 
 seive=[2]
 i=3
 while i<n:
  k=0
  for j in seive:
   if i%j==0:
    k=1
    break
  if k==0:
   seive=seive+[i]
  i=i+1
 return seive

def numprimelessthan(n):
 return len(primeseiveupto(n))

def primeseiventh(n):
 seive=[2]
 i=3
 count=1
 while count<n:
  k=0
  for j in seive:
   if i%j==0:
    k=1
    break
  if k==0:
   seive=seive+[i]
   count=count+1
  i=i+1
 return seive
def nthprime(n):
 seive=[2]
 i=3
 count=1
 while count<n:
  k=0
  for j in seive:
   if i%j==0:
    k=1
    break
  if k==0:
   seive=seive+[i]
   count=count+1
  i=i+1
 return seive[-1]


#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&



def primefaci(n,i):
 k = 1 + math.floor(math.sqrt(n))
 if i>k-1:
  return [n]
 else:
  if n==1:
   return []
  else:
   while i<k and n%i != 0:
    i=i+1
   if i==k and n%k!=0:
    return [n]
   else:
    return [i]+primefaci(n/i,i)

def primefac(n):
 a=primefaci(n,2)
 return a

def listy(l):
 j=l[0]
 a=[]
 counti=0
 for i in l:
  if j==i:
   counti=counti+1
  else:
   a.append((j,counti))
   j=i
   counti=1
 a.append((j,counti))
 return a

def primefactors(n):
 return listy(primefac(n))

#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

def numfac(n):
 j=1
 for i in primefactors(n):
  j=j*(i[1]+1)
 return j

def numpfac(n):
 return len(primefactors(n))



Friday, September 13, 2013

Problem 12 - Highly Divisible Triangular Number

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:
 1: 1
 3: 1,3
 6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred divisors?
_________________________________________________________________________________

def trg(n):
 return n*(n+1)/2

def fac(n):
 i=2
 j=1
 while i<= n/2:
  if n%i==0:
   j=j+1
  i=i+1
 return j+1
   
def fact(n):
 if n%2==0:
  return fac(n/2)*fac(n+1)
 else:
  return fac(n)*fac((n+1)/2)

i=1
while fact(i)<500:
 i=i+1

print i, trg(i), fact(i)


Problem 9 - Special Pythagorean Triplet

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
_________________________________________________________________________________

for a in range(1,334):
 for b in range(a,1000):
  if a**2 + b**2 == (1000-a-b)**2:
   print a,b,1000-a-b,a*b*(1000-a-b)