1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2025-05-18 12:02:53 +02:00

feat(solutions): add `is-prime-number/cs/function

`
This commit is contained in:
Divlo
2021-09-10 22:37:50 +02:00
parent a1f20afb36
commit 2e15a21206
4 changed files with 56 additions and 4 deletions

View File

@ -0,0 +1,3 @@
# is-prime-number/cs/function
Created by [@Divlo](https://github.com/Divlo) on 10 September 2021.

View File

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
namespace Solution
{
class Program
{
static void Main()
{
int number = int.Parse(Console.ReadLine());
if (IsPrime(number))
{
Console.WriteLine("true");
}
else
{
Console.WriteLine("false");
}
}
/// <summary>
/// Checks if a number is prime.
/// </summary>
/// <param name="number">The number to check.</param>
/// <returns>True if the number is prime, false otherwise.</returns>
static public bool IsPrime(int number)
{
return GetDividers(number).Length == 2;
}
/// <summary>
/// Gets the dividers of a number.
/// </summary>
/// <param name="number">The number to get the dividers of.</param>
/// <returns>An array of the dividers of the number.</returns>
static public int[] GetDividers(int number)
{
List<int> dividers = new List<int>();
for (int index = 1; index <= number; index++)
{
if (number % index == 0)
{
dividers.Add(index);
}
}
return dividers.ToArray();
}
}
}