2011-07-01から1ヶ月間の記事一覧

Project Eueler: problem16

問題 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 calc(num) 2**num end def do_sum(num) sum = 0 str = num.to_s 0.upto(str.size.to_i-1) do |n| sum += str[n,1].to_…

Project Eueler: problem17

問題 If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many le…

Project Eueler: problem14

問題 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 th…

Project Eueler: problem15

問題 Starting in the top left corner of a 22 grid, there are 6 routes (without backtracking) to the bottom right corner. How many routes are there through a 2020 grid? 回答 def root(x,y,c) cnt = 0 return c[x][y] if c[x][y] return cnt += 1 …

Project Eueler: problem13

問題 Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.37107287533902102798797998220837590246510135740250 46376937677490009712648124896970078050417018260538 ..... 回答 ar = [] DATA.each do |line| ar << …

Project Eueler: problem12

問題 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 fact…

Project Eueler: problem11

問題 In the 2020 grid below, four numbers along a diagonal line have been marked in red.08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 4…

Project Eueler: problem10

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.Find the sum of all the primes below two million. prime = [2] sum = 2 limit = 2000000 (3..limit).each do |x| flg = true prime.each do |y| if x % y == 0 flg = false ;break end end if flg …

Project Eueler: problem9

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. (1..333).each do …

Project Eueler: problem8

Find the greatest product of five consecutive digits in the 1000-digit number. #Ruby str = "" DATA.each do |x| x = x.chomp! str = str + x end res = 0 (0..995).each do |x| t = 1 (0..4).each do |y| t = t*str[x+y,1].to_i end if res < t res = …

Project Eueler: problem7

問題 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? 回答 prime = [2] flg = 0 (3..9999999).each do |n| flg = 0 prime.each do |p| if n % p == 0 flg = 1 br…

Project Eueler: problem6

問題 The sum of the squares of the first ten natural numbers is,12 + 22 + ... + 102 = 385 The square of the sum of the first ten natural numbers is,(1 + 2 + ... + 10)2 = 552 = 3025 Hence the difference between the sum of the squares of the…

Project Euler : problem4

問題 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. 回答 res = 0 t = 0 (1..999).e…