commitd8fdcf620add801d2b308df24801091a235b6ca9parent0df5c5fecc5365f673fc17572b3dff8f6ce4011fAuthor:Samir Parikh <noreply@samirparikh.com>Date:Sat, 23 Apr 2022 01:44:23 +0000 get all tests to passDiffstat:

A | prime-factors/HELP.md | | | 36 | ++++++++++++++++++++++++++++++++++++ |

A | prime-factors/Prime.pm | | | 26 | ++++++++++++++++++++++++++ |

A | prime-factors/README.md | | | 52 | ++++++++++++++++++++++++++++++++++++++++++++++++++++ |

A | prime-factors/prime.t | | | 99 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |

4 files changed, 213 insertions(+), 0 deletions(-)diff --git a/prime-factors/HELP.md b/prime-factors/HELP.md@@ -0,0 +1,35 @@ +# Help + +## Running the tests + +There is a Perl 5 script with the extension `.t`, which will be used to test +your solution. You can run through the tests by using the command: + +```bash +`prove .` +``` + +## Submitting your solution + +You can submit your solution using the `exercism submit Prime.pm` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Perl 5 track's documentation](https://exercism.org/docs/tracks/perl5) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +To get help if you're having trouble, you can use one of the following resources: + +- [/r/perl5](https://www.reddit.com/r/perl5) is the perl5 subreddit. +- [StackOverflow](http://stackoverflow.com/questions/tagged/perl5) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions. +\ No newline at end of filediff --git a/prime-factors/Prime.pm b/prime-factors/Prime.pm@@ -0,0 +1,26 @@ +package Prime; +use strict; +use warnings; + +sub factor { + my $factors = []; + my $number = shift; + until ( $number == 1 ) { + foreach my $divisor ( 2 .. $number ) { + if ( $number % $divisor == 0 ) { + push @{ $factors } => $divisor; + $number = $number / $divisor; + last; + } + } + } + return $factors; +} + +sub factors { + my ($num) = @_; + return factor( $num ); +} + +1; +diff --git a/prime-factors/README.md b/prime-factors/README.md@@ -0,0 +1,51 @@ +# Prime Factors + +Welcome to Prime Factors on Exercism's Perl 5 Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Compute the prime factors of a given natural number. + +A prime number is only evenly divisible by itself and 1. + +Note that 1 is not a prime number. + +## Example + +What are the prime factors of 60? + +- Our first divisor is 2. 2 goes into 60, leaving 30. +- 2 goes into 30, leaving 15. + - 2 doesn't go cleanly into 15. So let's move on to our next divisor, 3. +- 3 goes cleanly into 15, leaving 5. + - 3 does not go cleanly into 5. The next possible factor is 4. + - 4 does not go cleanly into 5. The next possible factor is 5. +- 5 does go cleanly into 5. +- We're left only with 1, so now, we're done. + +Our successful divisors in that computation represent the list of prime +factors of 60: 2, 2, 3, and 5. + +You can check this yourself: + +- 2 \* 2 \* 3 * 5 +- = 4 * 15 +- = 60 +- Success! + +## Source + +### Created by + +- @szabgab + +### Contributed to by + +- @kytrinyx +- @m-dango +- @rfilipo + +### Based on + +The Prime Factors Kata by Uncle Bob - http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata +\ No newline at end of filediff --git a/prime-factors/prime.t b/prime-factors/prime.t@@ -0,0 +1,99 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +my $module = 'Prime'; + +use Test2::Bundle::More; +use JSON::PP qw(decode_json); +use FindBin qw($Bin); +use lib $Bin, "$Bin/local/lib/perl5"; + +my $cases; +{ + local $/ = undef; + $cases = decode_json scalar <DATA>; +} + +#plan 3 + @$cases; +#diag explain $cases; + +ok -e "$Bin/$module.pm", "missing $module.pm" + or BAIL_OUT( + "You need to create a class called $module.pm with a constructor called factors." + ); + +eval "use $module"; +ok !$@, "Cannot load $module.pm" + or BAIL_OUT("Does $module.pm compile? Does it end with 1; ? ($@)"); + +can_ok( $module, 'factors' ) + or BAIL_OUT("Missing package $module; or missing sub factors()"); + +my $sub = $module . '::factors'; + +foreach my $c (@$cases) { + no strict 'refs'; + is_deeply $sub->( $c->{input} ), $c->{expected}, $c->{name}; +} + +done_testing(); + +__DATA__ +[ + { + "input" : 1, + "expected" : [], + "name" : "test_1" + }, + { + "input" : 2, + "expected" : [2], + "name" : "test_2" + }, + { + "input" : 3, + "expected" : [3], + "name" : "test_3" + }, + { + "input" : 4, + "expected" : [2, 2], + "name" : "test_4" + }, + { + "input" : 6, + "expected" : [2, 3], + "name" : "test_6" + }, + { + "input" : 8, + "expected" : [2, 2, 2], + "name" : "test_8" + }, + { + "input" : 9, + "expected" : [3, 3], + "name" : "test_9" + }, + { + "input" : 27, + "expected" : [3, 3, 3], + "name" : "test_27" + }, + { + "input" : 625, + "expected" : [5, 5, 5, 5], + "name" : "test_625" + }, + { + "input" : 901255, + "expected" : [5, 17, 23, 461], + "name" : "test_901255" + }, + { + "input" : 93819012551, + "expected" : [11, 9539, 894119], + "name" : "test_93819012551" + } +]