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...
#include <iostream>
#include <cmath>
#include <vector>
bool is_prime( int );
int main(int argc, constchar * 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?
/* Sieve of Eratosthenes, no optimizations */
#include <iostream>
#include <bitset>
usingnamespace std;
int main()
{
unsigned p[999] {2};
unsignedconst 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).
#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 ) returnfalse ;
if( n == 2 ) returntrue ; // 2 is a prime number
if( n%2 == 0 ) returnfalse ; // 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 ) returnfalse ;
returntrue ; // 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()
{
constint 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) ;
}