forked from malbolgee/URI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1032.c
81 lines (54 loc) · 869 Bytes
/
1032.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
@autor: Malbolge;
@data: 11/01/2019;
@nome: O Primo de Josephus;
*/
#include <stdio.h>
#define true 1
#define false 0
#define MAXSIZE 3502
#define MAXPRIME 32650
int josephus(int n);
_Bool isPrime(int x);
void preenchePrimo();
int primos[MAXSIZE] = { 0 };
void main ()
{
preenchePrimo();
int n;
while (true)
{
scanf("%d", &n);
if (n == 0)
break;
printf("%d\n", josephus(n));
}
}
int josephus(int n)
{
int i;
int retorno = 0;
// Recorrência;
for (i = 1; i <= n; ++i)
retorno = (retorno + primos[n - i]) % i;
return ++retorno;
}
_Bool isPrime(int x)
{
int i;
if (x == 2)
return true;
if (!(x & 1))
return false;
for (i = 3; i * i <= x; i += 2)
if (x % i == 0)
return false;
return true;
}
void preenchePrimo()
{
int i, j;
for (i = 2, j = 0; i < MAXPRIME; ++i)
if (isPrime(i))
primos[j++] = i;
}