We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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
}
}
}
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #153: Investigating Gaussian Integers
You are viewing a single comment's thread. Return to all 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;
}
void square_sum(int n,int count){
}