Objective:Understand the logic and process of developing a program.

Problem:Find the prime numbers of a certain number X.

How would we solve this problem in real life?

Example: X = 32

32 / 2 = 16

16 / 2 = 8

8 / 2 = 4

4 / 2 = 2

2 / 2 = 1

FINISH: 2*2*2*2*2 = 32

What concepts should we know before we begin writing our program?

Before you write any programs, you should know the basics of the language your writing on, in this case C. If you're reading this, I assume you know about data types, loops, etc. (the basics).

How can we imitate this prime number finding procedure in a program?

Well, first we might want to notice some patterns. We see that we start with a number X, and we divide that by a prime number Z. The result we call it Y. If the division X / Z gives a whole number Y (without any remainder), then we divide Y by Z, until we get a remainder in the division. When we get a remainder, we increment Z to the next prime number, unless Y is also a prime number (this is hard to figure out, unless you have a list of prime numbers).

We know what to do, now lets identify patterns!

Now that we know the process to find the prime numbers of X, we want to find a pattern so we can loop through it instead of having to manually code in the same thing over and over again.

Referring to the previous step, let Y be a variable, X a constant, and Z an array of prime numbers (constants).

Let Z have the following values: 2, 3, 5, 7, 11 (a total of 5 different values). We access the first value by saying Z[0], which equals 2. Then Z[1] = 3, and so on until Z[4] = 11.

We see that we divide Y by Z[index] and assign Y the value of Y / Z (Y = Y/Z) if and only if Y % Z is equal 0 (the remainder of Y / Z is 0). If Y / Z is not a whole number (without decimals, in other words, with remainder) we proceed to the next prime number.

Procedure:

- Once we have our environment set, we can proceed by initiating variables that we know are necessary. After that the first thing we should do is find out what is the number X we are finding the primes for:

Code:#include <stdio.h> //main() is what the program executes when you launch it... int main() { char input_buffer[20]; //A string where we store the input from the user (Number X) unsigned int factorial_list[5]; //An array of u ints (no negatives, so we can have a 2^32 max. number, google C data types for more info) unsigned result[128]; //Store the primes with find from number X, we can store 128 of them... more than enough... unsigned int x = 0; //Where we store the integer the user inputs unsigned int y = 0; //This is the changing variable we talked about unsigned int index_A = 0; //Index used to navigate through factorial_list[] unsigned int index_B = 0; //Index used to navigate through the result[] //Initiate the values for the factorial_list factorial[0]=2; factorial[1]=3; factorial[2]=5; factorial[3]=7; factorial[4]=11; //Initiate values for result[] for(index_B; index_B < 128; index_B++) { result[index_B] = 0; } index_B = 0; printf("Enter a positive 32bit integer number: "); //I announced the restraints for the number X if(fgets(input_buffer, sizeof input_buffer, stdin) == 1) //If a int is in input... { if(sscanf(input_buffer, "%d", &x) != NULL) //If we take the number and assign it to int x successfully (&x is the address to the int x, so we assign the put the value in that address, google pointers) { y = x; //Assign y the value of number X } } return 0; }- If the input passes through the if statements, then it's a valid integer trk with. We already assigned Y the value of X, so now all that is left is create the loop.

Code://Import standard input and output C library #include <stdio.h> //main() is what the program executes when you launch it... int main() { char input_buffer[20]; //A string where we store the input from the user (Number X) unsigned int factorial_list[5]; //An array of u ints (no negatives, so we can have a 2^32 max. number, google C data types for more info) unsigned result[128]; //Store the primes with find from number X, we can store 128 of them... more than enough... unsigned int x = 0; //Where we store the integer the user inputs unsigned int y = 0; //This is the changing variable we talked about unsigned int index_A = 0; //Index used to navigate through factorial_list[] unsigned int index_B = 0; //Index used to navigate through the result[] //Initiate the values for the factorial_list factorial[0]=2; factorial[1]=3; factorial[2]=5; factorial[3]=7; factorial[4]=11; //Initiate values for result[] for(index_B; index_B < 128; index_B++) { result[index_B] = 0; } index_B = 0; printf("Enter a positive 32bit integer number: "); //I announced the restraints for the number X if(fgets(input_buffer, sizeof input_buffer, stdin) != NULL) //If a int is in input... { if(sscanf(input_buffer, "%d", &x) == 1) //If we take the number and assign it to int x successfully (&x is the address to the int x, so we assign the put the value in that address, google pointers) { y = x; //Assign y the value of number X for(index_A=0; index_A < 5; index_A++) //Do while index_A is less than 5, because 5 is the total amount of prime numbers in the list. { if(y % factorial[index_A] == 0 && y != 0) //if the remainder of y/primenumber = 0 AND y is NOT 0. Why? Because if y =0, then y/prime = 0 remainder 0. { y = y / factorial[index_A]; //Assign new value to Y result[index_B] = factorial[index_A]; //store a valid prime in the result array index_A--; //decrease index_A so we repeat with the same factor again index_B++; //increase index_B so we don't overwrite the current result in the next entry } } } } return 0; }- Lastly, we display and prove that our result is correct:

