How to resolve the algorithm Prime decomposition step by step in the AWK programming language
How to resolve the algorithm Prime decomposition step by step in the AWK programming language
Table of Contents
Problem Statement
The prime decomposition of a number is defined as a list of prime numbers which when all multiplied together, are equal to that number.
Write a function which returns an array or collection which contains the prime decomposition of a given number
n
{\displaystyle n}
greater than 1. If your language does not have an isPrime-like function available, you may assume that you have a function which determines whether a number is prime (note its name before your code). If you would like to test code from this task, you may use code from trial division or the Sieve of Eratosthenes. Note: The program must not be limited by the word size of your computer or some other artificial limit; it should work for any number regardless of size (ignoring the physical limits of RAM etc).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Prime decomposition step by step in the AWK programming language
Source code in the awk programming language
# Usage: awk -f primefac.awk
function pfac(n, r, f){
r = ""; f = 2
while (f <= n) {
while(!(n % f)) {
n = n / f
r = r " " f
}
f = f + 2 - (f == 2)
}
return r
}
# For each line of input, print the prime factors.
{ print pfac($1) }
You may also check:How to resolve the algorithm Sort stability step by step in the Perl programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the MOO programming language
You may also check:How to resolve the algorithm Program name step by step in the Jsish programming language
You may also check:How to resolve the algorithm Generate random chess position step by step in the R programming language
You may also check:How to resolve the algorithm Reflection/List methods step by step in the Perl programming language