Update examples
This commit is contained in:
parent
8b7ed96e15
commit
3892ea46e0
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user