Friday, January 13, 2012

Camarical Number Checker Camarical Number is a number which is not prime but has >=3 prime factors.

#include<stdio.h>
#include<math.h>


long num;


int is_prime()
{
int i, loop;
if (num == 2) return 1;
loop = sqrt(num);


for(i = 3; i <= loop; i += 2)
if (num % i == 0) return 0;
return 1;
}
long long modulus(long long n, long power)
{
long long mod;
if (n == 0)      return n;
if (power == 1)  return n;


    mod = n * n;
mod = mod % num;
mod = modulus(mod, power / 2);


if (power % 2 == 1)
{
        mod = mod * n;
mod = mod % num;
    }
return mod;
}


int is_carmichael()
{
long long i;
for(i = 2; i < num; i++)
{
if (i != modulus(i ,num) ) return 0;
}
return 1;
}


int main()
{
    while(scanf(" %ld", &num) == 1 && num)
    {
if( is_prime() ) printf("%ld is normal.",num);
else
{
if (!is_carmichael()) printf("%ld is normal.", num);
else printf("The number %ld is a Carmichael number.", num);
}
printf("\n");
}
return 0;
}
/*
Camarical Number:561 1105 1729 2465 2821 6601 8911 10585 15841 29341 41041
Not Camarical : 1 To 560 && so on...
*/

No comments:

Post a Comment