Skip to content

Commit

Permalink
Solve Joseph’s Cousin in python
Browse files Browse the repository at this point in the history
  • Loading branch information
deniscostadsc committed Jul 29, 2024
1 parent f46a2ee commit 94c8812
Show file tree
Hide file tree
Showing 6 changed files with 7,115 additions and 0 deletions.
70 changes: 70 additions & 0 deletions solutions/beecrowd/1032/1032.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import sys
from math import sqrt

sys.setrecursionlimit(3505)

primes = {
2: 3,
3: 5,
5: 7,
7: 11,
11: 13,
13: 17,
17: 19,
19: 23,
23: 29,
29: 31,
31: 37,
37: 41,
41: 43,
43: 47,
47: 53,
53: 59,
59: 61,
61: 67,
67: 71,
71: 73,
73: 79,
79: 83,
83: 89,
89: 97,
97: 101,
}


def is_prime(number):
if number == 2:
return True
if number <= 1 or number % 2 == 0:
return False
for divisor in range(3, int(sqrt(number)) + 1, 2):
if number % divisor == 0:
return False
return True


def next_prime(number):
if number in primes:
return primes[number]

next = number + 1
while not is_prime(next):
next += 1
primes[number] = next

return primes[number]


def josephus(n, k=2) -> int:
if n == 1:
return 1
return ((josephus(n - 1, next_prime(k)) + k - 1) % n) + 1


for line in sys.stdin:
n = int(line)

if n == 0:
break

print(josephus(n))
6 changes: 6 additions & 0 deletions solutions/beecrowd/1032/generate_in.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

set -euo pipefail

seq 3501
echo 0
Loading

0 comments on commit 94c8812

Please sign in to comment.