From 8c3b72aa23fe2ca60dbcf176dca5b7f9548673d4 Mon Sep 17 00:00:00 2001 From: Scimon Date: Mon, 29 Nov 2021 09:52:27 +0000 Subject: Challenge 1 --- challenge-141/simon-proctor/raku/ch-1.raku | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 challenge-141/simon-proctor/raku/ch-1.raku (limited to 'challenge-141') diff --git a/challenge-141/simon-proctor/raku/ch-1.raku b/challenge-141/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..526bad0135 --- /dev/null +++ b/challenge-141/simon-proctor/raku/ch-1.raku @@ -0,0 +1,15 @@ +#!/usr/bin/env raku + +#| Find the first N positive integers with exactly X divisors +sub MAIN( + \N = 10, #= Number of integers (default 10) + \X = 8, #= Number of divisors (default 8) +) { + .say for (1..*).grep( + -> $v { + (1..$v).grep( + -> $d { + $v %% $d + } ).elems == X + })[^N]; +} -- cgit From 29cd549b7300651b3eee9da4c8a43ecde8595a07 Mon Sep 17 00:00:00 2001 From: Alexander Karelas Date: Mon, 29 Nov 2021 11:52:30 +0200 Subject: alexander karelas's solution to challenge 141 #2 --- challenge-141/alexander-karelas/ch-2.pl | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100755 challenge-141/alexander-karelas/ch-2.pl (limited to 'challenge-141') diff --git a/challenge-141/alexander-karelas/ch-2.pl b/challenge-141/alexander-karelas/ch-2.pl new file mode 100755 index 0000000000..37bd52afe9 --- /dev/null +++ b/challenge-141/alexander-karelas/ch-2.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +use v5.32; +use warnings; + +use List::Util 'any'; + +use experimental 'signatures'; + +sub find_like ($m, $n) { + my @digits = split //, $m; + my $length = @digits; + my @return; + + for (my $i = 1; $i <= 2 ** $length - 2; $i++) { + my @onoff = split //, sprintf("%0${length}b", $i); + my $idx = 0; + my $candidate = join '', map { + my $digit = $digits[$_]; + my $onoff = $onoff[$_]; + $onoff ? ($digit) : (); + } (0 .. $length-1); + push @return, $candidate if $candidate % $n == 0; + } + + return sort {$a <=> $b} @return; +} + +say foreach find_like(@ARGV); -- cgit From 5ea9537a28ed490439fcc2f9d8bd062903bf045b Mon Sep 17 00:00:00 2001 From: Scimon Date: Mon, 29 Nov 2021 09:58:26 +0000 Subject: Challenge 2 --- challenge-141/simon-proctor/raku/ch-2.raku | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 challenge-141/simon-proctor/raku/ch-2.raku (limited to 'challenge-141') diff --git a/challenge-141/simon-proctor/raku/ch-2.raku b/challenge-141/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..1bb40b5556 --- /dev/null +++ b/challenge-141/simon-proctor/raku/ch-2.raku @@ -0,0 +1,9 @@ +#!/usr/bin/env raku + +#| Given a two number M and N find the number of "Like Numbers" can be found +sub MAIN ( + UInt \M, #= Integer used to make sub values + UInt \N #= Integer subvales of M should be divisible by +) { + M.comb.combinations(1..^(M.codes)).map( *.join() ).grep( * %% N ).elems.say; +} -- cgit From b44388d3b4a2250f04235fd1d51096614bf72998 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 29 Nov 2021 09:59:04 +0000 Subject: Task 1 & 2 --- challenge-141/perlboy1967/perl/ch-1.pl | 49 +++++++++++++++++++++++++++++++ challenge-141/perlboy1967/perl/ch-2.pl | 53 ++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100755 challenge-141/perlboy1967/perl/ch-1.pl create mode 100755 challenge-141/perlboy1967/perl/ch-2.pl (limited to 'challenge-141') diff --git a/challenge-141/perlboy1967/perl/ch-1.pl b/challenge-141/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..2c8fa60b22 --- /dev/null +++ b/challenge-141/perlboy1967/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 140 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-140/#TASK1 + +Author: Niels 'PerlBoy' van Dijke + +TASK #1 › Number Divisors +Submitted by: Mohammad S Anwar + +Write a script to find lowest 10 positive integers having exactly 8 divisors. + +=cut + +use v5.16; +use strict; +use warnings; + +use Data::Printer output => 'stdout'; + +sub hasNDivisors($$); + +my $n = 24; +my @n; + +while (scalar(@n) < 10) { + if (hasNDivisors($n,8)) { + push(@n,$n); + } + $n++; +} + +p @n; + + +sub hasNDivisors($$) { + my ($n,$count) = @_; + + my $i = 1; + my @d = ($i); + while ($i < $n) { + push(@d,$i) if ($n % $i == 0); + $i++; + } + + return (scalar(@d) == $count); +} diff --git a/challenge-141/perlboy1967/perl/ch-2.pl b/challenge-141/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..0fb48e8b99 --- /dev/null +++ b/challenge-141/perlboy1967/perl/ch-2.pl @@ -0,0 +1,53 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 140 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-140/#TASK2 + +Author: Niels 'PerlBoy' van Dijke + +TASK #2 › Like Numbers +Submitted by: Mohammad S Anwar + +i You are given positive integers, $m and $n. + +Write a script to find total count of integers created using the digits of $m +which is also divisible by $n. + +Repeating of digits are not allowed. Order/Sequence of digits can’t be +altered. You are only allowed to use (n-1) digits at the most. For example, +432 is not acceptable integer created using the digits of 1234. Also for 1234, +you can only have integers having no more than three digits. + +=cut + +use v5.16; +use strict; +use warnings; + +use List::MoreUtils qw(uniq); +use Algorithm::Combinatorics qw(combinations); + +sub likeNumbers($$); + +my @n = likeNumbers($ARGV[0],$ARGV[1]); +printf "%d => (%s)\n", scalar(@n), join(',',@n); + + +sub likeNumbers($$) { + my ($m,$n) = @_; + + my @res; + + my @digits = uniq split //,$m; + foreach my $len (1 .. scalar(@digits)) { + my $iter = combinations(\@digits,$len); + while (my $ar = $iter->next) { + my $val = join('',@$ar); + push(@res,$val) if ($val % $n == 0 and $val != $m); + } + } + + return @res; +} -- cgit From 0a494c3c02450c957127182b7b63d418cd1aaca6 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 29 Nov 2021 10:00:43 +0000 Subject: Wrong weeknumber (fixed typos) --- challenge-141/perlboy1967/perl/ch-1.pl | 4 ++-- challenge-141/perlboy1967/perl/ch-2.pl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'challenge-141') diff --git a/challenge-141/perlboy1967/perl/ch-1.pl b/challenge-141/perlboy1967/perl/ch-1.pl index 2c8fa60b22..9cb31d273f 100755 --- a/challenge-141/perlboy1967/perl/ch-1.pl +++ b/challenge-141/perlboy1967/perl/ch-1.pl @@ -2,8 +2,8 @@ =pod -The Weekly Challenge - 140 - - https://perlweeklychallenge.org/blog/perl-weekly-challenge-140/#TASK1 +The Weekly Challenge - 141 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-141/#TASK1 Author: Niels 'PerlBoy' van Dijke diff --git a/challenge-141/perlboy1967/perl/ch-2.pl b/challenge-141/perlboy1967/perl/ch-2.pl index 0fb48e8b99..51364c6f0b 100755 --- a/challenge-141/perlboy1967/perl/ch-2.pl +++ b/challenge-141/perlboy1967/perl/ch-2.pl @@ -2,8 +2,8 @@ =pod -The Weekly Challenge - 140 - - https://perlweeklychallenge.org/blog/perl-weekly-challenge-140/#TASK2 +The Weekly Challenge - 141 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-141/#TASK2 Author: Niels 'PerlBoy' van Dijke -- cgit From 6f7aba1c1236b38b185cb678647b00a61420ba34 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 29 Nov 2021 11:02:43 +0100 Subject: Task 1 done --- challenge-141/luca-ferrari/raku/ch-1.p6 | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100755 challenge-141/luca-ferrari/raku/ch-1.p6 (limited to 'challenge-141') diff --git a/challenge-141/luca-ferrari/raku/ch-1.p6 b/challenge-141/luca-ferrari/raku/ch-1.p6 new file mode 100755 index 0000000000..621db2a13e --- /dev/null +++ b/challenge-141/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,19 @@ +#!raku + + +sub MAIN( Int $divisors = 8, Int $count = 10 ) { + + "Searching $count numbers with $divisors divisors...".say; + + my %solutions; + + for $divisors + 1 .. Inf -> $current-number { + my @intra-solutions = ( 1 .. $current-number ).grep( { $current-number %% $_ } ); + + %solutions{ $current-number } = @intra-solutions if @intra-solutions.elems == $divisors; + + last if %solutions.keys.elems >= $count; + } + + "$_ has $divisors divisors: { %solutions{ $_ }.join( ', ' ) }".say for %solutions.keys.sort; +} -- cgit From df46be1b4580869cfe827a54f18545a469adb82c Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 29 Nov 2021 10:15:27 +0000 Subject: Solutions for challenge #141 --- challenge-141/roger-bell-west/perl/ch-1.pl | 42 +++++++++++++++ challenge-141/roger-bell-west/perl/ch-2.pl | 28 ++++++++++ challenge-141/roger-bell-west/postscript/ch-1.ps | 66 ++++++++++++++++++++++++ challenge-141/roger-bell-west/postscript/ch-2.ps | 35 +++++++++++++ challenge-141/roger-bell-west/python/ch-1.py | 36 +++++++++++++ challenge-141/roger-bell-west/python/ch-2.py | 26 ++++++++++ challenge-141/roger-bell-west/raku/ch-1.p6 | 39 ++++++++++++++ challenge-141/roger-bell-west/raku/ch-2.p6 | 26 ++++++++++ challenge-141/roger-bell-west/ruby/ch-1.rb | 43 +++++++++++++++ challenge-141/roger-bell-west/ruby/ch-2.rb | 33 ++++++++++++ challenge-141/roger-bell-west/rust/ch-1.rs | 39 ++++++++++++++ challenge-141/roger-bell-west/rust/ch-2.rs | 30 +++++++++++ 12 files changed, 443 insertions(+) create mode 100755 challenge-141/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-141/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-141/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-141/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-141/roger-bell-west/python/ch-1.py create mode 100755 challenge-141/roger-bell-west/python/ch-2.py create mode 100755 challenge-141/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-141/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-141/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-141/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-141/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-141/roger-bell-west/rust/ch-2.rs (limited to 'challenge-141') diff --git a/challenge-141/roger-bell-west/perl/ch-1.pl b/challenge-141/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..4853fc58d3 --- /dev/null +++ b/challenge-141/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,42 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 1; + +is_deeply(divisors(8,10),[24,30,40,42,54,56,66,70,78,88],'example 1'); + +sub factorcount { + my $n=shift; + if ($n==1) { + return 1; + } + my $f=2; + my $s=int(sqrt($n)); + if ($s*$s == $n) { + $s--; + $f++; + } + foreach my $pf (2..$s) { + if ($n % $pf == 0) { + $f+=2; + } + } + return $f; +} + +sub divisors { + my ($count,$n)=@_; + my $nn=$n; + my @a; + my $t=0; + while ($nn) { + $t++; + if (factorcount($t)==$count) { + push @a,$t; + $nn--; + } + } + return \@a; +} diff --git a/challenge-141/roger-bell-west/perl/ch-2.pl b/challenge-141/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..2d43ec2eff --- /dev/null +++ b/challenge-141/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,28 @@ +#! /usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 2; + +is(likenumber(1234,2),9,'example 1'); +is(likenumber(768,4),3,'example 2'); + +sub likenumber { + my ($source,$factor)=@_; + my @s=split '',$source; + my $m=scalar @s; + my $n=0; + foreach my $mask (1..(1<<$m)-2) { + my $c=0; + foreach my $di (0..$m-1) { + if ($mask & 1<<$di) { + $c=$c*10+$s[$di]; + } + } + if ($c % $factor == 0) { + $n++; + } + } + return $n; +} diff --git a/challenge-141/roger-bell-west/postscript/ch-1.ps b/challenge-141/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..9c71ddf223 --- /dev/null +++ b/challenge-141/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,66 @@ +%!PS + +/aeq { + 2 dict begin + /a exch def + /b exch def + a length b length eq { + /e true def + 0 1 a length 1 sub { + dup a exch get + exch b exch get ne { + /e false def + exit + } if + } for + e + } { + false + } ifelse + end +} bind def + +/apush { % [a b] c -> [a b c] + /t exch def + [ exch aload pop t ] +} bind def + +/factorctr { + /n exch def + n 1 eq { + 1 + } { + /f 2 def + /s n sqrt cvi def + s s mul n eq { + /s s 1 sub def + /f f 1 add def + } if + 2 1 s { + n exch mod 0 eq { + /f f 2 add def + } if + } for + f + } ifelse +} bind def + +/divisors { + /nn exch def + /ctr exch def + /a 0 array def + /t 0 def + { + nn 0 le { + exit + } if + /t t 1 add def + t factorctr ctr eq { + /a a t apush def + /nn nn 1 sub def + } if + } loop + a +} bind def + +8 10 divisors [ 24 30 40 42 54 56 66 70 78 88 ] aeq { (Pass) } { (FAIL) } ifelse = diff --git a/challenge-141/roger-bell-west/postscript/ch-2.ps b/challenge-141/roger-bell-west/postscript/ch-2.ps new file mode 100644 index 0000000000..2a6606f5b3 --- /dev/null +++ b/challenge-141/roger-bell-west/postscript/ch-2.ps @@ -0,0 +1,35 @@ +%!PS + +/apush { % [a b] c -> [a b c] + /t exch def + [ exch aload pop t ] +} bind def + +/i2s { + dup log cvi 1 add string cvs +} bind def + +/likenumber { + /factor exch def + /source exch def + /s source i2s def + /m s length def + /n 0 def + 1 1 1 m bitshift 2 sub { + /mask exch def + /c 0 def + 0 1 m 1 sub { + /di exch def + mask 1 di bitshift and 0 gt { + /c c 10 mul s di get cvi add def + } if + } for + c factor mod 0 eq { + /n n 1 add def + } if + } for + n +} bind def + +1234 2 likenumber 9 eq { (Pass) } { (FAIL) } ifelse print ( ) print +768 4 likenumber 3 eq { (Pass) } { (FAIL) } ifelse = diff --git a/challenge-141/roger-bell-west/python/ch-1.py b/challenge-141/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..b1e8726449 --- /dev/null +++ b/challenge-141/roger-bell-west/python/ch-1.py @@ -0,0 +1,36 @@ +#! /usr/bin/python3 + +import unittest + +from math import sqrt + +def factorcount(n): + if n==1: + return 1 + f=2 + s=int(sqrt(n)) + if s*s==n: + s-=1 + f+=1 + for pf in range(2,s+1): + if n % pf == 0: + f += 2 + return f + +def divisors(count,n): + nn=n + a=[] + t=0 + while nn: + t+=1 + if factorcount(t)==count: + a.append(t) + nn-=1 + return a + +class TestDivisors(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(divisors(8,10),[24,30,40,42,54,56,66,70,78,88],'example 2') + +unittest.main() diff --git a/challenge-141/roger-bell-west/python/ch-2.py b/challenge-141/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..1bcf983c5f --- /dev/null +++ b/challenge-141/roger-bell-west/python/ch-2.py @@ -0,0 +1,26 @@ +#! /usr/bin/python3 + +import unittest + +def likenumber(source,factor): + s=[int(i) for i in str(source)] + m=len(s) + n=0 + for mask in range(1,(1< $pf { + if ($n % $pf == 0) { + $f+=2; + } + } + return $f; +} + +sub divisors($count,$n) { + my $nn=$n; + my @a; + my $t=0; + while ($nn) { + $t++; + if (factorcount($t)==$count) { + push @a,$t; + $nn--; + } + } + return @a; +} diff --git a/challenge-141/roger-bell-west/raku/ch-2.p6 b/challenge-141/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..982293ba2b --- /dev/null +++ b/challenge-141/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,26 @@ +#! /usr/bin/perl6 + +use Test; + +plan 2; + +is(likenumber(1234,2),9,'example 1'); +is(likenumber(768,4),3,'example 2'); + +sub likenumber($source,$factor) { + my @s=$source.comb; + my $m=@s.elems; + my $n=0; + for (1..(1 +< $m)-2) -> $mask { + my $c=0; + for (0..$m-1) -> $di { + if ($mask +& (1 +< $di)) { + $c=$c*10+@s[$di]; + } + } + if ($c % $factor == 0) { + $n++; + } + } + return $n; +} diff --git a/challenge-141/roger-bell-west/ruby/ch-1.rb b/challenge-141/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..ce0c339fb6 --- /dev/null +++ b/challenge-141/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,43 @@ +#! /usr/bin/ruby + +def factorcount(n) + if n==1 then + return 1 + end + f=2 + s=Math.sqrt(n).floor + if s*s==n then + s-=1 + f+=1 + end + 2.upto(s) do |pf| + if n % pf == 0 then + f+= 2 + end + end + return f +end + +def divisors(count,n) + nn=n + a=[] + t=0 + while nn>0 do + t+=1 + if factorcount(t)==count then + a.push(t) + nn-=1 + end + end + return a +end + +require 'test/unit' + +class TestDivisors < Test::Unit::TestCase + + def test_ex1 + assert_equal([24,30,40,42,54,56,66,70,78,88],divisors(8,10)) + end + +end diff --git a/challenge-141/roger-bell-west/ruby/ch-2.rb b/challenge-141/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..b48d253329 --- /dev/null +++ b/challenge-141/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,33 @@ +#! /usr/bin/ruby + +def likenumber(source,factor) + s=source.to_s.split('').map {|i| i.to_i} + m=s.length + n=0 + 1.upto((1<0 then + c=c*10+s[di] + end + end + if c % factor == 0 then + n+=1 + end + end + return n +end + +require 'test/unit' + +class TestLikenumber < Test::Unit::TestCase + + def test_ex1 + assert_equal(9,likenumber(1234,2)) + end + + def test_ex2 + assert_equal(3,likenumber(768,4)) + end + +end diff --git a/challenge-141/roger-bell-west/rust/ch-1.rs b/challenge-141/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..d1ea4d6669 --- /dev/null +++ b/challenge-141/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,39 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(divisors(8,10),vec![24,30,40,42,54,56,66,70,78,88]); +} + +fn factorcount(n: u32) -> u32 { + if n==1 { + return 1; + } + let mut f=2; + let mut s=(n as f64).sqrt() as u32; + if s*s==n { + s-=1; + f+=1; + } + for pf in 2..=s { + if n % pf == 0 { + f+=2; + } + } + f +} + +fn divisors(count: u32,n: u32) -> Vec { + let mut nn=n; + let mut a=vec![]; + let mut t=0; + while nn>0 { + t+=1; + if factorcount(t)==count { + a.push(t); + nn-=1 + } + } + a +} diff --git a/challenge-141/roger-bell-west/rust/ch-2.rs b/challenge-141/roger-bell-west/rust/ch-2.rs new file mode 100755 index 0000000000..694f781072 --- /dev/null +++ b/challenge-141/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,30 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(likenumber(1234,2),9); +} + +#[test] +fn test_ex2() { + assert_eq!(likenumber(768,4),3); +} + +fn likenumber(source: u32,factor: u32) -> u32 { + let s: Vec=source.to_string().chars().map(|i| i.to_digit(10).unwrap()).collect(); + let m=s.len(); + let mut n=0; + for mask in 1..((1< 0 { + c=c*10+s[di]; + } + } + if c % factor == 0 { + n+=1; + } + } + n +} -- cgit From f8b59640efe08afb9d3e45a9327fbe52e5d21f3f Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 29 Nov 2021 12:23:14 +0000 Subject: Challenge 141 Solutions (Raku) --- challenge-141/mark-anderson/raku/ch-1.raku | 5 +++++ challenge-141/mark-anderson/raku/ch-2.raku | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 challenge-141/mark-anderson/raku/ch-1.raku create mode 100644 challenge-141/mark-anderson/raku/ch-2.raku (limited to 'challenge-141') diff --git a/challenge-141/mark-anderson/raku/ch-1.raku b/challenge-141/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..2a2f90f510 --- /dev/null +++ b/challenge-141/mark-anderson/raku/ch-1.raku @@ -0,0 +1,5 @@ +#!/usr/bin/env raku + +use Prime::Factor; + +say (^Inf).race.grep({ divisors($_) == 8 })[^10]; diff --git a/challenge-141/mark-anderson/raku/ch-2.raku b/challenge-141/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..b0b3b69fe7 --- /dev/null +++ b/challenge-141/mark-anderson/raku/ch-2.raku @@ -0,0 +1,18 @@ +#!/usr/bin/env raku + +use Test; +plan 4; + +is like-numbers(1234, 2), 9, '1234, 2 = (2 4 12 14 24 34 124 134 234)'; +is like-numbers( 768, 4), 3, ' 768, 4 = (8 76 68)'; +is like-numbers(1008, 2), 6, '1008, 2 = (0 8 10 18 100 108)'; +is like-numbers(7777, 7), 3, '7777, 7 = (7 77 777)'; + +sub like-numbers($m is copy, \n) +{ + $m .= comb; + $m.combinations(1..^+$m).map(+*.join) + .grep(* %% n) + .unique + .Int; +} -- cgit From 69686f4bcbe2412b8a50ba9dd490dc335d5acb8e Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 29 Nov 2021 14:46:03 +0100 Subject: Task 2 done --- challenge-141/luca-ferrari/raku/ch-2.p6 | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100755 challenge-141/luca-ferrari/raku/ch-2.p6 (limited to 'challenge-141') diff --git a/challenge-141/luca-ferrari/raku/ch-2.p6 b/challenge-141/luca-ferrari/raku/ch-2.p6 new file mode 100755 index 0000000000..ae278b19e1 --- /dev/null +++ b/challenge-141/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,6 @@ +#!raku + +sub MAIN( Int $m, Int $n ) { + $m.comb.combinations( 1 ..^ $m.Str.chars ).map( *.join.Int ).grep( * %% $n ).unique.sort.say; + +} -- cgit From 4b18088d2c8639ff4e884e0d6b72303aaaa0b96d Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 29 Nov 2021 13:47:02 +0000 Subject: Challenge 141 Solutions (Raku) --- challenge-141/mark-anderson/raku/ch-1.raku | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'challenge-141') diff --git a/challenge-141/mark-anderson/raku/ch-1.raku b/challenge-141/mark-anderson/raku/ch-1.raku index 2a2f90f510..f5547da629 100644 --- a/challenge-141/mark-anderson/raku/ch-1.raku +++ b/challenge-141/mark-anderson/raku/ch-1.raku @@ -1,5 +1,16 @@ #!/usr/bin/env raku use Prime::Factor; +use Test; +plan 2; -say (^Inf).race.grep({ divisors($_) == 8 })[^10]; +is-deeply number-divisors(8)[^10], + (24, 30, 40, 42, 54, 56, 66, 70, 78, 88), 'example 1'; + +is-deeply number-divisors(8)[^10000].tail(5), + (45626, 45627, 45638, 45642, 45646), 'race ftw!'; + +sub number-divisors(\n) +{ + (^Inf).race.grep({ divisors($_) == n }); +} -- cgit From ae5ffc0af2fd30754b16536d506605a3c55255de Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 29 Nov 2021 13:53:38 +0000 Subject: Challenge 141 Solutions (Raku) --- challenge-141/mark-anderson/raku/ch-1.raku | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'challenge-141') diff --git a/challenge-141/mark-anderson/raku/ch-1.raku b/challenge-141/mark-anderson/raku/ch-1.raku index f5547da629..5a65a0f2e3 100644 --- a/challenge-141/mark-anderson/raku/ch-1.raku +++ b/challenge-141/mark-anderson/raku/ch-1.raku @@ -8,9 +8,9 @@ is-deeply number-divisors(8)[^10], (24, 30, 40, 42, 54, 56, 66, 70, 78, 88), 'example 1'; is-deeply number-divisors(8)[^10000].tail(5), - (45626, 45627, 45638, 45642, 45646), 'race ftw!'; + (45626, 45627, 45638, 45642, 45646), 'hyper ftw!'; sub number-divisors(\n) { - (^Inf).race.grep({ divisors($_) == n }); + (^Inf).hyper.grep({ divisors($_) == n }); } -- cgit From c62eea0af3ad4e45249f0c17ea597f2b220aad60 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 29 Nov 2021 15:58:12 +0100 Subject: Task 1 in PostgreSQL --- challenge-141/luca-ferrari/postgresql/ch-1.sql | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 challenge-141/luca-ferrari/postgresql/ch-1.sql (limited to 'challenge-141') diff --git a/challenge-141/luca-ferrari/postgresql/ch-1.sql b/challenge-141/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..c68e881175 --- /dev/null +++ b/challenge-141/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,32 @@ +CREATE OR REPLACE FUNCTION +f_find_divisors( divisors int default 8, count int default 10 ) +RETURNS SETOF int +AS $CODE$ +DECLARE + current_number int; + current_divisor int; + current_found int; +BEGIN + FOR current_number IN 1 .. 999999 LOOP + IF count = 0 THEN + EXIT; + END IF; + + current_found := 0; + + FOR current_divisor IN 1 .. current_number LOOP + IF current_number % current_divisor = 0 THEN + current_found := current_found + 1; + END IF; + END LOOP; + + IF current_found = divisors THEN + count := count - 1; + RETURN NEXT current_number; + END IF; + END LOOP; + + RETURN; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 705640f5611e95eb41001b853b84c6b3d1bd22bd Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 29 Nov 2021 16:22:22 +0100 Subject: Task 2 in PostgreSQL --- challenge-141/luca-ferrari/postgresql/ch-2.sql | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 challenge-141/luca-ferrari/postgresql/ch-2.sql (limited to 'challenge-141') diff --git a/challenge-141/luca-ferrari/postgresql/ch-2.sql b/challenge-141/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..239d846b58 --- /dev/null +++ b/challenge-141/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,21 @@ +CREATE OR REPLACE FUNCTION +f_live_numbers( m int, n int ) +RETURNS SETOF int +AS $CODE$ +WITH RECURSIVE +numbers AS ( SELECT unnest( regexp_split_to_array( m::text, '' ) ) AS n ) +, combinations( i, v, c ) AS ( +SELECT 1, n, n +FROM numbers +UNION +SELECT i + 1, n, c || num.n +FROM combinations, numbers num +WHERE length( c || num.n ) < length( m::text ) - 1 +AND num.n IN ( SELECT n FROM numbers WHERE n::int > i ) +) + +SELECT c::int FROM combinations +WHERE c::int % n = 0; + +$CODE$ +LANGUAGE sql; -- cgit From c89584624fde5c8d698e132b340a2ac0c462983e Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 29 Nov 2021 16:43:05 +0100 Subject: Blog references --- challenge-141/luca-ferrari/blog-1.txt | 1 + challenge-141/luca-ferrari/blog-2.txt | 1 + challenge-141/luca-ferrari/blog-3.txt | 1 + challenge-141/luca-ferrari/blog-4.txt | 1 + 4 files changed, 4 insertions(+) create mode 100644 challenge-141/luca-ferrari/blog-1.txt create mode 100644 challenge-141/luca-ferrari/blog-2.txt create mode 100644 challenge-141/luca-ferrari/blog-3.txt create mode 100644 challenge-141/luca-ferrari/blog-4.txt (limited to 'challenge-141') diff --git a/challenge-141/luca-ferrari/blog-1.txt b/challenge-141/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..80cbe14380 --- /dev/null +++ b/challenge-141/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/11/29/PerlWeeklyChallenge141.html#task1 diff --git a/challenge-141/luca-ferrari/blog-2.txt b/challenge-141/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..35e7946b2f --- /dev/null +++ b/challenge-141/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/11/29/PerlWeeklyChallenge141.html#task2 diff --git a/challenge-141/luca-ferrari/blog-3.txt b/challenge-141/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..e064c87e1a --- /dev/null +++ b/challenge-141/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/11/29/PerlWeeklyChallenge141.html#task1pg diff --git a/challenge-141/luca-ferrari/blog-4.txt b/challenge-141/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..0c5caa9c08 --- /dev/null +++ b/challenge-141/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2021/11/29/PerlWeeklyChallenge141.html#task2pg -- cgit From c568b97a2e3dbf5da3aea5252444b8cf2082aeff Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 29 Nov 2021 18:01:15 +0100 Subject: Add solutions to 141: Number Divisors & Like Numbers by E. Choroba --- challenge-141/e-choroba/perl/ch-1.pl | 49 ++++++++++++++++++++++++++++++++++++ challenge-141/e-choroba/perl/ch-2.pl | 23 +++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100755 challenge-141/e-choroba/perl/ch-1.pl create mode 100755 challenge-141/e-choroba/perl/ch-2.pl (limited to 'challenge-141') diff --git a/challenge-141/e-choroba/perl/ch-1.pl b/challenge-141/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..ef9aabe4bd --- /dev/null +++ b/challenge-141/e-choroba/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +use constant { + DIVISOR_TALLY => 8, + RESULT_TALLY => 10, +}; + +sub number_divisors_full { + my @results; + my $i = 1; + while (@results < RESULT_TALLY) { + my @d = grep 0 == $i % $_, 1 .. $i; + push @results, $i if @d == DIVISOR_TALLY; + } continue { + ++$i; + } + return @results +} + +sub number_divisors_short { + my @results; + my $i = 1; + while (@results < RESULT_TALLY) { + my $divisor_tally = 0; + my $s = sqrt $i; + for my $d (1 .. $s) { + $divisor_tally += ($d == $s) ? 1 : 2 if 0 == $i % $d; + } + push @results, $i if $divisor_tally == DIVISOR_TALLY; + } continue { + ++$i; + } + return @results +} + +say join ', ', number_divisors_short(); + +use Test2::V0; +plan 1; +is [number_divisors_short], [number_divisors_full], 'same'; + +use Benchmark qw{ cmpthese }; +cmpthese(-3, { + full => \&number_divisors_full, + short => \&number_divisors_short, +}); diff --git a/challenge-141/e-choroba/perl/ch-2.pl b/challenge-141/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..82eeaabaee --- /dev/null +++ b/challenge-141/e-choroba/perl/ch-2.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw{ signatures bitwise }; + +my @MASK = ("\x00", "\xFF"); +sub like_numbers ($digits, $divisor) { + my $length = length $digits; + my $count = 0; + for my $indicator (1 .. 2 ** $length - 2) { + my $mask = sprintf('%0*b', $length, $indicator) + =~ s/(.)/$MASK[$1]/gr; + my $candidate = ($mask &. $digits) =~ tr/\x00//dr; + ++$count if 0 == $candidate % $divisor; + } + return $count +} + +use Test2::V0; +plan 2; + +is like_numbers(1234, 2), 9, 'Example 1'; +is like_numbers(768, 4), 3, 'Example 2'; -- cgit From bfbe46021b65c553df3ecc6a4f311d0ab2b23f07 Mon Sep 17 00:00:00 2001 From: dasJake Date: Mon, 29 Nov 2021 21:10:29 +0100 Subject: 141 initial commit for 141/ch-1.pl --- challenge-141/jake/perl/ch-1.pl | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 challenge-141/jake/perl/ch-1.pl (limited to 'challenge-141') diff --git a/challenge-141/jake/perl/ch-1.pl b/challenge-141/jake/perl/ch-1.pl new file mode 100644 index 0000000000..711f5cf4f7 --- /dev/null +++ b/challenge-141/jake/perl/ch-1.pl @@ -0,0 +1,4 @@ +#!/usr/bin/env perl + +use warnings; +use strict; -- cgit From 931699cba0d6373b7f7a9e645ff1a6ef59ad6e5d Mon Sep 17 00:00:00 2001 From: dasJake Date: Mon, 29 Nov 2021 22:33:02 +0100 Subject: 141 create hash for number attributes --- challenge-141/jake/perl/ch-1.pl | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) mode change 100644 => 100755 challenge-141/jake/perl/ch-1.pl (limited to 'challenge-141') diff --git a/challenge-141/jake/perl/ch-1.pl b/challenge-141/jake/perl/ch-1.pl old mode 100644 new mode 100755 index 711f5cf4f7..13e4c85102 --- a/challenge-141/jake/perl/ch-1.pl +++ b/challenge-141/jake/perl/ch-1.pl @@ -2,3 +2,27 @@ use warnings; use strict; +use feature 'say'; + +my $number_attributes = { + number => '1', + divisor_counter => '0', + subtractor => '0', +}; +count_divisors( $number_attributes ); +# modulo number n through n, n-1, and so on +# for each modulo 0 increase counter +# when counter hits 8 add number to number array, proceed to next number and increase number counter +# if number counter hits 10 output number array or if array size becomes 10 + +sub count_divisors { + say ( $number_attributes->{divisor_counter}, $number_attributes->{subtractor}, $number_attributes->{number} ); + while ( $number_attributes->{divisor_counter} < 9 ) { + say ( $number_attributes->{divisor_counter}, $number_attributes->{subtractor}, $number_attributes->{number} ); + $number_attributes->{divisor_counter}++ if $number_attributes->{number} % ( $number_attributes->{number} - $number_attributes->{subtractor} ) == 0; + $number_attributes->{subtractor}++; + print "div_c: $number_attributes->{divisor_counter}, subtr: $number_attributes->{subtractor}\n"; + $number_attributes->{number}++; + die if $number_attributes->{number} == 10; + } +} \ No newline at end of file -- cgit From 9d0eba1c03c1c9503fcccc9fa71e193d029f4fa8 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 29 Nov 2021 17:25:05 -0500 Subject: 141 --- challenge-141/dave-jacoby/blog.txt | 1 + challenge-141/dave-jacoby/perl/ch-1.pl | 27 ++++++++++++++++++++ challenge-141/dave-jacoby/perl/ch-2.pl | 45 ++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 challenge-141/dave-jacoby/blog.txt create mode 100644 challenge-141/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-141/dave-jacoby/perl/ch-2.pl (limited to 'challenge-141') diff --git a/challenge-141/dave-jacoby/blog.txt b/challenge-141/dave-jacoby/blog.txt new file mode 100644 index 0000000000..83cb987fe0 --- /dev/null +++ b/challenge-141/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2021/11/29/i-like-numbers-and-hate-division-the-weekly-challenge-141.html \ No newline at end of file diff --git a/challenge-141/dave-jacoby/perl/ch-1.pl b/challenge-141/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..77368c6f5e --- /dev/null +++ b/challenge-141/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say state postderef signatures }; +no warnings qw{ experimental }; + +my @num_divisors = get_number_divisors( 1, 10, 8 ); + +sub get_number_divisors ( $start, $count, $divisors ) { + my @output; + my $s = $start; + while (1) { + my @divisors; + for my $i ( 1 .. $s ) { + push @divisors, $i if $s % $i == 0; + } + if ( scalar @divisors == $divisors ) { + say join " ", $s, ':', ( scalar @divisors ), ':', @divisors; + push @output, $s; + } + last if $count == scalar @output; + $s++; + } + return @output; +} + diff --git a/challenge-141/dave-jacoby/perl/ch-2.pl b/challenge-141/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..2ecc251f97 --- /dev/null +++ b/challenge-141/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say postderef signatures state }; +no warnings qw{ experimental }; + +use Carp; +use Getopt::Long; + +my $m = 1234; +my $n = 2; + +GetOptions( + 'm=i' => \$m, + 'n=i' => \$n, +); + +croak q{$m is not positive} if $m < 1; +croak q{$n is not positive} if $n < 1; + +my @like_numbers = like_numbers( $m, $n ); +say join ' ', @like_numbers; + +sub like_numbers ( $m, $n ) { + my @numbers = make_numbers($m); + return + + sort { $a <=> $b } + grep { $_ % $n == 0 } + grep { $_ != $m } + + @numbers; +} + +sub make_numbers ( $number, $n = '' ) { + my @output; + for my $i ( 0 .. -1 + length $number ) { + my $x = $n . substr( $number, $i, 1 ); + my $y = substr( $number, $i + 1 ); + push @output, $x; + push @output, make_numbers( $y, $x ) if length $y; + } + return @output; +} -- cgit From 351adc907c30510c93e4dae7e36dd168d227ab47 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 29 Nov 2021 21:32:05 -0600 Subject: Solve PWC141 --- challenge-141/wlmb/blog.txt | 1 + challenge-141/wlmb/perl/ch-1.pl | 21 +++++++++++++++++++++ challenge-141/wlmb/perl/ch-2.pl | 24 ++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 challenge-141/wlmb/blog.txt create mode 100755 challenge-141/wlmb/perl/ch-1.pl create mode 100755 challenge-141/wlmb/perl/ch-2.pl (limited to 'challenge-141') diff --git a/challenge-141/wlmb/blog.txt b/challenge-141/wlmb/blog.txt new file mode 100644 index 0000000000..d0ece13f8f --- /dev/null +++ b/challenge-141/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2021/11/29/PWC141/ diff --git a/challenge-141/wlmb/perl/ch-1.pl b/challenge-141/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..1eb780b7d7 --- /dev/null +++ b/challenge-141/wlmb/perl/ch-1.pl @@ -0,0 +1,21 @@ +#!/usr/bin/env perl +# Perl weekly challenge 141 +# Task 1: Number divisors +# +# See https://wlmb.github.io/2021/11/29/PWC141/#task-1-number-divisors +use v5.12; +use warnings; +use PDL; +use PDL::NiceSlice; +#Set defaults and params. from com. line +my %params=(try=>100, divisors=>8, results=>10, @ARGV); +my ($try, $divisors, $results) + =@params{qw(try divisors results)}; +my $cells=zeroes $try; +# count divisors for each number +$cells($_:-1:$_)+=1 for(1..$try-1); +# find all d-multiples +my $multiples=$cells->xvals->where($cells==$divisors); +die "Need to increase try" unless $multiples->nelem>=$results; +my $out=$multiples(0:$results-1); +say "try=$try, divisors=$divisors, results=$results, out=$out"; diff --git a/challenge-141/wlmb/perl/ch-2.pl b/challenge-141/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..03f3c416e1 --- /dev/null +++ b/challenge-141/wlmb/perl/ch-2.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl +# Perl weekly challenge 141 +# Task 2: Like numbers +# +# See https://wlmb.github.io/2021/11/29/PWC141/#task-2-like-numbers +use v5.12; +use warnings; +use PDL; +use PDL::NiceSlice; + +die "Usage: ./ch-2.pl m n" unless @ARGV==2; +my ($m,$n)=@ARGV; +my $l=length $m; +my $digits=pdl[split "", $m]; +my $multiples= pdl [ + sort {$a<=>$b} + grep {$_!~/^0/&&$_%$n==0} + map { + my $bits=pdl([split "", sprintf "%b", $_]); + join "", $digits->where($bits->(-1:0))->list; + } 1..2**$l-2 + ]; +say "m=$m, n=$n, Out=", $multiples->nelem, + "\nas multiples=$multiples"; -- cgit From 44bcd3ae0c35b887bd579e60c17198360f3497c3 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 30 Nov 2021 20:04:35 +0100 Subject: Solutions week 141 --- challenge-141/abigail/README.md | 3 +- challenge-141/abigail/awk/ch-1.awk | 31 ++++++++++++++ challenge-141/abigail/awk/ch-2.awk | 32 ++++++++++++++ challenge-141/abigail/bash/ch-1.sh | 29 +++++++++++++ challenge-141/abigail/bash/ch-2.sh | 40 ++++++++++++++++++ challenge-141/abigail/bc/ch-1.bc | 28 +++++++++++++ challenge-141/abigail/c/ch-1.c | 35 ++++++++++++++++ challenge-141/abigail/c/ch-2.c | 42 +++++++++++++++++++ challenge-141/abigail/go/ch-1.go | 36 ++++++++++++++++ challenge-141/abigail/go/ch-2.go | 49 ++++++++++++++++++++++ challenge-141/abigail/java/ch-1.java | 32 ++++++++++++++ challenge-141/abigail/java/ch-2.java | 37 +++++++++++++++++ challenge-141/abigail/lua/ch-1.lua | 36 ++++++++++++++++ challenge-141/abigail/lua/ch-2.lua | 37 +++++++++++++++++ challenge-141/abigail/node/ch-1.js | 30 ++++++++++++++ challenge-141/abigail/node/ch-2.js | 32 ++++++++++++++ challenge-141/abigail/pascal/ch-1.p | 39 +++++++++++++++++ challenge-141/abigail/pascal/ch-2.p | 54 ++++++++++++++++++++++++ challenge-141/abigail/perl/ch-1.pl | 26 ++++++++++++ challenge-141/abigail/perl/ch-2.pl | 76 ++++++++++++++++++++++++++++++++++ challenge-141/abigail/python/ch-1.py | 30 ++++++++++++++ challenge-141/abigail/python/ch-2.py | 32 ++++++++++++++ challenge-141/abigail/r/ch-1.r | 32 ++++++++++++++ challenge-141/abigail/r/ch-2.r | 42 +++++++++++++++++++ challenge-141/abigail/ruby/ch-1.rb | 34 +++++++++++++++ challenge-141/abigail/ruby/ch-2.rb | 35 ++++++++++++++++ challenge-141/abigail/scheme/ch-1.scm | 31 ++++++++++++++ challenge-141/abigail/scheme/ch-2.scm | 40 ++++++++++++++++++ challenge-141/abigail/t/ctest.ini | 12 ++++++ challenge-141/abigail/t/input-1-1 | 0 challenge-141/abigail/t/input-2-1 | 2 + challenge-141/abigail/t/input-2-2 | 2 + challenge-141/abigail/t/output-1-1.exp | 10 +++++ challenge-141/abigail/t/output-2-1.exp | 2 + challenge-141/abigail/t/output-2-2.exp | 2 + challenge-141/abigail/tcl/ch-1.tcl | 31 ++++++++++++++ challenge-141/abigail/tcl/ch-2.tcl | 35 ++++++++++++++++ 37 files changed, 1094 insertions(+), 2 deletions(-) create mode 100644 challenge-141/abigail/awk/ch-1.awk create mode 100644 challenge-141/abigail/awk/ch-2.awk create mode 100644 challenge-141/abigail/bash/ch-1.sh create mode 100644 challenge-141/abigail/bash/ch-2.sh create mode 100644 challenge-141/abigail/bc/ch-1.bc create mode 100644 challenge-141/abigail/c/ch-1.c create mode 100644 challenge-141/abigail/c/ch-2.c create mode 100644 challenge-141/abigail/go/ch-1.go create mode 100644 challenge-141/abigail/go/ch-2.go create mode 100644 challenge-141/abigail/java/ch-1.java create mode 100644 challenge-141/abigail/java/ch-2.java create mode 100644 challenge-141/abigail/lua/ch-1.lua create mode 100644 challenge-141/abigail/lua/ch-2.lua create mode 100644 challenge-141/abigail/node/ch-1.js create mode 100644 challenge-141/abigail/node/ch-2.js create mode 100644 challenge-141/abigail/pascal/ch-1.p create mode 100644 challenge-141/abigail/pascal/ch-2.p create mode 100644 challenge-141/abigail/perl/ch-1.pl create mode 100644 challenge-141/abigail/perl/ch-2.pl create mode 100644 challenge-141/abigail/python/ch-1.py create mode 100644 challenge-141/abigail/python/ch-2.py create mode 100644 challenge-141/abigail/r/ch-1.r create mode 100644 challenge-141/abigail/r/ch-2.r create mode 100644 challenge-141/abigail/ruby/ch-1.rb create mode 100644 challenge-141/abigail/ruby/ch-2.rb create mode 100644 challenge-141/abigail/scheme/ch-1.scm create mode 100644 challenge-141/abigail/scheme/ch-2.scm create mode 100644 challenge-141/abigail/t/ctest.ini create mode 100644 challenge-141/abigail/t/input-1-1 create mode 100644 challenge-141/abigail/t/input-2-1 create mode 100644 challenge-141/abigail/t/input-2-2 create mode 100644 challenge-141/abigail/t/output-1-1.exp create mode 100644 challenge-141/abigail/t/output-2-1.exp create mode 100644 challenge-141/abigail/t/output-2-2.exp create mode 100644 challenge-141/abigail/tcl/ch-1.tcl create mode 100644 challenge-141/abigail/tcl/ch-2.tcl (limited to 'challenge-141') diff --git a/challenge-141/abigail/README.md b/challenge-141/abigail/README.md index 0707d1fbd5..0ebe05c131 100644 --- a/challenge-141/abigail/README.md +++ b/challenge-141/abigail/README.md @@ -2,7 +2,7 @@ ## Part 1 -* [GNU AWK](awk/ch-1.gawk) +* [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) * [Bc](bc/ch-1.bc) * [C](c/ch-1.c) @@ -22,7 +22,6 @@ * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) -* [Bc](bc/ch-2.bc) * [C](c/ch-2.c) * [Go](go/ch-2.go) * [Java](java/ch-2.java) diff --git a/challenge-141/abigail/awk/ch-1.awk b/challenge-141/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..9b9ae8dc67 --- /dev/null +++ b/challenge-141/abigail/awk/ch-1.awk @@ -0,0 +1,31 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk +# + +BEGIN { + count = 10; + nr_of_divisors = 8; + + for (n = 1; count > 0; n ++) { + s = int sqrt (n); + if (n == s * s) { + continue + } + c = 0; + for (m = 1; m <= s && c <= nr_of_divisors; m ++) { + if (n % m == 0) { + c += 2 + } + } + if (c == nr_of_divisors) { + print (n) + count -- + } + } +} diff --git a/challenge-141/abigail/awk/ch-2.awk b/challenge-141/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..ac15121558 --- /dev/null +++ b/challenge-141/abigail/awk/ch-2.awk @@ -0,0 +1,32 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +function substrings (n, m, prefix, max, fc, tail, n_prefix) { + if (length (n) == 0) { + # + # End of recursion + # + return prefix > -1 && \ + prefix < max && \ + prefix % m == 0 ? 1 : 0 + } + + fc = substr (n, 1, 1) + tail = substr (n, 2) + n_prefix = 10 * (prefix == -1 ? 0 : prefix) + fc + + return substrings(tail, m, n_prefix, max) + \ + substrings(tail, m, prefix, max) +} + +{ + print (substrings($1, $2, -1, $1)) +} + diff --git a/challenge-141/abigail/bash/ch-1.sh b/challenge-141/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..c3d8fb5299 --- /dev/null +++ b/challenge-141/abigail/bash/ch-1.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh +# + + +((count = 10)) +((nr_of_divisors = 8)) + +for ((n = 1; count > 0; n ++)) +do ((c = 0)) + for ((d = 1; c <= nr_of_divisors && d * d <= n; d ++)) + do if ((n % d == 0)) + then ((c = c + 2)) + if ((d * d == n)) + then ((c = c - 1)) + fi + fi + done + if ((c == nr_of_divisors)) + then echo $n + ((count = count - 1)) + fi +done diff --git a/challenge-141/abigail/bash/ch-2.sh b/challenge-141/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..fdcf74406b --- /dev/null +++ b/challenge-141/abigail/bash/ch-2.sh @@ -0,0 +1,40 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +function substrings () { + local n=$1 + local m=$2 + local prefix=$3 + local max=$4 + local r + if [ -z "$n" ] + then if (( prefix > -1 && prefix < max && prefix % m == 0)) + then ((r = 1)) + else ((r = 0)) + fi + else local tail=${n:1} + local fc=${n:0:1} + local n_prefix=$fc + if ((prefix > -1)) + then ((n_prefix = 10 * prefix + fc)) + fi + substrings "$tail" $m $n_prefix $max; local r1=$substrings + substrings "$tail" $m $prefix $max; local r2=$substrings + ((r = r1 + r2)) + fi + substrings=$r +} + +while read n m +do substrings $n $m -1 $n + echo $substrings +done diff --git a/challenge-141/abigail/bc/ch-1.bc b/challenge-141/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..d9f6ee0645 --- /dev/null +++ b/challenge-141/abigail/bc/ch-1.bc @@ -0,0 +1,28 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-1.bc +# + +count = 10 +nr_of_divisors = 8 + +for (n = 1; count > 0; n ++) { + c = 0 + for (m = 1; m * m <= n && c <= nr_of_divisors; m ++) { + if (n % m == 0) { + c = c + 2 + if (n == m * m) { + c = c - 1 + } + } + } + if (c == nr_of_divisors) { + n + count = count - 1 + } +} + +halt diff --git a/challenge-141/abigail/c/ch-1.c b/challenge-141/abigail/c/ch-1.c new file mode 100644 index 0000000000..ae9f916829 --- /dev/null +++ b/challenge-141/abigail/c/ch-1.c @@ -0,0 +1,35 @@ +# include +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +# define COUNT 10 +# define NR_OF_DIVISORS 8 + +int main (void) { + int count = COUNT; + for (int n = 1; count > 0; n ++) { + long s = lrintf (sqrt (n)); + int m, c; + if (n == s * s) { + continue; + } + for (m = 1, c = 0; m <= s && c <= NR_OF_DIVISORS; m ++) { + if (n % m == 0) { + c += 2; + } + } + if (c == NR_OF_DIVISORS) { + printf ("%d\n", n); + count --; + } + } +} diff --git a/challenge-141/abigail/c/ch-2.c b/challenge-141/abigail/c/ch-2.c new file mode 100644 index 0000000000..10e729d8a3 --- /dev/null +++ b/challenge-141/abigail/c/ch-2.c @@ -0,0 +1,42 @@ +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + + + +int substrings (char * n, int m, int prefix, int max) { + int fc, n_prefix; + char * tail; + if (!* n) { + return prefix > -1 && prefix < max && prefix % m == 0; + } + + fc = * n - '0'; + tail = n + 1; + n_prefix = 10 * (prefix == -1 ? 0 : prefix) + * n - '0'; + + return substrings (tail, m, n_prefix, max) + + substrings (tail, m, prefix, max); +} + +int main (void) { + int n, m; + char * num; + if ((num = (char *) malloc (32 * sizeof (char))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + while (scanf ("%d %d", &n, &m) == 2) { + sprintf (num, "%d", n); + printf ("%d\n", substrings (num, m, -1, n)); + } + return (0); +} diff --git a/challenge-141/abigail/go/ch-1.go b/challenge-141/abigail/go/ch-1.go new file mode 100644 index 0000000000..9da5e9763b --- /dev/null +++ b/challenge-141/abigail/go/ch-1.go @@ -0,0 +1,36 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import ( + "fmt" + "math" +) + +func main () { + count := 10 + nr_of_divisors := 8 + + for n := 1; count > 0; n ++ { + s := int (math . Sqrt (float64 (n))) + if n == s * s { + continue + } + c := 0 + for m := 1; m <= s && c <= nr_of_divisors; m ++ { + if n % m == 0 { + c += 2 + } + } + if c == nr_of_divisors { + fmt . Println (n) + count -- + } + } +} diff --git a/challenge-141/abigail/go/ch-2.go b/challenge-141/abigail/go/ch-2.go new file mode 100644 index 0000000000..56ed3073a2 --- /dev/null +++ b/challenge-141/abigail/go/ch-2.go @@ -0,0 +1,49 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-2.go < input-file +// + +import ( + "fmt" + "strconv" +) + +func substrings (n string, m int, prefix int, max int) int { + var n_prefix int; + if len (n) == 0 { + if prefix > -1 && + prefix < max && + prefix % m == 0 { + return 1 + } else { + return 0 + } + } + + fc, _ := strconv . Atoi (n [0:1]) + if prefix == -1 { + n_prefix = fc + } else { + n_prefix = 10 * prefix + fc + } + tail := n [1:] + + return substrings (tail, m, n_prefix, max) + + substrings (tail, m, prefix, max) +} + +func main () { + for { + var n, m int + c, err := fmt . Scanf ("%d %d", &n, &m) + if c != 2 || err != nil { + break + } + fmt . Println (substrings (strconv . Itoa (n), m, -1, n)) + } +} diff --git a/challenge-141/abigail/java/ch-1.java b/challenge-141/abigail/java/ch-1.java new file mode 100644 index 0000000000..00f394d01e --- /dev/null +++ b/challenge-141/abigail/java/ch-1.java @@ -0,0 +1,32 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 < input-file +// + +import java.util.*; + +public class ch1 { + public static void main (String [] args) { + int count = 10; + int nr_of_divisors = 8; + for (int n = 1; count > 0; n ++) { + int s = (int) Math . sqrt (n); + if (n == s * s) { + continue; + } + int c = 0; + for (int d = 1; d <= s && c <= nr_of_divisors; d ++) { + if (n % d == 0) { + c += 2; + } + } + if (c == nr_of_divisors) { + System . out . println (n); + count --; + } + } + } +} diff --git a/challenge-141/abigail/java/ch-2.java b/challenge-141/abigail/java/ch-2.java new file mode 100644 index 0000000000..bb03482e67 --- /dev/null +++ b/challenge-141/abigail/java/ch-2.java @@ -0,0 +1,37 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-2.java ch2.java; javac ch2.java; java ch2 < input-file +// + +import java.util.*; + +public class ch2 { + public static int substrings (String n, int m, int prefix, int max) { + if (n . length () == 0) { + return prefix > -1 && + prefix < max && + prefix % m == 0 ? 1 : 0; + } + + int fc = Integer . parseInt (n . substring (0, 1)); + int n_prefix = 10 * (prefix == -1 ? 0 : prefix) + fc; + String tail = n . substring (1, n. length ()); + + return substrings (tail, m, n_prefix, max) + + substrings (tail, m, prefix, max); + } + + + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + while (scanner . hasNextInt ()) { + int n = scanner . nextInt (); + int m = scanner . nextInt (); + System . out . println ( + substrings (Integer . toString (n), m, -1, n)); + } + } +} diff --git a/challenge-141/abigail/lua/ch-1.lua b/challenge-141/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..f9b626f6a9 --- /dev/null +++ b/challenge-141/abigail/lua/ch-1.lua @@ -0,0 +1,36 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua +-- + +local count = 10 +local nr_of_divisor = 8 + +local n = 0 +while count > 0 do + n = n + 1 + local s = math . floor (math . sqrt (n)) + if n == s * s then + goto end_while + end + local c = 0 + for d = 1, s do + if n % d == 0 then + c = c + 2 + if c > nr_of_divisor then + goto end_while + end + end + end + if c == nr_of_divisor then + print (n) + count = count - 1 + end + + ::end_while:: +end diff --git a/challenge-141/abigail/lua/ch-2.lua b/challenge-141/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..b7f87027f4 --- /dev/null +++ b/challenge-141/abigail/lua/ch-2.lua @@ -0,0 +1,37 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +function substrings (n, m, prefix, max) + if n : len () == 0 then + if prefix > -1 and prefix < max and prefix % m == 0 then + return 1 + else + return 0 + end + end + + local fc = tonumber (n : sub (1, 1)) + local tail = n : sub (2) + local n_prefix + if prefix == -1 then + n_prefix = fc + else + n_prefix = 10 * prefix + fc + end + + return substrings (tail, m, n_prefix, max) + + substrings (tail, m, prefix, max) +end + + +for line in io . lines () do + local _, _, n, m = line : find ("([0-9]+)%s+([0-9]+)") + print (substrings (n, tonumber (m), -1, tonumber (n))) +end diff --git a/challenge-141/abigail/node/ch-1.js b/challenge-141/abigail/node/ch-1.js new file mode 100644 index 0000000000..e246fffd08 --- /dev/null +++ b/challenge-141/abigail/node/ch-1.js @@ -0,0 +1,30 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js +// + + +let count = 10 +let nr_of_divisors = 8 + +for (let n = 1; count > 0; n ++) { + let s = Math . floor (Math . sqrt (n)) + if (n == s * s) { + continue + } + let c = 0 + for (let d = 1; d <= s && c <= nr_of_divisors; d ++) { + if (n % d == 0) { + c += 2 + } + } + if (c == nr_of_divisors) { + console . log (n) + count -- + } +} diff --git a/challenge-141/abigail/node/ch-2.js b/challenge-141/abigail/node/ch-2.js new file mode 100644 index 0000000000..53b7b7ac44 --- /dev/null +++ b/challenge-141/abigail/node/ch-2.js @@ -0,0 +1,32 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js < input-file +// + +function substrings (n, m, prefix, max) { + if (n . length == 0) { + return prefix > -1 && + prefix < max && + prefix % m == 0 ? 1 : 0 + } + + let fc = n . substr (0, 1) + let tail = n . substr (1) + let n_prefix = 10 * (prefix == -1 ? 0 : prefix) + + n . substr (0, 1) + + return substrings (tail, m, n_prefix, max) + + substrings (tail, m, prefix, max); +} + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + let [n, m] = line . split (" ") + console . log (substrings (n, +m, -1, +n)) +}) + diff --git a/challenge-141/abigail/pascal/ch-1.p b/challenge-141/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..7ade307f44 --- /dev/null +++ b/challenge-141/abigail/pascal/ch-1.p @@ -0,0 +1,39 @@ +Program XXX; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out *) +(* *) + +var + count: integer = 10; + nr_of_divisors: integer = 8; + n, s, d, c: integer; + + +begin + n := 0; + while count > 0 do begin + inc (n); + s := round (sqrt (n)); + if n = s * s then begin + continue; + end; + c := 0; + for d := 1 to s do begin + if n mod d = 0 then begin + c := c + 2; + if c > nr_of_divisors then begin + continue; + end + end + end; + if c = nr_of_divisors then begin + writeln (n); + dec (count); + end + end +end. diff --git a/challenge-141/abigail/pascal/ch-2.p b/challenge-141/abigail/pascal/ch-2.p new file mode 100644 index 0000000000..e2919ebbf5 --- /dev/null +++ b/challenge-141/abigail/pascal/ch-2.p @@ -0,0 +1,54 @@ +Program XXX; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-2.out ch-2.p; ./ch-2.out < input-file *) +(* *) + +uses + sysutils; + +function substrings (n: string; m, prefix, max: integer): integer; + var + fc, n_prefix: integer; + tail: string; + + begin + if length (n) = 0 then begin + if (prefix > -1) and + (prefix < max) and + (prefix mod m = 0) then begin + substrings := 1; + end + else begin + substrings := 0; + end; + end + else begin + fc := strtoint (copy (n, 1, 1)); + if prefix = -1 then begin + n_prefix := fc; + end + else begin + n_prefix := 10 * prefix + fc; + end; + + tail := copy (n, 2, length (n)); + + substrings := substrings (tail, m, n_prefix, max) + + substrings (tail, m, prefix, max); + end + end; + +var + n, m: Integer; + +begin + while not eof do begin + readln (n, m); + writeln (substrings (inttostr (n), m, -1, n)); + end; +end. diff --git a/challenge-141/abigail/perl/ch-1.pl b/challenge-141/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..382f1b8557 --- /dev/null +++ b/challenge-141/abigail/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +no warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-1.pl < input-file +# +# Math::Prime::Util has a method returning the number of divisors of +# a given number. So, it's just a matter of trying all numbers in order +# and reporting the first 10 with 8 divisors. +# + +use Math::Prime::Util qw [divisors]; + +8 == divisors (++ $::n) && say ($::n) && $::c ++ while $::c < 10; diff --git a/challenge-141/abigail/perl/ch-2.pl b/challenge-141/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..3417e54e1d --- /dev/null +++ b/challenge-141/abigail/perl/ch-2.pl @@ -0,0 +1,76 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-2.pl < input-file +# + +# +# The challenge isn't clear whether it wants a count of all possible +# numbers which match, or the number of unique numbers. For instance, +# an input of +# 1232 2 +# +# would yield 12 twice. Do we count them both? Or are we supposed to +# only count unique once? The examples are of no help. +# +# Furthermore, what about leading 0s? An input of +# +# 102 2 +# +# yield both 02 and 2. (And something like '100 3' will yield +# 0, 0 and 00). +# +# We've decided to be as inclusive as possible. So +# +# 1232 2 +# +# will give 12, 122, 132, 12, 2, 232, 22, 32, and 2, for a total of 9. +# +# and +# 102 2 +# +# will give 10, 12, 0, 02, and 2, for a total of 5. +# + +# +# Recursive function to count all substrings for which $m is +# a proper divisor. +# +sub substrings ($n, $m, $prefix = -1, $max = $n) { + if (!length $n) { + # + # If $n is empty, we have reached the end of recursion. + # If $prefix isn't -1, not equal to the full string, + # and if $m properly divides $prefix, we count $prefix, + # else, we don't. + # + return $prefix > -1 && + $prefix < $max && + $prefix % $m == 0 ? 1 : 0; + } + # + # Recurse, once by picking up the first character of $n, and + # once by skipping the first character. + # + my $fc = substr ($n, 0, 1); + my $next = 10 * ($prefix == -1 ? 0 : $prefix) + $fc; + substrings (substr ($n, 1), $m, $next, $max) + + substrings (substr ($n, 1), $m, $prefix, $max); +} + + + +say substrings split while <>; diff --git a/challenge-141/abigail/python/ch-1.py b/challenge-141/abigail/python/ch-1.py new file mode 100644 index 0000000000..cd4c0b5d3b --- /dev/null +++ b/challenge-141/abigail/python/ch-1.py @@ -0,0 +1,30 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py +# + +import math + +count = 10 +nr_of_divisors = 8 + +n = 0 +while count > 0: + n = n + 1 + s = math . floor (math . sqrt (n)) + if n == s * s: + continue + c = 0 + for d in range (1, s + 1): + if n % d == 0: + c = c + 2 + if c > nr_of_divisors: + break + if c == nr_of_divisors: + print (n) + count = count - 1 diff --git a/challenge-141/abigail/python/ch-2.py b/challenge-141/abigail/python/ch-2.py new file mode 100644 i