# Thread: Closest prime number in an array?

1. ## Closest prime number in an array?

Code:
```#include <stdio.h>

int main(){
int N,min,max,i,j,temp;

printf ("Enter N.\n"); scanf ("%d",&N);

int polje[N];

for (i=0;i<N;i++){

printf ("Enter %d. number:\n",i+1); scanf ("%d",&temp);
polje [i] = temp;
}

i=0;
max = polje[i];

for (i=1;i<N;i++)
if (polje[i]>max) max = polje[i];

i=0;
min=polje [i];

for (i=1;i<N;i++)
if (polje[i]<min) min = polje[i];

printf ("%d is biggest, and %d smallest.\n",max,min);

// Find and print out only the closest prime number to the max
//Need help here...

return 0;
}```

2. google "c code generate prime numbers"

3. What's the plan to find this prime number? Two options pop out:

1) find all the primes < the max number, and keep the highest one

2) begin testing the odd numbers (in decreasing value), starting with max-1 (or -2 if max is even), and find the prime that way.

The Sieve of Eratosthenes is well suited for #1, and Trial by division method is best suited for #2.

Wikipedia has a good article on the Sieve of E.

4. Wow, another post completely devoid of indentation and any accompanying questions or discussion. (And no, text inside the code block does not count)
Did you not learn something in your previous 17 posts?

5. Can you look this code? i wrote this at past, and finds all the prime number dividers of a number.You can use the d[i] array and a for loop to calculate your max prime number and the second max prime number.Deal with your codes

Code:
```

#include <stdio.h>

p(int asal)
{
int y=1;

int i4;
for(i4=2;i4<asal;i4++)
{
if (asal%i4==0)
{
y=0;
}
else
y=1*y;
}
return y;
}

int main()
{
int x,i,i1,i2,r;
int f=0;
int c[100];
int a=1;
int si=0;
int cntr=0;
int kontrol=1;
int d[100];
int ix=0;

for(i1=0;i1<=50;i1++) c[i1]=0;

printf("enter a number");
scanf("%d",&r);

for(i1=0;i1<=100;i1++)
{
c[i1]=0;
}
x=r;
for(i=2;i<=x; )
{
si=si+1;
if(si%20==0) i++;

if ((x%i==0)&&(p(i)==1))
{
c[f]=i;
f++;
x=x/i;

}
else
{
i++;
x=r;
}
}
printf("\nbolenleri:");

for(i2=0;i2<=99;i2++)
{

if ((c[i2]>1))
{
d[ix]=c[i2];
printf(" %d, ",d[ix]);
cntr++;
ix++;
}
}
for(i=0;i<cntr;i++)
{
i2=i+1;
for( ;i2<cntr;i2++)
{
if (d[i]==d[i2]){
kontrol=0;
}
else
kontrol*=1;
}
}
if (kontrol==1)
printf("\nCarpanlari arasinda, ayni asal sayi yoktur.(there are no same prime number divider ");
else
printf("\nCarpanlari arasinda, ayni asal sayidan bulunmaktadir.(there are more than 1 same prime number dividers ");

scanf("%d",&a);

}```

6. Originally Posted by rac1
Can you look this code?
Indent the code properly

Then I'll look at it.