Answers for "first prime numbers"

C++
0

the first n prime numbers

def prime_numbers(n):
    i = 2
    while i <= n + 1:
        if i % i == 0 and i % 2 != 0 and i % 3 != 0 and i % 5 != 0 and i % 7 != 0 \
                or i == 2 or i == 3 or i == 5 or i == 7:
            print(i)
            i = i + 1
        else:
            i = i + 1
            n = n + 1


prime_numbers(25)
Posted by: Guest on October-04-2021
0

first prime numbers less than

#include<bits/stdc++.h>
using namespace std;
bool Is_Prime(long long x){
	if(x%2==0)return false;
	for(int i=3;i*i<=x;i+=2)
		if(x%i==0)return false;
	return true;
}
int main(){
	//primes less than n
	int n;
	cin>>n;
	int primes=0,i=1;
	while(primes++<n){
		while(!Is_Prime(++i));
		cout<<i<<" ";
	}
}
Posted by: Guest on April-30-2020
0

first prime numbers

#include<bits/stdc++.h>
using namespace std;
bool Is_Prime(long long x){
	if(x%2==0)return false;
	for(int i=3;i*i<=x;i+=2)
		if(x%i==0)return false;
	return true;
}
int main(){
	//first n prime numbers
	int n;
	cin>>n;
	int i=1;
	while(n--){
		while(!Is_Prime(++i));
		cout<<i<<" ";
	}
}
Posted by: Guest on April-30-2020

Browse Popular Code Answers by Language