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/ruby | |
| t=[];p={};h={} | |
| 1.step(99999).map{|i|t.push(i*(i+1)/2);p[i*(3*i-1)/2]=1;h[i*(2*i-1)]=1} | |
| p t.select{|e|p[e]&&h[e]}.drop(2).first #this select can be lazy |
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/ruby | |
| require 'prime' | |
| (9..1/0.0).each{|i| | |
| if i%2==0 || i.prime? then next end | |
| 1.step(i){|j| | |
| if 2*j**2 > i then p i;exit end | |
| if (i-2*j**2).prime? then break 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/ruby | |
| require 'prime' | |
| c=0;i=1 | |
| while c<4 do | |
| i+=1 | |
| c=Prime.prime_division(i).count==4?c+1:0 | |
| end | |
| p i-3 |
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/ruby | |
| p 1.step(1000).reduce(0){|s,i|s+=i**i}%10**10 |
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/ruby | |
| require 'prime' | |
| h=Hash.new{|h,k|h[k]=[]} | |
| Prime.each(9999).drop_while{|e|e<1000}.map{|e|h[e.to_s.split('').sort.join].push(e)} | |
| h.each_value{|v| | |
| if v.length<3 then next end | |
| 0.step(v.length-3){|i| | |
| (i+1).step(v.length-2){|j| | |
| (j+1).step(v.length-1){|k| | |
| if v[k]-v[j]==v[j]-v[i] then puts [v[i],v[j],v[k]].join 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/ruby | |
| require 'prime' | |
| a=Prime.each(100000).map{|e|e} | |
| m=543 | |
| while true do | |
| z=(a.length-m).times.map{|i|a[i,m].reduce(&:+)}.select{|e|e.prime?}[0] | |
| if z then p z;exit end | |
| m-=1 | |
| 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
| 1/7.0 is 0.(142857), so the answer is 142857. |
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/ruby | |
| class Integer | |
| def comb(r) | |
| if r==0 then return 1 end | |
| n=self | |
| ret=1 | |
| if r>n/2 then r=n-r end | |
| r.times{|i| | |
| ret=ret*(n-i)/(i+1) | |
| } |
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/ruby | |
| p 1.step(99).map{|a|1.step(99).map{|b|(a**b).to_s.split('').map(&:to_i).reduce(&:+)}.max}.max |
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/ruby | |
| q=[1,1];r=[0,1] | |
| p 2.step(1001).select{|i|q.push(2*q[i-1]+q[i-2]);r.push(2*r[i-1]+r[i-2]);q[i].to_s.length>r[i].to_s.length}.count |