Some coding questions.

I am currently in the process of learning C++ (I started about a week ago), and the book I have been reading (Jumping into C++) asked me to create a code of which displays all numbers from 0-100, but only if their prime factors add up to 7. So I have spent 4 days trying to figure out how to do this, and I have still been unsuccessful. I have gotten a fair way into it, but have yet to get it working (I use GCC compiler for Windows, if that is important or something). If you could help, that would be greatly appreciated. But please, since I was given this task as a test to make sure I understand all about "figuring out the algorithm", please, do not tell me exactly what to do to get it going, but if you could answer some questions or *hint* at where to go next, thanks.

My current questions are:
1) How can I clean up this code a bit more, it seems a bit messy right now to me.
2) For each of my "if" statements in the "main" function, excluding the first one, I get an error saying "IOS C++ forbids comparison between a pointer and an integer". When I googled this error, I found results where people wrote a "string" instead of a "char"...I did not do either. Help?
3) If you think hinting where to go won't ruin my thing about having to figure it out myself, hint just one thing please?

My code (sorry it's so large! Can't find "spoiler's" or anything to make it smaller):
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include using namespace std; bool isDivisible (int number, int divisor); bool notPrime (int number); int factor_test = 2; int prime_factor1; int prime_factor2; int main() { for ( int i = 1; i < 100; i++ ) { if ( notPrime( i ) ) { if ( i % factor_test == 0 && i / factor_test == notPrime ) { switch ( factor_test ) { default: factor_test == prime_factor1; if ( i % prime_factor1 == 0 && i / factor_test == notPrime ) { switch ( factor_test ) { default: prime_factor1 == prime_factor2; } } } } else { factor_test++; if ( i % factor_test == 0 && i / factor_test == notPrime ) { factor_test == prime_factor2; } } } else { return false; } } } bool notPrime (int number) { for ( int j = 2; j < number; j++) { if ( isDivisible( number, j ) ) { return true; } else { return false; } } } bool isDivisible (int number, int divisor) { return number % divisor != 0; }``````
Last edited on
I don't understand.
Do you mean that you can display only numbers from 1 to 100 not divisible for 2,3,5 and 7? (prime numbers until 7, excluding prime number "1")
Last edited on
notPrime is a function. Hint: look up how to call a function.
I know how to call the function, I called it. Didn't I?? Maybe I did it wrong...

@Nobun, sorry if I confused you. The program should: Check if the number is NOT Prime, this way it has prime factors. Then, figure out what each of the prime factors are, add them all together, and display the number if those factors add to equal 7. Does that make more sense?

So 100 prime factors are: 2, 2, 5, and 5, equaling 14 (so 100 would not be displayed). (100/2=50, 50/2= 25, 25/5=5, 5/5=1)

EDIT: Fixed the error problem by replacing "i / factor_test == notPrime" with "i / factor_test != 1".
Last edited on
 I know how to call the function, I called it. Didn't I??

No. And it wouldn't matter if you did. notPrime is notCorrect.

 ``1234`` ``````bool notPrime (int number) { return isDivisible(number,2) ; }``````

is equivalent to your current implementation.
But that would not work properly. Not every number divides by 2, and not all odd numbers are prime. So I cannot keep it as 2 otherwise some numbers that are not prime will not be checked in the program.
1) Your code is not messy. Messy code is code that isn't organized well, such as having multiple commands on the same line, or not using indents, etc etc. Example :

 ``12345`` ``````#include Mymessycode.h using namespace std; int main(){int x; x=0; if(x==1){x=x++;}else {return 1;}return 0;}``````

What I believe you are talking about is efficiency, and that all boils down to your math knowledge. You don't need to be excused on how much you know, because we are all always learning.

2) When using if statements and loops, you need to use boolean to set parameters. For example, If i want a number between 1 and 100, I would use
 ``1234`` ``````if (x < 0 && x >= 100) { x = x++; }``````

the same applies to loops. Try re-writing the conditions on your if statements and loops with this in mind and you should find your problem solved.

3) Get yourself acquainted with what different types there are. For example, what the difference is between an int and a float, or what the definition of a char or string is. For example, the number 1 can be an int or char. the numbers 1 and 2 would be a string.

