Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
32
Task/Prime-decomposition/PowerShell/prime-decomposition.psh
Normal file
32
Task/Prime-decomposition/PowerShell/prime-decomposition.psh
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
function eratosthenes ($n) {
|
||||
if($n -gt 1){
|
||||
$prime = @(1..($n+1) | foreach{$true})
|
||||
$prime[1] = $false
|
||||
$m = [Math]::Floor([Math]::Sqrt($n))
|
||||
function multiple($i) {
|
||||
for($j = $i*$i; $j -le $n; $j += $i) {
|
||||
$prime[$j] = $false
|
||||
}
|
||||
}
|
||||
multiple 2
|
||||
for($i = 3; $i -le $m; $i += 2) {
|
||||
if($prime[$i]) {multiple $i}
|
||||
}
|
||||
1..$n | where{$prime[$_]}
|
||||
} else {
|
||||
Write-Error "$n is not greater than 1"
|
||||
}
|
||||
}
|
||||
function prime-decomposition ($n) {
|
||||
$array = eratosthenes $n
|
||||
$prime = @()
|
||||
foreach($p in $array) {
|
||||
while($n%$p -eq 0) {
|
||||
$n /= $p
|
||||
$prime += @($p)
|
||||
}
|
||||
}
|
||||
$prime
|
||||
}
|
||||
"$(prime-decomposition 12)"
|
||||
"$(prime-decomposition 100)"
|
||||
Loading…
Add table
Add a link
Reference in a new issue