ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1023. Buttons

i'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
Posted by TheBlaNK 1 Mar 2002 18:17
#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(-)
Posted by Miguel Angel 3 Mar 2002 04:22
> #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
Posted by TheBlaNK 3 Mar 2002 20:36
> > #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 :) (-)
Posted by Miguel Angel 4 Mar 2002 13:00
> > > #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
Posted by TheBlaNK 7 Mar 2002 00:54
> > > > #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;
> > > > }