Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- class ExploringNumbers{
- public:
- set<int> s;
- bool prime(int x){
- if(x == 1)return false;
- for(int i = 2; i * i <= x; i++){
- if(x % i == 0){
- return false;
- }
- }
- return true;
- }
- int getnext(int n){
- int _n = 0;
- while(n > 0){
- int d = n % 10;
- _n += d * d;
- n /= 10;
- }
- return _n;
- }
- int numberOfSteps(int n){
- int ans = 0;
- while(true){
- ans++;
- s.insert(n);
- if(prime(n)){
- return ans;
- }
- n = getnext(n);
- if(s.count(n) || ans == n){
- return -1;
- }
- }
- return -1;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement