forked from gutty333/Medium-Programming-Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path27_PrimeChecker.cpp
97 lines (89 loc) · 1.91 KB
/
27_PrimeChecker.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// For this challenge you will determine if you can arrange a number to be a prime number.
// have the function PrimeChecker(num) take num and return 1 if any arrangement of num comes out to be a prime number, otherwise return 0. For example: if num is 910, the output should be 1 because 910 can be arranged into 109 or 019, both of which are primes.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
// Function to check if the argument is prime
bool isPrime(string number)
{
int value;
istringstream(number) >> value;
if (value == 1)
{
return false;
}
for (int x = 2; x < value; x++)
{
if (value%x == 0)
{
return false;
}
}
return true;
}
int PrimeChecker(int num) {
// Convert to a string for editing
stringstream convert;
convert << num;
string temp = convert.str();
char current;
//Check if the number is prime when is only 1 digit
if (temp.length() == 1)
{
if (isPrime(temp))
{
return 1;
}
else
{
return 0;
}
}
// Check if the number is prime when there are only 2 digits
else if (temp.length() == 2)
{
if (isPrime(temp))
{
return 1;
}
current = temp[0];
temp[0] = temp[1];
temp[1] = current;
if (isPrime(temp))
{
return 1;
}
}
else
{
// Loop to arrange the number when dealing with multiple digits
for (int x = 0; x < temp.length(); x++)
{
for (int y = 0; y < temp.length()-1; y++)
{
if (isPrime(temp)) // Check the number to see if is prime
{
return 1;
}
else // Rearrange the digits
{
current = temp[y];
temp[y] = temp[y + 1];
temp[y + 1] = current;
}
}
}
}
return 0;
}
int main() {
// keep this function call here
cout << PrimeChecker(98) << endl; // 1
cout << PrimeChecker(598) << endl; // 1
cout << PrimeChecker(910) << endl; // 1
cout << PrimeChecker(22) << endl; // 0
cout << PrimeChecker(71) << endl; // 1
cout << PrimeChecker(100) << endl; // 0
return 0;
}