diff options
| author | Myoungjin JEON <jeongoon@gmail.com> | 2020-09-05 19:16:39 +1000 |
|---|---|---|
| committer | Myoungjin JEON <jeongoon@gmail.com> | 2020-09-05 19:16:39 +1000 |
| commit | fcf669522c9be085d0be50e630349df3cf3ec698 (patch) | |
| tree | 8a637d848858d273a03c83ac3757b044cec428ea | |
| parent | 3e828ea5b04b00a78a60cb1914f5bd15ba290078 (diff) | |
| parent | 6afd0d631e5abf75309cac73b24031cc7db27ba7 (diff) | |
| download | perlweeklychallenge-club-fcf669522c9be085d0be50e630349df3cf3ec698.tar.gz perlweeklychallenge-club-fcf669522c9be085d0be50e630349df3cf3ec698.tar.bz2 perlweeklychallenge-club-fcf669522c9be085d0be50e630349df3cf3ec698.zip | |
Merge remote-tracking branch 'upstream/master' into ch-076
36 files changed, 2008 insertions, 1171 deletions
diff --git a/challenge-076/colin-crain/blog.txt b/challenge-076/colin-crain/blog.txt new file mode 100644 index 0000000000..5d98339307 --- /dev/null +++ b/challenge-076/colin-crain/blog.txt @@ -0,0 +1 @@ +https://colincrain.wordpress.com/2020/09/05/wear-a-wig-to-play-the-goldbach-variations/ diff --git a/challenge-076/lubos-kolouch/perl/ch-1.pl b/challenge-076/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..f4f1c54b7c --- /dev/null +++ b/challenge-076/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-1.pl +# +# USAGE: ./ch-1.pl +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-076/ +# +# Task 1 - Prime Sum +# +# AUTHOR: Lubos Kolouch +#=============================================================================== + +use strict; +use warnings; +use Math::Prime::Util qw/is_prime/; + +sub get_nr_primes { + my $what = shift; + + # if the number is prime, we need just 1 number to represent it + + return 1 if is_prime($what); + + # if the number is even, we need 2 primes thanks to Goldbach's conjecture + return 2 if $what % 2 == 1; + + # if the number - 2 is prime, return 2 + return 2 if is_prime($what - 2); + + # if the number -3 is prime, return 2 (3 and the prime) + return 2 if is_prime($what - 3); + + # otherwise return 3 - it is 3 and 2 primes forming $what - 3 thanks to + # Goldbach's conjecture + + return 3 +} + + +use Test::More; + +is(get_nr_primes(9), 2, 'test 9'); +is(get_nr_primes(10), 2, 'test 10'); +is(get_nr_primes(2), 1, 'test 2'); + +done_testing; diff --git a/challenge-076/lubos-kolouch/python/ch-1.py b/challenge-076/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..e3ad8abce6 --- /dev/null +++ b/challenge-076/lubos-kolouch/python/ch-1.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +""" Perl weekly challenge 076 """ +# =============================================================================== +# +# FILE: ch-1.py +# +# USAGE: ./ch-1.py +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-076/ +# +# Task 1 - Prime Sum +# +# AUTHOR: Lubos Kolouch +# =============================================================================== +from sympy import isprime + + +def get_nr_primes(what): + """ Calculate and return the number of primes needed """ + + # if the number is prime, we need just 1 number to represent it + + if isprime(what): + return 1 + + # if the number is even, we need 2 primes thanks to Goldbach's conjecture + if what % 2 == 1: + return 2 + + # if the number - 2 is prime, return 2 + if isprime(what - 2): + return 2 + + # if the number -3 is prime, return 2 (3 and the prime) + if isprime(what - 3): + return 2 + + # otherwise return 3 - it is 3 and 2 primes forming $what - 3 thanks to + # Goldbach's conjecture + return 3 + + +assert get_nr_primes(2) == 1 +assert get_nr_primes(9) == 2 +assert get_nr_primes(10) == 2 +assert get_nr_primes(12) == 3 diff --git a/challenge-076/mohammad-anwar/perl/ch-1.pl b/challenge-076/mohammad-anwar/perl/ch-1.pl index ac66f3fd48..a2152c3528 100755 --- a/challenge-076/mohammad-anwar/perl/ch-1.pl +++ b/challenge-076/mohammad-anwar/perl/ch-1.pl @@ -15,35 +15,34 @@ use Algorithm::Combinatorics qw(combinations); my $SUM = $ARGV[0]; print "USAGE: perl $0 <positive_number>\n" and exit unless defined $SUM; -print prime_sum(find_prime_upto($SUM), $SUM); +_print(prime_sum(find_prime_upto($SUM), $SUM)); # # # METHODS +sub _print { + my ($prime_sum) = @_; + + foreach (@$prime_sum) { + print sprintf("%s\n", join ", ", @$_); + } +} + sub prime_sum { my ($primes, $sum) = @_; - print sprintf("Primes: %s\n", join(", ", @$primes)); my $prime_sum = []; foreach my $i (1 .. $sum) { last if ($i > @$primes); foreach my $comb (combinations($primes, $i)) { my $_sum = 0; $_sum += $_ for @$comb; - if ($_sum == $sum) { - if ((@$prime_sum == 0) || (@$prime_sum > @$comb)) { - $prime_sum = $comb; - } - } - - if (@$prime_sum) { - return sprintf("Prime Sum: %s\n", join ", ", @$prime_sum); - } + push @$prime_sum, $comb if ($_sum == $sum); } } - return "None found.\n"; + return $prime_sum; } sub find_prime_upto { diff --git a/challenge-076/mohammad-anwar/perl/ch-1.t b/challenge-076/mohammad-anwar/perl/ch-1.t index bd749b9f80..d1247b0001 100755 --- a/challenge-076/mohammad-anwar/perl/ch-1.t +++ b/challenge-076/mohammad-anwar/perl/ch-1.t @@ -11,10 +11,18 @@ use strict; use warnings; use Test::More; +use Test::Deep; use Algorithm::Combinatorics qw(combinations); -is(prime_sum(find_prime_upto(9), 9), "2, 7", "testing prime sum = 9"); -is(prime_sum(find_prime_upto(12), 12), "5, 7", "testing prime sum = 12"); +is_deeply(prime_sum(find_prime_upto(6), 6), + [], + "testing prime sum = 6"); +is_deeply(prime_sum(find_prime_upto(9), 9), + [[2, 7]], + "testing prime sum = 9"); +is_deeply(prime_sum(find_prime_upto(12), 12), + [[5, 7], [2, 3, 7]], + "testing prime sum = 12"); done_testing; @@ -31,19 +39,11 @@ sub prime_sum { foreach my $comb (combinations($primes, $i)) { my $_sum = 0; $_sum += $_ for @$comb; - if ($_sum == $sum) { - if ((@$prime_sum == 0) || (@$prime_sum > @$comb)) { - $prime_sum = $comb; - } - } - - if (@$prime_sum) { - return join ", ", @$prime_sum; - } + push @$prime_sum, $comb if ($_sum == $sum); } } - return 0; + return $prime_sum; } sub find_prime_upto { diff --git a/challenge-076/mohammad-anwar/raku/ch-1.raku b/challenge-076/mohammad-anwar/raku/ch-1.raku new file mode 100755 index 0000000000..0e72857504 --- /dev/null +++ b/challenge-076/mohammad-anwar/raku/ch-1.raku @@ -0,0 +1,33 @@ +#!/usr/bin/env raku + +# +# Perl Weekly Challenge - 076 +# +# Task #1: Prime Sum +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-076 +# + +use v6.d; + +sub MAIN(Int $SUM where $SUM > 0) { + prime-sum($SUM).join("\n").say; +} + +sub prime-sum(Int $sum) { + + my @prime = find-prime-upto($sum); + my @prime-sum = Empty; + for 1..$sum -> $i { + for @prime.combinations: $i -> $j { + my $_sum = [+] $j; + @prime-sum.push: $j if $_sum == $sum; + } + } + + return @prime-sum; +} + +sub find-prime-upto(Int $sum) { + return (2..$sum).grep: { .is-prime }; +} diff --git a/challenge-076/mohammad-anwar/raku/ch-1.t b/challenge-076/mohammad-anwar/raku/ch-1.t new file mode 100755 index 0000000000..24bfb82432 --- /dev/null +++ b/challenge-076/mohammad-anwar/raku/ch-1.t @@ -0,0 +1,39 @@ +#!/usr/bin/env raku + +# +# Perl Weekly Challenge - 076 +# +# Task #1: Prime Sum +# +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-076 +# + +use Test; + +is-deeply prime-sum(6).<>, [], "prime sum = 6"; +is-deeply prime-sum(9).<>, [(2, 7),], "prime sum = 9"; +is-deeply prime-sum(12).<>, [(5, 7), (2, 3, 7)], "prime sum = 12"; + +done-testing; + +# +# +# METHODS + +sub prime-sum(Int $sum) { + + my @prime = find-prime-upto($sum); + my @prime-sum = Empty; + for 1..$sum -> $i { + for @prime.combinations: $i -> $j { + my $_sum = [+] $j; + @prime-sum.push: $j if $_sum == $sum; + } + } + + return @prime-sum; +} + +sub find-prime-upto(Int $sum) { + return (2..$sum).grep: { .is-prime }; +} diff --git a/challenge-076/pete-houston/perl/ch-1.pl b/challenge-076/pete-houston/perl/ch-1.pl new file mode 100644 index 0000000000..f7610133d7 --- /dev/null +++ b/challenge-076/pete-houston/perl/ch-1.pl @@ -0,0 +1,71 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 7601.pl +# +# USAGE: ./7601.pl N +# +# DESCRIPTION: Find the sum of fewest primes to equal the target N +# +# REQUIREMENTS: Math::Prime::Util, Lingua::EN::Inflexion, Math::Combinatorics, +# List::Util +# NOTES: N must be an integer greater than 1. +# 1 is not considered prime. +# +# This is as fast as I could make it for fairly large N. +# Completes in < 1 sec for N < 10^6 without requiring +# crazy amounts of RAM. +# +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 03/09/20 +#=============================================================================== + +use strict; +use warnings; + +use Math::Prime::Util 'primes'; +use Lingua::EN::Inflexion; +use List::Util qw/min/; + +my $target = $ARGV[0]; +die "Target must be an integer greater than 1" + unless defined $target && $target == int $target && $target > 1; + +my @primelist = min_prime_sum ($target); + +my $primetot = @primelist; +print inflect ("<#n:$primetot> as sum of $primetot prime <N:numbers> i.e. ") . + join (' and ', @primelist) . " is same as the input number\n" . + join (' + ', @primelist) . " = $target.\n"; + +sub min_prime_sum { + my $target = shift; + my @primes = @{primes ($target)}; + my %uniques = map { $target - $_ => $_ } @primes; + + # Target is prime + return ($target) if $uniques{0}; + + # sum of 2 primes + for my $try (@primes) { + return ($try, $uniques{$try}) if $uniques{$try}; + } + + # sum of 3 primes + require Math::Combinatorics; + my @freqs = map { int min (2, $target / $_) } @primes; + my $combinator = Math::Combinatorics->new ( + count => 2, + data => \@primes, + frequency => \@freqs + ); + while (my @multiset = $combinator->next_multiset) { + my $sum = $multiset[0] + $multiset[1]; + return (@multiset, $uniques{$sum}) if $uniques{$sum}; + } + + # Don't see this happening! + die qq#Goldbach says, "No".\n#; +} diff --git a/challenge-076/pete-houston/perl/ch-2.pl b/challenge-076/pete-houston/perl/ch-2.pl new file mode 100644 index 0000000000..7a543b57c2 --- /dev/null +++ b/challenge-076/pete-houston/perl/ch-2.pl @@ -0,0 +1,118 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 7602.pl +# +# USAGE: ./7602.pl GRIDFILE [ DICTFILE ] +# +# DESCRIPTION: Solve the wordsearch! +# +# OPTIONS: If the DICTFILE argument is missing, defaults to system +# dictionary file +# REQUIREMENTS: Path::Tiny, Array::Transpose, List::Util, List::MoreUtils +# NOTES: Grid must be rectangular. Only Roman letters are considered. +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 01/09/20 +#=============================================================================== + +use strict; +use warnings; + +use Path::Tiny 'path'; +use Array::Transpose; +use List::Util 'first'; +use List::MoreUtils 'uniq'; + +my $minlength = 5; +my $gridstring = read_grid ($ARGV[0]); +my @words = read_dictionary ($ARGV[1], $minlength); + +my @matches = solve ($gridstring, @words); +print "Found: " . scalar @matches . + " words of length $minlength or more:\n@matches\n"; +exit; + +sub read_grid { + my @rows = path(shift)->lines ({chomp => 1}); + s/\s+//g for @rows; + my $c = '~'; # Any non-alpha char you like to serve as separator + # Horizontals + my $grid = join $c, @rows; + # Create an AoA for the other directions + $_ = [ split //, $_ ] for @rows; + # Verticals + $grid .= $c . join '', @$_ for transpose (\@rows); + $grid .= $c; + + # Now we need to do the diagonals. + # This is tortuous - there has to be a much neater way. + + my $rmax = $#rows; + my $cmax = $#{$rows[0]}; + + # \ diag + # lower diag + for my $row (0 .. $rmax) { + my $i = $row; + for my $col (0 .. $cmax) { + last if $i > $rmax; + $grid .= $rows[$i++]->[$col] + } + $grid .= $c; + } + # Upper diag + for my $col (1 .. $cmax) { + my $i = $col; + for my $row (0 .. $rmax) { + last if $i > $cmax; + $grid .= $rows[$row]->[$i++] + } + $grid .= $c; + } + + # / diag + # lower diag + for my $row (0 .. $rmax) { + my $i = $row; + for my $col (0 .. $cmax) { + last if $i > $rmax; + $grid .= $rows[$i++]->[$cmax - $col] + } + $grid .= $c; + } + # Upper diag + for my $col (0 .. $cmax - 1) { + my $i = $col; + for my $row (0 .. $rmax) { + last if $i < 0; + $grid .= $rows[$row]->[$i--] + } + $grid .= $c; + } + + # Uppercase + $grid = uc $grid; + # Append the reverse + $grid .= reverse $grid; + + return $grid; +} + +sub read_dictionary { + my ($dictfile, $min) = @_; + $dictfile = first { defined $_ && -r $_ } + $dictfile, + '/usr/share/dict/words', + '/usr/dict/words'; + return uniq + map { uc } + grep { length($_) >= $min && !/[^a-z]/i } + path ($dictfile)->lines ({chomp => 1}); +} + +sub solve { + my ($grid, @words) = @_; + return grep { index ($grid, $_) > -1 } @words; +} diff --git a/challenge-076/tyler-wardhaugh/clojure/README.md b/challenge-076/tyler-wardhaugh/clojure/README.md index 4bc8af0088..a46a7a9fc9 100644 --- a/challenge-076/tyler-wardhaugh/clojure/README.md +++ b/challenge-076/tyler-wardhaugh/clojure/README.md @@ -1,25 +1,25 @@ -# tw.weekly.c75 +# tw.weekly.c76 -The Weekly Challenge - #075 - Tyler Wardhaugh +The Weekly Challenge - #076 - Tyler Wardhaugh ## Usage Run the project directly (shows default output from both tasks): - $ clojure -m tw.weekly.c75 + $ clojure -m tw.weekly.c76 Run the project's tests (which are samples from the task descriptions): $ clojure -A:test:runner -Run Task #1 with input (SUM COIN [COIN]...) +Run Task #1 with input - $ clojure -m tw.weekly.ch-1 6 1 2 4 + $ clojure -m tw.weekly.ch-1 Run Task #2 with input: - $ clojure -m tw.weekly.ch-2 2 1 4 5 3 7 + $ clojure -m tw.weekly.ch-2 ## Project Template diff --git a/challenge-076/tyler-wardhaugh/clojure/deps.edn b/challenge-076/tyler-wardhaugh/clojure/deps.edn index 6f74027036..fff9b934dc 100644 --- a/challenge-076/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-076/tyler-wardhaugh/clojure/deps.edn @@ -1,15 +1,16 @@ {:paths ["src" "resources"] :deps {org.clojure/clojure {:mvn/version "1.10.1"} - metametadata/multiset {:mvn/version "0.1.1"}} + com.hypirion/primes {:mvn/version "0.2.2"} + org.clojure/math.combinatorics {:mvn/version "0.1.6"} + net.mikera/core.matrix {:mvn/version "0.62.0"}} :aliases {:test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}} - :runner - {:extra-deps {com.cognitect/test-runner - {:git/url "https://github.com/cognitect-labs/test-runner" - :sha "f7ef16dc3b8332b0d77bc0274578ad5270fbfedd"}} - :main-opts ["-m" "cognitect.test-runner" - "-d" "test"]} + :runner {:extra-deps {com.cognitect/test-runner + {:git/url "https://github.com/cognitect-labs/test-runner" + :sha "f7ef16dc3b8332b0d77bc0274578ad5270fbfedd"}} + :main-opts ["-m" "cognitect.test-runner" + "-d" "test"]} :uberjar {:extra-deps {seancorfield/depstar {:mvn/version "1.0.94"}} - :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c75.jar" - "-C" "-m" "tw.weekly.c75"]}}} + :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c76.jar" + "-C" "-m" "tw.weekly.c76"]}}} diff --git a/challenge-076/tyler-wardhaugh/clojure/pom.xml b/challenge-076/tyler-wardhaugh/clojure/pom.xml index 7e0da3dd93..5f42d66326 100644 --- a/challenge-076/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-076/tyler-wardhaugh/clojure/pom.xml @@ -2,10 +2,10 @@ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>tw.weekly</groupId> - <artifactId>tw.weekly.c75</artifactId> + <artifactId>tw.weekly.c76</artifactId> <version>0.1.0-SNAPSHOT</version> - <name>tw.weekly.c75</name> - <description>The Weekly Challenge - #075</description> + <name>tw.weekly.c76</name> + <description>The Weekly Challenge - #076</description> <url>https://github.com/manwar/perlweeklychallenge-club</url> <licenses> <license> @@ -25,9 +25,19 @@ <version>1.10.1</version> </dependency> <dependency> - <groupId>metametadata</groupId> - <artifactId>multiset</artifactId> - <version>0.1.1</version> + <groupId>com.hypirion</groupId> + <artifactId>primes</artifactId> + <version>0.2.2</version> + </dependency> + <dependency> + <groupId>org.clojure</groupId> + <artifactId>math.combinatorics</artifactId> + <version>0.1.6</version> + </dependency> + <dependency> + <groupId>net.mikera</groupId> + <artifactId>core.matrix</artifactId> + <version>0.62.0</version> </dependency> </dependencies> <build> diff --git a/challenge-076/tyler-wardhaugh/clojure/resources/dict.txt b/challenge-076/tyler-wardhaugh/clojure/resources/dict.txt new file mode 100644 index 0000000000..c0d8215b4a --- /dev/null +++ b/challenge-076/tyler-wardhaugh/clojure/resources/dict.txt @@ -0,0 +1,53 @@ +argos +margo +patna +traci +tracie +aimed +align +antes +arose +ashed +blunt +blunts +broad +buries +clove +cloven +constitution +constitutions +croon +depart +departed +enter +filch +garlic +goats +grieve +grieves +hazard +liens +malign +malignant +malls +midst +ought +ovary +parted +pudgiest +quash +quashed +ruses +shrine +shrines +social +socializing +spasm +spasmodic +succor +succors +theorem +theorems +virus +viruses +wigged diff --git a/challenge-076/tyler-wardhaugh/clojure/resources/grid.txt b/challenge-076/tyler-wardhaugh/clojure/resources/grid.txt new file mode 100644 index 0000000000..31cf2e0fd8 --- /dev/null +++ b/challenge-076/tyler-wardhaugh/clojure/resources/grid.txt @@ -0,0 +1,19 @@ +B I D E M I A T S U C C O R S T +L D E G G I W Q H O D E E H D P +U S E I R U B U T E A S L A G U +N G N I Z I L A I C O S C N U D +T G M I D S T S A R A R E I F G +S R E N M D C H A S I V E E L I +S C S H A E U E B R O A D M T E +H W O V L P E D D L A I U L S S +R Y O N L A S F C S T A O G O T +I G U S S R R U G O V A R Y O C +N R G P A T N A N G I L A M O O +E I H A C E I V I R U S E S E D +S E T S U D T T G A R L I C N H +H V R M X L W I U M S N S O T B +A E A O F I L C H T O D C A E U +Z S C D F E C A A I I R L N R F +A R I I A N Y U T O O O U T P F +R S E C I S N A B O S C N E R A +D R S M P C U U N E L T E S I L diff --git a/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/c76.clj b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/c76.clj new file mode 100644 index 0000000000..e155b703fb --- /dev/null +++ b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/c76.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c76 + (:require [tw.weekly.ch-1 :as ch-1]) + (:require [tw.weekly.ch-2 :as ch-2]) + (:gen-class)) + +(defn -main + "Run both tasks." + [& args] + (println "Task #1") + (ch-1/-main) + (println "\n\nTask #2") + (ch-2/-main)) diff --git a/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj new file mode 120000 index 0000000000..924a7a086e --- /dev/null +++ b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj @@ -0,0 +1 @@ +ch_1.clj
\ No newline at end of file diff --git a/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj new file mode 120000 index 0000000000..5a32e17ef9 --- /dev/null +++ b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj @@ -0,0 +1 @@ +ch_2.clj
\ No newline at end of file diff --git a/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj new file mode 100644 index 0000000000..300b89c7ff --- /dev/null +++ b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj @@ -0,0 +1,37 @@ +(ns tw.weekly.ch-1 + (:require [clojure.edn :as edn]) + (:require [clojure.math.combinatorics :as combo]) + (:require [com.hypirion.primes :as p])) + +; My initial attempt, which I've preserved here. +(defn find-min-primes-to-sum-brute-force + "Find the minimum number of prime numbers required, whose summation gives you the argument provided, by brute force." + [n] + (if (p/prime? n) [n] + (as-> n x + (p/take-below x) + (combo/partitions x :min 2 :max 3) + (mapcat identity x) + (filter #(= n (reduce + %)) x) + (sort-by count x) + (first x)))) + +; basis for algorithm: +; https://stackoverflow.com/a/35756072 +; StackOverflow answer by user448810 +(defn find-min-primes-to-sum + "Find the minimum number of prime numbers required, whose summation gives you the argument provided." + [n] + (cond + (p/prime? n) [n] + (even? n) (let [f (juxt #(- n %) identity)] (->> (p/primes) (filter #(p/prime? (- n %))) first f)) + (and (odd? n) (p/prime? (- n 2))) [2 (- n 2)] + :else (conj 3 (find-min-primes-to-sum (- n 3))))) + +(defn -main + "Run Task 1 with a number N, defaulting to the number given in the task example, 9." + [& args] + (let [N (or (some-> args first edn/read-string) 9)] + (let [solution (find-min-primes-to-sum N)] + (printf "%d prime number(s) are the minimum number required to sum to %d.\n" (count solution) N) + (println solution)))) diff --git a/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj new file mode 100644 index 0000000000..cd5109aab1 --- /dev/null +++ b/challenge-076/tyler-wardhaugh/clojure/src/tw/wee |
