breaking code into functions

as an exercise I have to write a program with specific functions. Find primes then put them into a vector then print them out.
The last 2 I think I can do its the fin d primes function I'm struggling with.
Here is my code and you will se what I mean...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cmath>
#include <vector>


bool is_prime(int number);

int main(int argc, const char * argv[]) {
    
    for(int prime_to_find{1}; prime_to_find <= 150; prime_to_find++){
        int counter{};
        for(int divisor{prime_to_find}; divisor >= 1; divisor--){
            if(prime_to_find % divisor == 0){
                ++counter;
            }//end if
        }//end inner for
        if(prime_to_find == 1){
            std::cout << prime_to_find << " ";
        }
        if(counter == 2){
            std::cout << prime_to_find << " ";
        }//end if
    }//end outer for
    
//    for(int i{1}; i <= 150; i++){
//        if(is_prime(i)){
//            std::cout << i << " ";
//        }
//    }
    
    std::cout << std::endl;
    
    return 0;
}

//bool is_prime(int prime_to_find){
//
//    //int prime_to_find{number};
//    int counter{};
//    for(int i{prime_to_find}; i >= 1; i--){
//        if(i % prime_to_find == 0){
//            ++counter;
//        }
//    }
//    std::cout << "counter: " << counter << std::endl;
//
//    return counter == 2 ? true : false;
//}
You have two versions now. I presume neither produces correct result. Which one should we look at?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>
#include <vector>

bool is_prime( int );

int main(int argc, const char * argv[]) {
    for ( int prime_to_find{1}; prime_to_find <= 150; prime_to_find++ ){
        if ( is_prime(prime_to_find) ) {
            std::cout << prime_to_find << " ";
        }
    }
    std::cout << std::endl;
}

bool is_prime( int prime_to_find ){
    int counter{};
    for (int divisor{prime_to_find}; divisor >= 1; divisor--) {
        if ( divisor % prime_to_find == 0 ) {
            ++counter;
        }
    }
    return counter == 2 ? true : false;
}

Your main() does not "put them into a vector", but lets skip that.

if ( divisor % prime_to_find == 0 )
In every case of that if, the divisor <= prime_to_find.
Only when divisor==prime_to_find does the condition become true.
For example, 1 % 42 == 1
(In your other version you have prime_to_find % divisor == 0.)


for ( int prime_to_find{1}; prime_to_find <= 150; prime_to_find++ )
Your loop starts from 1. Is 1 a prime? If not, then why test it at all?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* Sieve of Eratosthenes, no optimizations */
#include <iostream>
#include <bitset>
using namespace std;

int main()
{
    unsigned p[999] {2};
    unsigned const z(7919);
    unsigned m(0);
    unsigned n(3);
    unsigned i;
    
    bitset<z> s;
    s.reset();
    
    for (; n < z; n += 2)
    {
        if (s[n]) continue;
        p[++m] = n;
        for (i = n * n; i < z; i += 2 * n)
            s[i] = true;
    }
    for (i = 0; i <= m; i++)
        cout << p[i] << "\n";
}

Edit: This Sieve of Eratosthenes was inspired by a similar routine in REXX where it works. In C++ it fails. Reason are the predefined variables of fixed size. So unsigned i = n * n is in fact i = (n * n) modulo 2^32 what plays havoc with the procedures ancient logic.
Increasing z > 203897 the first remarkable discrepancy occures for n = 92683, square of it is 8590138489, assigned to unsigned i results in 203897 -- a prime which is marked as 'no prime' due to the falsely positive condition of the for() loop.
REXX knows only one type of variables, text of unlimited size (delimited only by available memory). = will not truncate (without error message).
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
#include <iomanip>

// function to check if n is a prime number
// uses simple trial division
bool is_prime( int n )
{
    if( n < 2 ) return false ;
    if( n == 2 ) return true ; // 2 is a prime number
    if( n%2 == 0 ) return false ; // even numbers greater than two are not prime

    // trial division by odd numbers up to the square root of n
    for( int div = 3 ; (div*div) <= n ; div += 2 ) if( n%div == 0 ) return false ;

    return true ; // not evenly divisible by any of the candidates
}

// function to put prime numbers in [1,ubound) into a vector
std::vector<int> get_primes_till( int ubound )
{
    std::vector<int> primes ;

    for( int n = 1 ; n < ubound ; ++n ) if( is_prime(n) ) primes.push_back(n) ;

    return primes ;
}

// function to print out numbers in a vector
void print_them( const std::vector<int>& numbers )
{
    int cnt = 0 ;
    for( int n : numbers ) // for each int n in the vector
    {
        std::cout << std::setw(7) << n << ' ' ; // print n
        if( ++cnt % 10 == 0 ) std::cout << '\n' ; // 10 numbers per line
    }
}

int main()
{
    const int n = 5'000 ;
    const auto prime_numbers = get_primes_till(n) ;

    std::cout << "list of prime numbers less than " << n
              << "\n-----------------------------------------\n" ;
    print_them(prime_numbers) ;
} 

http://coliru.stacked-crooked.com/a/05ddea929a7ade10
Topic archived. No new replies allowed.