
Даю 100 баллов на C++ надо написать программу задание с сириуса: Выведите в порядке возрастания
все простые числа на отрезке [l;r]. Оформите решение в виде функции bool isPrime(int n), проверяющей число на простоту, и функции vector primes(int l, int r), возвращающей список простых чисел на отрезке [l;r]. Входные данные Дано два натуральных числа l и r (l≤r≤1000). #include #include using namespace std; bool isPrime(int n) /* ВАШ КОД */ int main() { int l, r; cin >> l >> r; vector res = primes(l, r); for (int i = 0; i < res.size(); ++i){ cout << res[i] << " "; } return 0; } Вот мой фрагмент кода который валится на 3-ем тесте: { for(int i = 2; i <= n / 2; i++){ if(!(n % i)){ return 0; } } return 1; } vector primes(int l, int r){ vector A; for(int i = l; i <= r; i++){ if(isPrime(i)){ A.push_back(i); } } return A; }

Ответы на вопрос

#include <iostream>
#include <vector>
std::vector<int> primes(int l, int r);
bool isPrime(int n);
int main()
{
int l,r;
std::cin >> l >> r;
std::vector<int> res = primes(l, r);
for (int i = 0;i < res.size();i++)
{
std::cout << res[i] << " ";
}
return 0;
}
std::vector<int> primes(int l, int r)
{
std::vector<int> primes;
for (int i = l; i < r; i++)
{
if (isPrime(i))
{
primes.push_back(i);
}
}
return primes;
}
bool isPrime(int n)
{
if (n <= 2)
return false;
for (int i = 3; i < n/2+1; i++)
if (n % i == 0)
return false;
return true;
}



To solve the given task of printing all prime numbers in the range [l;r] in ascending order, you can use the provided code snippet as a starting point. However, there are a few issues in the code that need to be addressed.
Code Issues:
1. The function `isPrime` is declared as returning a `bool` value, but the implementation returns an `int` value. It should return a `bool` value indicating whether the number is prime or not. 2. The `vector` `res` is not declared before using it in the `main` function. It should be declared as `vectorUpdated Code:
Here is the updated code that addresses the issues mentioned above:```cpp
#include
bool isPrime(int n) { for (int i = 2; i <= n / 2; i++) { if (n % i == 0) { return false; } } return true; }
vector
int main() {
int l, r;
cin >> l >> r;
vector
Explanation:
The code snippet provided includes two functions: `isPrime` and `primes`. The `isPrime` function checks whether a given number `n` is prime or not. It does this by iterating from 2 to `n/2` and checking if `n` is divisible by any number in that range. If it is divisible, the function returns `false`, indicating that the number is not prime. If no divisors are found, the function returns `true`, indicating that the number is prime.The `primes` function takes two integers `l` and `r` as input, representing the range of numbers to check for primes. It initializes an empty vector `A` to store the prime numbers found. It then iterates from `l` to `r` and calls the `isPrime` function for each number. If a number is prime, it is added to the vector `A`. Finally, the function returns the vector `A` containing all the prime numbers in the range.
In the `main` function, the user is prompted to enter the values of `l` and `r`. The `primes` function is called with these values, and the resulting vector of prime numbers is stored in the `res` vector. The `res` vector is then printed to the console.
Please note that the updated code assumes that the input values `l` and `r` are valid and satisfy the condition `l ≤ r ≤ 1000`.
I hope this helps! Let me know if you have any further questions.


Похожие вопросы
Топ вопросов за вчера в категории Информатика







Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili