-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigits.echo
More file actions
63 lines (55 loc) · 1.15 KB
/
Copy pathdigits.echo
File metadata and controls
63 lines (55 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
; Digit tricks — sum of digits, reverse digits, numeric palindrome.
; Run: xo run examples/algos/digits.echo
/ std/io
/ std/str
$ abs = (n) {
? n < 0 {
^ 0 - n
}
^ n
}
$ digit_sum = (n) {
~ x = abs(n)
~ s = 0
* x > 0 {
~ s = s + x % 10
~ x = x / 10
}
^ s
}
$ reverse_digits = (n) {
~ neg = n < 0
~ x = abs(n)
~ r = 0
* x > 0 {
~ r = r * 10 + x % 10
~ x = x / 10
}
? neg {
^ 0 - r
}
^ r
}
$ is_palindrome = (n) {
^ n == reverse_digits(n)
}
; Digital root: repeated digit_sum until a single digit (equiv. n mod 9).
$ digital_root = (n) {
~ x = abs(n)
? x == 0 {
^ 0
}
* x >= 10 {
~ x = digit_sum(x)
}
^ x
}
io.print(str.from_int(digit_sum(12345)))
io.print(str.from_int(digit_sum(0)))
io.print(str.from_int(reverse_digits(1234)))
io.print(str.from_int(reverse_digits(-120)))
io.print(str.from_int(is_palindrome(12321)))
io.print(str.from_int(is_palindrome(1234)))
io.print(str.from_int(digital_root(9999)))
io.print(str.from_int(digital_root(38)))
\ digit_sum, reverse_digits, is_palindrome, digital_root, abs