-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem5.ps1
47 lines (45 loc) · 1.49 KB
/
Problem5.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<#
<p>$2520$ is the smallest number that can be divided by each of the numbers from $1$ to $10$ without any remainder.</p>
<p>What is the smallest positive number that is <strong class="tooltip">evenly divisible<span class="tooltiptext">divisible with no remainder</span></strong> by all of the numbers from $1$ to $20$?</p>
#>
Function Find-LCM {
PARAM (
[Parameter(ValueFromPipeline = $true)]
[System.String]$String,
[System.Double]$Number
)
$array = @()
[System.Double]$product = 1
$Numbers = $String.Split(",")
foreach ($Number in $Numbers) {
$sqrt = [math]::sqrt($number)
$Factor = 2
$count = 0
while ( ($Number % $Factor) -eq 0) {
$count += 1
$Number = $Number / $Factor
if (($array | Where-Object { $_ -eq $Factor }).count -lt $count) {
$array += $Factor
}
}
$count = 0
$Factor = 3
while ($Factor -le $sqrt) {
while ( ($Number % $Factor) -eq 0) {
$count += 1
$Number = $Number / $Factor
if (($array | Where-Object { $_ -eq $Factor }).count -lt $count) {
$array += $Factor
}
}
$Factor += 2
$count = 0
}
if ($array -notcontains $Number) {
$array += $Number
}
}
foreach ($arra in $array) { $product = $product * $arra }
$product
}
Find-LCM (1..20 -join ',')