April 17th, 2009
This proble is similiar to Projet Euler 76.
The Python program:
'''
a_1*x_1+a_2*x_2+...+a_V*V=N
A=[a_1,a_2,...,a_V]
A=[2,3,5,7,11,...]
'''
try:
import psyco
psyco.full()
except ImportError:
pass
from euler import allprime
def P(V,N):
if N< =3 or V==[2]:
return 1
elif N<V[-1]:
return P(V[:-1],N)
elif V==[2,3]:
return N/6+1
else:
return sum([P(V[:-1],N-V[-1]*x) for x in range(N/V[-1]+1)])
V,N=sorted(allprime(100).keys()),1
while P(V,N)< 5e3:
N=N+1
print N
Posted in Uncategorized | 1 Comment »
April 17th, 2009
The problem is equivalent to obtain the number of the integer solutions of the diophantine equation a_1*x_1+a_2*x_2+…+a_V*V=N.
We define the number of the integer solutions of this diophantine equation as P(V,N).
It can be found that P(V,N) can be expressed as a recursive function as the following:
P(V,N)=P(V-1,N)+P(V-1,N-V)+…+P(V-1,N-[N/V]*V).
So This problem can be easily solved by defining this recursive function.
'''
The number of the integer solutions of a_1*x_1+a_2*x_2+...+a_V*V=N
'''
try:
import psyco
psyco.full()
except ImportError:
pass
def P(V,N):
if N< =1 or V==2:
return N/2+1
elif V==1:
return 1
elif N<V:
return P(N-1,N)+1
else:
return sum([P(V-1,N-V*x) for x in xrange(N/V+1)])
print P(99,100)
Posted in Uncategorized | 1 Comment »
February 20th, 2009
It is well known that if the square root of a natural number is not an integer, then it is irrational. The decimal expansion of such square roots is infinite without any repeating pattern at all.
The square root of two is 1.41421356237309504880…, and the digital sum of the first one hundred decimal digits is 475.
For the first one hundred natural numbers, find the total of the digital sums of the first one hundred decimal digits for all the irrational square roots.
import math
from sympy import sqrt,evalf
a=filter(lambda x:math.sqrt(x)>int(math.sqrt(x)),range(1,100))
for i in a:
k=list(str(sqrt(i).evalf(105)))
k.remove('.')
s=s+sum(map(int,k[:100]))
print s
Posted in Uncategorized | No Comments »
February 2nd, 2009
This question is from ProjectEuler 04.
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.
Answer:
def palindromic(n):
s=list(str(n))
if s==s[::-1]:
return 1
else:
return 0
m,n=999,99
p=[]
for i in range(m,n,-1):
for j in range(m,i-1,-1):
if palindromic(i*j):
p.append(i*j)
break
print max(p)
Tags: palindrome, projecteuler, python
Posted in Math | No Comments »
January 8th, 2009
from math import sqrt
def isprime(n):
if n==1:
return 0
elif n==2 or n==3:
return 1
elif n%2==0 or n%3==0 or sqrt(n)==int(sqrt(n)):
return 0
a=1
N=int(sqrt(n))+2
for x in range(3,N,2):
if n%x==0:
a=0
break
return a
def factor(n):
result=[]
while n%2==0:
result.append(2)
n=n/2
while n!=1:
if isprime(n):
result.append(n)
break
j=3
for i in range(j,int(sqrt(n))+1,2):
if n%i==0:
result.append(i)
n=n/i
j=i
break
return result
Tags: factor, python
Posted in Math | No Comments »