More fun with the chinese prime number algorigthm

This is a discussion on More fun with the chinese prime number algorigthm within the C Programming forums, part of the General Programming Boards category; I wrote a program that is supposed to take a chinese prime number factoring algorithm and print a list of ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    28

    More fun with the chinese prime number algorigthm

    I wrote a program that is supposed to take a chinese prime number factoring algorithm and print a list of x prime numbers. The code compiles with no errors and runs. But the program does nothing. Ive played around with it and havent been able to get anything to change. I don't quite know where my logic went wrong. Any ideas?

    Code:
    primes.h:
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int is_prime(int n); // function definition
    
    is_prime.c:
    
    #include "primes.h"	// Includes the header file
    
    int is_prime(int n)
    {
    	int k, limit; // Initialization of local variables
    
    	if (n == 2)	// This returns to '1' if n is equal to 2
    		return 1;
    	if (n % 2 == 0)	
    		return 0; // The modulus determines if the number is divisible by 2 (remainder) and returns a boolerian 1 or 0 depending on the number input
    	limit = n / 2; // This part of the code I dont understand
    	for (k = 3; k <= limit; k += 2)	// The for statement assigns a value of 3 to k and checks if the number is divisible by 3
    		if (n % k == 0)	// if the modulus of n and k (3) is equal to zero it returns a 0
    			return 0; // returns 0 for odd non-prime numbers
    	return 1; // returns 1 for prime numbers
    }
    
    main.c:
    
    #include "primes.h"	// includes the header file
    
    int limit, n;
    
    int main(void) // main program
    {
    	int i = 1; // counter variable
    	printf("PRIMES WILL BE PRINTED.\n\n"); // printed messages
    	printf("How many do you want to see?\n");
    	scanf("%d", &limit); // assigns the upper value of primes to find
    	while (i <= limit) { // loops until the incrementer reaches the end number of primes
    		is_prime(n);
                      if(n == 1)
                           printf("%6d:      %10d\n",i++,n); // prints out prime number and counter, then increments the counter
    		  else
    			  i++; // increment counter without printing 
         }
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    Perhaps calling
    n = is_prime(i);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Change this

    Code:
    	while (i <= limit) { // loops until the incrementer reaches the end number of primes
    		is_prime(n);
                      if(n == 1)
                           printf("%6d:      %10d\n",i++,n); // prints out prime number and counter, then increments the counter
    		  else
    			  i++; // increment counter without printing 
         }
    to
    Code:
        while (i <= limit) { // loops until the incrementer reaches the end number of primes
            n = is_prime(i);
            if(n == 1)
                printf("%6d:      %10d\n",i,n);
    
            i++;
        }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    28
    Thanks!

    It didnt quite fix the problem, but it got me going in the right direction, and now the program works like a champ.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In this function
    Code:
    int is_prime(int n)
    {
    	int k, limit; // Initialization of local variables
    
    	if (n == 2)	// This returns to '1' if n is equal to 2
    		return 1;
    	if (n % 2 == 0)	
    		return 0; // The modulus determines if the number is divisible by 2 (remainder) and returns a boolerian 1 or 0 depending on the number input
    	limit = n / 2; // This part of the code I dont understand
    	for (k = 3; k <= limit; k += 2)	// The for statement assigns a value of 3 to k and checks if the number is divisible by 3
    		if (n % k == 0)	// if the modulus of n and k (3) is equal to zero it returns a 0
    			return 0; // returns 0 for odd non-prime numbers
    	return 1; // returns 1 for prime numbers
    }
    limit is undeclared, but you use it in the loop anyway! You probably meant to change the first red line to
    Code:
    int k, limit = n; // Initialization of local variables
    Last edited by dwks; 09-01-2005 at 05:46 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help: How to write a prime number greater than one billion
    By kenryuakuma in forum C++ Programming
    Replies: 6
    Last Post: 12-19-2008, 08:55 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  3. prime number.
    By tdoctasuess in forum C Programming
    Replies: 13
    Last Post: 05-13-2004, 09:03 AM
  4. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-26-2001, 12:45 AM
  5. How is to check prime number?
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 10-05-2001, 12:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21