Skip to content

Instantly share code, notes, and snippets.

@andrewkowalik
Last active December 17, 2015 14:39
Show Gist options
  • Save andrewkowalik/5625633 to your computer and use it in GitHub Desktop.
Save andrewkowalik/5625633 to your computer and use it in GitHub Desktop.
Josephus Problem, add one to method return.
def josephus(count, skip)
#catch errors
return 0 if count == 1
#base case
return count-1 if skip == 1
#generally calls base case
if skip>count
return (josephus(count-1,skip)+skip) % count
end
#eliminates numbers
sub_count = count / skip
rem = josephus(count-sub_count,skip)
rem -= count % skip
if rem<0
rem+= count
else
rem+= rem/(skip - 1)
end
rem
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment