Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Fórum ➡ Prime numbers implemented using LINQ statements AND using FOR loop ♦
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 #define USE_LINQ_STATEMENTS using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; // aljodav // http://rextester.com/users/376 // in reply to the MSDN thread: // http://bit.ly/2ecZbzL // Prime numbers using Sieve of Eratosthenes algorithm. // Implementations through LINQ statements and through for-loop. namespace Rextester { public class Program { public static void Main(string[] args) { const int maxNumber=12345; // user defined if(maxNumber<2)throw new Exception("Invalid Maximum Number"); Console.Write("Prime numbers up to {0}:\n\n2 ",maxNumber); if(maxNumber>2){ List<int> nonPrimes=new List<int>(); // helper list #if USE_LINQ_STATEMENTS Enumerable .Range(3,maxNumber-2) .Where(x=>(x&1)==1&&false==nonPrimes.Contains(x)) .Select(x=>{ nonPrimes.AddRange(Enumerable.Range(3,maxNumber/x).Where(y=>(y&1)==1).Select(y=>y*x)); return x; }) .ToList() .ForEach(x=>Console.Write("{0} ",x)) ; #else for(int i=3;i<=maxNumber;i+=2){ if(false==nonPrimes.Contains(i)){ Console.Write("{0} ",i); for(int j=3;j<=maxNumber/i;j+=2)nonPrimes.Add(j*i); } } #endif } Console.WriteLine("\n\nHello, world!"); } } }
run
|
edit
|
history
|
help
0
Plt-D v.0.9.3.1
text
6
t
all possible sub strings of a given string
character occurrence in the given string
Md5 hash
SAF Mid Terms
PhoneBook- Dictionary
Fibonacci of array elements C#