Đây là chương trình in các số nguyên tố trong một phạm vi nhất định.
Thuật toán
Begin Declare a user define datatype stl. Declare a vector number to the stl datatype. Declare variable a to the stl datatype. Declare vector Prime_Number to the Boolean datatype. Prime_Number[0] = false. Prime_Number[1] = false. Declare b to the integer datatype. Initialize b = sqrt(a). for (stl pr=2; pr<=b; pr++) if (Prime_Number[pr]) then for (stl i=pr*2; i<=a; i += pr) Prime_Number[i] = false. Declare result to the vector type. for (int i=0;i<a;i++) if (Prime_Number[i]) then result.push_back(i). return result. End Begin Declare function remove_zero() to the Boolean type. Declare i to the stl datatype. Return i = 0. End Begin Declare a vector Number_Range to the stl datatype. Declare First_Num, Last_Num to the stl datatype. Pass them as parameter. Declare two vector s1 and s2 to the stl datatype. Initialize s1 = number(First_Num) to find primes from o to First_Num. Initialize s2 = number(Last_Num) to find primes from o to Last_Num. Declare vector result(Last_Num-First_Num) to the stl datatype. Call set_difference(s2.begin(), s2.end(), s1.begin(), s2.end(), result.begin()) to find set difference of two vectors. Declare an iterator itr to the vector type. Initialize itr = remove_if(result.begin(),result.end(),remove_zero) to remove extra zeros. Call resize (itr-result.begin()) to resize the result. Return result. Declare First_Num, Last_Num to the stl datatype. Initialize First_Num = 20, Last_Num = 50. Declare a vector result to the stl datatype. Initialize result = Number_Range(First_Num,Last_Num). Print "The Prime Numbers from 20 to 50 are: “ for (auto i:result) Print the values of i. End.
Ví dụ
#include<bits/stdc++.h> using namespace std; typedef unsigned long long int stl; vector<stl> number(stl a) { vector<bool> Prime_Number(a+1,true); Prime_Number[0] = false; Prime_Number[1] = false; int b = sqrt(a); for (stl pr=2; pr<=b; pr++) { if (Prime_Number[pr]) { for (stl i=pr*2; i<=a; i += pr) Prime_Number[i] = false; } } vector<stl> result; for (int i=0;i<a;i++) if (Prime_Number[i]) result.push_back(i); return result; } bool remove_zero(stl i) { // Used to remove zeros from a vector return i == 0; } vector<stl> Number_Range(stl First_Num,stl Last_Num) { vector<stl> s1 = number(First_Num); // find primes from o to First_Num vector<stl> s2 = number(Last_Num); // find primes from o to Last_Num vector<stl> result(Last_Num-First_Num); set_difference(s2.begin(), s2.end(), s1.begin(), s2.end(), result.begin()); // find set //difference of two vectors vector<stl>::iterator itr = remove_if(result.begin(),result.end(),remove_zero); //remove extra zeros. result.resize(itr-result.begin()); return result; } int main(void) { stl First_Num = 20, Last_Num = 50; vector<stl> result = Number_Range(First_Num,Last_Num); cout<<"The Prime Numbers from "<<First_Num<<" to "<<Last_Num<< " are: "; for (auto i:result) cout<<i<<' '; return 0; }
Đầu ra
The Prime Numbers from 20 to 50 are: 23 29 31 37 41 43 47