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

Sort by

recency

|

70 Discussions

|

  • + 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;
    
    }
    
  • + 0 comments

    here is my full code.. only four test cases are passed and for rest it shows timedout please help me where the problem is

    import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;

    public class Solution {

    public static void main(String[] args)
    {
    Scanner sc=new Scanner(System.in);
       double kk=sc.nextDouble();
        long k=(long)kk; 
        long i,j,sum,total=0,r;
         for(i=1;i<=k;i++)
        {
            j=i;
            sum=0;
            while(j>0)
            {
                r=j%10;
                j=j/10;
                sum=sum+(r*r);
    
            }
            long n=(long)(Math.sqrt(sum));
            if((n*n)==sum)
                    total=total+i;
        }
        long p=(long)(Math.pow(10,9)+7);
        System.out.print(total%p);  
    
    
    }
    

    }