Thread: Help With Struggles

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    76

    Help With Struggles

    I'm in a class right now and have really done pretty well so far, if I say so myself, but am struggling with nested loops. It took me quite awhile to get a program that catches prime numbers to work so now I want to practice.

    Does anyone know the first 100 humble numbers program. Before I kill myself with it I want to know if this can be done with just nested loops, no array's or pointers. Basically this is what I think I need to tell the computer to do.

    1. Start a loop that goes from the first humble number to the 100th.
    2. Test each number to make sure is divisable by 2,3,5 or 7 if it isn't dicard it (use while loop with 4 || statements?
    3. Discard anything that isn't a multiple of those numbers.
    4. Now starting from 11, the first prime number that isn't 2,3,5 or 7, I need to test any multiples of 2,3,5 or 7 up to the number to get any prime numbers between 11 and number.
    5. Discard anything that is not prime.
    6. Next I need to test the number to see if they are multiple by any of the primes pulled from the last step.
    7. If they are they are not humble.
    8. If they aren't they are humble.

    Right track??? I have started some coding but it doesn't work, I can post it if necessary. This is not homework, just practice.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It seems like it's worth a shot - give it a go!
    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.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Post your code when you're finished, if there's something wrong.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dolfaniss View Post
    I'm in a class right now and have really done pretty well so far, if I say so myself, but am struggling with nested loops. It took me quite awhile to get a program that catches prime numbers to work so now I want to practice.
    There's no harm in being experimental. I've tried a lot of stuff that didn't work, more than did, in fact. It's all part of the learning experience. (Read my signature...)

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    76
    I've been slowly working at it, the program I'm going to post runs and displays a pontential humble number and and prime number between 11 and the number. Now I need to test them one at a time, using modulus, vs the number and if I get a 0, the number is not hunble. I'm having a tough time with this, any clues?

    Code:
    #include <stdio.h>
    
    int main (void)
    
    {
    	int test, num1;
    	int num;
    	int humble = 0;
    	int is_prime = 0;
    	int prime = 0;
    
    
    	//Start for loop at 2, 1 declared not humble
    	for (num = 2; num < 23; num++)
    	
    
    		//Give numbber inital test
    		if (num % 2 == 0 || num % 3 == 0 || num % 5 == 0 || num % 7 == 0)
    		{
    				//Assign num to num1
    
    				for (num1 = 11; num1 < num; num1++)
    				{
    				//Start for loop to get all primes between starting at 11
    				for (test = 2; test < num1; test++)
    				{
    					//Test for prime number
    					is_prime = num1 % test;
    
    					//not prime 
    					if (is_prime == 0)
    
    					{
    						//printf ("%d\n", test);
    						break;
    					}
    				
    				}
    
    					//If prime
    					if (test == num1)
    					{
    						prime = num1;
    						printf ("%d is a prime number in between 11 and %d\n", test, num);
    					}
    				
    				}
    		//printf("%d\n", num);
    					
    		
    }
    
    }

Popular pages Recent additions subscribe to a feed