#!/usr/bin/env ruby # this redefinition of Integer was Eliah Hecht's solution to Ruby Quiz #25 # http://www.rubyquiz.com/quiz25.html # it adds a to_en method which returns the english form of the Integer # for example, 123232.to_en returns # "one hundred twenty-three thousand, two hundred and thirty-two" # # I modify this below to add the to_syllablecount method class Integer def teen case self when 0: "ten" when 1: "eleven" when 2: "twelve" else in_compound + "teen" end end def ten case self when 1: "ten" when 2: "twenty" else in_compound + "ty" end end def in_compound case self when 3: "thir" when 5: "fif" when 8: "eigh" else to_en end end def to_en(ands=true) small_nums = [""] + %w[one two three four five six seven eight nine] if self < 10: small_nums[self] elsif self < 20: (self % 10).teen elsif self < 100: result = (self/10).ten result += "-" if (self % 10) != 0 result += (self % 10).to_en return result elsif self < 1000 if self%100 != 0 and ands (self/100).to_en(ands)+" hundred and "+(self%100).to_en(ands) else ((self/100).to_en(ands)+ " hundred "+(self%100).to_en(ands)).chomp(" ") end else front,back = case (self.to_s.length) % 3 when 0: [0..2,3..-1].map{|i| self.to_s[i]}.map{|i| i.to_i} when 2: [0..1,2..-1].map{|i| self.to_s[i]}.map{|i| i.to_i} when 1: [0..0,1..-1].map{|i| self.to_s[i]}.map{|i| i.to_i} end degree = [""] + %w[thousand million billion trillion quadrillion quintillion sextillion septillion octillion nonillion decillion undecillion duodecillion tredecillion quattuordecillion quindecillion sexdecillion septdecillion novemdecillion vigintillion unvigintillion duovigintillion trevigintillion quattuorvigintillion quinvigintillion sexvigintillion septvigintillion octovigintillion novemvigintillion trigintillion untregintillion duotrigintillion googol] result = front.to_en(false) + " " + degree[(self.to_s.length-1)/3] result += if back > 99: ", " elsif back > 0: ands ? " and " : " " else "" end result += back.to_en(ands) return result.chomp(" ") end end end # my modified syllable-counting code class Integer def teen_sc case self when 0: 1 when 1: 2 when 2: 1 else in_compound_sc + 1 end end def ten_sc case self when 1: 1 when 2: 2 else in_compound_sc + 1 end end def in_compound_sc case self when 3: 1 when 5: 1 when 8: 1 else to_syllable_count end end def small_nums_sc return 1 unless self==7 return 2 end def to_syllable_count(ands=true) if self < 10: small_nums_sc elsif self < 20: (self % 10).teen_sc elsif self < 100: result = (self/10).ten_sc #result += "-" if (self % 10) != 0 result += (self % 10).to_syllable_count return result elsif self < 1000 if self%100 != 0 and ands (self/100).to_syllable_count(ands)+2+(self%100).to_syllable_count(ands) else ((self/100).to_syllable_count(ands)+ 2+(self%100).to_syllable_count(ands)) end else front,back = case (self.to_s.length) % 3 when 0: [0..2,3..-1].map{|i| self.to_s[i]}.map{|i| i.to_i} when 2: [0..1,2..-1].map{|i| self.to_s[i]}.map{|i| i.to_i} when 1: [0..0,1..-1].map{|i| self.to_s[i]}.map{|i| i.to_i} end degree = [2, 2, 2, 2, 3, 3, 3, 3,3] # thousand, million, billion, etc #p "front: #{front}" #p "back: #{back}" #p "degree: #{degree[(self.to_s.length-1)/3]}" result = front.to_syllable_count(false) + degree[(self.to_s.length-1)/3] result += back.to_syllable_count(ands) unless back==0 return result end end end # main syllables_per_sescond = 6 seconds_per_day = 16*60*60 # not counting the 8 hours sleeping avg_lifespan_years = 70 avg_lifespan_seconds = avg_lifespan_years * 365 * seconds_per_day total_syllables = avg_lifespan_seconds * syllables_per_sescond print "You have #{avg_lifespan_seconds} seconds (or #{avg_lifespan_seconds/seconds_per_day} days) to spend counting before you reach a killion.\n" print "That's enough time to utter #{total_syllables.to_en} syllables.\n" print "Calculating killion now...\n" total = i= 0 start_time = Time.now until total >= total_syllables do i+=1 print "Up to #{i.to_en} so far\n (#{(Time.now-start_time).to_i} seconds, #{total} syllables}\n" if i%100_000 == 0 c = i.to_syllable_count total += c end p "killion = #{i.to_en}" p "avg syllables per number=#{total.to_f/i}"