diff options
| -rw-r--r-- | challenge-133/abigail/README.md | 18 | ||||
| -rw-r--r-- | challenge-133/abigail/awk/ch-1.awk | 11 | ||||
| -rw-r--r-- | challenge-133/abigail/bash/ch-1.sh | 24 | ||||
| -rw-r--r-- | challenge-133/abigail/bc/ch-1.bc | 18 | ||||
| -rw-r--r-- | challenge-133/abigail/c/ch-1.c | 22 | ||||
| -rw-r--r-- | challenge-133/abigail/go/ch-1.go | 25 | ||||
| -rw-r--r-- | challenge-133/abigail/java/ch-1.java | 23 | ||||
| -rw-r--r-- | challenge-133/abigail/lua/ch-1.lua | 13 | ||||
| -rw-r--r-- | challenge-133/abigail/node/ch-1.js | 15 | ||||
| -rw-r--r-- | challenge-133/abigail/pascal/ch-1.p | 20 | ||||
| -rw-r--r-- | challenge-133/abigail/perl/ch-1a.pl | 36 | ||||
| -rw-r--r-- | challenge-133/abigail/perl/ch-2.pl | 4 | ||||
| -rw-r--r-- | challenge-133/abigail/python/ch-1.py | 15 | ||||
| -rw-r--r-- | challenge-133/abigail/ruby/ch-1.rb | 14 | ||||
| -rw-r--r-- | challenge-133/abigail/scheme/ch-1.scm | 22 | ||||
| -rw-r--r-- | challenge-133/abigail/t/ctest.ini | 19 | ||||
| -rw-r--r-- | challenge-133/abigail/t/input-1-1 | 4 | ||||
| -rw-r--r-- | challenge-133/abigail/t/input-2-1 | 0 | ||||
| -rw-r--r-- | challenge-133/abigail/t/output-1-1.exp | 4 | ||||
| -rw-r--r-- | challenge-133/abigail/t/output-2-1.exp | 10 | ||||
| -rw-r--r-- | challenge-133/abigail/tcl/ch-1.tcl | 11 |
21 files changed, 328 insertions, 0 deletions
diff --git a/challenge-133/abigail/README.md b/challenge-133/abigail/README.md index d51d3d73e2..48c489dd8e 100644 --- a/challenge-133/abigail/README.md +++ b/challenge-133/abigail/README.md @@ -2,8 +2,26 @@ ## Part 1 +### "No buildin function at all" + * [Perl](perl/ch-1.pl) +### "No buildin sqrt function" + +* [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) +* [Lua][lua/ch-1.lua) +* [Node.js][node/ch-1.js) +* [Pascal][pascal/ch-1.p) +* [Perl][perl/ch-1a.pl) +* [Python][python/ch-1.py) +* [Ruby][ruby/ch-1.rb) +* [Scheme][scheme/ch-1.scm) +* [Tcl][tcl/ch-1.tcl) + ## Part 2 * [C](c/ch-2.c) diff --git a/challenge-133/abigail/awk/ch-1.awk b/challenge-133/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..825c4ba485 --- /dev/null +++ b/challenge-133/abigail/awk/ch-1.awk @@ -0,0 +1,11 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +{print int (exp (log ($1) / 2))} diff --git a/challenge-133/abigail/bash/ch-1.sh b/challenge-133/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..19b65855f8 --- /dev/null +++ b/challenge-133/abigail/bash/ch-1.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +while read num +do min=1 + max=$num + while ((min + 1 < max)) + do mid=$(((min + max) / 2)) + if ((mid * mid < num)) + then ((min = mid)) + else ((max = mid)) + fi + done + echo $min +done diff --git a/challenge-133/abigail/bc/ch-1.bc b/challenge-133/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..22bc6047db --- /dev/null +++ b/challenge-133/abigail/bc/ch-1.bc @@ -0,0 +1,18 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-1.bc < input-file +# + + +while (1) { + n = read () + if (n == 0) {break} + p = l (n) / 2 + o = scale + scale = 0 + e (p) + scale = o +} diff --git a/challenge-133/abigail/c/ch-1.c b/challenge-133/abigail/c/ch-1.c new file mode 100644 index 0000000000..b750302613 --- /dev/null +++ b/challenge-133/abigail/c/ch-1.c @@ -0,0 +1,22 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> +# include <math.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +int main (void) { + int n; + + while (scanf ("%d", &n) == 1) { + printf ("%.0f\n", floor (exp (log ((double) n) / 2))); + } + + return (0); +} diff --git a/challenge-133/abigail/go/ch-1.go b/challenge-133/abigail/go/ch-1.go new file mode 100644 index 0000000000..6ddd49fbfa --- /dev/null +++ b/challenge-133/abigail/go/ch-1.go @@ -0,0 +1,25 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import ( + "fmt" + "math" +) + +func main () { + for { + var num int + n, err := fmt . Scanf ("%d", &num) + if n != 1 || err != nil { + break + } + fmt . Printf ("%.0f\n", math . Exp (math . Log (float64 (num)) / 2)) + } +} diff --git a/challenge-133/abigail/java/ch-1.java b/challenge-133/abigail/java/ch-1.java new file mode 100644 index 0000000000..82efa23cf9 --- /dev/null +++ b/challenge-133/abigail/java/ch-1.java @@ -0,0 +1,23 @@ +// +// 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) { + Scanner scanner = new Scanner (System . in); + while (scanner . hasNextInt ()) { + System . out . printf ("%.0f\n", + Math . exp ( + Math . log (scanner . nextInt ()) / 2 + ) + ); + } + } +} diff --git a/challenge-133/abigail/lua/ch-1.lua b/challenge-133/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..f846c84e91 --- /dev/null +++ b/challenge-133/abigail/lua/ch-1.lua @@ -0,0 +1,13 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +for num in io . lines () do + print (math . floor (math . exp (math . log (tonumber (num)) / 2))) +end diff --git a/challenge-133/abigail/node/ch-1.js b/challenge-133/abigail/node/ch-1.js new file mode 100644 index 0000000000..401e7ca4f4 --- /dev/null +++ b/challenge-133/abigail/node/ch-1.js @@ -0,0 +1,15 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', num => { + console . log (Math . floor (Math . exp (Math . log (+num) / 2))) +}) diff --git a/challenge-133/abigail/pascal/ch-1.p b/challenge-133/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..4d02d8f0ae --- /dev/null +++ b/challenge-133/abigail/pascal/ch-1.p @@ -0,0 +1,20 @@ +Program XXX; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out < input-file *) +(* *) + +uses math; + +var n: integer; + +begin + while not eof do begin + readln (n); + writeln (Exp (lnxp1 (n - 1) / 2) : 1 : 0); + end +end. diff --git a/challenge-133/abigail/perl/ch-1a.pl b/challenge-133/abigail/perl/ch-1a.pl new file mode 100644 index 0000000000..dfe877ae0f --- /dev/null +++ b/challenge-133/abigail/perl/ch-1a.pl @@ -0,0 +1,36 @@ +#!/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-1a.pl < input-file +# + +# +# Unlike ch-1.pl, this is a solution which only avoids sqrt (and int for +# that matter) +# +# Given that: +# +# * sqrt (a) == a^(1/2) +# * a^b == exp (log (a^b)), a > 0 +# * log (a^b) == b * log (a), a > 0 +# +# We can write sqrt (N) as exp (log (N) / 2) +# +# We avoid using int() by just stripping the decimal point and +# anything following it using a regular expression. +# + +say (exp (log ($_) / 2) =~ s/\..*//r) while <>; diff --git a/challenge-133/abigail/perl/ch-2.pl b/challenge-133/abigail/perl/ch-2.pl index 506e4ca1e6..28e85343f5 100644 --- a/challenge-133/abigail/perl/ch-2.pl +++ b/challenge-133/abigail/perl/ch-2.pl @@ -10,6 +10,10 @@ use experimental 'signatures'; use experimental 'lexical_subs'; # +# Run as: perl ch-2.pl < input-file +# + +# # We've done factorization in previous challenges, so now I'll just # use a module to give me the prime factors of a number. # diff --git a/challenge-133/abigail/python/ch-1.py b/challenge-133/abigail/python/ch-1.py new file mode 100644 index 0000000000..9fc7b1c3fd --- /dev/null +++ b/challenge-133/abigail/python/ch-1.py @@ -0,0 +1,15 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput +import math + +for num in fileinput . input (): + print (math . floor (math . exp (math . log (int (num)) / 2))) diff --git a/challenge-133/abigail/ruby/ch-1.rb b/challenge-133/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..efd3e4b8a0 --- /dev/null +++ b/challenge-133/abigail/ruby/ch-1.rb @@ -0,0 +1,14 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do + |num| + puts ((Math . exp(Math . log(num . to_i) / 2)) . to_i) +end diff --git a/challenge-133/abigail/scheme/ch-1.scm b/challenge-133/abigail/scheme/ch-1.scm new file mode 100644 index 0000000000..d934c11a0e --- /dev/null +++ b/challenge-133/abigail/scheme/ch-1.scm @@ -0,0 +1,22 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-1.scm < input-file +;;; + + +(use-modules (ice-9 format)) + +(define (main) + (define num (read)) + (if (not (eof-object? num)) + (begin + (format #t "~,d~%" (inexact->exact (floor (exp (/ (log num) 2))))) + (main) + ) + ) +) + +(main) diff --git a/challenge-133/abigail/t/ctest.ini b/challenge-133/abigail/t/ctest.ini new file mode 100644 index 0000000000..3e0bbdbb23 --- /dev/null +++ b/challenge-133/abigail/t/ctest.ini @@ -0,0 +1,19 @@ +#
+# Configuration file for running tests, using ctest.
+# See https://github.com/Abigail/Misc/blob/master/ctest
+#
+
+[names]
+1-1 = Given Examples
+2-1 = Fixed output
+
+[1-1/perl]
+skip = Does not write to stdout
+
+[1-1/bc]
+add_to_input = 0
+exe_args = -l %RUN_FILE
+
+[2-1]
+no_input = 1
+
diff --git a/challenge-133/abigail/t/input-1-1 b/challenge-133/abigail/t/input-1-1 new file mode 100644 index 0000000000..58649d3b17 --- /dev/null +++ b/challenge-133/abigail/t/input-1-1 @@ -0,0 +1,4 @@ +10 +27 +85 +101 diff --git a/challenge-133/abigail/t/input-2-1 b/challenge-133/abigail/t/input-2-1 new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/challenge-133/abigail/t/input-2-1 diff --git a/challenge-133/abigail/t/output-1-1.exp b/challenge-133/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..17d29e8c97 --- /dev/null +++ b/challenge-133/abigail/t/output-1-1.exp @@ -0,0 +1,4 @@ +3 +5 +9 +10 diff --git a/challenge-133/abigail/t/output-2-1.exp b/challenge-133/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..59bee5f2c3 --- /dev/null +++ b/challenge-133/abigail/t/output-2-1.exp @@ -0,0 +1,10 @@ +4 +22 +27 +58 +85 +94 +121 +166 +202 +265 diff --git a/challenge-133/abigail/tcl/ch-1.tcl b/challenge-133/abigail/tcl/ch-1.tcl new file mode 100644 index 0000000000..2f06c4e8b6 --- /dev/null +++ b/challenge-133/abigail/tcl/ch-1.tcl @@ -0,0 +1,11 @@ +# +# See ../README.md +# + +# +# Run as: tclsh ch-1.tcl < input-file +# + +while {[gets stdin num] >= 0} { + puts [expr int (exp (log ($num) / 2))] +} |
