aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Shitov <andy@shitov.ru>2020-12-04 09:17:37 +0100
committerAndrew Shitov <andy@shitov.ru>2020-12-04 09:17:37 +0100
commitfdd800775ca52e7600d78f38e5345a5197cc086c (patch)
tree83e6da7d9535c716d29a57bab98f2a91cdcf4994
parentd19b0f983bbefca06f6139624711c079ac18eb6e (diff)
downloadperlweeklychallenge-club-fdd800775ca52e7600d78f38e5345a5197cc086c.tar.gz
perlweeklychallenge-club-fdd800775ca52e7600d78f38e5345a5197cc086c.tar.bz2
perlweeklychallenge-club-fdd800775ca52e7600d78f38e5345a5197cc086c.zip
Week 89 Issue 1
-rw-r--r--challenge-089/ash/bash/ch-1.bash37
-rw-r--r--challenge-089/ash/c/ch-1.c37
-rw-r--r--challenge-089/ash/cpp/ch-1.cpp29
-rw-r--r--challenge-089/ash/csharp/ch-1.cs26
-rw-r--r--challenge-089/ash/d/ch1.d20
-rw-r--r--challenge-089/ash/dart/ch-1.dart18
-rw-r--r--challenge-089/ash/fortran/ch-1.f9542
-rw-r--r--challenge-089/ash/go/ch-1.go35
-rw-r--r--challenge-089/ash/java/ch-1.java29
-rw-r--r--challenge-089/ash/javascript/ch-1.js24
-rw-r--r--challenge-089/ash/julia/ch-1.julia15
-rw-r--r--challenge-089/ash/kotlin/ch-1.kt34
-rw-r--r--challenge-089/ash/lisp/ch-1.lisp18
-rw-r--r--challenge-089/ash/lua/ch-1.lua24
-rw-r--r--challenge-089/ash/pascal/ch-1.pas40
-rw-r--r--challenge-089/ash/perl/ch-1.pl19
-rw-r--r--challenge-089/ash/php/ch-1.php27
-rw-r--r--challenge-089/ash/python/ch-1.py14
-rw-r--r--challenge-089/ash/raku/ch-1.raku9
-rw-r--r--challenge-089/ash/readme.md31
-rw-r--r--challenge-089/ash/ruby/ch-1.rb14
-rw-r--r--challenge-089/ash/rust/ch-1.rs40
-rw-r--r--challenge-089/ash/scala/ch-1.scala22
23 files changed, 604 insertions, 0 deletions
diff --git a/challenge-089/ash/bash/ch-1.bash b/challenge-089/ash/bash/ch-1.bash
new file mode 100644
index 0000000000..cb3cd93dbb
--- /dev/null
+++ b/challenge-089/ash/bash/ch-1.bash
@@ -0,0 +1,37 @@
+#!/usr/bin/bash
+
+# Usage:
+# $ bash ch-1.bash 100
+# 13015
+#
+# N. B. Try with smaller arguments, as computations
+# for the input number of 100 can take a while (~ a minute).
+
+gcd() {
+ a=$1;
+ b=$2;
+ while [ $b -ne 0 ]; do
+ t=$b
+ b=`expr $a % $b`
+ a=$t
+ done
+
+ gcd=$a
+}
+
+n=${1:-3} # default is 3
+
+s=0
+x=1
+while [ $x -le $n ]; do
+ next=`expr $x + 1`;
+ y=$next;
+ while [ $y -le $n ]; do
+ y=`expr $y + 1`;
+ gcd $x $y;
+ s=`expr $s + $gcd`;
+ done
+ x=$next;
+done
+
+echo $s
diff --git a/challenge-089/ash/c/ch-1.c b/challenge-089/ash/c/ch-1.c
new file mode 100644
index 0000000000..efabf62f7f
--- /dev/null
+++ b/challenge-089/ash/c/ch-1.c
@@ -0,0 +1,37 @@
+/*
+ Compile:
+ $ gcc ch-1.c
+
+ Test run:
+ $ ./a.out 100
+ 13015
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int gcd(int a, int b) {
+ while (b) {
+ int t = b;
+ b = a % b;
+ a = t;
+ }
+
+ return a;
+}
+
+int main(int argc, char** argv) {
+ int n = 3;
+ if (argc != 1) {
+ n = atoi(argv[1]);
+ }
+
+ int s = 0;
+ for (int x = 1; x <= n; x++) {
+ for (int y = x + 1; y <= n; y++) {
+ s += gcd(x, y);
+ }
+ }
+
+ printf("%i\n", s);
+}
diff --git a/challenge-089/ash/cpp/ch-1.cpp b/challenge-089/ash/cpp/ch-1.cpp
new file mode 100644
index 0000000000..b2500a4630
--- /dev/null
+++ b/challenge-089/ash/cpp/ch-1.cpp
@@ -0,0 +1,29 @@
+// Compile as:
+// $ g++ -std=c++17 ch-1.cpp
+
+// Test run:
+// $ ./a.out 100
+// 13015
+
+#include <iostream>
+#include <numeric>
+#include <sstream>
+
+using namespace std;
+
+int main(int argc, char** argv) {
+ int n = 3;
+ if (argc != 1) {
+ stringstream input(argv[1]);
+ input >> n;
+ }
+
+ int s = 0;
+ for (int x = 1; x <= n; x++) {
+ for (int y = x + 1; y <= n; y++) {
+ s += gcd(x, y);
+ }
+ }
+
+ cout << s << endl;
+}
diff --git a/challenge-089/ash/csharp/ch-1.cs b/challenge-089/ash/csharp/ch-1.cs
new file mode 100644
index 0000000000..2b2cc9a160
--- /dev/null
+++ b/challenge-089/ash/csharp/ch-1.cs
@@ -0,0 +1,26 @@
+// Compile and run on a Mac:
+// $ csc -r:System.Numerics.dll ch-1.cs
+// $ mono ch-1.exe 100
+// 13015
+
+using System;
+using System.Numerics;
+
+class Program {
+ static int Main(string[] args)
+ {
+ int n = 3;
+ if (args.Length == 1) n = int.Parse(args[0]);
+
+ var s = new BigInteger(0);
+ for (int x = 1; x <= n; x++) {
+ for (int y = x + 1; y <= n; y++) {
+ s += BigInteger.GreatestCommonDivisor(x, y);
+ }
+ }
+
+ System.Console.WriteLine(s);
+
+ return 0;
+ }
+}
diff --git a/challenge-089/ash/d/ch1.d b/challenge-089/ash/d/ch1.d
new file mode 100644
index 0000000000..ca05ffd74a
--- /dev/null
+++ b/challenge-089/ash/d/ch1.d
@@ -0,0 +1,20 @@
+// How to run:
+// $ rdmd ch1.d 100
+// 13015
+
+import std.stdio;
+import std.numeric;
+import std.conv;
+
+void main(string[] args) {
+ auto n = args.length == 2 ? to!int(args[1]) : 3;
+
+ auto s = 0;
+ for (auto x = 1; x <= n; x++) {
+ for (auto y = x + 1; y <= n; y++) {
+ s += gcd(x, y);
+ }
+ }
+
+ writeln(s);
+}
diff --git a/challenge-089/ash/dart/ch-1.dart b/challenge-089/ash/dart/ch-1.dart
new file mode 100644
index 0000000000..bce73b312a
--- /dev/null
+++ b/challenge-089/ash/dart/ch-1.dart
@@ -0,0 +1,18 @@
+// To run:
+// $ dart ch-1.dart 100
+// 13015
+
+void main(List<String> args) {
+ var n = 3;
+ if (args.length == 1)
+ n = int.parse(args[0]);
+
+ var s = 0;
+ for (var x = 1; x <= n; x++) {
+ for (var y = x + 1; y <= n; y++) {
+ s += x.gcd(y);
+ }
+ }
+
+ print(s);
+}
diff --git a/challenge-089/ash/fortran/ch-1.f95 b/challenge-089/ash/fortran/ch-1.f95
new file mode 100644
index 0000000000..71f766dbd0
--- /dev/null
+++ b/challenge-089/ash/fortran/ch-1.f95
@@ -0,0 +1,42 @@
+! To compile and run:
+!
+! $ gfortran ch-1.f95
+! $ ./a.out 100
+! 13015
+
+function mygcd(a, b) result(x)
+ integer, intent(in) :: a, b
+ integer :: x, y, t
+
+ x = a
+ y = b
+
+ do while (y /= 0)
+ t = y
+ y = mod(x, y)
+ x = t
+ end do
+end function
+
+program hello
+ integer :: n, x, y, s
+
+ character(len=20) :: arg
+ character(len=20) :: nstr
+
+ if (iargc() == 1) then
+ call getarg(1, nstr)
+ read(nstr,*) n
+ else
+ n = 3
+ end if
+
+ s = 0
+ do x = 1, n
+ do y = x + 1, n
+ s = s + mygcd(x, y)
+ end do
+ end do
+
+ write(*,*) s
+end program
diff --git a/challenge-089/ash/go/ch-1.go b/challenge-089/ash/go/ch-1.go
new file mode 100644
index 0000000000..8d3fb5dfb6
--- /dev/null
+++ b/challenge-089/ash/go/ch-1.go
@@ -0,0 +1,35 @@
+// To compile and run:
+// $ go run ch-1.go 100
+// 13015
+
+package main
+
+import (
+ "fmt"
+ "os"
+ "strconv"
+)
+
+func gcd(a, b int) int {
+ for b != 0 {
+ b, a = a%b, b
+ }
+
+ return a
+}
+
+func main() {
+ n := 3
+ if len(os.Args) == 2 {
+ n, _ = strconv.Atoi(os.Args[1])
+ }
+
+ s := 0
+ for x := 1; x <= n; x++ {
+ for y := x + 1; y <= n; y++ {
+ s += gcd(x, y)
+ }
+ }
+
+ fmt.Println(s)
+}
diff --git a/challenge-089/ash/java/ch-1.java b/challenge-089/ash/java/ch-1.java
new file mode 100644
index 0000000000..9e41cf211f
--- /dev/null
+++ b/challenge-089/ash/java/ch-1.java
@@ -0,0 +1,29 @@
+// To run:
+// $ java ch-1.java 100
+// 13015
+
+class Main {
+ static int gcd(int a, int b) {
+ while (b != 0) {
+ int t = b;
+ b = a % b;
+ a = t;
+ }
+
+ return a;
+ }
+
+ public static void main(String[] args) {
+ int n = args.length == 1 ? Integer.parseInt(args[0]) : 3;
+
+ int s = 0;
+ for (int x = 1; x <= n; x++) {
+ for (int y = x + 1; y <= n; y++) {
+
+ s += gcd(x, y);
+ }
+ }
+
+ System.out.println(s);
+ }
+}
diff --git a/challenge-089/ash/javascript/ch-1.js b/challenge-089/ash/javascript/ch-1.js
new file mode 100644
index 0000000000..c090b44808
--- /dev/null
+++ b/challenge-089/ash/javascript/ch-1.js
@@ -0,0 +1,24 @@
+// Test run:
+// $ node ch-1.js 100
+// 13015
+
+let n = process.argv.length == 3 ? process.argv[2] : 3;
+
+let s = 0;
+for (let x = 1; x <= n; x++) {
+ for (let y = x + 1; y <= n; y++) {
+ s += gcd(x, y);
+ }
+}
+
+console.log(s);
+
+function gcd(a, b) {
+ while (b) {
+ let t = b;
+ b = a % b;
+ a = t;
+ }
+
+ return a;
+}
diff --git a/challenge-089/ash/julia/ch-1.julia b/challenge-089/ash/julia/ch-1.julia
new file mode 100644
index 0000000000..22f30004b4
--- /dev/null
+++ b/challenge-089/ash/julia/ch-1.julia
@@ -0,0 +1,15 @@
+# To run:
+# $ julia ch-1.julia 100
+# 13015
+
+n = length(ARGS) == 1 ? parse(Int, ARGS[1]) : 3
+
+s = 0
+for x in range(1, stop = n)
+ for y in range(x + 1, stop = n)
+ global s
+ s += gcd(x, y)
+ end
+end
+
+println(s)
diff --git a/challenge-089/ash/kotlin/ch-1.kt b/challenge-089/ash/kotlin/ch-1.kt
new file mode 100644
index 0000000000..b3b6607e1f
--- /dev/null
+++ b/challenge-089/ash/kotlin/ch-1.kt
@@ -0,0 +1,34 @@
+// How to run:
+// $ kotlinc ch-1.kt -include-runtime -d ch1.jar
+// $ java -jar ch1.jar 100
+// 13015
+
+fun gcd(a: Int, b: Int): Int {
+ var x = a
+ var y = b
+
+ while (y != 0) {
+ val t = y
+ y = x % y
+ x = t
+ }
+
+ return x
+}
+
+fun main(args: Array<String>) {
+ var n: Int
+ if (args.size == 1)
+ n = args[0].toInt()
+ else
+ n = 3
+
+ var s = 0
+ for (x in 1..n) {
+ for (y in x + 1 .. n) {
+ s += gcd(x, y)
+ }
+ }
+
+ println(s)
+}
diff --git a/challenge-089/ash/lisp/ch-1.lisp b/challenge-089/ash/lisp/ch-1.lisp
new file mode 100644
index 0000000000..4bcdcceb36
--- /dev/null
+++ b/challenge-089/ash/lisp/ch-1.lisp
@@ -0,0 +1,18 @@
+;;; How to run:
+;;; $ sbcl --script ch-1.lisp 100
+;;;
+;;; 13015
+
+(defvar n (parse-integer (nth 1 *posix-argv*)))
+
+(defvar s 0)
+
+(loop for x from 1 to n
+ do (
+ loop for y from (+ 1 x) to n
+ do (setq s (+ s (gcd x y)))
+ )
+)
+
+(print s)
+(terpri)
diff --git a/challenge-089/ash/lua/ch-1.lua b/challenge-089/ash/lua/ch-1.lua
new file mode 100644
index 0000000000..8d9ff3bdda
--- /dev/null
+++ b/challenge-089/ash/lua/ch-1.lua
@@ -0,0 +1,24 @@
+-- How to run:
+-- $ lua ch-1.lua 100
+-- 13015
+
+function gcd(a, b)
+ while b > 0 do
+ t = b
+ b = a % b
+ a = t
+ end
+
+ return a
+end
+
+n = arg[1] and arg[1] or 3
+
+s = 0
+for x = 1, n do
+ for y = x + 1, n do
+ s = s + gcd(x, y)
+ end
+end
+
+print(s)
diff --git a/challenge-089/ash/pascal/ch-1.pas b/challenge-089/ash/pascal/ch-1.pas
new file mode 100644
index 0000000000..aeb64d4b38
--- /dev/null
+++ b/challenge-089/ash/pascal/ch-1.pas
@@ -0,0 +1,40 @@
+(*
+ To compile and run:
+ $ fpc ch-1.pas
+ $ ./ch-1 100
+ 13015
+*)
+
+program Hello(input, output);
+
+uses sysutils;
+
+var
+ n, x, y, s: integer;
+
+function gcd(a, b: integer): integer;
+var
+ t: integer;
+begin
+ while b <> 0 do begin
+ t := b;
+ b := a mod b;
+ a := t;
+ end;
+
+ gcd := a
+end;
+
+begin
+ if paramCount() = 0 then
+ n := 3
+ else
+ n := StrToInt(paramStr(1));
+
+ s := 0;
+ for x := 1 to n do
+ for y := x + 1 to n do
+ s += gcd(x, y);
+
+ writeln(s);
+end.
diff --git a/challenge-089/ash/perl/ch-1.pl b/challenge-089/ash/perl/ch-1.pl
new file mode 100644
index 0000000000..4eb4beac91
--- /dev/null
+++ b/challenge-089/ash/perl/ch-1.pl
@@ -0,0 +1,19 @@
+# To run, install Math::Utils first:
+# $ cpanm Math::Utils
+# $ perl ch-1.pl 100
+# 13015
+
+use v5.12;
+
+use Math::Utils qw(gcd);
+
+my $n = $ARGV[0] // 3;
+
+my $s = 0;
+for my $x (1 .. $n) {
+ for my $y ($x + 1 .. $n) {
+ $s += gcd($x, $y);
+ }
+}
+
+say $s;
diff --git a/challenge-089/ash/php/ch-1.php b/challenge-089/ash/php/ch-1.php
new file mode 100644
index 0000000000..9209076d61
--- /dev/null
+++ b/challenge-089/ash/php/ch-1.php
@@ -0,0 +1,27 @@
+<?php
+ // Run as:
+ // $ php ch-1.php 100
+ // 13015
+
+ function gcd($a, $b) {
+ while ($b) {
+ $t = $b;
+ $b = $a % $b;
+ $a = $t;
+ }
+
+ return $a;
+ }
+
+ $n = $argc == 2 ? $argv[1] : 3;
+
+ $s = 0;
+ for ($x = 1; $x <= $n; $x++) {
+ for ($y = $x + 1; $y <= $n; $y++) {
+ $s += gcd($x, $y);
+ }
+ }
+
+ echo "$s\n";
+
+?>
diff --git a/challenge-089/ash/python/ch-1.py b/challenge-089/ash/python/ch-1.py
new file mode 100644
index 0000000000..b962d9c617
--- /dev/null
+++ b/challenge-089/ash/python/ch-1.py
@@ -0,0 +1,14 @@
+# Test run:
+# $ python3 ch-1.py 100
+# 13015
+
+import sys, math
+
+n = 3 if len(sys.argv) == 1 else int(sys.argv[1])
+
+s = 0
+for x in range(1, n + 1):
+ for y in range(x + 1, n + 1):
+ s += math.gcd(x, y)
+
+print(s)
diff --git a/challenge-089/ash/raku/ch-1.raku b/challenge-089/ash/raku/ch-1.raku
new file mode 100644
index 0000000000..7738ca31ff
--- /dev/null
+++ b/challenge-089/ash/raku/ch-1.raku
@@ -0,0 +1,9 @@
+#!/usr/bin/env raku
+
+# Test run:
+# $ raku ch-1.raku 100
+# 13015
+
+my $n = @*ARGS[0] // 3;
+
+say [+] (1..$n).combinations(2).map: {[gcd] $_};
diff --git a/challenge-089/ash/readme.md b/challenge-089/ash/readme.md
new file mode 100644
index 0000000000..9870595c8c
--- /dev/null
+++ b/challenge-089/ash/readme.md
@@ -0,0 +1,31 @@
+# Week 89, Issue 1
+
+Implemenations of [Problem 1 of Week 89](https://perlweeklychallenge.org/blog/perl-weekly-challenge-089/#TASK1) of The Weekly Challenge in different programming languages.
+
+## Using built-in or library functions for GCD
+
+1. [Raku](raku/ch-1.raku)
+1. [Python](python/ch-1.py)
+1. [C++ (C++17)](cpp/ch-1.cpp)
+1. [Perl](perl/ch-1.pl)
+1. [Ruby](ruby/ch-1.rb)
+1. [Lua](lua/ch-1.lua)
+1. [Scala](scala/ch-1.scala)
+1. [C#](csharp/ch-1.cs)
+1. [Dart](dart.ch-1.dart)
+1. [Julia](julia/ch-1.julia)
+1. [D](d/ch-1.d)
+1. [Lisp (SBCL)](lisp/ch-1.lisp)
+
+## With a custom implementation of GCD
+
+1. [C](c/ch-1.c)
+1. [node.js (JavaScript)](javascript/ch-1.js)
+1. [Java](java/ch-1.java)
+1. [Rust](rust/ch-1.rs)
+1. [Pascal (FreePascal)](pascal/ch-1.pas)
+1. [Go](go/ch-1.go)
+1. [Fortran (Fortran 95)](fortran/ch-1.f95)
+1. [PHP](php/ch-1.php)
+1. [Kotlin](kotlin/ch-1.kt)
+1. [Bash](bash/ch-1.bash)
diff --git a/challenge-089/ash/ruby/ch-1.rb b/challenge-089/ash/ruby/ch-1.rb
new file mode 100644
index 0000000000..19b9d813fc
--- /dev/null
+++ b/challenge-089/ash/ruby/ch-1.rb
@@ -0,0 +1,14 @@
+# Usage:
+# $ ruby ch-1.rb 100
+# 13015
+
+n = ARGV.length == 1 ? ARGV[0].to_i : 3
+
+s = 0
+for x in 1 .. n
+ for y in x + 1 .. n
+ s += x.gcd(y)
+ end
+end
+
+puts s
diff --git a/challenge-089/ash/rust/ch-1.rs b/challenge-089/ash/rust/ch-1.rs
new file mode 100644
index 0000000000..f72d85746a
--- /dev/null
+++ b/challenge-089/ash/rust/ch-1.rs
@@ -0,0 +1,40 @@
+// To compile and run:
+// $ rustc ch-1.rs
+// $ ./ch-1 100
+// 13015
+
+use std::env;
+
+fn gcd(a: u32, b: u32) -> u32 {
+ let mut x = a;
+ let mut y = b;
+
+ while y != 0 {
+ let t = y;
+ y = x % y;
+ x = t;
+ }
+
+ return x;
+}
+
+fn main() {
+ let args: Vec<String> = env::args().collect();
+
+ let n;
+ if args.len() == 2 {
+ n = args[1].parse().unwrap();
+ }
+ else {
+ n = 3;
+ }
+
+ let mut s = 0;
+ for x in 1 .. (n + 1) {
+ for y in (x + 1) .. (n + 1) {
+ s += gcd(x, y);
+ }
+ }
+
+ println!("{}", s);
+}
diff --git a/challenge-089/ash/scala/ch-1.scala b/challenge-089/ash/scala/ch-1.scala
new file mode 100644
index 0000000000..054e544f5f
--- /dev/null
+++ b/challenge-089/ash/scala/ch-1.scala
@@ -0,0 +1,22 @@
+/*
+ To run:
+ $ scala ch-1.scala 100
+ 13015
+*/
+
+import math.BigInt
+
+object Main {
+ def main(args: Array[String]): Unit = {
+ var n: Int = if (args.size == 1) args(0).toInt else 3
+
+ var s: BigInt = 0
+ for (x <- 1 to n) {
+ for (y <- x + 1 to n) {
+ s = s + BigInt(x).gcd(y)
+ }
+ }
+
+ println(s)
+ }
+}