How to resolve the algorithm Palindromic gapful numbers step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Palindromic gapful numbers step by step in the Wren programming language

Table of Contents

Problem Statement

Numbers   (positive integers expressed in base ten)   that are (evenly) divisible by the number formed by the first and last digit are known as   gapful numbers.

Evenly divisible   means divisible with   no   remainder.

All   one─   and two─digit   numbers have this property and are trivially excluded.   Only numbers   ≥ 100   will be considered for this Rosetta Code task.

1037   is a   gapful   number because it is evenly divisible by the number   17   which is formed by the first and last decimal digits of   1037.

A palindromic number is   (for this task, a positive integer expressed in base ten),   when the number is reversed,   is the same as the original number.

For other ways of expressing the (above) requirements, see the   discussion   page.

All palindromic gapful numbers are divisible by eleven.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Palindromic gapful numbers step by step in the Wren programming language

Source code in the wren programming language

import "/fmt" for Conv, Fmt

var reverse = Fn.new { |s|
    var e = 0
    while (s > 0) {
        e = e * 10 + (s % 10)
        s = (s/10).floor
    }
    return e
}

var MAX = 100000
var data = [ [1, 20, 7], [86, 100, 8], [991, 1000, 10], [9995, 10000, 12], [99996, 100000, 14] ]
var results = {}
for (d in data) {
    for (i in d[0]..d[1]) results[i] = List.filled(9, 0)
}
var p
for (d in 1..9) {
    var next_d = false
    var count = 0
    var pow = 1
    var fl = d * 11
    for (nd in 3..19) {
        var slim = (d + 1) * pow
        for (s in d*pow...slim) {
            var e = reverse.call(s)
            var mlim = (nd%2 != 1) ? 1 : 10
            for (m in 0...mlim) {
                if (nd%2 == 0) {
                    p = s*pow*10 + e
                } else {
                    p = s*pow*100 + m*pow*10 + e
                }
                if (p%fl == 0) {
                    count = count + 1
                    var rc = results[count]
                    if (rc != null) rc[d-1] = p
                    if (count == MAX) next_d = true
                }
                if (next_d) break
            }
            if (next_d) break
        }
        if (next_d) break
        if (nd%2 == 1) pow = pow * 10
    }    
}

for (d in data) {
    var s1 = Fmt.ordinalize(d[0])
    var s2 = Fmt.ordinalize(d[1]) 
    System.print("%(s1) to %(s2) palindromic gapful numbers (> 100) ending with:")
    for (i in 1..9) {
        System.write("%(i): ")
        for (j in d[0]..d[1]) System.write("%(Fmt.d(d[2], results[j][i-1])) ")
        System.print()
    }
    System.print()
}

  

You may also check:How to resolve the algorithm Thue-Morse step by step in the Delphi programming language
You may also check:How to resolve the algorithm MD5/Implementation step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Relation programming language
You may also check:How to resolve the algorithm Real constants and functions step by step in the Clojure programming language
You may also check:How to resolve the algorithm Logical operations step by step in the Phixmonti programming language