Thread: Goldbach's conjecture

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    116

    Goldbach's conjecture

    Hey guys:

    One of the assignment for class is Goldbach's conjecture. Below is my code, but I am having problems with the actually computation. It goes into an infinite loop when it prints the first conjecture which is 4 =2 +2. To me, it doesn't seem like my code would do that, but obviously it does.

    Can someone help me out and figure out why it's going to an infinite loop?

    Thanks



    Code:
    #include<stdio.h>
    #include<stdbool.h>
    
    
    bool is_prime(int );
    
    void goldbach(int n, int m)
    {
      int i, j, k;
    
    
      for(i=n; i<= m; i+2)
        {
          for(j=2; j<= m; j++)
            {
              if(is_prime(j))
                {
                  for(k=2; k<= m; k++)
                    {
                      if(is_prime(k))
                        {
                          if( i == (j + k))
                            printf("&#37;d = %d + %d\n", i, j, k);
    
                        }
                    }
                }
            }
        }
    
    
    }
    
    bool is_prime(int j)
    {
      int divisor;
    
      for(divisor = 2; divisor*divisor <= j; divisor++)
        {
          if(j % divisor == 0)
            return false;
        }
    
      return true;
    
    }
    
    int main(void)
    {
    
      int i, j, n, m;
    
      printf("\nEnter the value of n: ");
      scanf("%d", &n);
    
      printf("Enter the value of m: ");
      scanf("%d", &m);
    
    
      /* ********** SETS BOUNDRIES CORRECTLY ********** */
    
      if(n < 4)
          n = 4;
    
      if((n % 2) != 0)
          n = n + 1;
    
      if((m % 2) != 0)
        m = m - 1;
    
      /* ********************************************* */
    
    
      goldbach( n, m);
    
      printf("\n");
    
    
    
      return(0);
    }
    Last edited by dcwang3; 10-13-2008 at 05:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Goldbach's Conjecture
    By cashmerelc in forum C Programming
    Replies: 7
    Last Post: 07-19-2010, 10:41 PM
  2. Godbach conjecture!!
    By Leojeen in forum C Programming
    Replies: 10
    Last Post: 04-20-2008, 06:42 PM
  3. Replies: 3
    Last Post: 11-13-2005, 02:53 AM
  4. Goldbach Conjecture
    By StarOrbs in forum C++ Programming
    Replies: 19
    Last Post: 03-17-2005, 04:42 PM