Thread: Closest prime number in an array?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    30

    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. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    google "c code generate prime numbers"

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    72
    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. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rac1 View Post
    Can you look this code?
    Indent the code properly

    Then I'll look at it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-27-2010, 12:57 PM
  2. finding the two closest numbers in an array
    By ominub in forum C Programming
    Replies: 9
    Last Post: 10-21-2009, 03:21 AM
  3. largest number prime number that can be produced...
    By ElemenT.usha in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 01:44 AM
  4. Replies: 3
    Last Post: 03-29-2005, 04:24 PM
  5. Replies: 9
    Last Post: 11-20-2003, 08:55 PM