How can you remove all numbers except the simple one from the input sequence in a doubly linked list?
Closed due to the fact that off-topic participants αλεχολυτ , cheops , Harry , Nicolas Chabanovsky ♦ 17 Nov '16 at 5:40 .
- Most likely, this question does not correspond to the subject of Stack Overflow in Russian, according to the rules described in the certificate .
- You ask how to determine if the number is simple, or something else? - Vlad from Moscow
- Yes, I ask how to define simple it or not - Marishca
- For example using the Sieve of Eratosthenes - Isaev
- onePossible duplicate question: The function that checks a simple number - αλεχολυτ
- 2According to community rules, questions should not be reduced to completing tasks for students. Give an example of your implementation and ask a question describing specific problems. - Nicolas Chabanovsky ♦
|
1 answer
The function to determine if a number is simple may look like this.
bool is_prime( int value ) { bool prime = ( value == 2 ) || ( value > 2 && value % 2 ); for ( int i = 3; prime && i <= value / i; i += 2 ) { prime = value % i; } return prime; } Here is a demo program.
#include <iostream> bool is_prime( int value ) { bool prime = ( value == 2 ) || ( value > 2 && value % 2 ); for ( int i = 3; prime && i <= value / i; i += 2 ) { prime = value % i; } return prime; } int main() { const int N = 100; for ( int i = 1; i < N; i++ ) { if ( is_prime( i ) ) std::cout << i << ' '; } std::cout << std::endl; return 0; } Its output to the console:
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, it seems just a bunch of mistakes. - Marishca
- @Marishca Not at all. If it helped, you can close the question by choosing the best answer from your point of view. :) - Vlad from Moscow
|