Code://Import standard input and output C library #include <stdio.h> //main() is what the program executes when you launch it... int main() { char input_buffer[20]; //A string where we store the input from the user (Number X) unsigned int factorial[5]; //An array of u ints (no negatives, so we can have a 2^32 max. number, google C data types for more info) unsigned result[128]; //Store the primes with find from number X, we can store 128 of them... more than enough... unsigned int x = 0; //Where we store the integer the user inputs unsigned int y = 0; //This is the changing variable we talked about unsigned int index_A = 0; //Index used to navigate through factorial unsigned int index_B = 0; //Index used to navigate through the result[] //Initiate the values for the factorial array factorial[0]=2; factorial[1]=3; factorial[2]=5; factorial[3]=7; factorial[4]=11; //Initiate values for result[] for(index_B; index_B < 128; index_B++) { result[index_B] = 0; } index_B = 0; printf("Enter a positive 32bit integer number: "); //I announced the restraints for the number X if(fgets(input_buffer, sizeof input_buffer, stdin) != NULL) //If a int is in input... { if(sscanf(input_buffer, "%d", &x) == 1) //If we take the number and assign it to int x successfully (&x is the address to the int x, so we assign the put the value in that address, google pointers) { y = x; //Assign y the value of number X for(index_A=0; index_A < 5; index_A++) //Do while index_A is less than 5, because 5 is the total amount of prime numbers in the list. { if(y % factorial[index_A] == 0 && y != 0) //if the remainder of y/primenumber = 0 AND y is NOT 0. Why? Because if y =0, then y/prime = 0 remainder 0. { y = y / factorial[index_A]; //Assign new value to Y result[index_B] = factorial[index_A]; //store a valid prime in the result array index_A--; //decrease index_A so we repeat with the same factor again index_B++; //increase index_B so we don't overwrite the current result in the next entry } } index_B = 0; //Set index to 0 again... x = 1; //Set x to 1 so we can multiply all the primes with it and prove if it's right or wrong printf("\n"); //new line while(result[index_B] != 0) //While the results haven't ended (remember, we initiated all the array slots with a 0 value, those that have results are not 0s!!) { x = x*result[index_B]; //Multiplying all the primes in the results together... printf("%d*", result[index_B]); //print into screen the multiplication we're doing index_B++; } printf("1=%d",x); //last prime would be 1, that's inferred, but we put it any ways to complete the multiplications, otherwise we would have an extra "*" printf("\n"); } } return 0; }

That's it, not that hard, eh?

Compiled under GCC and tested in Fedora.

All the work was done by me, Riddle, please don't copy, unless you link back.

Results 1 to 11 of 11

- 10 Nov. 2009 06:04am #1
## Think like a programmer: Problem Solving in C (Beginner)

Last edited by Riddle; 10 Nov. 2009 at 06:30pm.

- 10 Nov. 2009 06:12am #2
This is helpful in putting the programmers in the right mindset, though I still have problems figuring out how to put code together to do what I want. For example, I think "okay I want this done." I then try figure out what it needs for it to be done and I try to attempt it, but it usually leaves me stumped. Then I'd go ask a friend and he'd give me a logical explaination and even show me how he got it without using ANY foreign code. All he did was put two and two together and it go results. How do I obtain this mindset does it just take practice?

Hopefully you understand what I mean?Last edited by HTML; 10 Nov. 2009 at 06:16am.

- 10 Nov. 2009 04:06pm #3
Yes, practice is the key. Problem solving has a lot to do with experience, logic, and math. If you understand the fundamentals of logic (for example: if...then statements) and you know your algebra, you can solve many different problems.

Practice makes perfection, the more you practice the more you learn, and the more you learn the easier it is to apply problem solving skills for everyday problems.

For example, most engineering introductory courses for Electrical Engineering, Computer Engineering, and Computer Scientist majors (all deal with problem solving and electronics) are based in problem solving through trivial riddles. I have a book full of riddles, and I can post some for people to practice if it's necessary.

- 10 Nov. 2009 05:22pm #4
Great guide. If you plan on doing more of these I might actually learn how to program

Keep it up!

- 10 Nov. 2009 06:32pm #5
Thanks Shane.

Hopefully other people will start learning and create their own programs instead of leeching of other's work all the time.

- 10 Nov. 2009 06:35pm #6
Well if you keep posting such good guides we'll have more programmers.

Good job again!

- 11 Nov. 2009 02:17pm #7
**Another great guide, I'll probably have a good use of these guides, thanks to you**

- 16 Nov. 2009 06:11am #8

- Age
- 26
- Join Date
- Nov. 2009
- Location
- Anaheim, California

- Posts
- 1,065
- Reputation
- 99
- LCash
- 200.00

In the words of some of the greatest programmers that have ever brought their genius upon us.

"It is not the completion, but the methodology to obtain completion that makes programming so brilliantly beautiful."

<3

- 16 Nov. 2009 05:53pm #9
Great quote! Do you know who said that?

- 16 Nov. 2009 09:28pm #10

- Age
- 26
- Join Date
- Nov. 2009
- Location
- Anaheim, California

- Posts
- 1,065
- Reputation
- 99
- LCash
- 200.00

- 16 Nov. 2009 10:24pm #11
I agree with you, it does feels like it's true.