Skip to content

Commit 23e4510

Browse files
committed
Adding N-digit Armstrong Number in Racket for issue #187
1 parent 13da61e commit 23e4510

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Algorithms/armstrong-Number.rkt

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#lang racket
2+
3+
;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.
4+
;
5+
;Let's try to understand why 153 is an Armstrong number.
6+
;
7+
;153 = (1*1*1)+(5*5*5)+(3*3*3)
8+
;where:
9+
;(1*1*1)=1
10+
;(5*5*5)=125
11+
;(3*3*3)=27
12+
;So:
13+
;1+125+27=153
14+
15+
;; I got help from this article
16+
;; 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.
17+
18+
;; number -> boolean
19+
;; code
20+
;(define (armstrong-Number n) false) ; stub
21+
22+
(define (armstrong-Number n)
23+
(local [(define (armstrong-sum n) ;; helper function to find the sum, of cubes, of digits, of n
24+
(cond [(zero? n) 0] ;; basecase to stop structural recirsion
25+
[else
26+
(+ ;; add them all
27+
(expt (modulo n 10) 3) ;; get the last digit of n by modulo 10 of n
28+
(armstrong-sum (quotient n 10))) ]))] ;; remove the last digit of n by quotient 10 of n - integer division by 10
29+
(= (armstrong-sum n) n))) ;; compare the sum of cube of digits of n, and n
30+
31+
32+
33+
34+
;; test examples
35+
;(equal? (armstrong-Number 0) true)
36+
;(equal? (armstrong-Number 1) true)
37+
;(equal? (armstrong-Number 9) false)
38+
;(equal? (armstrong-Number 153) true)
39+
;(equal? (armstrong-Number 100) false)
40+
;(= 153 (+ (expt 1 3) (expt 5 3) (expt 3 3)))
41+
;

0 commit comments

Comments
 (0)