forked from ershad/Snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkRoundPrime.c
76 lines (67 loc) · 1.4 KB
/
checkRoundPrime.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
/*
* checkRoundPrime.c
*
* Copyright 2011 Ershad K [email protected]
* Licensed under GPL Version 3
*
* Code is inefficient, will post a new one soon :)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 20
int permutations(char *string, int k, int m);
void swap(char *x, char *y);
int checkPrime(int n);
int checkRoundPrime(int n);
int flag = 0;
unsigned long int globalnum;
int main(int argc, char *argv[])
{
unsigned long int i;
for (i = 0; i < 1000000; i++)
if (checkRoundPrime(i))
printf("%ld\n", i);
return 0;
}
void swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
int permutations(char *string, int k, int m)
{
int i;
if (k == m) {
if(!checkPrime(atol(string)) || (atol(string) < globalnum))
flag = 1;
}
else
for (i = k; i < m; i++){
swap(&string[k], &string[i]);
permutations(string, k+1, m);
swap(&string[k], &string[i]);
}
}
int checkPrime(int n)
{
register int i;
for (i = 2; i <= (n/2); i++)
if (n % i == 0)
return 0;
return 1;
}
int checkRoundPrime(int n)
{
char cnumber[MAX];
sprintf(cnumber, "%d", n);
flag = 0;
globalnum = atol(cnumber);
permutations(cnumber, 0, strlen(cnumber));
if (flag == 0)
return 1;
else
return 0;
}