nek-lang/examples/euler4.nek
2022-02-11 01:19:45 +01:00

32 lines
615 B
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// A palindromic number reads the same both ways. The largest palindrome made from the product of
// two 2-digit numbers is 9009 = 91 × 99.
// Find the largest palindrome made from the product of two 3-digit numbers.
//
// 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 <- i;
loop k < 1_000; k = k + 1 {
num <- i * k;
num_rev <- reverse(num);
if num == num_rev && num > res {
res = num;
}
}
}
print res;