How to resolve the algorithm Numbers with equal rises and falls step by step in the CLU programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Numbers with equal rises and falls step by step in the CLU programming language
Table of Contents
Problem Statement
When a number is written in base 10, adjacent digits may "rise" or "fall" as the number is read (usually from left to right).
Given the decimal digits of the number are written as a series d:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Numbers with equal rises and falls step by step in the CLU programming language
Source code in the clu programming language
% Find how many rises and falls a number has
rises_falls = proc (n: int) returns (int,int)
rises: int := 0
falls: int := 0
while n >= 10 do
dl: int := n//10
n := n / 10
dh: int := n//10
if dh < dl then rises := rises + 1
elseif dl < dh then falls := falls + 1
end
end
return (rises, falls)
end rises_falls
% Generate all numbers with equal rises and falls
equal_rises_falls = iter () yields (int)
n: int := 1
rises, falls: int
while true do
rises, falls := rises_falls(n)
if rises = falls then yield (n) end
n := n + 1
end
end equal_rises_falls
% Show the first 200 and the 10,000,000th
start_up = proc ()
po: stream := stream$primary_output()
count: int := 0
for n: int in equal_rises_falls() do
count := count + 1
if count <= 200 then
stream$putright(po, int$unparse(n), 5)
if count//10 = 0 then stream$putc(po, '\n') end
elseif count = 10000000 then
stream$putl(po, "\nThe 10,000,000th number is: "
|| int$unparse(n))
break
end
end
end start_up
You may also check:How to resolve the algorithm Loops/With multiple ranges step by step in the Kotlin programming language
You may also check:How to resolve the algorithm N-queens problem step by step in the jq programming language
You may also check:How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the Elixir programming language
You may also check:How to resolve the algorithm Function composition step by step in the Clojure programming language
You may also check:How to resolve the algorithm Plasma effect step by step in the AWK programming language