Hope this helps.
 I know how to call the function, I called it. Didn't I?? Maybe I did it wrong...

Look again at lines 18, 24 and 37. Apologies if I'm being too vague - but you did say you wanted "hints" :)
Seanharrs wrote:
But that would not work properly.

Exactly. Just as your code which is entirely equivalent does not work properly.

roger911 wrote:
2) When using if statements and loops, you need to use boolean to set parameters. For example, If i want a number between 1 and 100, I would use

 ``1234`` ``````if (x < 0 && x >= 100) { x = x++; }``````

I don't know what, exactly, you were attempting to accomplish with that code, even given the stated intention. The if condition can never evaluate to true (x can never be both less than zero and greater than or equal to 100) and `x=x++` lives in the land of undefined behavior.
you mean:

if (x > 0 && x <=100)
{
x++;
}

?

Also, I fixed my code so there is no compiler errors, which happened at lines 18,24,37. Now my only real problem is assigning each number to a value, without having to use a switch case for every number from 1-100 just to make sure it gets everything.

EDIT: That probably made no sense. I mean when I am having factor_test assign it's current number to prime_factor1 then prime_factor2, etc. Trying to do this without messing it up. Technically, at this part in the book I should not know about arrays, but seeing as how I read ahead to that part, would a single-dimensional array work here?
 create a code of which displays all numbers from 0-100, but only if their prime factors add up to 7

I'm going to post you an example answer so you understand how to better solve the problem. Mostly because I just wrote the prime calculation method for another thread :)

You have over complicated your solution by introducing things like switch statements when you only need 2 for loops and 2 if statements. A switch statement is to only be used when you know every possible outcome in advanced and need to type them all out (e.g enums)

You need to create your solution in steps as well. After you complete each step check to ensure it works correctly before moving on to the next one.
Step 1. Find all of the factors
Step 2. Check if each factor is prime
Step 3. Find sum total of all prime factors

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````#include #include #include using namespace std; // This is a recursive method that will find if a number is prime or not. bool IsPrime(unsigned& test_number, unsigned check_number) { ++check_number; if (test_number <= check_number) return true; if (test_number % check_number == 0) return false; return IsPrime(test_number, check_number); } int main() { // loop from 0 to 100 for (unsigned i = 0; i <= 100; ++i) { // keep a total of the summed prime numbers unsigned prime_factor_total = 0; // loop through the factors for this number. Start from // 2 because we know everything is divisable by 1 and it's not // a prime for (unsigned factor = 2; factor <= i; ++factor) { // Check if number is equally divisiable by factor if (i % factor == 0) { // Check if factor is a prime. unsigned test_factor = factor; if (IsPrime(test_factor, 1)) { // add prime factor to total prime_factor_total += factor; } } } // check if prime factor total is 7, if so print if (prime_factor_total == 7) cout << "Match: " << i << endl; } cout << "Done" << endl; return 0; }``````
Last edited on
That would work, and is neat...but, again (I seem to be really bad at explaining this), that isn't the task I am trying to solve. Your code there prints it if there are 7 prime factors for the number, right? Mine is trying to print it only if the factors add to give 7.

e.g. the number 10. Divides by 5, then by 2.

factor1 = 5
factor2 = 2

factor1 + factor2 = 7, so print the number.
I thought I know how to read code, but I found my self really confused when trying to understand something like that:
( i % factor_test == 0 && i / factor_test == notPrime )
I don't want you to explain to me what you tried to do here, just use brackets.
The compiler does have strict rules on operator precedence. But humans often fail to remember or apply them to understand the final result.
did you mean writing
( ((i % prime_factor1) == 0) && ((i / factor_test) == notPrime ))?
if yes, just write that.

besides, I didn't understand what did you say that forcing you to use the switch statements? I mean- what wrong happens when you remove them?

Nothing...? I can remove them, I just had them in there because I was using lots of switch cases, but then removed all but default, rather than just turning it to an if/else.
@Seanharrs you asked for the "prime factors" that sum to 7. I have provided you with code that code that.