-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay1.lua
More file actions
56 lines (51 loc) · 1.17 KB
/
Day1.lua
File metadata and controls
56 lines (51 loc) · 1.17 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
-- Write a function called ends_in_3(num) that returns true
-- if the final digit of num is 3, and false otherwise.
function ends_in_3(num)
l = string.len(tostring(num))
return string.sub(num, l, l) == "3"
end
--
-- print(ends_in_3(0))
-- print(ends_in_3(3))
-- print(ends_in_3(123))
-- print(ends_in_3(332))
-- Now, write a similar function called is_prime(num) to test
-- if a number is prime (that is, it’s divisible only by itself and 1).
function is_prime(num)
count = 0
divisor = num
while divisor > 0 and count < 3 do
if num % divisor == 0 then
count = count + 1
divisor = divisor - 1
else
divisor = divisor - 1
end
end
if count == 2 then
return true
else
return false
end
end
-- print(is_prime(3))
-- for i=1, 9 do
-- print(is_prime(i))
-- i = i + 1
-- end
-- Create a program to print the first n prime numbers that end in 3.
function primes_ending_with_3(n)
if ends_in_3(n) then
if is_prime(n) then
return true
else
return false
end
else
return false
end
end
print(primes_ending_with_3(3))
print(primes_ending_with_3(13))
print(primes_ending_with_3(7))
print(primes_ending_with_3(3333))