Project Euler #229: Four Representations using Squares

Sort by

recency

|

6 Discussions

|

  • + 0 comments

    static LinkedHashMap counts = new

    LinkedHashMap();

    public static void FourRepresentation(long x) {
    
    
        // i^2 + j^2
        for (long i = 1; i <= (int) Math.sqrt(x); i++) {
            for (long j = 1; j <= (int) Math.sqrt(x); j++) {
    
                if (i * i + 7 * j * j < x  && i * i + 7 * j * j >190 ) {
                    counts.put(i * i + 7 * j * j, 1);
                }
    
            }
        }
    
    
        loop:
        for(long i=1;i<= (int)Math.sqrt(x) ;i++){
        for(long j=1;j<=(int)Math.sqrt(x); j++){
    
        if(counts.containsKey(i*i +  j *j) && counts.get(i*i + j *j) == 1){
        counts.put( (i*i + j*j), counts.get(i*i +  j*j)+1);
        }
        }
        }
    
        loop2:
        for(long i=1;i<= (int)Math.sqrt(x) ;i++){
        for(long j=1;j<= (int)Math.sqrt(x) ; j++){
        if(counts.containsKey(i*i + 3 * j *j) && counts.get(i*i + 3 * j *j) == 2 ){
        counts.put( (i*i + 3 * j*j), counts.get(i*i + 3 * j*j)+1);
    
        }
        }
        }
    
        loop1:
        for(long i=1;i<= (int)Math.sqrt(x) ;i++){
        for(long j=1;j<=(int)Math.sqrt(x) ; j++){
    
        if(counts.containsKey(i*i + 2 * j *j) && counts.get(i*i + 2 * j *j) == 3 ){
        counts.put( (i*i + 2 * j*j),counts.get(i*i + 2* j*j)+1);
    
        }
    
        }
        }
    
    
    
                **How to optimize this ! thanks**
    
    
        System.out.println(Collections.frequency(counts.values(), 4));
    
    1. }
  • + 0 comments

    def count(y): flag1 , flag2,flag3,flag4,count=0,0,0,0,0 for n in range(y): i = 1 while i * i <= n : j = i while(j * j <= n) : if (i * i + j * j == n) : flag1=1 if (i*i+2*j*j == n): flag2=1 if (i*i+3*j*j == n): flag3=1 if (i*i+7*j*j == n): flag4=1 j+=1 if flag1 is 1 and flag2 is 1 and flag3 is 1 and flag4 is 1: count+=1 break i+=1 flag1,flag2,flag3,flag4=0,0,0,0 n+=1; return count

    if name=='main': n=int(input().strip()) for i in range(n): x=int(input().strip()) r=count(x) print(r)

    I WAS DOING THE PROBLEM ON FOUR REPRESENTAION USING SQUARES.
    WELL THIS IS MY CODE AND I AM GETTING RUNTIME ERROR.PLEASE HELP ME RESOLVE THIS.

  • + 1 comment

    import java.io.; import java.util.; public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    
    Scanner scan= new Scanner(System.in);
    int a= scan.nextInt();
    long []ar= new long[a];
    for(int i=0;i<a;i++)
    {
    ar[i]= scan.nextInt();
    }  
    long count=0;
    long count1=0;
    for(long x=1;x<ar[0];x++)
    {
        for(int y=1;y<ar[0];y++)
        {
          if(((ar[0]==((x*x)+(y*y)))) &&((ar[0]==((x*x)+(2*y*y))))
          && ((ar[0]==((x*x)+(3*y*y)))) &&((ar[0]==((x*x)+(7*y*y)))))
           {
            ++count;
           }
        }
    }
    for(long x=1;x<ar[1];x++)
    {
         for(int y=1;y<ar[1];y++)
        {
        if(((ar[1]==((x*x)+(y*y)))) &&((ar[1]==((x*x)+(2*y*y))))
          && ((ar[1]==((x*x)+(3*y*y)))) &&((ar[1]==((x*x)+(7*y*y)))))
           {
            ++count1;
           }
    }
    }
    System.out.println(count);     
    System.out.println(count1);
    }
    

    }

    NOTE:- Guys m' not able to find the mistake please refer this code and ping me.

  • + 3 comments
    import java.util.*;
    class Euler229
    {
        private boolean isEuler(final int Number)
        {
            boolean Fin=true,unfin[]={false,false,false,false};
            for(int i=1;i<=Math.sqrt(Number);i++)
            {
                for(int j=1;j<=Math.sqrt(Number);j++)
                {
                    if(((i*i)+(j*j))==Number)
                    {
                        unfin[0]=true;
                        break;
                    }
                }
            }
            for(int i=1;i<=Number/2;i++)
            {
                for(int j=1;j<=Number/2;j++)
                {
                    if(((i*i)+(2*j*j))==Number)
                    {
                        unfin[1]=true;
                        break;
                    }
                }
            }
            for(int i=1;i<=Number/2;i++)
            {
                for(int j=1;j<=Number/2;j++)
                {
                    if(((i*i)+(3*j*j))==Number)
                    {
                        unfin[2]=true;
                        break;
                    }
                }
            }for(int i=1;i<=Number/2;i++)
            {
                for(int j=1;j<=Number/2;j++)
                {
                    if(((i*i)+(7*j*j))==Number)
                    {
                        unfin[3]=true;
                        break;
                    }
                }
            }
            //System.out.println(unfin[0]+unfin[1]+unfin[2]);
            return unfin[0]&&unfin[1]&&unfin[2]&&unfin[3];
        }
    
        public static void main(String []args)
        {
            Scanner sc=new Scanner(System.in);
            int q=sc.nextInt();
            int c=0;
            Euler229 obj=new Euler229();
            for(int i=1;i<=q;i++)
            {
                int n=sc.nextInt();
                for(int j=1;j<=n;j++)
                    if(obj.isEuler(j)){
                        System.out.println("Workin' on it");
                        c++;}
                System.out.println(c);
                c=0;
            }
        }
    }
    

    tell if it is wrong

  • + 1 comment

    Question is complex can't able to understand