Skip to content

Instantly share code, notes, and snippets.

@syntacticsugar
Created May 24, 2013 16:41
Show Gist options
  • Save syntacticsugar/5644792 to your computer and use it in GitHub Desktop.
Save syntacticsugar/5644792 to your computer and use it in GitHub Desktop.
Project Euler #14
$array = []
module Collatz
def self.how_many n
if n == 1
($array.push n).length
elsif n.even?
$array.push n
n = n / 2
how_many n
elsif n.odd?
$array.push n
n = 3 * n + 1
how_many n
end
puts $array
$array = []
end
end
puts Collatz::how_many 13
$array = []
class Collatzie
def self.countme(n,counter)
# PUT 1 FIRST!!!!!!!!!!! duhhhhhhhhhh!!!!!!!!!
# do not make that mistake again!!! RAWRR!!
# in fact, i got it right the first time!
# why did i put 1 last this time!? eugh!
if n == 1
counter+=1
puts "n is: #{counter}!"
counter
elsif n.even?
n = n/2
counter+=1
puts "FOR EVEN: n is #{n} and counter is #{counter}."
countme(n,counter)
elsif n.odd?
n = n * 3 + 1
counter+=1
puts "FOR ODD: n is #{n} and counter is #{counter}."
countme(n,counter)
end
end
def self.longest
self.sort_by{ |x| x.length }.reverse.first.first
end
end
print "Answer is " + Collatzie.countme(13,0).to_s + "\n"
# Thu 23 May 22:28:15 2013, Lincoln Center. sitting at the fountain and coding.
# 1e6-1.downto(1).map { |x| Collatzie.countme(x,0) }.Collatzie.longest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment