Update examples
This commit is contained in:
parent
8b7ed96e15
commit
3892ea46e0
@ -18,10 +18,10 @@ loop number > 1 {
|
|||||||
|
|
||||||
div = div + 1;
|
div = div + 1;
|
||||||
if div * div > number {
|
if div * div > number {
|
||||||
if number > 1 & number > result {
|
if number > 1 && number > result {
|
||||||
result = number;
|
result = number;
|
||||||
}
|
}
|
||||||
number = 0;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,22 +4,25 @@
|
|||||||
//
|
//
|
||||||
// Correct Answer: 906609
|
// Correct Answer: 906609
|
||||||
|
|
||||||
|
fun reverse(n) {
|
||||||
|
rev <- 0;
|
||||||
|
loop n {
|
||||||
|
rev = rev * 10 + n % 10;
|
||||||
|
n = n / 10;
|
||||||
|
}
|
||||||
|
return rev;
|
||||||
|
}
|
||||||
|
|
||||||
res <- 0;
|
res <- 0;
|
||||||
|
|
||||||
i <- 100;
|
i <- 100;
|
||||||
loop i < 1_000; i = i + 1 {
|
loop i < 1_000; i = i + 1 {
|
||||||
k <- 100;
|
k <- i;
|
||||||
loop k < 1_000; k = k + 1 {
|
loop k < 1_000; k = k + 1 {
|
||||||
num_rev <- 0;
|
|
||||||
num <- i * k;
|
num <- i * k;
|
||||||
tmp <- num;
|
num_rev <- reverse(num);
|
||||||
|
|
||||||
loop tmp {
|
if num == num_rev && num > res {
|
||||||
num_rev = num_rev*10 + tmp % 10;
|
|
||||||
tmp = tmp / 10;
|
|
||||||
}
|
|
||||||
|
|
||||||
if num == num_rev & num > res {
|
|
||||||
res = num;
|
res = num;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,19 +4,19 @@
|
|||||||
#
|
#
|
||||||
# Correct Answer: 906609
|
# Correct Answer: 906609
|
||||||
|
|
||||||
|
def reverse(n):
|
||||||
|
rev = 0
|
||||||
|
while n:
|
||||||
|
rev = rev * 10 + n % 10
|
||||||
|
n //= 10
|
||||||
|
return rev
|
||||||
|
|
||||||
res = 0
|
res = 0
|
||||||
|
|
||||||
for i in range(100, 999):
|
for i in range(100, 1_000):
|
||||||
for k in range(100, 999):
|
for k in range(i, 1_000):
|
||||||
|
|
||||||
num = i * k
|
num = i * k
|
||||||
tmp = num
|
num_rev = reverse(num)
|
||||||
|
|
||||||
num_rev = 0
|
|
||||||
while tmp != 0:
|
|
||||||
num_rev = num_rev*10 + tmp % 10
|
|
||||||
tmp = tmp // 10
|
|
||||||
|
|
||||||
if num == num_rev and num > res:
|
if num == num_rev and num > res:
|
||||||
res = num
|
res = num
|
||||||
|
|||||||
@ -3,24 +3,21 @@
|
|||||||
//
|
//
|
||||||
// Correct Answer: 232_792_560
|
// Correct Answer: 232_792_560
|
||||||
|
|
||||||
num <- 20;
|
fun gcd(x, y) {
|
||||||
should_continue <- 1;
|
loop y {
|
||||||
loop should_continue {
|
tmp <- x;
|
||||||
should_continue = 0;
|
x = y;
|
||||||
|
y = tmp % y;
|
||||||
i <- 20;
|
|
||||||
loop i >= 2; i = i - 1 {
|
|
||||||
if num % i {
|
|
||||||
should_continue = 1;
|
|
||||||
|
|
||||||
// break
|
|
||||||
i = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if should_continue {
|
return x;
|
||||||
num = num + 20;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
# Correct Answer: 232_792_560
|
||||||
|
|
||||||
num = 20
|
def gcd(x, y):
|
||||||
|
while y:
|
||||||
|
x, y = y, x % y
|
||||||
|
return x
|
||||||
|
|
||||||
while True:
|
result = 1
|
||||||
for i in range(20, 2, -1):
|
for i in range(1, 21):
|
||||||
if num % i != 0:
|
result *= i // gcd(i, result)
|
||||||
break
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
num = num + 20
|
|
||||||
|
|
||||||
print(num)
|
print(result)
|
||||||
|
|||||||
@ -1,19 +1,4 @@
|
|||||||
width <- 10;
|
fun print_field(field, width, height) {
|
||||||
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
|
|
||||||
y <- 0;
|
y <- 0;
|
||||||
loop y < height; y = y+1 {
|
loop y < height; y = y+1 {
|
||||||
x <- 0;
|
x <- 0;
|
||||||
@ -27,92 +12,123 @@ loop runs < 20; runs = runs + 1 {
|
|||||||
print "\n";
|
print "\n";
|
||||||
}
|
}
|
||||||
print "\n";
|
print "\n";
|
||||||
|
}
|
||||||
|
|
||||||
// Update from field into field2
|
fun count_neighbours(field, x, y, width, height) {
|
||||||
y <- 0;
|
neighbours <- 0;
|
||||||
loop y < height; y = y+1 {
|
if y > 0 {
|
||||||
x <- 0;
|
if x > 0 {
|
||||||
loop x < width; x = x+1 {
|
if field[(y-1)*width + (x-1)] {
|
||||||
|
// Top left
|
||||||
neighbours <- 0;
|
neighbours = neighbours + 1;
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if x > 0 {
|
if field[(y-1)*width + x] {
|
||||||
// Left
|
// Top
|
||||||
if field[y*width + (x-1)] {
|
neighbours = neighbours + 1;
|
||||||
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
|
if x > 0 {
|
||||||
y <- 0;
|
if field[y*width + (x-1)] {
|
||||||
loop y < height; y = y+1 {
|
// Left
|
||||||
x <- 0;
|
neighbours = neighbours + 1;
|
||||||
loop x < width; x = x+1 {
|
|
||||||
field[y*width + x] = field2[y*width + x];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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