How to resolve the algorithm Count in factors step by step in the PowerShell programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Count in factors step by step in the PowerShell programming language
Table of Contents
Problem Statement
Write a program which counts up from 1, displaying each number as the multiplication of its prime factors. For the purpose of this task, 1 (unity) may be shown as itself.
2 is prime, so it would be shown as itself. 6 is not prime; it would be shown as
2 × 3
{\displaystyle 2\times 3}
. 2144 is not prime; it would be shown as
2 × 2 × 2 × 2 × 2 × 67
{\displaystyle 2\times 2\times 2\times 2\times 2\times 67}
.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Count in factors step by step in the PowerShell programming language
Source code in the powershell programming language
function eratosthenes ($n) {
if($n -ge 1){
$prime = @(1..($n+1) | foreach{$true})
$prime[1] = $false
$m = [Math]::Floor([Math]::Sqrt($n))
for($i = 2; $i -le $m; $i++) {
if($prime[$i]) {
for($j = $i*$i; $j -le $n; $j += $i) {
$prime[$j] = $false
}
}
}
1..$n | where{$prime[$_]}
} else {
"$n must be equal or 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
}
$OFS = " x "
"$(prime-decomposition 2144)"
"$(prime-decomposition 100)"
"$(prime-decomposition 12)"
You may also check:How to resolve the algorithm Read a configuration file step by step in the J programming language
You may also check:How to resolve the algorithm Boolean values step by step in the Mirah programming language
You may also check:How to resolve the algorithm Execute HQ9+ step by step in the Go programming language
You may also check:How to resolve the algorithm Colour pinstripe/Display step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the bash programming language