Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#include <stdio.h>
#define nDigit 10
int nArray[nDigit];
int prime=1;
int isPrime(int number)
{
int i;
for(i=2;i<=number-1;i++)
{ if(number%i==0) { return 0; }}
return 1;
}
int nPow(int x,int n)
{
// x^n
int i;
int result=1;
for(i=1;i<=n;i++) { result*=x; }
return result;
}
void check(const int *v, const int size)
{
int i;
if (v != 0) {
int n=0;
for (i = 0; i < size; i++) {
n+=v[i]*nPow(10,size-1-i);
}
if(isPrime(n)==0) { prime=0; }
}
}
void permute(int *v, const int start, const int n)
{
int i;
if (start == n-1) { check(v, n); }
else {
for (i = start; i < n; i++) {
int tmp = v[i];
v[i] = v[start];
v[start] = tmp;
permute(v, start+1, n);
v[start] = v[i];
v[i] = tmp;
}
}
}
int calcDigit(int number)
{
int i;
int digit=0;
if(number==0) { return 1; }
for(i=number;i>0;i=i/10) { digit++; }
return digit;
}
void moveArray(int number)
{
int i;
int digit=calcDigit(number);
for(i=0;i<=digit-1;i++)
{
nArray[i]=number/nPow(10,digit-1-i);
number%=nPow(10,digit-1-i);
}
}
int main()
{
int i;
int num;
do{
scanf("%d",&num);
moveArray(num);
permute(nArray, 0, calcDigit(num));
if(num<=0) { break; };
{ if(prime==0) { printf("%d'in rakamlarinin tum kombinasyonlari asal degildir\n",num );}
else { printf("%d'in rakamlarinin tum kombinasyonlari asaldir\n",num );} }
prime=1;
}while(num>0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment