fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. int a[1550];
  6.  
  7. #define LL long long int
  8. // returns the square sum of digits
  9. int sqSum(LL num){
  10. int ans=0;
  11.  
  12. while(num){
  13. ans=ans+(num%10)*(num%10);
  14. num/=10;
  15. }
  16.  
  17. return ans;
  18.  
  19. }
  20.  
  21. void pre(){
  22. fill(a,a+1550,0);
  23.  
  24. int temp,last;
  25.  
  26. a[0]=0;a[1]=1;
  27.  
  28. for(int i=2;i<1550;i++){
  29. if(a[i]==0){// condition if the current value is not processed
  30. a[i]=-1;// let us assume -1 ->infinite loop and +1 for other case
  31.  
  32. //here i'm assuming that series is infinote
  33. temp=sqSum(i);
  34. while(a[temp]==0){
  35. a[temp]=-1;
  36. temp=sqSum(temp);
  37. }
  38. // if series is not infinite then again calculate
  39. if(a[temp]==1){
  40. a[i]=1;
  41.  
  42. temp=sqSum(i);
  43.  
  44. while(a[temp]!=1){
  45. a[temp]=1;
  46. temp=sqSum(temp);
  47. }
  48. }
  49. }
  50. }
  51.  
  52.  
  53.  
  54. }
  55. int main() {
  56. pre();
  57.  
  58. LL n,m;
  59.  
  60. while(1){
  61. scanf("%lld %lld",&n,&m);
  62. if(!n&&!m) break;
  63.  
  64. LL count=0;
  65.  
  66. for(LL i=n;i<=m;i++){
  67. if(a[sqSum(i)]==-1) count++;
  68. }
  69.  
  70. printf("%lld\n",count);
  71.  
  72. }
  73. return 0;
  74. }
Success #stdin #stdout 0s 3476KB
stdin
1 10
1 100
0 0
stdout
7
80