Hey guys.
I don't know why I'm getting OLE from my code. I don't seem to encounter any infinite loops whatsoever...
Also what happens if you put in '1' ? Is it just 1 = 1?
Help appreciated
[cpp]#include <stdio.h>
#include <math.h>
bool *primes;
long c=46341;
long nextprime(long i)
{
for (i++;i<=c;i++)
if (primes[i-1]) return i;
return -1;
}
bool isprime(long long n) {
if (n==2 || n==3) return true;
if (n==1) return false;
for (long i=2;i<=(long)sqrt(n); i=nextprime(i))
if (n%i==0) return false;
return true;
}
int main()
{
long long x;
int i,k;
primes=new bool[c];
primes[0]=false; primes[1]=primes[2]=true;
for (i=3;i<c;i++) primes[i]=true;
for (i=3,k=2;k*k<=c;i+=k)
{
if (!primes[k-1] || i>=c) {k++; i=k-1;}
else primes[i]=false;
}
long counter;
bool y;
while (scanf("%lld",&x)==1)
{
if (x==0) break;
counter=2;
if (x<0) {printf("%lld = -1 x ",x); x*=-1;}
else printf("%lld = ",x);
if (x==1 || isprime(x)) { printf("%lld\n",x); continue; }
for (y=false;;counter=nextprime(counter))
{
if (counter*counter>x) { printf("%lld\n",x); break; }
while (x%counter==0) {
x/=counter;
if (x==1) {printf("%ld\n",counter); y=true; break;}
else printf("%ld x ",counter);
}
if (y) break;
}
}
delete [] primes;
return 0;
}
[/cpp]