Advertisement
Guest User

Untitled

a guest
Apr 12th, 2016
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. class ExploringNumbers{
  6. public:
  7. set<int> s;
  8. bool prime(int x){
  9. if(x == 1)return false;
  10. for(int i = 2; i * i <= x; i++){
  11. if(x % i == 0){
  12. return false;
  13. }
  14. }
  15. return true;
  16. }
  17. int getnext(int n){
  18. int _n = 0;
  19. while(n > 0){
  20. int d = n % 10;
  21. _n += d * d;
  22. n /= 10;
  23. }
  24. return _n;
  25. }
  26. int numberOfSteps(int n){
  27. int ans = 0;
  28. while(true){
  29. ans++;
  30. s.insert(n);
  31. if(prime(n)){
  32. return ans;
  33. }
  34. n = getnext(n);
  35. if(s.count(n) || ans == n){
  36. return -1;
  37. }
  38. }
  39. return -1;
  40. }
  41. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement