""" Consider the following puzzle: Two n-sided dice are labeled 1 to n. When rolled, their sum is a random number between 2 and 2n. Is there a different way to label these dice with positive integers which results in the same random distribution? This script will solve this puzzle. Simply change the value of n as desired, then hit run. """ n = 6 # OPTIONAL: Rename p to be any positive integer which divides n to get other solutions. p = 0 def product(A,B): return [sum([A[i]*B[z-i]for i in range(max(z-len(B)+1,0),min(len(A),z+1))])\ for z in range(len(A)+len(B)-1)] def quotient(A,B): Q = [] while len(A)>= len(B): q = A[0] / B[0] A = [A[i] - q*B[i] for i in range(1, len(B) ) ] + A[len(B):] Q += [q] while (len(Q)>0 and Q[-1]==0): Q = Q[:-1] #remove trailing zeroes return Q def nonstandard_dice(n,p): """Gives labels for two dice, whose sum behaves like two dice labeled 1 to n The second input p must be a factor of n. """ q = n/p die_1 = product([1]*p,[1]*q) die_2 = quotient(product([1]*n,[1]*n),die_1) return (Faces(die_1),Faces(die_2)) def Faces(A): return sum([[i+1]*A[::-1][i] for i in range(len(A))],[]) if (not isinstance(p,int)) or (p < 1 or n%p): p = 2 while p<= n**0.5 and n%p: p += 1 if n%p: p = 1 (d1,d2) = nonstandard_dice(n,p) print "Die 1 Labels: ",d1 print "Die 2 Labels: ",d2
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question