I am trying to find the prime numbers from 1 to 100. The problem I encounter is that when the program assigns the value 2 to the variable j of type integer. The value of variable j does not change. Does anyone know why this happens?
Create a program to find all the prime numbers between 1 and 100.
One way to do this is to write a function that will check if a number is prime (ie, see if the number can be divided by a prime number smaller than itself) using a vector of primes in order (so that if the vector is called primes, primes[0]==2, primes[1]==3, primes[2]==5, etc.). Then write a loop that goes from 1 to 100, checks each number to see if it is a prime, and stores each prime found in a vector. Write another loop that lists the primes you found. You might check your result by comparing your vector of prime numbers with primes. Consider 2 the first prime.
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<int> primes = { 2 };
for (int i = 2; i <= 100; ++i)
for (int j = 2; j < i; ++j)
if (i % j == 0)
break;
else
primes.push_back(j);
for (int i = 0; i < primes.size(); ++i)
std::cout << primes[i] << ' ';
}
There are some mistakes in your initial implementation:
primes
vector Some more cleaned up code would look a bit similar to this:
#include <iostream>
#include <string>
#include <vector>
namespace {
bool isPrime(const std::vector<int> &previousPrimes, int possiblePrime) {
for (auto prevPrime : previousPrimes)
if (possiblePrime % prevPrime == 0)
return false;
return true;
}
}
int main()
{
auto primes = std::vector<int>({2});
for (int i = 3 /*2 is already a prime*/; i <= 100; ++i)
if (isPrime(primes, i))
primes.push_back(i);
for (auto prime : primes)
std::cout << prime << ' ';
std::cout << std::endl;
}
However, as this question looks like a homework assignment, don't blindly copy this and try to understand all the concepts used in here first.
#include <vector>
#include <iostream>
#include <string>
int main()
{
std::vector<int> primes;
primes.push_back(2);
for(int i=3; i < 100; i++)
{
bool prime=true;
for(int j=0;j<primes.size() && primes[j]*primes[j] <= i;j++)
{
if(i % primes[j] == 0)
{
prime=false;
break;
}
}
if(prime)
{
primes.push_back(i);
std::cout << i << " ";
}
}
return 0;
}
this will print the fallowing :
3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
We keep track of all of our previously calculated primes. If a number is divisible by a non-prime number, there is also some prime <= that divisor which it is also divisble by. This reduces computation by a factor of primes_in_range/total_range.
For starters there is written in the assignment that you have to write a function that checks whether a number is prime using a vector of prime numbers.
This loop
for (int j = 2; j < i; ++j)
if (i % j == 0)
break;
else
primes.push_back(j);
does not make sense because it does not use the vector to check whether i
is divisible by an element of the vector. Also a new prime number should be added to the vector after the loop provided that I is divisible by neither element of the vector.
Also take into account that there is written in the assignment
Then write a loop that goes from 1 to 100 , checks each number to see if it is a prime
Thus only my code shown below satisfies the assignment.:)
The program can look the following way. Instead of type int
I used type unsigned int
because you are checking only non-negative values.
#include <iostream>
#include <vector>
bool is_prime( const std::vector<unsigned int> &primes, unsigned int x )
{
std::vector<unsigned int>::size_type i = 0;
while ( i < primes.size() && x % primes[i] != 0 ) ++i;
return x != 1 && i == primes.size();
}
int main()
{
const unsigned int N = 100;
std::vector<unsigned int> primes;
for ( unsigned int i = 1; i <= N; ++i )
{
if ( is_prime( primes, i ) ) primes.push_back( i );
}
for ( unsigned int x : primes ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
The program output is
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
//Thank you for all your answers, but I just figured out why. The codes are following:
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<int> primes = { 2 };
for (int i = 2; i <= 100; ++i)
for (int j = 2; j < i; ++j) {
if (i % j == 0)
break;
if (j == i - 1)
primes.push_back(i);
}
for (int x: primes)
std::cout << x << ' ';
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.