Project Euler #153: Investigating Gaussian Integers

Sort by

recency

|

7 Discussions

|

  • + 0 comments

    I think that I have got right logic, but it reports compile time out. Can anyone guide me?

    include

    include

    include

    define max 1000000

    using namespace std; int num,gsn=0; int *fact = new int[max];

    int factorize_real(int); void square_sum(int,int);

    int main(){ int loop=2,count; cin>>num; if(num!=1){ while(loop<=num){ count = factorize_real(loop); /* now fact[] has all real factors of the input number from 0 to count-1 for all x+iy a complex number to be a guassian divisor ((x^2)+(y^2)) should belong to fact[] for this we are using square_sum(n) */ square_sum(loop,count); loop++; } gsn++;//to add for one cout<

    } else{ gsn=1; cout<

    } delete [] fact; return 0; }

    int factorize_real(int num){ //gives real factors of an integer number int count=2; int i=2,j=1,end; int *front = new int[max]; int *back = new int[max]; end=num; front[0]=1; back[0]=num;

    //end marks the last number in factor comparison as given in while loop
    while(i<end){
        if(num%i==0){
            front[j]=i;
            back[j]=end=(num/i);
            (front[j]!=back[j])?(count+=2):count++;
            j++;
        }
        i++;
    }
    //count determines the number of real factors
    for(i=0; i<count/2; i++){
        fact[i]=front[i];
        gsn+=fact[i];
    }
    j=i;
    if(count%2 != 0){
        //this means num is a square and is not 1
        fact[j]=front[j];
        gsn+=fact[j];
        j++;
    }
    i--;//because now i is used to read from behind in back[]
    for(;i>=0;i--){
        fact[j]=back[i];
        gsn+=fact[j];
        j++;
    }
    
    delete [] front;
    delete [] back;
    return count;
    

    }

    void square_sum(int n,int count){

    int x,y,x2,y2,root_n,multiple;
    root_n=(int) (sqrt(n));
    for(x=1;x<=root_n;x++){
        x2=x*x;
        y=(int) (sqrt(n-x2));
        y2=y*y;
        if( n%(x2+y2) ==0 && y2!=0){
            gsn+=(2*(x));//multiply it with 2 as conjugate pairs will exist and add only real part x
            multiple=n/(x2+y2);
            for(int i=1;i<count;i++){
                if(multiple%fact[i] ==0){
                    //this is for cases where the conjugate pair multiplied a real factor is also a Gaussian intger divisor
                    gsn+=(2*(fact[i]*x));//cases like 2(1+i) and 2(1-i) that divide 8 and 4 are satisfied here
                }
            }
        }
    }
    

    }

  • + 1 comment

    Can anyone tell me the correct solution for input = 1000?

  • + 1 comment

    i think my logic is right..just it is taking more time...please someone help :( .............................. 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);
        long n=sc.nextInt();
        long sum=0;
        for(long ni=1;ni<=n;ni++)
            {
            for(long a=1;a<=ni;a++)
                {
                for(long b=0;b<ni;b++)
                    {
                    long den1=a*a+b*b,num_a=ni*a,num_b=ni*b;
                    if((num_a%den1==0)&(num_b%den1==0)&(b!=0))
                        sum=sum+2*a;
                    if((num_a%den1==0)&(num_b%den1==0)&(b==0))
                        sum=sum+a;
    
                }
            }
        }
        System.out.println(sum);
    }
    

    }

  • + 0 comments

    hi can anybody tell me the application for this question in real live projects.

  • + 2 comments

    Hi guys,

    would anybody be so nice to explain to me, how 2+2i is a divisor of 4. My result is

    (4*(2-2i)) / 8

    Which shouldn't be a divisor if I understood everything correctly.

    Thanks in advance.