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

Published on 12 May 2024 09:40 PM

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

Table of Contents

Problem Statement

A Disarium number is an integer where the sum of each digit raised to the power of its position in the number, is equal to the number.

135 is a Disarium number: 11 + 32 + 53 == 1 + 9 + 125 == 135 There are a finite number of Disarium numbers.

Let's start with the solution:

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

Source code in the forth programming language

: pow 1 swap 0 ?do over * loop nip ;
: len 1 swap begin dup 10 >= while 10 / swap 1+ swap repeat drop ;

: dps 0 swap dup len 
  begin dup while
    swap 10 /mod swap
    2 pick pow
    3 roll +
    rot 1- rot
    swap
  repeat
  2drop 
;

: disarium dup dps = ; 
: disaria 2700000 0 ?do i disarium if i . cr then loop ; 

disaria
bye


  

You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the MAXScript programming language
You may also check:How to resolve the algorithm Abbreviations, simple step by step in the Ruby programming language
You may also check:How to resolve the algorithm Flow-control structures step by step in the Julia programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the Erlang programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the BASIC programming language