exercism-perl5

Repository for my Perl 5 Exercism exercises
git clone git://git.samirparikh.com/exercism-perl5
Log | Files | Refs | README

commit 70d183143d2656c2874489a5eac500cf969b064e
parent 2d49fa5dc3f5979fe9f0d8e86005de0292ff24a3
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Wed, 25 May 2022 17:11:16 +0000

initial commit for sieve

Diffstat:
Asieve/HELP.md | 36++++++++++++++++++++++++++++++++++++
Asieve/README.md | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Asieve/Sieve.pm | 12++++++++++++
Asieve/sieve.t | 247+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 347 insertions(+), 0 deletions(-)

diff --git a/sieve/HELP.md b/sieve/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 Sieve.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 file diff --git a/sieve/README.md b/sieve/README.md @@ -0,0 +1,51 @@ +# Sieve + +Welcome to Sieve on Exercism's Perl 5 Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Use the Sieve of Eratosthenes to find all the primes from 2 up to a given +number. + +The Sieve of Eratosthenes is a simple, ancient algorithm for finding all +prime numbers up to any given limit. It does so by iteratively marking as +composite (i.e. not prime) the multiples of each prime, starting with the +multiples of 2. It does not use any division or remainder operation. + +Create your range, starting at two and continuing up to and including the given limit. (i.e. [2, limit]) + +The algorithm consists of repeating the following over and over: + +- take the next available unmarked number in your list (it is prime) +- mark all the multiples of that number (they are not prime) + +Repeat until you have processed each number in your range. + +When the algorithm terminates, all the numbers in the list that have not +been marked are prime. + +The wikipedia article has a useful graphic that explains the algorithm: +[https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) + +Notice that this is a very specific algorithm, and the tests don't check +that you've implemented the algorithm, only that you've come up with the +correct list of primes. A good first test is to check that you do not use +division or remainder operations (div, /, mod or % depending on the +language). + +## Source + +### Created by + +- @bistik + +### Contributed to by + +- @kytrinyx +- @m-dango +- @rfilipo + +### Based on + +Sieve of Eratosthenes at Wikipedia - http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes +\ No newline at end of file diff --git a/sieve/Sieve.pm b/sieve/Sieve.pm @@ -0,0 +1,12 @@ +package Sieve; +use strict; +use warnings; +use Exporter qw<import>; +our @EXPORT_OK = qw<find_primes>; + +sub find_primes { + my ($limit) = @_; + return undef; +} + +1; diff --git a/sieve/sieve.t b/sieve/sieve.t @@ -0,0 +1,247 @@ +#!/usr/bin/env perl +use Test2::V0; +use JSON::PP; +use constant JSON => JSON::PP->new; + +use FindBin qw<$Bin>; +use lib $Bin, "$Bin/local/lib/perl5"; + +use Sieve qw<find_primes>; + +my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; }; + +imported_ok qw<find_primes> or bail_out; + +for my $case (@test_cases) { + is( find_primes( $case->{input}{limit} ), + $case->{expected}, $case->{description}, ); +} + +done_testing; + +__DATA__ +[ + { + "description": "no primes under two", + "expected": [], + "input": { + "limit": 1 + }, + "property": "primes" + }, + { + "description": "find first prime", + "expected": [ + 2 + ], + "input": { + "limit": 2 + }, + "property": "primes" + }, + { + "description": "find primes up to 10", + "expected": [ + 2, + 3, + 5, + 7 + ], + "input": { + "limit": 10 + }, + "property": "primes" + }, + { + "description": "limit is prime", + "expected": [ + 2, + 3, + 5, + 7, + 11, + 13 + ], + "input": { + "limit": 13 + }, + "property": "primes" + }, + { + "description": "find primes up to 1000", + "expected": [ + 2, + 3, + 5, + 7, + 11, + 13, + 17, + 19, + 23, + 29, + 31, + 37, + 41, + 43, + 47, + 53, + 59, + 61, + 67, + 71, + 73, + 79, + 83, + 89, + 97, + 101, + 103, + 107, + 109, + 113, + 127, + 131, + 137, + 139, + 149, + 151, + 157, + 163, + 167, + 173, + 179, + 181, + 191, + 193, + 197, + 199, + 211, + 223, + 227, + 229, + 233, + 239, + 241, + 251, + 257, + 263, + 269, + 271, + 277, + 281, + 283, + 293, + 307, + 311, + 313, + 317, + 331, + 337, + 347, + 349, + 353, + 359, + 367, + 373, + 379, + 383, + 389, + 397, + 401, + 409, + 419, + 421, + 431, + 433, + 439, + 443, + 449, + 457, + 461, + 463, + 467, + 479, + 487, + 491, + 499, + 503, + 509, + 521, + 523, + 541, + 547, + 557, + 563, + 569, + 571, + 577, + 587, + 593, + 599, + 601, + 607, + 613, + 617, + 619, + 631, + 641, + 643, + 647, + 653, + 659, + 661, + 673, + 677, + 683, + 691, + 701, + 709, + 719, + 727, + 733, + 739, + 743, + 751, + 757, + 761, + 769, + 773, + 787, + 797, + 809, + 811, + 821, + 823, + 827, + 829, + 839, + 853, + 857, + 859, + 863, + 877, + 881, + 883, + 887, + 907, + 911, + 919, + 929, + 937, + 941, + 947, + 953, + 967, + 971, + 977, + 983, + 991, + 997 + ], + "input": { + "limit": 1000 + }, + "property": "primes" + } +]