Skip to content
This repository has been archived by the owner on Oct 6, 2021. It is now read-only.

added SieveOfEratosthenes C# Issue #172 #259

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions C#/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@

[Linked list is palindrome](./linked_list_is_palindrome.cs)

[sieve of eratosthenes](./SieveOfEratosthenes.cs)

[Quick Sort](./quick_sort.cs)
69 changes: 69 additions & 0 deletions C#/SieveOfEratosthenes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using System.Collections;

namespace DefaultNamespace
{
public class SieveOfEratosthenes
{
private static void Main(string[] args)
{
Console.Write("Please enter a random integer number: ");
if (int.TryParse(Console.ReadLine(), out var num))
{
Console.Write($"All primary numbers between 2 and {num} is : ");
GetAllPrimaryNumbers(num);
}

Console.WriteLine();
}

/// <summary>
/// implementing Sieve_of_Eratosthenes
/// </summary>
/// <param name="num">input</param>
/// <returns>list of primary numbers equals or less than input</returns>
private static void GetAllPrimaryNumbers(int num)
{
if (num <= 1) return;

var cells = new BitArray(num + 1, true);
// var cells = new bool[num+1];
var p = 2;
while (p <= num)
{
// continue if marked false
if (!cells[p])
{
p++;
continue;
}

Console.Write(p + ",");

// mark all multiples of p as false
for (var i = p; i < num; i++)
{
if (p * i < 2 || p * i > num) break;
cells[p * i] = false;
}

p++;
}
}
}
}


/*
// 1
Please enter a random number: 10
All primary numbers between 2 and 10 is : 2,3,5,7,
It took 3957 ms.

// 2
Please enter a random number: 200
All primary numbers between 2 and 200 is : 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,
83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,
It took 1425 ms.

*/