A better way to do it?

Started by
24 comments, last by JoeCooper 13 years, 6 months ago
Quote:Original post by D4rk_Angel
Yeah, it's a number that you cannot factorize, right?
That's the point of the exercise :)
You're supposed to write an algorithm that can determine if a number can be factorised, and then use this algorithm to test the numbers from 1 to 100.

These links should give you an idea of how to write your algorithm:
The C++ Modulus Operator
Primality test
Advertisement
Quote:Original post by agisler
You need to use a loop that checks within the loop if the current number is 100. Example:

for (int i=100;i<=100;i++)
{

//check if "i" is a prime number}

I realize it's a typo, but just to avoid any confusing for OP: you start counting at one (and you can skip the even numbers obviously). [smile]
Edit Bug fix, pointed out by SiCrane.

for (int test_value = 1; test_value <= 100; test_value++){    if( is_prime(test_value) ){        cout << test_value << endl;    }}

All that remains is to write the function called is_prime().

[Edited by - AngleWyrm on October 10, 2010 10:32:00 AM]
--"I'm not at home right now, but" = lights on, but no ones home
Quote:Original post by AngleWyrm
*** Source Snippet Removed ***
All that remains is to write the function called is_prime().


That algorithm will miss the prime 2.
Thanks for everything, you guys. But I don't think that I'm at that level just yet.
Can you describe a procedure for determining if a number is prime or not?

This is the heart of programming; procedural thinking.

Forget about the language for a minute and try to describe a procedure. If I give you the number 42, can you describe a process that proves it is or is not a prime?
It is not prime because:

42 | 2
21 | 3
7 | 7
1
42 is a composite number.
Quote:Original post by JoeCooper
If I give you the number 42, can you describe a process that proves it is or is not a prime?

Start with the square root of 42, and work down towards 2.
Sqrt(42) = 6.48, so begin with 6.
42/6 = 7. Oops, found an exact match, so it's not prime.
If 6 hadn't divided evenly into 42, we'de move on to 5:
42/5 = 8.4, not an integer, keep going
42/4 =10.5, not an integer, keep going
42/3 =14. Oops, found an integer divisor greater than 1, so not a prime
42/2 =21. Found an integer divisor greater than one, so not prime.

But really, only have to stop at the first one that divides evenly, which was 6. Putting it into source code:

#include <iostream>#include <cmath>using namespace std;bool is_prime(int test_value);int main(){    for(int test=1; test<=100; test++){        if( is_prime(test) ){            cout << test << endl;        }    }}bool is_prime(int test_value){    int divisor = (int) sqrt(test_value);    while(divisor >= 2){        if (test_value % divisor == 0){            return false;        }        divisor--;    }    return true;}


[Edited by - AngleWyrm on October 10, 2010 11:49:26 AM]
--"I'm not at home right now, but" = lights on, but no ones home
I'm such a dunce, I wrote a post expanding on all of that and how to check if a number is evenly divisible and completely forgot about the modulus operator. That little % sign in AngleWyrm's example is called the modulus operator and is used to check if a number is evenly divisible by another number. It is a critical step in finding primes. Go look it up to better understand the sample.

I think it might also be worth explaining casting. In Wyrm's example:

int divisor = (int) sqrt(test_value);


sqrt() takes and returns a double, which is like a float but more precise. It lets you store non-whole numbers. sqrt(42) would yield 6.48. But what he wants is 6, not 6.48. So it needs to be converted somehow...

Recall than the int datatype cannot store the number 6.48. If you wrote code like this:

int divisor = sqrt(test_value);


It would take the value 6.48 and shove it into an int slot. (The computer will convert it on the fly.) The value 6 will be stored, which is what's wanted.

But the compiler will give you a warning about an implicit cast. It gives you this warning because in that conversion, data is thrown away, and if you wrote such code on accident, it can break your program. Under some circumstances it could also misunderstand what you want. So...

You must communicate to the compiler that you specifically want to cast it from float to integer.

By putting the target data type in parenthesis before a value, you perform an explicit cast. Here's the code again with the cast highlighted:

int divisor = (int) sqrt(test_value);


Putting that before the value tells the compiler "I want to convert this from its current data type to an int; I understand what this does and have made a conscious decision".

And that will get rid of the warning.
An exercise means: exercise.

You are supposed to do it on your own. Otherwise it's pointless. We really shouldn't help with this kind of stuff.

OP: Do it on your own. If you are not satisfied with the goodness of it, write a better solution.

Okay, forget it...

This topic is closed to new replies.

Advertisement