diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2022-01-31 21:38:02 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2022-01-31 21:38:02 +0800 |
| commit | 786a071b0dab9f93c0c230ead72fda7fcfac3746 (patch) | |
| tree | 01273f218894c06198ba73635b8d70974bb9dccd | |
| parent | 4a60f293b696b8da32539511b1ee45caf3fa6110 (diff) | |
| download | perlweeklychallenge-club-786a071b0dab9f93c0c230ead72fda7fcfac3746.tar.gz perlweeklychallenge-club-786a071b0dab9f93c0c230ead72fda7fcfac3746.tar.bz2 perlweeklychallenge-club-786a071b0dab9f93c0c230ead72fda7fcfac3746.zip | |
week 150 task 2
| -rw-r--r-- | challenge-150/cheok-yin-fung/perl/ch-2.pl | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/challenge-150/cheok-yin-fung/perl/ch-2.pl b/challenge-150/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..2e861f4876 --- /dev/null +++ b/challenge-150/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,34 @@ +# The Weekly Challenge 150 +# Task 2 Square-Free Numbers +# Usage: $ ch-2.pl [upper_bound] + +use v5.22.0; +use warnings; + +my $N = $ARGV[0] || 500; + +say join ", ", sq_free($N)->@*; + + + +sub sq_free { + my $n = $_[0]; + my @is_sqfree = (1) x ($n+1); + + my $sqrtn = int sqrt $n; + for my $i (2..$sqrtn) { + for my $j (1..$n/$i) { + $is_sqfree[$i*$i*$j] = 0; + } + } + return [grep {$is_sqfree[$_]} (1..$n)]; +} + + + +use Test::More tests => 1; +use Test::Deep; +cmp_deeply(sq_free(30), [1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, 30]); + +# REF: https://stackoverflow.com/a/5313337 , +# http://mathworld.wolfram.com/Squarefree.html |
