How to resolve the algorithm Esthetic numbers step by step in the Forth programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Esthetic numbers step by step in the Forth programming language

Table of Contents

Problem Statement

An esthetic number is a positive integer where every adjacent digit differs from its neighbour by 1.

These examples are nominally in base 10 but the concept extends easily to numbers in other bases. Traditionally, single digit numbers are included in esthetic numbers; zero may or may not be. For our purposes, for this task, do not include zero (0) as an esthetic number. Do not include numbers with leading zeros. Esthetic numbers are also sometimes referred to as stepping numbers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Esthetic numbers step by step in the Forth programming language

Source code in the forth programming language

\ Returns the next esthetic number in the given base after n, where n is an
\ esthetic number in that base or one less than a power of base.
: next_esthetic_number { n base -- n }
  n 1+ base < if n 1+ exit then
  n base / dup base mod
  dup n base mod 1+ = if dup 1+ base < if 2drop n 2 + exit then then
  drop base recurse
  dup base mod
  dup 0= if 1+ else 1- then
  swap base * + ;

: print_esthetic_numbers { min max per_line -- }
  ." Esthetic numbers in base 10 between " min 1 .r ."  and " max 1 .r ." :" cr
  0
  min 1- 10 next_esthetic_number
  begin
    dup max <=
  while
    dup 4 .r
    swap 1+ dup per_line mod 0= if cr else space then swap
    10 next_esthetic_number
  repeat
  drop
  cr ." count: " . cr ;

: main
  17 2 do
    i 4 * i 6 * { min max }
    ." Esthetic numbers in base " i 1 .r ."  from index " min 1 .r ."  through index " max 1 .r ." :" cr
    0
    max 1+ 1 do
      j next_esthetic_number
      i min >= if dup ['] . j base-execute then
    loop
    drop
    cr cr
  loop
  1000 9999 16 print_esthetic_numbers cr
  100000000 130000000 8 print_esthetic_numbers ;

main
bye


  

You may also check:How to resolve the algorithm Hailstone sequence step by step in the beeswax programming language
You may also check:How to resolve the algorithm Compound data type step by step in the Maple programming language
You may also check:How to resolve the algorithm Perfect totient numbers step by step in the Raku programming language
You may also check:How to resolve the algorithm Sequence of primes by trial division step by step in the Fortran programming language
You may also check:How to resolve the algorithm Happy numbers step by step in the C programming language