|
|
back to boardi've got AC by this program(inside) but it take 3 sec .Can anybody give me some hint to solve it in 0.02 sec #include <stdio.h> int main() { long n,limit,i; scanf("%ld",&n); limit=n/2; for(i=3;i<=limit;i++) if(n%i==0) { n=i; break; } printf("%ld",n-1); return 0; } By Maths, you only need to check to the square root of limit(-) > #include <stdio.h> > int main() > { > long n,limit,i; > scanf("%ld",&n); > limit=n/2; > for(i=3;i<=limit;i++) > if(n%i==0) { n=i; break; } > printf("%ld",n-1); > return 0; > } but if n=2*(prime number) eg. n=26 and another case > > #include <stdio.h> > > int main() > > { > > long n,limit,i; > > scanf("%ld",&n); > > limit=n/2; > > for(i=3;i<=limit;i++) > > if(n%i==0) { n=i; break; } > > printf("%ld",n-1); > > return 0; > > } So, two special cases arise :) (-) > > > #include <stdio.h> > > > int main() > > > { > > > long n,limit,i; > > > scanf("%ld",&n); > > > limit=n/2; > > > for(i=3;i<=limit;i++) > > > if(n%i==0) { n=i; break; } > > > printf("%ld",n-1); > > > return 0; > > > } Thank you very much for ur help :-D > > > > #include <stdio.h> > > > > int main() > > > > { > > > > long n,limit,i; > > > > scanf("%ld",&n); > > > > limit=n/2; > > > > for(i=3;i<=limit;i++) > > > > if(n%i==0) { n=i; break; } > > > > printf("%ld",n-1); > > > > return 0; > > > > } |
|
|