Perl solution, not using any math libraries:
#!/usr/bin/perl use warnings; use strict; use utf8; use feature qw{ say }; use open OUT => ':encoding(UTF-8)', ':std'; sub is_prime { my ($p) = @_; for my $d (2 .. sqrt $p) { return if 0 == $p % $d; } return 1 } sub factor { my ($n) = @_; my @f; for my $p (2 .. $n) { next unless is_prime($p); if (0 == $n % $p) { push @f, $p; $n = $n / $p; redo } } return @f } { my %digit; @digit{0 .. 9} = qw( ⁰ ¹ ² ³ ⁴ ⁵ ⁶ ⁷ ⁸ ⁹ ); sub exponent { join "", map $digit{$_}, split //, shift } } my %exponent; my $n = shift; for my $p (2 .. $n) { ++$exponent{$_} for factor($p); } say join ' * ', map $_ . ($exponent{$_} == 1 ? "" : exponent($exponent{$_})), sort { $a <=> $b } keys %exponent;
For 22, it prints 2¹⁹ * 3⁹ * 5⁴ * 7³ * 11² * 13 * 17 * 19.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Perl solution, not using any math libraries:
For 22, it prints 2¹⁹ * 3⁹ * 5⁴ * 7³ * 11² * 13 * 17 * 19.