Skip to content

Instantly share code, notes, and snippets.

@tallpeak
Last active September 7, 2022 06:09
Show Gist options
  • Save tallpeak/0f9c5a5ce5b27c1a3ca6090677363c6a to your computer and use it in GitHub Desktop.
Save tallpeak/0f9c5a5ce5b27c1a3ca6090677363c6a to your computer and use it in GitHub Desktop.
# give.a.dollar.jl
# from tc:
# http://primepuzzle.com/not.just.tiny.c/give.a.dollar.try.tc?fbclid=IwAR2p5YBQgJJDY3-g3lN3rRHw7v7cA0VB0BcNWv4dHybH-XCCgxQQIrbF6sY
# ref: https://www.facebook.com/groups/299317782048/posts/10160467717107049/
# give.a.dollar.tc - lrb
# tc give.a.dollar.tc -r "give(lim,sd)"
using Random
using Printf
function gn()
n = parse(Float64,readline())
return convert(Int64,n)
end
print("give.a.dollar.tc - lrb - 9/3/22 -- translation to Julia by AWW 9/6/22\n")
limit = 0 # 1000
seed = 0
if limit == 0
print("number of rounds, e.g. 50..10000 : ")
limit = gn()
print("seed please (0 for no seed) : ")
seed = gn()
if seed != 0
Random.seed!(seed)
end
end
#println("")
start = time()
# for i in 1:45 # give everybody $45 to start
# dollar[i] = 45
# end
dollar = fill(Int32(45), 45) :: Vector{Int32} # zeros(Int32,44)
for j in 1:limit
rand45 = rand(1:44,45) # grab an array of 45 randoms, just in case this is faster
for i in 1:45 # // inner loop
if dollar[i] > 0 # can't give if you don't have
rn=rand45[i]
if rn>=i
rn+=1
end
dollar[rn] += 1 # give a random person $1
dollar[i] -= 1 # and deduct $1 from the giver
end
end # i
#print(".") # show progress
end # j
#print("\n\n")
for i in 1:45 # display results
@printf("%2d ", dollar[i])
end
stop = time()
elapsed = stop - start
#@printf("\n\nstart time: %.3f\nstop time: %.3f", start, stop)
@printf("\nelapsed: %.3f seconds", elapsed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment