How to resolve the algorithm First power of 2 that has leading decimal digits of 12 step by step in the Racket 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 Racket 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 Racket programming language

Source code in the racket programming language

#lang racket
(define (fract-part f)
  (- f (truncate f)))

(define ln10 (log 10))
(define ln2/ln10 (/ (log 2) ln10))
  
(define (inexact-p-test L)
  (let ((digit-shift (let loop ((L (quotient L 10)) (shift 1))
                       (if (zero? L) shift (loop (quotient L 10) (* 10 shift))))))
    (λ (p) (= L (truncate (* digit-shift (exp (* ln10 (fract-part (* p ln2/ln10))))))))))

(define (p L n)
  (let ((test? (inexact-p-test L)))  
    (let loop ((j 1) (n (sub1 n)))
      (cond [(not (test? j)) (loop (add1 j) n)]
            [(zero? n) j]
            [else (loop (add1 j) (sub1 n))]))))

(module+ main
  (define (report-p L n)
    (time (printf "p(~a, ~a) = ~a~%" L n (p L n))))
  
  (report-p 12 1)
  (report-p 12 2)
  (report-p 123 45)
  (report-p 123 12345)
  (report-p 123 678910))


#lang typed/racket

(: fract-part (-> Float Float))
(: ln10 Positive-Float)
(: ln2/ln10 Positive-Float)
(: p (-> Positive-Index Positive-Index Positive-Integer))

(define (fract-part f)
  (- f (truncate f)))

(define ln10 (cast (log 10) Positive-Float))
(define ln2/ln10 (cast (/ (log 2) ln10) Positive-Float))
  
(define (inexact-p-test [L : Positive-Index])
  (let ((digit-shift : Nonnegative-Float
                     (let loop ((L (quotient L 10)) (shift : Nonnegative-Float 1.))
                       (if (zero? L) shift (loop (quotient L 10) (* 10. shift)))))
        (l (exact->inexact L)))
    (: f (-> Nonnegative-Float Boolean))
    (define (f p) (= l (truncate (* digit-shift (exp (* ln10 (fract-part (* p ln2/ln10))))))))
    f))

(define (p L n)
  (let ((test? (inexact-p-test L)))  
    (let loop : Positive-Integer ((j : Positive-Float 1.) (n : Index (sub1 n)))
      (cond [(not (test? j)) (loop (add1 j) n)]
            [(zero? n) (assert (exact-round j) positive?)]
            [else (loop (add1 j) (sub1 n))]))))

(module+ main
  (: report-p (-> Positive-Index Positive-Index Void))
  (define (report-p L n)
    (time (printf "p(~a, ~a) = ~a~%" L n (p L n))))
  
  (report-p 12 1)
  (report-p 12 2)
  (report-p 123 45)
  (report-p 123 12345)
  (report-p 123 678910))


  

You may also check:How to resolve the algorithm 24 game/Solve step by step in the Nim programming language
You may also check:How to resolve the algorithm Date format step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Magic squares of singly even order step by step in the Rust programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Ioke programming language
You may also check:How to resolve the algorithm Random Latin squares step by step in the Python programming language