# P36 - Determine the prime factors of a given positive integer (2).

*Author: Curtis Poe*

Hint: The problem is similar to problem P13.

This was originally a blog post: http://blogs.perl.org/users/ovid/2010/08/prime-factors-in-perl-6.html.

# Specification

P36 (**) Determine the prime factors of a given positive integer (2). Construct a list containing the prime factors and their multiplicity.

# Example

> prime_factors_mult(315).perl.say (3 => 2, 5 => 1, 7 => 1)

Source code: P36-ovid.pl

use v6; constant PRIMES = grep { .is-prime }, 2 .. *; sub prime-factors(Int $number-to-factor where * > 1 --> Hash) { return { $number-to-factor => 1 } if $number-to-factor.is-prime; my %factors; my $number = $number-to-factor; for PRIMES.cache -> $prime { last if $prime ** 2 > $number; while $number %% $prime { %factors{$prime}++; $number div= $prime; } } %factors{$number}++ if $number != 1; # we have a prime left over return %factors; } for 2, 17, 53, 90, 94, 200, 289, 62710561 -> $number { say "Prime factors of $number are: {prime-factors($number).perl}"; }