# Tassandar/Project-Euler

last problems

1 parent 21479b3 commit a5afda08c8292c867eea82fb2b2014b496de1ce0 committed Feb 7, 2012
Showing with 253 additions and 0 deletions.
1. +64 −0 18.rb
2. +27 −0 19.rb
3. +15 −0 20.rb
4. +33 −0 21.rb
5. +24 −0 22.rb
6. +38 −0 23.rb
7. +17 −0 24.rb
8. +35 −0 25.rb
64 18.rb
 @@ -0,0 +1,64 @@ +=begin +Problem 18 + +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 +2 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 + +xNOTE: 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) + +=end +STR = "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" +\$arr = STR.split("\n").map {|item| item = item.split("\s")} +\$sum = 0 +def calc(col,row,temp) + if row >= \$arr.count - 1 + \$sum = temp if temp > \$sum + return + end + left = temp + \$arr[row+1][col].to_i + right = temp + \$arr[row+1][col+1].to_i + calc(col,row+1,left) + calc(col+1,row+1,right) +end +calc(0,0,\$arr[0][0].to_i) +p \$sum + +
27 19.rb
 @@ -0,0 +1,27 @@ +=begin +Problem 19 + +You are given the following information, but you may prefer to do some research for yourself. + + 1 Jan 1900 was a Monday. + Thirty days has September, + April, June and November. + All the rest have thirty-one, + Saving February alone, + Which has twenty-eight, rain or shine. + And on leap years, twenty-nine. + A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. + +How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? + +=end +require "Date" +\$time = Date.new(1901, 1, 1) +\$counter = 0 +while \$time < Date.new(2001, 1, 1) + \$counter +=1 if \$time.wday == 0 + \$time >>=1 +end +p \$counter + +
15 20.rb
 @@ -0,0 +1,15 @@ +=begin +Problem 20 +n! means n × (n − 1) × ... × 3 × 2 × 1 + +For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, +and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. + +Find the sum of the digits in the number 100! + +=end + + +NUM = 100.downto(1).inject {|sum, item| sum*=item}.to_s +count = NUM.length +p (0...count).inject(0) { |sum ,item| sum+=NUM[item].chr.to_i}
33 21.rb
 @@ -0,0 +1,33 @@ +=begin +Problem 21 +Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). +If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers. + +For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220. + +Evaluate the sum of all the amicable numbers under 10000. + +=end + require "mathn" + class Integer + def custom + return [1] if self < 2 + temp = [] + pd = prime_division.map { |item| [item[0]]*item[1] }.flatten + temp = pd.uniq + (2..(pd.size)).each do |t| + temp += pd.combination(t).map { |e| e.inject(1) {|m,t2| m*=t2} }.to_a + end + [1] + temp.uniq - [self] + end + end + \$hh = Hash.new + (1..10000).each do |num| + \$hh[num] = num.custom.inject(0) {|sum,ii| sum+=ii} + end + sum = 0 + \$hh.each { |k,v| sum+=v if \$hh[v]==k && v != k } + p sum + + +
24 22.rb
 @@ -0,0 +1,24 @@ +=begin +Problem 22 +Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score. + +For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714. + +What is the total of all the name scores in the file? + +=end + +class String + def calc + each_byte.inject(0) {|sum,v| sum+= v-64} + end +end + +file = File.open("names.txt").to_a +\$sum = 0 +file.sort!.each_with_index do |value,index| + \$sum += (index+1)*(value.calc) +end +file.close +p \$sum +
38 23.rb
 @@ -0,0 +1,38 @@ +=begin + NO.23 Euler Subject +A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number. + +A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n. + +As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit. + +Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers. + +=end + require "mathn" + class Integer + def custom + return [1] if self < 2 + temp = [] + pd = prime_division.map { |item| [item[0]]*item[1] }.flatten + temp = pd.uniq + (2..(pd.size)).each do |t| + temp += pd.combination(t).map { |e| e.inject(1) {|m,t2| m*=t2} }.to_a + end + [1] + temp.uniq - [self] + end + end + class Array + def sum + inject(0) {|num,sum| sum+= num} + end + end + class Integer + def abundent_num? + custom.sum > self + end + end + \$abundent_num = [] +(1...28123).each {|n| \$abundent_num << n << n if n.abundent_num?} + +p ((1...28123).to_a-\$abundent_num.combination(2).collect {|n| n[0]+n[1]}).sum
17 24.rb
 @@ -0,0 +1,17 @@ +=begin +Problem 24 + +A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are: + +012 021 102 120 201 210 + +What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9? +=end +a = (0..9).to_a +def pailie(list) + return [list] if (1 == list.size) + newlist = [] + list.each {|i| pailie(list - [i]).each {|j| newlist.push [i] + j}} + return newlist +end +p pailie(a).at(1_000_000-1)
35 25.rb
 @@ -0,0 +1,35 @@ +=begin +Problem 25 +The Fibonacci sequence is defined by the recurrence relation: + + Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. + +Hence the first 12 terms will be: + + F1 = 1 + F2 = 1 + F3 = 2 + F4 = 3 + F5 = 5 + F6 = 8 + F7 = 13 + F8 = 21 + F9 = 34 + F10 = 55 + F11 = 89 + F12 = 144 + +The 12th term, F12, is the first term to contain three digits. + +What is the first term in the Fibonacci sequence to contain 1000 digits? + +=end +fib = [1,1] + +temp,num=0,2 +while num.to_s.length < 1000 + num = fib[temp] + fib[temp+1] + fib.push(num) + temp+=1 +end +p temp