How to resolve the algorithm Count in factors step by step in the PARI/GP programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Count in factors step by step in the PARI/GP 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 PARI/GP programming language
Source code in the pari/gp programming language
fnice(n)={
my(f,s="",s1);
if (n < 2, return(n));
f = factor(n);
s = Str(s, f[1,1]);
if (f[1, 2] != 1, s=Str(s, "^", f[1,2]));
for(i=2,#f[,1], s1 = Str(" * ", f[i, 1]); if (f[i, 2] != 1, s1 = Str(s1, "^", f[i, 2])); s = Str(s, s1));
s
};
n=0;while(n++<21, printf("%2s: %s\n",n,fnice(n)))
You may also check:How to resolve the algorithm Möbius function step by step in the F# programming language
You may also check:How to resolve the algorithm Input loop step by step in the Euphoria programming language
You may also check:How to resolve the algorithm String concatenation step by step in the Wren programming language
You may also check:How to resolve the algorithm Literals/Floating point step by step in the Wren programming language
You may also check:How to resolve the algorithm Execute HQ9+ step by step in the Lua programming language