How to resolve the algorithm Numbers with equal rises and falls step by step in the AWK 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 AWK 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 AWK programming language

Source code in the awk programming language

# syntax: GAWK -f NUMBERS_WITH_EQUAL_RISES_AND_FALLS.AWK
# converted from Go
BEGIN {
    print("1-200:")
    while (1) {
      if (rises_equals_falls(++n)) {
        if (++count <= 200) {
          printf("%4d",n)
          if (count % 20 == 0) {
            printf("\n")
          }
        }
        if (count == 1E7) {
          printf("\n%d: %d",count,n)
          break
        }
      }
    }
    exit(0)
}
function rises_equals_falls(n,  d,falls,prev,rises) {
    if (n < 10) {
      return(1)
    }
    prev = -1
    while (n > 0) {
      d = n % 10
      if (prev >= 0) {
        if (d < prev) {
          rises++
        }
        else if (d > prev) {
          falls++
        }
      }
      prev = d
      n = int(n / 10)
    }
    return(rises == falls)
}


  

You may also check:How to resolve the algorithm Pragmatic directives step by step in the Wren programming language
You may also check:How to resolve the algorithm Topic variable step by step in the Standard ML programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the MyDef programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the 8th programming language
You may also check:How to resolve the algorithm Langton's ant step by step in the Wren programming language