algorithm - calculate a row of numbers(see context for details) -


there 2 rows of numbers, row 1 consecutive numbers starting 0, ask fill out row 2 make sure number in row 2 times of correspoding number in row 1 appearing in row 2.

for example:

0 1 2 3 4 5 6 7 8 9

_ _ _ _ _ _ _ _ _ _

to more specific, use row1 row 1 , row2 row 2, fill out row2 make sure satisies: row2[i] = count(row2, row1[i]). count(row2, row1[i]) means frequency count of row1[i] among row2.

out of 1000 runs solution had run loop average of 3.608 times

import random  def f(x):     l = []     in range(10):         l.append(x.count(i))     return l  fast = list(range(10))  while f(fast) != fast:     fast = []     slow = []     in range(10):         r = random.randint(0,9)         fast.append(r)         slow.append(r)     while true:         fast = f(f(fast))         slow = f(slow)         if fast == slow:             break  print(fast) 

f(x) takes guess, x, , returns counts. looking solution such f(x) = x.

we first choose 10 random integers 0-9 , make list. our goal repeatedly set list equal until either find solution or run cycle. check cycles, use tortoise , hair algorithm, move @ 2 speeds. fast speed twice quick slow speed. if these equal, have run cycle , start new random scenario.

i ran through few times, , found general solution n>6 (where in case n = 10). of form [n-4,2,1,0...,0,1,0,0,0]


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -