-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathgame_of_primes.cpp
71 lines (66 loc) · 1.04 KB
/
game_of_primes.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
/*
Game of Primes
input
1
30
3
output
19
We need to find such as number within the given range that it should be a prime number
and also a happy number.
*/
#include<iostream>
#include<math.h>
using namespace std;
bool isHappy(int n)
{
int sum =0;
while(n>0)
{
sum = sum + (n%10) * (n%10);
n = n / 10;
}
if(sum==1)
{
return true;
}
else if(sum==4) {
return false;
}
isHappy(sum);
}
bool isPrime(int n)
{
if(n==1)
return false;
else{
for(int i=2;i<=(int)sqrt(n);i++)
{
if(n%i == 0)
return false;
}
return true;
}
}
int main(int argc, char const *argv[])
{
int a,b,count=0,n;
cin>>a>>b>>n;
for(int i=a;i<=b;i++)
{
if(isPrime(i) && isHappy(i))
{
count++;
}
if(count == n)
{
cout<<i;
break;
}
}
if(count < n)
{
cout<<"No number is present at this index.";
}
return 0;
}