← Back to topics
Topic

RelativelyPrime

D
Dgleich
Can someone take a look into my code and why does it fails on 18 and 20 test case on time ? when I test it home for limit number it work's fast ?
http://www.z-trening.com/new/www/html/submit.php?submit=7100054306&subm_code=1
R
RobertGerbicz
As I can see your solution's complexity is f(n)=largest prime divisor of n,
so if n=p prime, then it takes O(n) time, too large!

You can factorize in O(sqrt(n)) time in every case. Use that.
D
Dgleich
I don't understand what are you meaning?
D
Dgleich
Solved please delete the topic...
m
msantl
http://www.z-trening.com/new/www/html/submit.php?subm_stat=1&submit=7100054585

what am i doing wrong ?
m
msantl
can anybody look at my code and write what am i doing wrong ?
h
halil
Change 'IsP'. Try:

bool isP(lnt a){
int q = sqrt ( a );
for(lnt x=2;x<=q;++x){
if(a%x==0)return false;
}
return true;
}
m
msantl
that's the same thing,
x*x <= a , or x <= sqrt(a)
s
smiljko
Try to do the same, without checking wheter the number is prime or not... :)
m
msantl
stil fails on time at 17. TC

http://www.z-trening.com/new/www/html/submit.php?subm_stat=1&submit=7100054767
s
smiljko
You don't need isP() function, I think you can simply delete her... :)
m
msantl
thanks for your help :)