How to resolve the algorithm Numbers with equal rises and falls step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

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

This code implements a function (rises_and_falls) that counts how many times the digits in a number increase (rises) and decrease (falls). The function isA296712 checks if the number of rises and falls is the same and genA296712 generates the A296712 sequence, which are those numbers where the count of rises and falls is the same.

Here's a breakdown of the code:

  • The rises_and_falls function takes a number n as input and returns a tuple (rises, falls) indicating how many times the digits in n increase and decrease.

    • It first checks if n is less than 10. If it is, the function returns (0, 0) since there are no digits to compare.
    • It then extracts the last digit of n and initializes the rises and falls counters to 0.
    • While n is not 0, it divides n by 10 and gets the remainder r.
    • It then compares r to the last digit and increments rises if r is greater, or falls if r is less.
    • It updates the last digit to r and continues the loop.
  • The isA296712 function takes a number x as input and returns true if the number of rises and falls is the same, and false otherwise. It expects that rises and falls are the first and second elements of the input tuple, respectively.

  • The genA296712 function takes two numbers N and M as input. It uses the Lazy.range(1) generator to generate a sequence of numbers starting from 1, and then filters this sequence using the filter function to get only the numbers where the number of rises and falls is the same.

    • The take function is used to iterate over the first N elements of the filtered sequence and print them.
    • The for loop iterates over the remaining elements of the filtered sequence until it finds the M-th element.
    • The println function is used to print the M-th element of the sequence.

When you call genA296712(200, 10_000_000), it generates the A296712 sequence, prints the first 200 elements, and then finds the 10,000,000-th element of the sequence.

Source code in the julia programming language

using Lazy

function rises_and_falls(n)
    if n < 10
        return 0, 0
    end
    lastr, rises, falls = n % 10, 0, 0
    while n != 0
        n, r = divrem(n, 10)
        if r > lastr
            falls += 1
        elseif r < lastr
            rises += 1
        end
        lastr = r
    end
    return rises, falls
end

isA296712(x) = ((a, b) = rises_and_falls(x); return a == b)

function genA296712(N, M)
    A296712 = filter(isA296712, Lazy.range(1));
    j = 0
    for i in take(200, A296712)
        j += 1
        print(lpad(i, 4), j % 20 == 0 ? "\n" : "")
    end
    for i in take(M, A296712)
        j = i
    end
    println("\nThe $M-th number in sequence A296712 is $j.")
end

genA296712(200, 10_000_000)


  

You may also check:How to resolve the algorithm Resistor mesh step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Kronecker product step by step in the Swift programming language
You may also check:How to resolve the algorithm Remove lines from a file step by step in the Erlang programming language
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the Maple programming language
You may also check:How to resolve the algorithm Colour bars/Display step by step in the C programming language