This file contains 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
#! ruby | |
def fact(n) | |
n == 0 ? 1 : n * fact(n - 1) | |
end | |
a = (0..9).map {|v| [v.to_s, fact(v)] } | |
facts = Hash[*a.flatten(1)] | |
ans = 3.upto(9999999).select {|n| n.to_s.split(//).map {|v| facts[v] }.inject(:+) == n } |
This file contains 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
#! ruby | |
require "prime" | |
ans = [] | |
1.upto(999999) do |n| | |
a = n.to_s.split(//) | |
flag = true | |
a.size.times do |i| | |
b = a.dup |
This file contains 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
class String | |
def palindrome? | |
self == self.split(//).reverse.join | |
end | |
end | |
ans = [] | |
1.upto(1000000) do |n| | |
if [n.to_s, n.to_s(2)].all? {|v| v.palindrome? } | |
p "#{n} : #{n.to_s(2)}" |
This file contains 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
require 'set' | |
s = Set.new | |
class Array | |
def pandigital?(r = 1..9) | |
r = r.to_a unless r.instance_of?(self.class) | |
self.sort == r.sort | |
end | |
end |
This file contains 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
#! ruby | |
require 'prime' | |
ans = [] | |
Prime.each do |n| | |
next if n < 10 | |
flag = true | |
l = n.to_s.split(//) | |
until l.empty? |
This file contains 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
#! ruby | |
ans = [] | |
1.upto(0 / 0.0) do |n| | |
a = Array.new(2) { n }.map.with_index {|v, i| (v * (i + 1)).to_s }.join | |
break if a.size > 9 | |
2.upto(0 / 0.0) do |m| | |
a = Array.new(m) { n }.map.with_index {|v, i| (v * (i + 1)).to_s }.join | |
break if a.size > 9 | |
next unless a.size == 9 |
This file contains 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
#! ruby | |
max = 1000 | |
ans = (0..max).to_a.fill(0) | |
1.upto(max) do |a| | |
a.upto(max) do |b| | |
c = Math.hypot(a, b) | |
next unless c % 1 == 0 | |
p = a + b + c |
This file contains 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
#! ruby | |
d = "" | |
0.upto(0 / 0.0) do |i| | |
d << i.to_s | |
break if d.size > 10 ** 6 | |
end | |
p 0.upto(6).inject(1) {|v, i| v * d[10 ** i].to_i } |
This file contains 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
#! ruby | |
require 'prime' | |
9.downto(1) do |n| | |
(1..n).to_a.reverse.permutation do |a| | |
y = a.join.to_i | |
if Prime.prime?(y) | |
p y | |
exit |
This file contains 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
class Integer | |
def triangle_number? | |
((Math.sqrt(8 * self + 1) - 1) / 2) % 1 == 0 | |
end | |
end | |
alphabets = Hash[*("A".."Z").to_a.map.with_index {|c, i| [c, i + 1] }.flatten] | |
File.open("words.txt") {|f| | |
p f.read.chomp.gsub("\"", "").split(/,/).count {|w| w.split(//).inject(0) {|v, i| v + alphabets[i] }.triangle_number? } |