prime numbers
A number only divisible by 1 and itself
Prime Numbers
using System;
public class Program
{
static void Main(string[] args)
{
var results = GenerateSieve(1000);
var i=0;
foreach (var item in results)
{
if(item) Console.Write(i + " ");
i++;
}
}
static bool[] GenerateSieve(int num)
{
// Creating an array indicating whether numbers are prime.
bool[] isPrime = new bool[num + 1];
for (int i = 2; i <= num; i++) isPrime[i] = true;
// Removing out multiples.
for (int i = 2; i <= num; i++)
{
// Check if i is prime.
if (isPrime[i])
{
// Eliminate multiples of i.
for (int j = i * 2; j <= num; j += i)
isPrime[j] = false;
}
}
return isPrime;
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us