-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
116 lines (99 loc) · 2.77 KB
/
main.lua
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
mode = 0 -- 0=default, 1=input, 2=result
number = 0
number_is_prime = false
function is_prime(n)
if n <= 3 then
return n > 1
end
if n % 2 == 0 or n % 3 == 0 then
return false
end
local i = 5
while i ^ 2 <= n do
if n % i == 0 or n % (i + 2) == 0 then
return false
end
i = i + 6
end
return true
end
function on.enterKey()
-- switch to input mode
if mode == 0 or mode == 2 then
mode = 1
number = 0
platform.window:invalidate()
return
end
-- calculate
number_is_prime = is_prime(number)
mode = 2
platform.window:invalidate()
end
function on.backspaceKey()
-- delete last number
if mode == 1 then
number = (number - (number % 10)) / 10
platform.window:invalidate()
return
end
-- reset
mode = 1
number = 0
platform.window:invalidate()
return
end
function appendDigit(n, d)
return (10 * n) + d
end
function on.charIn(char)
if mode ~= 1 then return end
if char == '0' then number = appendDigit(number, 0) end
if char == '1' then number = appendDigit(number, 1) end
if char == '2' then number = appendDigit(number, 2) end
if char == '3' then number = appendDigit(number, 3) end
if char == '4' then number = appendDigit(number, 4) end
if char == '5' then number = appendDigit(number, 5) end
if char == '6' then number = appendDigit(number, 6) end
if char == '7' then number = appendDigit(number, 7) end
if char == '8' then number = appendDigit(number, 8) end
if char == '9' then number = appendDigit(number, 9) end
platform.window:invalidate()
end
function on.paint(gc)
-- static stuff
drawHeading(gc)
drawLink(gc)
resetFont(gc)
-- status
if mode == 0 then
gc:drawString("press enter to input the number", 0, 20, "top")
elseif mode == 1 then
setColor("blue", gc)
gc:drawString("press enter to check your number", 0, 20, "top")
elseif mode == 2 then
setColor("green", gc)
gc:drawString("press enter to check another number", 0, 20, "top")
end
resetFont(gc)
-- the number itself
local to_draw = "_"
if number ~= 0 then
to_draw = tostring(number)
if mode == 1 then to_draw = to_draw .. "_" end
end
gc:drawString(to_draw, 0, 40, "top")
-- result
if mode == 2 then
if number == 0 or number == 1 then
setColor("red", gc)
gc:drawString("number is zero/one", 0, 60, "top")
elseif number_is_prime then
setColor("green", gc)
gc:drawString("number IS prime", 0, 60, "top")
else
setColor("red", gc)
gc:drawString("number is NOT prime", 0, 60, "top")
end
end
end