How to resolve the algorithm Fraction reduction step by step in the Racket programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Fraction reduction step by step in the Racket programming language
Table of Contents
Problem Statement
A method to "reduce" some reducible fractions is to cross out a digit from the numerator and the denominator. An example is: resulting in:
Naturally, this "method" of reduction must reduce to the proper value (shown as a fraction). This "method" is also known as anomalous cancellation and also accidental cancellation.
(Of course, this "method" shouldn't be taught to impressionable or gullible minds.) 😇
Find and show some fractions that can be reduced by the above "method".
Show all output here, on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Fraction reduction step by step in the Racket programming language
Source code in the racket programming language
#lang racket
(require racket/generator
syntax/parse/define)
(define-syntax-parser for**
[(_ [x:id {~datum <-} (e ...)] rst ...) #'(e ... (λ (x) (for** rst ...)))]
[(_ e ...) #'(begin e ...)])
(define (permutations xs n yield #:lower [lower #f])
(let loop ([xs xs] [n n] [acc '()] [lower lower])
(cond
[(= n 0) (yield (reverse acc))]
[else (for ([x (in-list xs)] #:when (or (not lower) (>= x (first lower))))
(loop (remove x xs)
(sub1 n)
(cons x acc)
(and lower (= x (first lower)) (rest lower))))])))
(define (list->number xs) (foldl (λ (e acc) (+ (* 10 acc) e)) 0 xs))
(define (calc n)
(define rng (range 1 10))
(in-generator
(for** [numer <- (permutations rng n)]
[denom <- (permutations rng n #:lower numer)]
(for* (#:when (not (equal? numer denom))
[crossed (in-list numer)]
#:when (member crossed denom)
[numer* (in-value (list->number (remove crossed numer)))]
[denom* (in-value (list->number (remove crossed denom)))]
[numer** (in-value (list->number numer))]
[denom** (in-value (list->number denom))]
#:when (= (* numer** denom*) (* numer* denom**)))
(yield (list numer** denom** numer* denom* crossed))))))
(define (enumerate n)
(for ([x (calc n)] [i (in-range 12)])
(apply printf "~a/~a = ~a/~a (~a crossed out)\n" x))
(newline))
(define (stats n)
(define digits (make-hash))
(for ([x (calc n)]) (hash-update! digits (last x) add1 0))
(printf "There are ~a ~a-digit fractions of which:\n" (for/sum ([(k v) (in-hash digits)]) v) n)
(for ([digit (in-list (sort (hash->list digits) < #:key car))])
(printf " The digit ~a was crossed out ~a times\n" (car digit) (cdr digit)))
(newline))
(define (main)
(enumerate 2)
(enumerate 3)
(enumerate 4)
(enumerate 5)
(stats 2)
(stats 3)
(stats 4)
(stats 5))
(main)
You may also check:How to resolve the algorithm Repeat a string step by step in the Tcl programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Create a file step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Combinations step by step in the Arturo programming language
You may also check:How to resolve the algorithm Check that file exists step by step in the Stata programming language