From ebbc057eb000e3929024be56ae4e737c78678a39 Mon Sep 17 00:00:00 2001 From: Andrew Shitov Date: Sat, 5 Dec 2020 09:27:39 +0100 Subject: Syntax highlighting --- challenge-089/ash/index.md | 990 ++++++++++++++++++++++++--------------------- 1 file changed, 518 insertions(+), 472 deletions(-) diff --git a/challenge-089/ash/index.md b/challenge-089/ash/index.md index e0862060db..da00e5affb 100644 --- a/challenge-089/ash/index.md +++ b/challenge-089/ash/index.md @@ -53,33 +53,36 @@ All the programs can be run from the command line and accept the value of `$N` a In Raku, we’ve got all the things required for a compact solution. Actually, the most compact among all the solutions below. So, we’ve got the [`combinations` method](https://docs.raku.org/routine/combinations) to convert a range into unique pairs of numbers and the built-in [`gcd` routine](https://docs.raku.org/routine/gcd). We also have the pleasure to use [reduction operators](https://docs.raku.org/language/operators#index-entry-[]_(reduction_metaoperators)) `[ ... ]` even if they can be easily replaced. For example, the first usage of it `[+]` can be replaced with the call of `sum`, and the second `[gcd] $_` with something like `$_[0] gcd $_[1]`. By the way, notice that in Raku, `gcd` is an infix operator, so you use it in the form of `$a gcd $b` and not as a function `gcd($a, $b)`. - # Test run: - # $ raku ch-1.raku 100 - # 13015 +```perl +# Test run: +# $ raku ch-1.raku 100 +# 13015 - my $n = @*ARGS[0] // 3; +my $n = @*ARGS[0] // 3; - say [+] (1..$n).combinations(2).map: {[gcd] $_}; +say [+] (1..$n).combinations(2).map: {[gcd] $_}; +``` ### 2. Python In Python, there’s no built-in `gcd` or `combinations`, but both are available in the standard library. For diversity, I used `math.gcd` but implemented the combination creation with two nested `for` loops. The first loop iterates over the whole range between `1` and `$N`, while the inner loop starts with the current value of the outer loop. - # Test run: - # $ python3 ch-1.py 100 - # 13015 +```python +# Test run: +# $ python3 ch-1.py 100 +# 13015 - import sys, math +import sys, math - n = 3 if len(sys.argv) == 1 else int(sys.argv[1]) +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) +s = 0 +for x in range(1, n + 1): + for y in range(x + 1, n + 1): + s += math.gcd(x, y) +print(s) +``` ### 3. C++ @@ -87,228 +90,247 @@ Here is a solution in C++. As the author of the language says, ‘C++ feels like In the rest, the program also includes two nested loops and accumulates the sum for each combination. - // Compile as: - // $ g++ -std=c++17 ch-1.cpp +```cpp +// Compile as: +// $ g++ -std=c++17 ch-1.cpp - // Test run: - // $ ./a.out 100 - // 13015 +// Test run: +// $ ./a.out 100 +// 13015 - #include - #include - #include +#include +#include +#include - using namespace std; +using namespace std; - int main(int argc, char** argv) { - int n = 3; - if (argc != 1) { - stringstream input(argv[1]); - input >> n; - } +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); - } + 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; } + cout << s << endl; +} +``` + ### 4. Perl Good old modern and future Perl is here! As, among the rest, it is famous for CPAN, let us use a [module `Math::Utils`](https://metacpan.org/pod/Math::Utils) from there. Actually, this is the only solution where I use a module that is not available in the default installation. - # To run, install Math::Utils first: - # $ cpanm Math::Utils - # $ perl ch-1.pl 100 - # 13015 +```perl +# To run, install Math::Utils first: +# $ cpanm Math::Utils +# $ perl ch-1.pl 100 +# 13015 - use v5.20; +use v5.20; - use Math::Utils qw(gcd); +use Math::Utils qw(gcd); - my $n = $ARGV[0] // 3; +my $n = $ARGV[0] // 3; - my $s = 0; - for my $x (1 .. $n) { - for my $y ($x + 1 .. $n) { - $s += gcd($x, $y); - } +my $s = 0; +for my $x (1 .. $n) { + for my $y ($x + 1 .. $n) { + $s += gcd($x, $y); } +} - say $s; +say $s; +``` ### 5. Ruby The program in Ruby is also quite compact, I would say. Notice how the `gcd` method is used here: `x.gcd(y)`. It is indeed is _a method,_ and it is called on one of the numbers. We’ll see more examples of such approach later. - # Usage: - # $ ruby ch-1.rb 100 - # 13015 +```ruby +# Usage: +# $ ruby ch-1.rb 100 +# 13015 - n = ARGV.length == 1 ? ARGV[0].to_i : 3 +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 +s = 0 +for x in 1 .. n + for y in x + 1 .. n + s += x.gcd(y) end +end - puts s +puts s +``` ### 6. Scala In Scala, we have to deal with Java’s legacy and create a class to start the day. But nevertheless, the program is not that big. What you may want to enjoy is possibly the syntax of loop headers: `for (x <- 1 to n)` with the left arrow. Also notice that the `gcd` routine is again a method, and it’s a method defined for `BigInt`s: `BigInt(x).gcd(y)`. - /* - 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) - } - } +```scala +/* + To run: + $ scala ch-1.scala 100 + 13015 +*/ + +import math.BigInt - println(s) +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) } +} +``` ### 7. C# Here is the solution in C#. Welcome to the world of .NET, where many things are clearly spelt out: `BigInteger.GreatestCommonDivisor(x, y)`. In the rest, here we have the same two nested loops that build the pair of combinations. - // Compile and run on a Mac: - // $ csc -r:System.Numerics.dll ch-1.cs - // $ mono ch-1.exe 100 - // 13015 +```csharp +// 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); +using System; +using System.Numerics; - return 0; +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; } +} +``` ### 8. Dart To me, languages built on C++ have their specific charm. On one side, it is the same known language, but on the other, it embeds the things that you were missing earlier (see, for example, how easy it is to deal with command-line arguments). The `gcd` here is again a method called on a number: `x.gcd(y)`. - // To run: - // $ dart ch-1.dart 100 - // 13015 +```rust +// To run: +// $ dart ch-1.dart 100 +// 13015 - void main(List args) { - var n = 3; - if (args.length == 1) - n = int.parse(args[0]); +void main(List 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); - } + var s = 0; + for (var x = 1; x <= n; x++) { + for (var y = x + 1; y <= n; y++) { + s += x.gcd(y); } - - print(s); } + print(s); +} +``` + ### 9. Julia Julia is another interesting language that actually has many similarities with Raku. For our today’s need, there’s a built-in `gcd` function that I am happily using. The only unexpected obstacle is, maybe, the need to explicitly refer to the accumulator `s` as to a `global` variable. - # To run: - # $ julia ch-1.jl 100 - # 13015 +```julia +# To run: +# $ julia ch-1.jl 100 +# 13015 - n = length(ARGS) == 1 ? parse(Int, ARGS[1]) : 3 +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 +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) - +println(s) +``` ### 10. D Acter C comes D. And here we are. In the program, you can see extensive use of the `auto` declarator. I believe, the situation here resembles how some elements of Raku entered back in Perl, even if Raku was a continuation of Perl historically. Similarly, `auto` is now available in C++ too (while you need to practice not to forget to use `auto` automatically). - // 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); - } +```d +// 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); } + writeln(s); +} +``` + ### 11. Lisp -Move on to a different world (for a bit). Here is the solution in a functional programming language. Well, Common Lisp adds some syntax sugar to help us build loops without manually manipulating loop counters and recursive calls. In any case, it also has the built-in `gcd` function, which, of course, you call as `(gcd x y)`. The below program works with the SBCL implementation (mostly because of how it reads the command-line arguments). +Move on to a different world (for a bit). Here is the solution in a functional programming language. Well, Common Lisp adds some syntax sugar to help us build loops without manually manipulating loop counters and recursive calls. (Not to say about `setq` that sets the new value of a variable.) + +In any case, it also has the built-in `gcd` function, which, of course, you call as `(gcd x y)`. The below program works with the SBCL implementation (mostly because of how it reads the command-line arguments). - ;;; How to run: - ;;; $ sbcl --script ch-1.lisp 100 - ;;; - ;;; 13015 +```lisp +;;; How to run: +;;; $ sbcl --script ch-1.lisp 100 +;;; +;;; 13015 - (defvar n (parse-integer (nth 1 *posix-argv*))) +(defvar n (parse-integer (nth 1 *posix-argv*))) - (defvar s 0) +(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))) - ) +(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) +(print s) +(terpri) +``` ## Part 2 @@ -316,437 +338,461 @@ Move on to a different world (for a bit). Here is the solution in a functional p OK, and now we start the second part of the solutions list. Now, the GCD algorithm is implemented directly in the program itself. Refer to this implementation in C; this algorithm is used in all other solutions below. - int gcd(int a, int b) { - while (b) { - int t = b; - b = a % b; - a = t; - } - - return a; +```c +int gcd(int a, int b) { + while (b) { + int t = b; + b = a % b; + a = t; } + return a; +} +``` + It is important to say that due to the loop organisation, we always call this function with such arguments that `b` is always bigger than `a`, so you don’t have to care about other cases. So, here is the complete program in C: - /* - Compile: - $ gcc ch-1.c - - Test run: - $ ./a.out 100 - 13015 - */ +```c +/* + Compile: + $ gcc ch-1.c - #include - #include + Test run: + $ ./a.out 100 + 13015 +*/ - int gcd(int a, int b) { - while (b) { - int t = b; - b = a % b; - a = t; - } +#include +#include - return a; +int gcd(int a, int b) { + while (b) { + int t = b; + b = a % b; + a = t; } - int main(int argc, char** argv) { - int n = 3; - if (argc != 1) { - n = atoi(argv[1]); - } + return a; +} - int s = 0; - for (int x = 1; x <= n; x++) { - for (int y = x + 1; y <= n; y++) { - s += gcd(x, y); - } - } +int main(int argc, char** argv) { + int n = 3; + if (argc != 1) { + n = atoi(argv[1]); + } - printf("%i\n", s); + 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); +} +``` + ### 13. JavaScript A Node.js slash JavaScript program is here for your further entertainment. It resembles a lot the previously shown C program. A similar pair of nested loops, and a similar implementation of `gcd`. Could you imagine 10-15 years ago that you would be able to run a JS program from command line? - // Test run: - // $ node ch-1.js 100 - // 13015 +```javascript +// Test run: +// $ node ch-1.js 100 +// 13015 - let n = process.argv.length == 3 ? process.argv[2] : 3; +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); - } +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; - } +console.log(s); - return a; +function gcd(a, b) { + while (b) { + let t = b; + b = a % b; + a = t; } + return a; +} +``` + ### 14. Java Now, look at the solution in Java. Yes, we have to use classes again, but in the rest, a nice and clean program that doesn’t require many more comments, I think. - // 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; - } +```java +// To run: +// $ java ch-1.java 100 +// 13015 - return a; +class Main { + static int gcd(int a, int b) { + while (b != 0) { + int t = b; + b = a % b; + a = t; } - public static void main(String[] args) { - int n = args.length == 1 ? Integer.parseInt(args[0]) : 3; + return a; + } - int s = 0; - for (int x = 1; x <= n; x++) { - for (int y = x + 1; y <= n; y++) { - s += gcd(x, y); - } - } + public static void main(String[] args) { + int n = args.length == 1 ? Integer.parseInt(args[0]) : 3; - System.out.println(s); + 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); } +} +``` ### 15. Rust The program in Rust is an interesting combination of elements that we are familiar with from both C, JavaScript, and even Perl and Raku (for example, ranges `..`). Also, a nice addition of built-in type system, and maybe an unusual concept of macros — those ‘functions’ with an exclamation mark in their name such as `println!`. - // To compile and run: - // $ rustc ch-1.rs - // $ ./ch-1 100 - // 13015 - - use std::env; +```rust +// To compile and run: +// $ rustc ch-1.rs +// $ ./ch-1 100 +// 13015 - fn gcd(a: u32, b: u32) -> u32 { - let mut x = a; - let mut y = b; +use std::env; - while y != 0 { - let t = y; - y = x % y; - x = t; - } +fn gcd(a: u32, b: u32) -> u32 { + let mut x = a; + let mut y = b; - return x; + while y != 0 { + let t = y; + y = x % y; + x = t; } - fn main() { - let args: Vec = env::args().collect(); + return x; +} - let n; - if args.len() == 2 { - n = args[1].parse().unwrap(); - } - else { - n = 3; - } +fn main() { + let args: Vec = env::args().collect(); - let mut s = 0; - for x in 1 .. (n + 1) { - for y in (x + 1) .. (n + 1) { - s += gcd(x, y); - } - } + let n; + if args.len() == 2 { + n = args[1].parse().unwrap(); + } + else { + n = 3; + } - println!("{}", s); + let mut s = 0; + for x in 1 .. (n + 1) { + for y in (x + 1) .. (n + 1) { + s += gcd(x, y); + } } + println!("{}", s); +} +``` + ### 16. Pascal I have a question for you. When did you program in Pascal last time? My answer is ‘Something like 30 years ago’. But honestly, that nostalgic mood is a great thing for the evenings around Christmas time. And while there’s no `gcd` built in, you can implement it yourself if you remember to use `<>` instead of `!=` and a single `=` in comparisons: `if paramCount() = 0 then`. I also struggled a bit with semicolons, for example, no semicolon allowed before `else`. - (* - 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 +```pascal +(* + 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; - 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); + 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. + writeln(s); +end. +``` ### 17. Go And now fast forward to the present days. Here is Go with its strict requirements not to leave garbage in the code and use all variables and imported modules. Notice that I used multiple assignments and avoided a temporary variable in the implementation of `gcd`: `b, a = a%b, b`. Heh, and the Go standard formatter removed my spaces around the `%` operator. - // To compile and run: - // $ go run ch-1.go 100 - // 13015 +```go +// To compile and run: +// $ go run ch-1.go 100 +// 13015 - package main +package main - import ( - "fmt" - "os" - "strconv" - ) - - func gcd(a, b int) int { - for b != 0 { - b, a = a%b, b - } +import ( + "fmt" + "os" + "strconv" +) - return a +func gcd(a, b int) int { + for b != 0 { + b, a = a%b, b } - func main() { - n := 3 - if len(os.Args) == 2 { - n, _ = strconv.Atoi(os.Args[1]) - } + return a +} - s := 0 - for x := 1; x <= n; x++ { - for y := x + 1; y <= n; y++ { - s += gcd(x, y) - } - } +func main() { + n := 3 + if len(os.Args) == 2 { + n, _ = strconv.Atoi(os.Args[1]) + } - fmt.Println(s) + s := 0 + for x := 1; x <= n; x++ { + for y := x + 1; y <= n; y++ { + s += gcd(x, y) + } } + fmt.Println(s) +} +``` + ### 18. Lua Lua is the language that is loved by many people, and I believe there are many cases when its design with tables can be beneficial in many applications. For our practice, I used the standard approach: a few loops, and it’s done. I would still point your attention to the following line: `n = arg[1] and arg[1] or 3`, which is actually a ternary construction to assign the default value if the argument is missing. - -- 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 +```lua +-- How to run: +-- $ lua ch-1.lua 100 +-- 13015 - return a +function gcd(a, b) + while b > 0 do + t = b + b = a % b + a = t end - n = arg[1] and arg[1] or 3 + return a +end - s = 0 - for x = 1, n do - for y = x + 1, n do - s = s + gcd(x, y) - 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) +print(s) +``` ### 19. Fortran Going further to some science-related language. For a long time, Fortran was the language in which tons of algorithms were encoded for physics and mathematics. I believe it would be very beneficial to use this as the language to teach students (OK, consider that a joke with some elements of truth) :-) What would be really interesting is to browse into some sophisticated algorithms written decades ago — many of them may be unexpectedly efficient. - ! 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 +```fortran +! 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 + write(*,*) s +end program +``` ### 20. PHP Let me introduce another solution with sigils, which, as you may see, is not too frequent part among the languages. Perl, Raku, PHP, and Bash in this list do use them though. I believe, if you are familiar with either Perl or Raku, you can understand every bit of this program in PHP (yeah, and no `my` declarators). - +?> +``` ### 21. Kotlin In the program in Kotlin, we see the use of typed variables, C-like functions and...double-dot ranges! What a surprise for the Perl and Raku lovers again. - // How to run: - // $ kotlinc ch-1.kt -include-runtime -d ch1.jar - // $ java -jar ch1.jar 100 - // 13015 +```kotlin +// 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 - } +fun gcd(a: Int, b: Int): Int { + var x = a + var y = b - return x + while (y != 0) { + val t = y + y = x % y + x = t } - fun main(args: Array) { - 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) - } - } + return x +} - println(s) +fun main(args: Array) { + 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) +} +``` + ### 22. Bash And the last program on this page. At this point, I could add a poll to ask if you consider Bash a programming language or not. Honestly, I did not think it would be possible to solve today’s problem in Bash, but as you see, the result is not that much more complicated than the solutions in other languages. Enjoy it too! - #!/usr/bin/bash - - # Usage: - # $ bash ch-1.bash 100 - # 13015 - # - # N. B. Try with smaller arguments, as the computations - # for 100 as the input 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; +```bash +#!/usr/bin/bash + +# Usage: +# $ bash ch-1.bash 100 +# 13015 +# +# N. B. Try with smaller arguments, as the computations +# for 100 as the input 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 - echo $s + 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 +``` And that’s it for today. Tomorrow, there’s another post in this Advent Calendar, but don’t forget that there are more calendars this year, and there are more Weekly Challenges coming soon! -- cgit