1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-11-09 22:08:58 +01: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
No known key found for this signature in database
GPG Key ID: 6F24DA54DA3967CF
4 changed files with 56 additions and 4 deletions

View File

@ -13,8 +13,8 @@ def convert_string_to_float(string: str) -> float:
class Position:
def __init__(self, longitude: float, latitude: float) -> None:
self.longitude = self.convert_degrees_to_radien(longitude)
self.latitude = self.convert_degrees_to_radien(latitude)
self.longitude = self.convert_degrees_to_radian(longitude)
self.latitude = self.convert_degrees_to_radian(latitude)
@staticmethod
def calculation_distance(pointA: 'Position', pointB: 'Position') -> float:
@ -23,7 +23,7 @@ class Position:
y = pointB.latitude - pointA.latitude
return math.sqrt(math.pow(x, 2) + math.pow(y, 2)) * 6371
def convert_degrees_to_radien(self, degrees: float) -> float:
def convert_degrees_to_radian(self, degrees: float) -> float:
return degrees * (math.pi / 180)

View File

@ -6,7 +6,7 @@ Created by [@Divlo](https://github.com/Divlo) on 15 November 2020.
Write a function that takes a string input, and returns the first character that is not repeated anywhere in the string.
For example, if given the input `'stress'`, the function should return `'t'`, since the letter _t_ only occurs once in the string, and occurs first in the string.
For example, if given the input `'stress'`, the function should return `'t'`, since the letter `'t'` only occurs once in the string, and occurs first in the string.
If a string contains all repeating characters, it should return an empty string (`""`).

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();
}
}
}