Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Find Prime number
//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 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //pri int n =5; findPrimeNumberApproach1(n); findPrimeNumberApproach2(n); bool b = findPrimeNumberApproach3(n); if(b) { Console.WriteLine("{0} is a Prime Number", n); } else { Console.WriteLine("{0} is not a Prime Number", n); } } public static void findPrimeNumberApproach1(int n) { bool isPrime = true; int m = n/2; if (n <= 1 ) isPrime = false; for (int i = 2; i <= m; i++) { if (n % i == 0){ isPrime = false; } } if(isPrime) { Console.WriteLine("{0} is a Prime Number", n); } else { Console.WriteLine("{0} is not a Prime Number", n); } } public static void findPrimeNumberApproach2(int n) { int count = 0; for (int i = 1; i <= n; i++) { if (n % i == 0) { count++; } } if (count == 2) { Console.WriteLine("{0} is a Prime Number", n); } else { Console.WriteLine("{0} is not a Prime Number", n); } } internal static bool findPrimeNumberApproach3(int number) { if (number == 1) return false; if (number == 2) return true; if (number % 2 == 0) return false; var squareRoot = (int)Math.Floor(Math.Sqrt(number)); for (int i = 3; i <= squareRoot; i += 2) { if (number % i == 0) return false; } return true; } } }
run
|
edit
|
history
|
help
0
Flipping a bit
12 და 14 მარტს დამუშავებული
zasasasa
30272 Program Ex5_1 if else_2
asdfrgthyu6543wdsc
async example with HttpClient
Math Course: Remainders
Returning object as parameter (pre-example without overloading)
nguoi
custom Queue Implementation