社区里有不少project euler的题解,自己也写了些,主要是为了体验ruby的简洁(不过Mathematica好像更简单)
问题1: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.
(找出1000以内所有能被3或5整除的自然数之和)
s=0
1.upto 999 do |i|
if((i%3==0)||(i%5==0))
puts i
s+=i
end
end
puts s
问题2: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, ...
Find the sum of all the even-valued terms in the sequence which do not exceed four million.
(找出4百万以内Fibonacci数列中偶数之和)
sum=2
a=1
b=2
loop do
c=a+b
a=b
b=c
if(c%2==0)
sum+=c
puts "sum="+sum.to_s
end
break if(c>=4000000)
end
puts "terminal="+sum.to_s
问题3:The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
(找出600851475143的最大质因数)
ps:这问题好像是为了下面某些题做铺垫吧,如果只是用ruby内置的质数生成器也颇费时间(建议还是用sieve吧)
require 'mathn'
sum_prime=Prime.new
sum_prime.each{|prime| break if prime>=2000000;print " "+prime.to_s }
问题4: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.
(在所有两个三位数的积中找出最大的回文数)
r=0
t=0
100.upto(999) do |i|
100.upto(999) do |j|
is=true
s=(i*j).to_s
len=s.size
0.upto(len/2-1) do |k|
if s[k,1]!=s[-k-1,1]
is=false
break
end
end
if is==true
t=i*j
end
if t>r
r=t
end
end
end
puts r
问题5:2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
(找出能被1到20整除的最小正整数)
def p5(n)
r=1
2.upto(n-1) do |i|
if r<i+1
r=r*(i+1)/gcd(i+1,r)
else
r=r*(i+1)/gcd(r,i+1)
end
end
puts r
end
def gcd(a,b)
if b==0
a
else
gcd(b,a%b)
end
end
p5(20)
问题6:Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
(找出100以内所有自然数的平方和与和的平方的差值)
sum=0
count=0
1.upto(9) do |i|
(i+1).upto(10) do |j|
sum+=i*j
end
end
puts 2*sum
问题7: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 10001st prime number?
(找出第10001个素数)
public static void main(String[] args) {
int count = 0;
int n = 2;
while(count!=10001){
if(MathUtil.isPrime(n))
count++;
n++;
}
System.out.println(n-1);
}
}
问题8:Find the greatest product of five consecutive digits in the 1000-digit number.
(在给出的一千个数字中找出5个连续数字使他们的积最大)
ps:方法2比方法1速度更快
s="73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450"
a=s.split(//)
a.delete("\n")
length=a.size
0.upto(length) do |i|
a[i]=a[i].to_i
end
sum=1
def one(sum,a,length)
0.upto(length-5) do |i|
i.upto(i+4) do |k|
if a[k]==0
i=k+1
break
else
tmp=1
i.upto(i+4) do |j|
tmp*=a[j]
end
sum=tmp if tmp>sum
end
end #跳过包含0的序列
end
puts sum
end
def two(sum,a,length)
0.upto(length-5) do |i|
tmp=1
i.upto(i+4) do |j|
tmp*=a[j]
end
sum=tmp if tmp>sum
end
puts sum
end
one(sum,a,length)
two(sum,a,length)
require 'benchmark'
Benchmark.bmbm(7) do |t|
t.report("1"){one(sum,a,length)}
t.report("2"){two(sum,a,length)}
end
问题9:There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
(找出满足和为1000的勾股数,并计算他们的积)
ps:暴力搜索
public class Problem9 {
public static void main(String[] args) {
for(int a = 1; a<1000; a++)
for(int b = 1; b<1000; b++){
int t = a+b;
if(t>500 && t<1000){
if(Math.pow(a, 2)+Math.pow(b, 2)==Math.pow(1000-t, 2))
System.out.println(a+" "+b);
}
}
}
}
问题10:The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
(找出2百万以内所有素数之和)
ps:用优化的筛法效率更快
def array_sieve(n)
a=Array.new(n,0)
sum=2
r=((n-1)**0.5).floor
i=3
while(i<=r)
if(a.at(i)==0)
sum+=i
j=i*i
while(j<=n-1)
a[j]=1
j+=2*i
end
end
i+=2
end
if r%2==0
i=r+1
else
i=r+2
end
while(i<n)
if(a.at(i)==0)
sum+=i
end
i+=2
end
puts sum
end