Skip to content

Undulating numbers

editable example

Click the pencil to open this in an editor, change it, and run it in your browser. The same solution is posted on Rosetta Code.

ghul
use IO.Std.write_line
use Collections.LIST
use Ghul.Pipes
undulating_value(first: int, second: int, length: int) -> long => (
let value mut = 0L
for position in 0..length do
let digit = if position % 2 == 0 then first else second fi
value = value * 10L + cast long(digit)
od
value
)
let limit = 9007199254740992L
let values = LIST[long]()
for length in 3..17 do
for first in 1..10 do
for second in 0..10 do
if second != first then
let value = undulating_value(first, second, length)
if value < limit then
values.add(value)
fi
fi
od
od
od
let three_digit = values |> filter(v => v < 1000L) |> collect_list()
let four_digit = values
|> filter(v => v >= 1000L /\ v < 10000L)
|> collect_list()
write_line("three digit:")
for row in 0..9 do
write_line(
three_digit
|> skip(row * 9)
|> take(9)
|> map(v => "{v}")
|> join(" "))
od
write_line("four digit:")
for row in 0..9 do
write_line(
four_digit
|> skip(row * 9)
|> take(9)
|> map(v => "{v}")
|> join(" "))
od
prime(value: long) -> bool => (
let divisor mut = 2L
let factor_found = while divisor * divisor <= value do
if value % divisor == 0L then
break true
fi
divisor = divisor + 1L
od
value >= 2L /\ !(factor_found ?? false)
)
write_line("three digit primes:")
write_line(
three_digit |> filter(prime) |> map(v => "{v}") |> join(" "))
write_line("600th: {values[599]}")
write_line(
"{values.count} undulating numbers below 2^53, "
"the largest is {values[values.count - 1]}")