Project Euler #171: Finding numbers for which the sum of the squares of the digits is a square

Sort by

recency

|

71 Discussions

|

  • + 0 comments

    Not so easy task. It can be solved using clever dynamic programming, focusing on digit by digit processing. Search space should be defined by the maximum value of square digit sum. For given square digit sum we also need to keep track of its frequency. Maximum time is around 50 ms for values of k in the order of 10^100.

  • + 0 comments

    I have passed 5 test cases can anyone please help how to optimise code for larger input

    Enter your code here. Read input from STDIN. Print output to STDOUT

    if name=="main": n=int(input()) def squares(value): return sum(int(c)**2 for c in str(value)) a=0 for i in range(1,n+1): if (squares(i)**0.5)==int(squares(i)**0.5): a+=i else: pass print(a)

  • + 0 comments

    for all those guess who does not understant the question trick question sample case 100 how you get 826 then the logic behind that you searched the no from 0 to 100 then then you have to do that for example 86 when we break this no it get 8 and 6 whose square sum is 100 who is the precfect square so that why this the number 43 4^2=16 and 3^2=9 then its is a perfect square no hope youunderstand the logic

  • + 1 comment

    include

    include

    include

    include

    int main() { unsigned long i,j,n,squares_of_sum=0,sum=0,rem,square=0,temp; scanf("%d",&n); for(i=1;i<=n;i++) { temp=i; while(temp) { rem=temp%10; sum=(sum+(rem*rem)); temp/=10; } sum=sum%1000000007; rem=temp=0; square=sqrt(sum); if(sum==(pow(square,2))) squares_of_sum+=i; sum=square=0; }

    printf("%lu",abs(squares_of_sum));
    

    }

  • + 0 comments

    what's the error in it can anyone tell that...

    //Project Euler #171: Finding numbers for which the sum of the squares of the digits is a square
    
    #include<iostream>
    #include<math.h>
    using namespace std;
    
    bool isPerfectSquare(long double x)
    {
    // Find floating point value of
    // square root of x.
    long double sr = sqrt(x);
    
    // If square root is an integer
    return ((sr - floor(sr)) == 0);
    }
    
    
    int main(){
    
    unsigned long int range=0100,sum=0,rem=0,bsum=0,lo=0;
    cin>>range;
    unsigned long int hold;
    for(int i=1;i<=range;i++){
    
    
        hold=i;
        while(hold>0){
            rem=hold%10;
     hold/=10;
             sum+=rem*rem;
               lo+=sum;
              sum=0;
    
    
    
        }
    
         if (isPerfectSquare(lo)){
            bsum+=i;
    
         }
         lo=0;
    }
    cout<<bsum;
    
    }