[Algorithm code 1]
** Function version algorithm code ← Find the smallest prime number greater than a given number
#include <bits/stdc++.h>
using namespace std;
bool isPrime(int n) {
if(n==1) return false;
for(int i=2; i<=sqrt(n); i++) {
if(n%i==0) return false;
}
return true;
}
int getPrime(int n) { //Get least prime bigger than n
for(int i=n+1; ;i++) {
if(isPrime(i)) {
return i;
break;
}
}
}
int main(){
int n;
cin>>n;
cout<<getPrime(n)<<endl;
return 0;
}
/*
in:100
out:101
*/
Find the code for the largest prime number smaller than a given number, see: https://blog.csdn.net/hnjzsyjyj/article/details/127699346
[Algorithm code 2]
** Non-function version algorithm code ← Find the smallest prime number larger than a given number
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
for(int i=n+1; ;i++){
int flag=1;
for(int j=2;j<=sqrt(i);j++){
if(i%j==0){
flag=0;
break;
}
}
if(flag==1){
cout<<i<<endl;
break;
}
}
return 0;
}
/*
in:100
out:101
in:1000
out:1009
*/