How to resolve the algorithm Fusc sequence step by step in the Racket programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Fusc sequence step by step in the Racket programming language
Table of Contents
Problem Statement
The fusc integer sequence is defined as:
Note that MathWorld's definition starts with unity, not zero. This task will be using the OEIS' version (above).
where A is some non-negative integer expressed in binary, and where B is the binary value of A reversed.
Fusc numbers are also known as:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fusc sequence step by step in the Racket programming language
Source code in the racket programming language
#lang racket
(require racket/generator)
(define (memoize f)
(define table (make-hash))
(λ args (hash-ref! table args (thunk (apply f args)))))
(define fusc
(memoize
(λ (n)
(cond
[(<= n 1) n]
[(even? n) (fusc (/ n 2))]
[else (+ (fusc (/ (sub1 n) 2)) (fusc (/ (add1 n) 2)))]))))
(define (comma x)
(string-join
(reverse
(for/list ([digit (in-list (reverse (string->list (~a x))))] [i (in-naturals)])
(cond
[(and (= 0 (modulo i 3)) (> i 0)) (string digit #\,)]
[else (string digit)])))
""))
;; Task 1
(displayln (string-join (for/list ([i (in-range 61)]) (comma (fusc i))) " "))
(newline)
;; Task 2
(define gen
(in-generator
(let loop ([prev 0] [i 0])
(define result (fusc i))
(define len (string-length (~a result)))
(cond
[(> len prev)
(yield (list i result))
(loop len (add1 i))]
[else (loop prev (add1 i))]))))
(for ([i (in-range 5)] [x gen])
(match-define (list index result) x)
(printf "~a: ~a\n" (comma index) (comma result)))
You may also check:How to resolve the algorithm AKS test for primes step by step in the Maple programming language
You may also check:How to resolve the algorithm Four bit adder step by step in the TorqueScript programming language
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the FunL programming language
You may also check:How to resolve the algorithm Runtime evaluation/In an environment step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Truncatable primes step by step in the Factor programming language