Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Day1
- {
- class Program
- {
- static bool IsPrime(int number)
- {
- for (int i = 2; i*i <= number; i++)
- {
- if (number % i == 0)
- {
- return false;
- }
- }
- return true;
- }
- static List<int> Factorisation(int number)
- {
- List<int> factors = new List<int>();
- for (int i = 2; i*i <= number; i++)
- {
- while (number % i == 0)
- {
- factors.Add(i);
- number /= i;
- }
- }
- if (number > 1)
- {
- factors.Add(number);
- }
- return factors;
- }
- static List<int> EratosthenesPrimes(int n)
- {
- List<int> primes = new List<int>();
- int[] arr = new int[n + 1];
- for (int i = 2; i <= n; i++)
- {
- if (arr[i] == 0)
- {
- primes.Add(i);
- for (int j = 2*i; j <= n; j += i)
- {
- arr[j] = 1;
- }
- }
- }
- return primes;
- }
- static void Main(string[] args)
- {
- Console.Write("Enter number: ");
- int num = int.Parse(Console.ReadLine());
- //int num = 200;
- Console.WriteLine("Is prime({0}): {1}", num, IsPrime(num));
- Console.WriteLine("{0} = {1}", num, string.Join("*", Factorisation(num)));
- Console.WriteLine("Primes to {0}: {1}", num, string.Join(", ", EratosthenesPrimes(num)));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement