code deleted.
although it outputs correctly judge says wrong answer.please help.

Moderator: Board moderators
J&Jewel wrote:Here is my code I do not understand why this out Wrong Ans:..Plz Help me.....
#include<stdio.h>
int main()
{
int i;
long input,array[15]={561,1105,1729,2465,2821,6601,8911,10585,15841,29341,41041,46657,52633,62745,63973};
while(scanf("%ld",&input)==1)
{
if(input==0) break;
int k=0;
for(i=0;i<=14;i++)
{
if(input==array)
{
printf("The number %ld is a Charmichael number.\n",input);
k++;
break;
}
}
if(k==0) printf("%ld is normal.\n",input);
}
return 0;
}
Code: Select all
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
//adapted from prime_factorization()
bool isPrime(unsigned long x)
{
unsigned long i; /* counter */
unsigned long c; /* remaining product to factor */
c = x;
while ((c % 2) == 0) {
//printf("%ld\n",2);
c = c / 2;
}
i = 3;
while (i <= (sqrt((double)c)+1)) {
if ((c % i) == 0) {
//printf("%ld\n",i);
c = c / i;
}
else
i = i + 2;
}
if (c > 1) return (c == x);
return 0;
}
//calculates a^n(mod n)
unsigned long fermat(unsigned long a,unsigned long totheN,unsigned long modN) {
if (totheN==0)
return 1;
else if (totheN==1)
return a%modN;
else if (totheN%2==0)
return (fermat(a,totheN/2,modN)*fermat(a,totheN/2,modN))%modN;
else
return ((a%modN)*(fermat(a,totheN-1,modN)%modN))%modN;
}
bool carmnumber(unsigned long n)
{
for(unsigned int i = 2; i < n; i++)
{
if(i == fermat(i,n,n))
{
if(isPrime(n)) { return 0; break; }
return 1;
break;
}
else
{ return 0;
break;
}
}
}
int main()
{
double n;
cin >> n;
while(n != 0)
{
if((n-(int)n) > 0) { cout << n << " is normal.\n"; }
else {
if(carmnumber((unsigned long)n)) { cout << "The number " << n << " is a Carmichael number.\n"; }
else { cout << n << " is normal.\n"; }
}
cin >> n;
}
}
Well, precalculating really can take several seconds or even minutes, depending on what platform you use. So, what you can do is run a program that calculate this numbers and another program that returns this number instantly, using a switch case structure.Can someone give me a hint as to how to precalculate. I tried to precalculate by running through all the numbers from 2 to 65000 and putting it into an array if it was a carmichael number, but this wound up taking sooo much time.
Code: Select all
resolved after AC
instead of1729 is a Carmichael number.
Almost lost my head trying to find where the WA was...The number 1729 is a Carmichael number.