From 1b7e73844b3f5c832061b987127896cb83aafa1b Mon Sep 17 00:00:00 2001 From: zafar-hussain Date: Thu, 8 Oct 2020 16:28:36 +0500 Subject: [PATCH 1/3] Adding Fibonacci Sequence (recursive) - in Racket --- Mathematics/fibonacci/fib.rkt | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Mathematics/fibonacci/fib.rkt diff --git a/Mathematics/fibonacci/fib.rkt b/Mathematics/fibonacci/fib.rkt new file mode 100644 index 00000000..65543db7 --- /dev/null +++ b/Mathematics/fibonacci/fib.rkt @@ -0,0 +1,37 @@ +#lang racket + +;; The Fibonacci Sequence is the series of numbers: +;; 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... + +;; integer -> list of integers +;; building intution for fibn - inner helper function +;; fn = fn-1 + fn-2 where fn < 2 = n +;; f0 = 0, f1 = 1 ; basecases +;; f2 = f1 + f0 = 1 + 0 = 1 +;; f3 = f2 + f1 = 1 + 1 +;; f8 = f7 + f6 = 13 + 8 = 21 + +;; code +;(define (fib n) (list 0)) ; stub + +(define (fib n) + (local [(define (fibn n) ;; inner helper function to calculate (fib n) + (cond [(= n 0) 0] + [(= n 1) 1] + [else + (+ ;; add fn-1 to fn-2 + (fibn (- n 1)) + (fibn (- n 2)))]))] ;; self reference for structital recursion + (build-list (add1 n) fibn))) + +;; test examples for fibn +;(equal? (fibn 0) 0) ;; base case for structural recursion +;(equal? (fibn 1) 1) ;; base case for structural recursion +;(equal? (fibn 8) (+ (fibn 7) (fibn 6))) ;; intution +;(equal? (fibn 8) 21) +; + +;; test examples for fib +;(equal? (fib 0) (cons 0 empty)) ;; base case for structural recursion +;(equal? (fib 1) (list 0 1)) ;; base case for structural recursion +;(equal? (fib 8) (list 0 1 1 2 3 5 8 13 21)) \ No newline at end of file From b7f990c5e8d4c3b4f18181a9d33e6416101be8bf Mon Sep 17 00:00:00 2001 From: zafar-hussain Date: Thu, 8 Oct 2020 19:27:46 +0500 Subject: [PATCH 2/3] Adding N-digit Armstrong Number in Racket for issue #187 --- Algorithms/armstrong-Number.rkt | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Algorithms/armstrong-Number.rkt diff --git a/Algorithms/armstrong-Number.rkt b/Algorithms/armstrong-Number.rkt new file mode 100644 index 00000000..93e3acc8 --- /dev/null +++ b/Algorithms/armstrong-Number.rkt @@ -0,0 +1,41 @@ +#lang racket + +;Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers. +; +;Let's try to understand why 153 is an Armstrong number. +; +;153 = (1*1*1)+(5*5*5)+(3*3*3) +;where: +;(1*1*1)=1 +;(5*5*5)=125 +;(3*3*3)=27 +;So: +;1+125+27=153 + +;; I got help from this article +;; https://www.javatpoint.com/armstrong-number-in-c#:~:text=Armstrong%20number%20is%20a%20number,153%20is%20an%20Armstrong%20number.&text=Let's%20try%20to%20understand%20why%20371%20is%20an%20Armstrong%20number. + +;; number -> boolean +;; code +;(define (armstrong-Number n) false) ; stub + +(define (armstrong-Number n) + (local [(define (armstrong-sum n) ;; helper function to find the sum, of cubes, of digits, of n + (cond [(zero? n) 0] ;; basecase to stop structural recirsion + [else + (+ ;; add them all + (expt (modulo n 10) 3) ;; get the last digit of n by modulo 10 of n + (armstrong-sum (quotient n 10))) ]))] ;; remove the last digit of n by quotient 10 of n - integer division by 10 + (= (armstrong-sum n) n))) ;; compare the sum of cube of digits of n, and n + + + + +;; test examples +;(equal? (armstrong-Number 0) true) +;(equal? (armstrong-Number 1) true) +;(equal? (armstrong-Number 9) false) +;(equal? (armstrong-Number 153) true) +;(equal? (armstrong-Number 100) false) +;(= 153 (+ (expt 1 3) (expt 5 3) (expt 3 3))) +; From d2ef0b367822c69e7acc43de09eafe0dac760eca Mon Sep 17 00:00:00 2001 From: zafar-hussain Date: Thu, 8 Oct 2020 20:37:18 +0500 Subject: [PATCH 3/3] Adding solution for issue #36 'Check Palindrome of integers' --- problems/palindrome/isPalindrome?.rkt | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 problems/palindrome/isPalindrome?.rkt diff --git a/problems/palindrome/isPalindrome?.rkt b/problems/palindrome/isPalindrome?.rkt new file mode 100644 index 00000000..1c050467 --- /dev/null +++ b/problems/palindrome/isPalindrome?.rkt @@ -0,0 +1,39 @@ +#lang racket + +;Build a function that checks weather a given number is a palindrome or not without using a strings. Implement it using any language. +; +;For example: +;input: 12321 +;output: true +; +; input: 12344321 +; output: True + +;; intution +;; n and reverse n will be same for a palindrome +;; will use reverse-number (see pr #341 for issue #35) + +;; I got the Idea from link below +;; https://www.integers.co/questions-answers/is-12344321-a-palindrome-number.html + + + +;; code +(define (isPalinDrome n) + (local [ + (define (reverse-n n acc) ; using an decerasing accumulator + (cond [(= (order-of-magnitude n) 0) n] ; basecase, used to stop recursion + [else + (+ + (* (modulo n 10) (expt 10 acc)) ; get the right-most digit of n, multiply it with 10^x + (reverse-n (quotient n 10) (sub1 acc)))])) + + (define (reverse-number num) + (reverse-n num (order-of-magnitude num))) + ] + (= (reverse-number n) n))) + +; test examples +;(equal? (isPalinDrome 9) true) +;(equal? (isPalinDrome 12349321) false) +;(equal? (isPalinDrome 12321) true)