From 3892ea46e058403892abbd6f6c5923beb06c6f4d Mon Sep 17 00:00:00 2001 From: Daniel M Date: Fri, 11 Feb 2022 01:19:45 +0100 Subject: [PATCH] Update examples --- examples/euler3.nek | 4 +- examples/euler4.nek | 21 ++-- examples/euler4.py | 18 ++-- examples/euler5.nek | 31 +++--- examples/euler5.py | 17 ++-- examples/game_of_life.nek | 206 ++++++++++++++++++++------------------ 6 files changed, 156 insertions(+), 141 deletions(-) diff --git a/examples/euler3.nek b/examples/euler3.nek index 067650f..7ed4820 100644 --- a/examples/euler3.nek +++ b/examples/euler3.nek @@ -18,10 +18,10 @@ loop number > 1 { div = div + 1; if div * div > number { - if number > 1 & number > result { + if number > 1 && number > result { result = number; } - number = 0; + break; } } diff --git a/examples/euler4.nek b/examples/euler4.nek index 6ed638c..d48a8c4 100644 --- a/examples/euler4.nek +++ b/examples/euler4.nek @@ -4,22 +4,25 @@ // // Correct Answer: 906609 +fun reverse(n) { + rev <- 0; + loop n { + rev = rev * 10 + n % 10; + n = n / 10; + } + return rev; +} + res <- 0; i <- 100; loop i < 1_000; i = i + 1 { - k <- 100; + k <- i; loop k < 1_000; k = k + 1 { - num_rev <- 0; num <- i * k; - tmp <- num; + num_rev <- reverse(num); - loop tmp { - num_rev = num_rev*10 + tmp % 10; - tmp = tmp / 10; - } - - if num == num_rev & num > res { + if num == num_rev && num > res { res = num; } } diff --git a/examples/euler4.py b/examples/euler4.py index 08bf352..3c14924 100644 --- a/examples/euler4.py +++ b/examples/euler4.py @@ -4,19 +4,19 @@ # # Correct Answer: 906609 +def reverse(n): + rev = 0 + while n: + rev = rev * 10 + n % 10 + n //= 10 + return rev res = 0 -for i in range(100, 999): - for k in range(100, 999): - +for i in range(100, 1_000): + for k in range(i, 1_000): num = i * k - tmp = num - - num_rev = 0 - while tmp != 0: - num_rev = num_rev*10 + tmp % 10 - tmp = tmp // 10 + num_rev = reverse(num) if num == num_rev and num > res: res = num diff --git a/examples/euler5.nek b/examples/euler5.nek index bdf700f..64f0517 100644 --- a/examples/euler5.nek +++ b/examples/euler5.nek @@ -3,24 +3,21 @@ // // Correct Answer: 232_792_560 -num <- 20; -should_continue <- 1; -loop should_continue { - should_continue = 0; - - i <- 20; - loop i >= 2; i = i - 1 { - if num % i { - should_continue = 1; - - // break - i = 0; - } +fun gcd(x, y) { + loop y { + tmp <- x; + x = y; + y = tmp % y; } - if should_continue { - num = num + 20; - } + return x; } -print num; +result <- 1; + +i <- 1; +loop i <= 20; i = i + 1 { + result = result * (i / gcd(i, result)); +} + +print result; diff --git a/examples/euler5.py b/examples/euler5.py index fdddd39..1d099b7 100644 --- a/examples/euler5.py +++ b/examples/euler5.py @@ -3,14 +3,13 @@ # # Correct Answer: 232_792_560 -num = 20 +def gcd(x, y): + while y: + x, y = y, x % y + return x -while True: - for i in range(20, 2, -1): - if num % i != 0: - break - else: - break - num = num + 20 +result = 1 +for i in range(1, 21): + result *= i // gcd(i, result) -print(num) +print(result) diff --git a/examples/game_of_life.nek b/examples/game_of_life.nek index 8a5475b..63f5bb8 100644 --- a/examples/game_of_life.nek +++ b/examples/game_of_life.nek @@ -1,19 +1,4 @@ -width <- 10; -height <- 10; - -field <- [width*height]; -field2 <- [width*height]; - -field[1] = 1; -field[12] = 1; -field[20] = 1; -field[21] = 1; -field[22] = 1; - - -runs <- 0; -loop runs < 20; runs = runs + 1 { - // Print the field +fun print_field(field, width, height) { y <- 0; loop y < height; y = y+1 { x <- 0; @@ -27,92 +12,123 @@ loop runs < 20; runs = runs + 1 { print "\n"; } print "\n"; +} - // Update from field into field2 - y <- 0; - loop y < height; y = y+1 { - x <- 0; - loop x < width; x = x+1 { - - neighbours <- 0; - - if y > 0 { - if x > 0 { - // Top left - if field[(y-1)*width + (x-1)] { - neighbours = neighbours + 1; - } - } - - // Top - if field[(y-1)*width + x] { - neighbours = neighbours + 1; - } - - if x < width-1 { - // Top right - if field[(y-1)*width + (x+1)] { - neighbours = neighbours + 1; - } - } +fun count_neighbours(field, x, y, width, height) { + neighbours <- 0; + if y > 0 { + if x > 0 { + if field[(y-1)*width + (x-1)] { + // Top left + neighbours = neighbours + 1; } + } - if x > 0 { - // Left - if field[y*width + (x-1)] { - neighbours = neighbours + 1; - } + if field[(y-1)*width + x] { + // Top + neighbours = neighbours + 1; + } + + if x < width-1 { + if field[(y-1)*width + (x+1)] { + // Top right + neighbours = neighbours + 1; } - - if x < width-1 { - // Right - if field[y*width + (x+1)] { - neighbours = neighbours + 1; - } - } - - - if y < height-1 { - if x > 0 { - // Bottom left - if field[(y+1)*width + (x-1)] { - neighbours = neighbours + 1; - } - } - - // Bottom - if field[(y+1)*width + x] { - neighbours = neighbours + 1; - } - - if x < width-1 { - // Bottom right - if field[(y+1)*width + (x+1)] { - neighbours = neighbours + 1; - } - } - } - - - if neighbours < 2 || neighbours > 3 { - field2[y*width + x] = 0; - } else { - if neighbours == 3 { - field2[y*width + x] = 1; - } else { - field2[y*width + x] = field[y*width + x]; - } - } - } } - // Transfer from field2 to field - y <- 0; - loop y < height; y = y+1 { - x <- 0; - loop x < width; x = x+1 { - field[y*width + x] = field2[y*width + x]; + if x > 0 { + if field[y*width + (x-1)] { + // Left + neighbours = neighbours + 1; } } + + if x < width-1 { + if field[y*width + (x+1)] { + // Right + neighbours = neighbours + 1; + } + } + + + if y < height-1 { + if x > 0 { + if field[(y+1)*width + (x-1)] { + // Bottom left + neighbours = neighbours + 1; + } + } + + if field[(y+1)*width + x] { + // Bottom + neighbours = neighbours + 1; + } + + if x < width-1 { + if field[(y+1)*width + (x+1)] { + // Bottom right + neighbours = neighbours + 1; + } + } + } + return neighbours; +} + +fun copy(from, to, len) { + i <- 0; + loop i < len; i = i + 1 { + to[i] = from[i]; + } } + +// Set the width and height of the field +width <- 10; +height <- 10; + +// Create the main and temporary field +field <- [width*height]; +field2 <- [width*height]; + +// Preset the main field with a glider +field[1] = 1; +field[12] = 1; +field[20] = 1; +field[21] = 1; +field[22] = 1; + +fun run_gol(num_rounds) { + runs <- 0; + loop runs < num_rounds; runs = runs + 1 { + // Print the field + print_field(field, width, height); + + // Calculate next stage from field and store into field2 + y <- 0; + loop y < height; y = y+1 { + x <- 0; + loop x < width; x = x+1 { + + // Get the neighbours of the current cell + neighbours <- count_neighbours(field, x, y, width, height); + + // Set the new cell according to the neighbour count + if neighbours < 2 || neighbours > 3 { + field2[y*width + x] = 0; + } else { + if neighbours == 3 { + field2[y*width + x] = 1; + } else { + field2[y*width + x] = field[y*width + x]; + } + } + + } + } + + // Transfer from field2 to field + copy(field2, field, width*height); + } +} + +run_gol(32);