How to resolve the algorithm First power of 2 that has leading decimal digits of 12 step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm First power of 2 that has leading decimal digits of 12 step by step in the Wren programming language

Table of Contents

Problem Statement

(This task is taken from a   Project Euler   problem.) (All numbers herein are expressed in base ten.)

27   =   128   and   7   is the first power of   2   whose leading decimal digits are   12. The next power of   2   whose leading decimal digits are   12   is   80, 280   =   1208925819614629174706176.

Define     p(L,n)     to be the nth-smallest value of   j   such that the base ten representation of   2j   begins with the digits of   L .

You are also given that:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm First power of 2 that has leading decimal digits of 12 step by step in the Wren programming language

Source code in the wren programming language

import "/fmt" for Fmt
import "/math" for Math

var ld10 = Math.ln2 / Math.ln10

var p = Fn.new { |L, n|
    var i = L
    var digits = 1
    while (i >= 10) {
        digits = digits * 10
        i = (i/10).floor
    }
    var count = 0
    i = 0
    while (count < n) {
        var e = (Math.ln10 * (i * ld10).fraction).exp
        if ((e * digits).truncate == L) count = count + 1
        i = i + 1
    }
    return i - 1
}

var start = System.clock
var params = [ [12, 1] , [12, 2], [123, 45], [123, 12345], [123, 678910] ]
for (param in params) {
    Fmt.print("p($d, $d) = $,d", param[0], param[1], p.call(param[0], param[1]))
}

System.print("\nTook %(System.clock - start) seconds.")

  

You may also check:How to resolve the algorithm Generate Chess960 starting position step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Filter step by step in the Lua programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the AppleScript programming language
You may also check:How to resolve the algorithm General FizzBuzz step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Summarize and say sequence step by step in the C++ programming language