This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fact(n) | |
| n == 1 and return 1 | |
| n*fact(n-1) | |
| end | |
| puts fact(40) / fact(20) / fact(20) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MAXSIZE = 1000000 | |
| MEMO = Array.new(MAXSIZE) | |
| def collatz_size(i) | |
| size = 0 | |
| n = i | |
| while n != 1 do | |
| if n <= MAXSIZE && MEMO[n] | |
| size += MEMO[n] | |
| break |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def collatz(i) | |
| Enumerator.new do |y| | |
| n = i | |
| while n != 1 do | |
| n = n.even? ? n/2 : 3*n + 1 | |
| y << n | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| puts [ | |
| 37107287533902102798797998220837590246510135740250, | |
| 46376937677490009712648124896970078050417018260538, | |
| 74324986199524741059474233309513058123726617309629, | |
| 91942213363574161572522430563301811072406154908250, | |
| 23067588207539346171171980310421047513778063246676, | |
| 89261670696623633820136378418383684178734361726757, | |
| 28112879812849979408065481931592621691275889832738, | |
| 44274228917432520321923589422876796487670272189318, | |
| 47451445736001306439091167216856844588711603153276, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| r = (2..2000000).select do |i| | |
| !(2..Math.sqrt(i).floor).find {|j| i % j == 0 } # 素数判定 | |
| end.inject(:+) | |
| puts r | |
| # 142913828922 | |
| # 30.40s with Core i5 mem 4G |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (1..1000).each do |a| | |
| (a..(1000 - a)).each do |b| | |
| c = 1000 - a - b | |
| puts a*b*c or exit if a**2 + b**2 == c**2 | |
| end | |
| end | |
| # 31875000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # -*- coding: utf-8 -*- | |
| N = 731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511125406987471585238630507156932909632952274430435576689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749303589072962904915604407723907138105158593079608667017242712188399879790879227492190169972088809377665727333001053367881220235421809751254540594752243525849077116705560136048395864467063244157221553975369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412275886668811642717147992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042242190226710556263211111093705442175069416589604080719840385096245544436298123098787992724428490918884580156166097919133875499200524063689912560717606058861164671094050775410022569831552000559357297257163626956188267042825248 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # -*- coding: utf-8 -*- | |
| require 'prime' | |
| Prime.each.with_index do |n, i| | |
| puts n or break if (i + 1) == 10001 | |
| end | |
| # スクリプトキディ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # -*- coding: utf-8 -*- | |
| n = (1..100).each.inject {|sum, i| sum + i } | |
| r = (1..100).each.inject {|sum, i| sum + i**2 } | |
| puts n**2 - r |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| # -*- coding: utf-8 -*- | |
| require 'prime' | |
| factories_count = {} | |
| Prime.each(20) {|i| factories_count[i] = 0} | |
| (2..20).each do |n| | |
| factories_count.each_key do |k| # 素因数分解 | |
| break if n < k |