Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Fibonacci Sequence (recursive) - in Racket - for issue #15 #342

Merged
merged 3 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Algorithms/armstrong-Number.rkt
Original file line number Diff line number Diff line change
@@ -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)))
;
37 changes: 37 additions & 0 deletions Mathematics/fibonacci/fib.rkt
Original file line number Diff line number Diff line change
@@ -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))
39 changes: 39 additions & 0 deletions problems/palindrome/isPalindrome?.rkt
Original file line number Diff line number Diff line change
@@ -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)