-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTruncatablePrimes.cpp
82 lines (58 loc) · 1.13 KB
/
TruncatablePrimes.cpp
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
//
// main.cpp
// random
//
// Created by Pradyuman Dixit on 22/07/18.
// Copyright © 2018 Pradyuman Dixit. All rights reserved.
//
#include <iostream>
#include <math.h>
using namespace std;
bool isPrime(int x) {
if(x <= 1) return false;
if(x == 2 || x == 3) return true;
else if (x % 2 == 0) return false;
else {
for (int i = 3; i < (int)sqrt((float)x)+1; i++) {
if (x % i == 0) return false;
}
}
return true;
}
bool truncatableCheck(int x) {
if (x<=0) return false;
int len = 0;
int tmp = x;
while (tmp !=0)
{
len ++;
tmp /= 10;
}
tmp = x;
while (tmp != 0) {
if (isPrime(tmp) != true) {
return false;
}
tmp /= 10;
}
tmp = x;
while(len > 1 ) {
int y = tmp % (int)pow((float)10, len-1);
if (isPrime(y) != true) {
return false;
}
len--;
}
return true;
}
int main() {
int n;
int sum = 0;
cin>>n;
for(int i =8;i<=n;i++){
if (truncatableCheck(i))
sum += i;
}
cout<<sum<< endl;
return 0;
}