diff options
Diffstat (limited to 'challenge-123')
| -rw-r--r-- | challenge-123/abigail/README.md | 6 | ||||
| -rw-r--r-- | challenge-123/abigail/bc/ch-1.bc | 35 | ||||
| -rw-r--r-- | challenge-123/abigail/go/ch-1.go | 47 | ||||
| -rw-r--r-- | challenge-123/abigail/java/ch-1.java | 46 | ||||
| -rw-r--r-- | challenge-123/abigail/pascal/ch-1.p | 48 | ||||
| -rw-r--r-- | challenge-123/abigail/scheme/ch-1.scm | 43 | ||||
| -rw-r--r-- | challenge-123/abigail/tcl/ch-1.tcl | 37 |
7 files changed, 262 insertions, 0 deletions
diff --git a/challenge-123/abigail/README.md b/challenge-123/abigail/README.md index e71bda7e0f..465ad169d4 100644 --- a/challenge-123/abigail/README.md +++ b/challenge-123/abigail/README.md @@ -21,13 +21,19 @@ Output: 12 ### Solutions * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) +* [bc](bc/ch-1.bc) * [C](c/ch-1.c) +* [Go](go/ch-1.go) +* [Java](java/ch-1.java) * [Lua](lua/ch-1.lua) * [Node.js](node/ch-1.js) +* [Pascal](pascal/ch-1.p) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) * [R](r/ch-1.r) * [Ruby](ruby/ch-1.rb) +* [Scheme](scheme/ch-1.scm) +* [Tcl](tcl/ch-1.tcl) ### Blog [Perl Weekly Challenge 123: Ugly Numbers][blog1] diff --git a/challenge-123/abigail/bc/ch-1.bc b/challenge-123/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..17ac31515a --- /dev/null +++ b/challenge-123/abigail/bc/ch-1.bc @@ -0,0 +1,35 @@ +#!/usr/bin/bc + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-123 +# + +# +# Run as: bc ch-1.bc < input-file +# + +while (1) { + n = read () + if (n == 0) { + break + } + ugly [0] = 1 + next_2 = 0 + next_3 = 0 + next_5 = 0 + index = 1 + while (index < n) { + u2 = 2 * ugly [next_2] + u3 = 3 * ugly [next_3] + u5 = 5 * ugly [next_5] + if ((u2 <= u3) && (u2 <= u5)) {min = u2} + if ((u3 <= u2) && (u3 <= u5)) {min = u3} + if ((u5 <= u2) && (u5 <= u3)) {min = u5} + ugly [index] = min + if (2 * ugly [next_2] <= ugly [index]) {next_2 = next_2 + 1} + if (3 * ugly [next_3] <= ugly [index]) {next_3 = next_3 + 1} + if (5 * ugly [next_5] <= ugly [index]) {next_5 = next_5 + 1} + index = index + 1 + } + ugly [index - 1] +} diff --git a/challenge-123/abigail/go/ch-1.go b/challenge-123/abigail/go/ch-1.go new file mode 100644 index 0000000000..01e14fdef8 --- /dev/null +++ b/challenge-123/abigail/go/ch-1.go @@ -0,0 +1,47 @@ +package main + +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-123 +// + +// +// Run as: go run ch-1.go < input-file +// + +import ( + "fmt" +) + +func main () { + var max int + for { + n, err := fmt . Scanf ("%d", &max) + if (n != 1 || err != nil) { + break + } + ugly := make ([] int, max) + ugly [0] = 1 + count := 0 + next_2 := 0 + next_3 := 0 + next_5 := 0 + min := 0 + + for count < max - 1 { + count ++ + + c2 := 2 * ugly [next_2] + c3 := 3 * ugly [next_3] + c5 := 5 * ugly [next_5] + if c2 <= c3 && c2 <= c5 {min = c2} + if c3 <= c2 && c3 <= c5 {min = c3} + if c5 <= c2 && c5 <= c3 {min = c5} + ugly [count] = min + + if (c2 <= ugly [count]) {next_2 ++} + if (c3 <= ugly [count]) {next_3 ++} + if (c5 <= ugly [count]) {next_5 ++} + } + fmt . Println (ugly [count]) + } +} diff --git a/challenge-123/abigail/java/ch-1.java b/challenge-123/abigail/java/ch-1.java new file mode 100644 index 0000000000..51fc0881bb --- /dev/null +++ b/challenge-123/abigail/java/ch-1.java @@ -0,0 +1,46 @@ +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-123 +// + +// +// 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) { + Scanner scanner = new Scanner (System . in); + while (scanner . hasNextInt ()) { + int max = scanner . nextInt (); + long [] ugly = new long [max]; + + ugly [0] = 1L; + int count = 0; + int next_2 = 0; + int next_3 = 0; + int next_5 = 0; + + while (count < max - 1) { + count ++; + long min = 0; + + long c2 = 2 * ugly [next_2]; + long c3 = 3 * ugly [next_3]; + long c5 = 5 * ugly [next_5]; + + if (c2 <= c3 && c2 <= c5) {min = c2;} + if (c3 <= c2 && c3 <= c5) {min = c3;} + if (c5 <= c2 && c5 <= c3) {min = c5;} + + ugly [count] = min; + + if (c2 <= ugly [count]) {next_2 ++;} + if (c3 <= ugly [count]) {next_3 ++;} + if (c5 <= ugly [count]) {next_5 ++;} + } + + System . out . println (ugly [count]); + } + } +} diff --git a/challenge-123/abigail/pascal/ch-1.p b/challenge-123/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..f4f7b2515f --- /dev/null +++ b/challenge-123/abigail/pascal/ch-1.p @@ -0,0 +1,48 @@ +Program ch1; + +(* *) +(* See https://theweeklychallenge.org/blog/perl-weekly-challenge-123 *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out < input-file *) +(* *) + +var + max, count, next_2, next_3, next_5: integer; + min, c2, c3, c5: qword; + ugly: array of qword; + +begin + while not eof do begin + readln (max); + setlength (ugly, max); + + ugly [0] := 1; + count := 0; + next_2 := 0; + next_3 := 0; + next_5 := 0; + min := 0; + + while count < max - 1 do begin + inc (count); + + c2 := 2 * ugly [next_2]; + c3 := 3 * ugly [next_3]; + c5 := 5 * ugly [next_5]; + + if (c2 <= c3) and (c2 <= c5) then begin min := c2; end; + if (c3 <= c2) and (c3 <= c5) then begin min := c3; end; + if (c5 <= c2) and (c5 <= c3) then begin min := c5; end; + + ugly [count] := min; + + if c2 <= ugly [count] then begin inc (next_2); end; + if c3 <= ugly [count] then begin inc (next_3); end; + if c5 <= ugly [count] then begin inc (next_5); end; + end; + + writeln (ugly [count]); + end +end. diff --git a/challenge-123/abigail/scheme/ch-1.scm b/challenge-123/abigail/scheme/ch-1.scm new file mode 100644 index 0000000000..2311efef91 --- /dev/null +++ b/challenge-123/abigail/scheme/ch-1.scm @@ -0,0 +1,43 @@ +#!/usr/local/bin/guile +!# + +;;; +;;; See https://theweeklychallenge.org/blog/perl-weekly-challenge-123 +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-1.scm +;;; + + +(use-modules (ice-9 rdelim)) + + +(define (ugly uglylist mx next_2 next_3 next_5) + (define mymin) + (cond ((= mx (length uglylist)) (list-ref uglylist (- mx 1))) + (else + (set! mymin (min (* 2 (list-ref uglylist next_2)) + (* 3 (list-ref uglylist next_3)) + (* 5 (list-ref uglylist next_5)))) + + (ugly (append uglylist (list mymin) ) + mx + (if (<= (* 2 (list-ref uglylist next_2)) mymin) + (+ next_2 1) next_2) + (if (<= (* 3 (list-ref uglylist next_3)) mymin) + (+ next_3 1) next_3) + (if (<= (* 5 (list-ref uglylist next_5)) mymin) + (+ next_5 1) next_5))))) + +(define (main) + (define mx (read-line)) + (if (not (eof-object? mx)) + (begin + (display (ugly (list 1) (string->number mx) 0 0 0))(newline) + (main) + ) + ) +) + +(main) diff --git a/challenge-123/abigail/tcl/ch-1.tcl b/challenge-123/abigail/tcl/ch-1.tcl new file mode 100644 index 0000000000..4d9358ea5c --- /dev/null +++ b/challenge-123/abigail/tcl/ch-1.tcl @@ -0,0 +1,37 @@ +#!/usr/local/opt/tcl-tk/bin/tclsh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-123 +# + +# +# Run as: tclsh ch-1.tcl < input-file +# + +while {[gets stdin max] >= 0} { + set ugly(0) 1 + set count 0 + set next_2 0 + set next_3 0 + set next_5 0 + + while {$count < $max - 1} { + incr count + + set c2 [expr 2 * $ugly($next_2)] + set c3 [expr 3 * $ugly($next_3)] + set c5 [expr 5 * $ugly($next_5)] + + if {$c2 <= $c3 && $c2 <= $c5} {set min $c2} + if {$c3 <= $c2 && $c3 <= $c5} {set min $c3} + if {$c5 <= $c2 && $c5 <= $c3} {set min $c5} + + set ugly($count) $min + + if {$c2 <= $ugly($count)} {incr next_2} + if {$c3 <= $ugly($count)} {incr next_3} + if {$c5 <= $ugly($count)} {incr next_5} + } + + puts $ugly($count) +} |
