Skip to content

Instantly share code, notes, and snippets.

@myxoh
Created October 13, 2017 20:00
Show Gist options
  • Save myxoh/3e9b7c131d9241ce522d91c551852730 to your computer and use it in GitHub Desktop.
Save myxoh/3e9b7c131d9241ce522d91c551852730 to your computer and use it in GitHub Desktop.
module Fouriest
def to_base(x)
raise ArgumentError, "the base must be a positive > 1 number" if x < 2 || x != (x.to_i)
num = self
new_string = ""
loop do
new_string += (num % x).represent_number
num = num / x
break if num == 0
end
new_string.reverse!
end
def represent_number
self < 10 ? self.to_s : 'x'
end
def fouriest
fours = -1
fouriest = self.to_s
base = 2
best_base = 0
loop do
new_num = self.to_base(base)
new_fours = new_num.count("4")
if new_fours > fours
fouriest = new_num
fours = new_fours
best_base = base
end
break if new_num == 'x' || new_num.length <= fours || (self<10 && base >=10)
base+=1
end
"#{self} is the fouriest (#{fouriest}) on base #{best_base}"
end
end
Bignum.include(Fouriest)
Fixnum.include(Fouriest)
10000.times do |i|
puts i.fouriest
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment