Tuesday, November 12, 2013

C program to find the Pythagorean prime!!

Below C program is to find the  Pythagorean prime of a given number. If sum of the squares of any to numbers is prime number, then that number is called Pythagorean prime.

e.g
 5 = 1*1 + 2*2.
 13 = 2*2 + 3+3

All prime numbers are not pythagorean primes. for example 7. 7 is a prime number and not pythagorean prime. So to find the pythagorean prime, first we need to the given number is prime or not. If the given number is not prime, we can stop the process there itself.

#include<stdio.h>

int fact(int x)
{
  if(x==1)
   return 1;
  fact(x*(x-1));
}
int isPrime(int x)
{
  int i;
  for(i=2;i<x;i++)
  {
    if(x%i == 0)
     return 0i;
  }
  return 1;
}
int main()
{
  int number;
  int found = 0;
  int i=0,j=0;
  printf("enter the number\n");
  scanf("%d",&number);
  if(!isPrime(number)){
   printf("To find Pythagorean prime, number should be prime \n");
   return 0;
  }
  for(i=1;i<500;i++)
  {
     for(j=1;j<500;j++)
     {
        if(i*i+j*j==number)
        {
         found = 1;
         break;
        }
     }
     if(found)
      break;
  }
  if(found)
   printf("%d is Pythagorean prime\n",number);
  else 
   printf("%d is not Pythagorean prime\n",number);
}

Output:

$ ./a.out 
enter the number
137
137 is Pythagorean prime
$ ./a.out 
enter the number
23
23 is not Pythagorean prime
$ ./a.out
enter the number
1234
To find Pythagorean prime, number should be prime

No comments:

Popular Posts