它是在给定范围内打印质数的程序。
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.
#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; }
输出结果
The Prime Numbers from 20 to 50 are: 23 29 31 37 41 43 47
本文向大家介绍c语言求出给定范围内的所有质数,包括了c语言求出给定范围内的所有质数的使用技巧和注意事项,需要的朋友参考一下 程序功能: 输入一个整数,要求打印出这个整数以内的所有质数。 程序示例: 程序解析: 1,IsPrime()函数用来判断一个整数是不是质数,如果是返回true,否则返回false。在这个函数中因为调用了C语言库函数sqrt(),所以要在头文件中包含#include 。 2,s
我正在尝试编写一个Heap排序方法,它只在传入方法的给定范围内执行排序。传入的范围是低和高,这些值对应于堆中的值,而不是堆的索引。例如,输入数组可能是:28 10 49 20 59 61 17,如果low=49,high=61,Heap排序后的结果数组将看起来像这样:28 10 20 49 59 61 17。范围之外的值保持不变。我已经有了一个工作的Heap排序方法,但我的问题是如何修改这个方法以
本文向大家介绍C#生成给定范围内的随机整数,包括了C#生成给定范围内的随机整数的使用技巧和注意事项,需要的朋友参考一下 示例 生成一个介于minValue和之间的随机数maxValue - 1。
本文向大家介绍编写Golang程序以查找给定范围内的质数,包括了编写Golang程序以查找给定范围内的质数的使用技巧和注意事项,需要的朋友参考一下 例子 输入num1 = 3和num2 = 8 =>质数是:3、5、7 输入num1 = 8和num2 = 23 =>质数是:11,13,17,19,23 解决这个问题的方法 步骤1:定义一个接受两个数字num1和num2的函数,类型为int。 步骤2:
本文向大家介绍在C ++中计算给定范围内的阶乘数,包括了在C ++中计算给定范围内的阶乘数的使用技巧和注意事项,需要的朋友参考一下 给定范围是从变量保存的整数值开始,比如说从开始直到变量结束,而任务是计算给定范围内可用的阶乘数的总数。 什么是阶乘数 数字的阶乘是通过将数字中的数字相乘,同时将数字的值减1来计算的。它由符号“!”表示 即0!,1!,2!,3!,5!,....等 0阶乘!和1!始终为1
#include <iostream> #include <vector> using namespace std; int main () { vector<int> vec(10); // 10 zero-initialized elements for (int i = 0; i < vec.size(); i++) vec[i] = i; cout << "ve