-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgematriya.go
119 lines (115 loc) · 1.99 KB
/
gematriya.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Package gematriya provides a system of writing numbers as Hebrew letters.
// Numerical values are represented using letters of the Hebrew alef-bet
// (alphabet).
package gematriya
const geresh = "׳"
const gershayim = "״"
func num2heb(num int) string {
switch num {
case 1:
return "א"
case 2:
return "ב"
case 3:
return "ג"
case 4:
return "ד"
case 5:
return "ה"
case 6:
return "ו"
case 7:
return "ז"
case 8:
return "ח"
case 9:
return "ט"
case 10:
return "י"
case 20:
return "כ"
case 30:
return "ל"
case 40:
return "מ"
case 50:
return "נ"
case 60:
return "ס"
case 70:
return "ע"
case 80:
return "פ"
case 90:
return "צ"
case 100:
return "ק"
case 200:
return "ר"
case 300:
return "ש"
case 400:
return "ת"
case 500:
return "תק"
case 600:
return "תר"
case 700:
return "תש"
case 800:
return "תת"
case 900:
return "תתק"
case 1000:
return "תתר"
default:
return "*INVALID*"
}
}
func num2digits(number int) []int {
digits := make([]int, 0, 10)
num := number
for num > 0 {
if num == 15 || num == 16 {
digits = append(digits, 9)
digits = append(digits, num-9)
break
}
incr := 100
var i int
for i = 400; i > num; i -= incr {
if i == incr {
incr = incr / 10
}
}
digits = append(digits, i)
num -= i
}
return digits
}
// Gematriya converts a numerical value to a string of Hebrew letters.
//
// When specifying years of the Hebrew calendar in the present millennium,
// we omit the thousands (which is presently 5 [ה]).
func Gematriya(number int) string {
thousands := number / 1000
str := ""
if thousands > 0 && thousands != 5 {
tdigits := num2digits(thousands)
for _, digit := range tdigits {
str += num2heb(digit)
}
str += geresh
}
digits := num2digits(number % 1000)
if len(digits) == 1 {
return str + num2heb(digits[0]) + geresh
}
for idx, digit := range digits {
if idx+1 == len(digits) {
str += gershayim
}
str += num2heb(digit)
}
return str
}