Skip to content

Commit fb725eb

Browse files
cpp: added "Backwards Read Primes" kata solution
1 parent ec386bf commit fb725eb

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// https://www.codewars.com/kata/5539fecef69c483c5a000015
2+
#include<cmath>
3+
#include <bits/stdc++.h>
4+
#include <string>
5+
class BackWardsPrime
6+
{
7+
public:
8+
static std::string backwardsPrime(long long start, long long end){
9+
std::string res;
10+
bool prime, backwardsPrime, notSame;
11+
const auto sep = " ";
12+
for(auto i = start;i<=end;i++){
13+
auto reversed_item = reverse_item(i);
14+
prime = isPrime(i);
15+
backwardsPrime = isPrime(reversed_item);
16+
notSame = i != reversed_item;
17+
if(prime&&notSame&&backwardsPrime){
18+
res += std::to_string(i) + sep;
19+
}
20+
}
21+
return res.substr(0,res.length()-1);
22+
};
23+
static long long reverse_item(long long item){
24+
std::string s_item = std::to_string(item);
25+
std::reverse(s_item.begin(),s_item.end());
26+
return std::stoll(s_item);
27+
}
28+
static bool isPrime(long long item){
29+
auto sq_root = std::sqrt(item);
30+
for(auto i = 2; i <= sq_root; i++){
31+
if (item % i == 0) {
32+
return false;
33+
}
34+
}
35+
return true;
36+
};
37+
};

0 commit comments

Comments
 (0)