Skip to content

Instantly share code, notes, and snippets.

@mather
Last active December 19, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mather/5934486 to your computer and use it in GitHub Desktop.
Save mather/5934486 to your computer and use it in GitHub Desktop.
剰余環でフィボナッチ列を作ったらどんな周期が生まれるの?
# -*- encoding: utf-8 -*-
def fib_cycle(q,a,b)
fib = [a,b]
fib_array = [fib]
while true
fib = [fib[1], (fib[0] + fib[1]) % q]
if (index = fib_array.index(fib))
break
else
fib_array << fib
end
end
fib_uniq = fib_array.map{|a| a[0]}.sort.uniq
p fib_array
print "fib_cycle(#{q},#{a},#{b})"
print " => Loop #{fib_array.size}/#{q*q}, Index #{index}"
#p fib_uniq
fill_uniq = fib_uniq.size == q ? "!" : ""
puts " => #{fill_uniq}Unique elems #{fib_uniq.size}/#{q}"
fib_array
end
#100.times { |i| fib_cycle(10,i/10,i%10) }
#3.upto(100) { |n| fib_cycle(n,1,1) }
total = []
#total += fib_cycle(10,0,0)
#total += fib_cycle(10,0,5)
#total += fib_cycle(10,2,6)
#total += fib_cycle(10,1,8)
#total += fib_cycle(10,2,2)
#total += fib_cycle(10,1,1)
#100.times { |i| p i unless total.include?([i/10,i%10]) }
def fill_fib_cycle(n)
total = []
i = 0
while total.size < n**2
i += 1 while total.include?([i/n, i%n])
total += fib_cycle(n, i/n, i%n)
end
end
fill_fib_cycle(101)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment