Skip to content

Commit 9c294a0

Browse files
committed
perfect-numbers: use predicates instead exercism#232
1 parent 92b59ae commit 9c294a0

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

exercises/perfect-numbers/example.jl

+6-13
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,9 @@ function aliquot_sum(n)
99
return sum(i for i in 1:div(n, 2) if n % i == 0)
1010
end
1111

12-
function classify(n)
13-
if !isinteger(n) || n < 1
14-
throw(DomainError(n, "n must be a positive integer"))
15-
end
16-
s = aliquot_sum(n)
17-
if s > n
18-
:abundant
19-
elseif s < n
20-
:deficient
21-
else
22-
:perfect
23-
end
24-
end
12+
isnatural(n) = isinteger(n) && n > zero(n)
13+
assert_natural(n) = isnatural(n) || throw(DomainError(n, "n must be a positive integer"))
14+
15+
isabundant(n) = assert_natural(n) && aliquot_sum(n) > n
16+
isperfect(n) = assert_natural(n) && aliquot_sum(n) == n
17+
isdeficient(n) = assert_natural(n) && aliquot_sum(n) < n

exercises/perfect-numbers/runtests.jl

+17-13
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,67 @@ include("perfect-numbers.jl")
66
@testset "Perfect numbers" begin
77

88
@testset "Smallest perfect number is classified correctly" begin
9-
@test classify(6) == :perfect
9+
@test isperfect(6)
1010
end
1111

1212
@testset "Medium perfect number is classified correctly" begin
13-
@test classify(28) == :perfect
13+
@test isperfect(28)
1414
end
1515

1616
@testset "Large perfect number is classified correctly" begin
17-
@test classify(33550336) == :perfect
17+
@test isperfect(33550336)
1818
end
1919
end
2020

2121
@testset "Abundant numbers" begin
2222

2323
@testset "Smallest abundant number is classified correctly" begin
24-
@test classify(12) == :abundant
24+
@test isabundant(12)
2525
end
2626

2727
@testset "Medium abundant number is classified correctly" begin
28-
@test classify(30) == :abundant
28+
@test isabundant(30)
2929
end
3030

3131
@testset "Large abundant number is classified correctly" begin
32-
@test classify(33550335) == :abundant
32+
@test isabundant(33550335)
3333
end
3434
end
3535

3636
@testset "Deficient numbers" begin
3737

3838
@testset "Smallest prime deficient number is classified correctly" begin
39-
@test classify(2) == :deficient
39+
@test isdeficient(2)
4040
end
4141

4242
@testset "Smallest non-prime deficient number is classified correctly" begin
43-
@test classify(4) == :deficient
43+
@test isdeficient(4)
4444
end
4545

4646
@testset "Medium deficient number is classified correctly" begin
47-
@test classify(32) == :deficient
47+
@test isdeficient(32)
4848
end
4949

5050
@testset "Large deficient number is classified correctly" begin
51-
@test classify(33550337) == :deficient
51+
@test isdeficient(33550337)
5252
end
5353

5454
@testset "Edge case (no factors other than itself) is classified correctly" begin
55-
@test classify(1) == :deficient
55+
@test isdeficient(1)
5656
end
5757
end
5858

5959
@testset "Invalid inputs" begin
6060

6161
@testset "Zero is rejected (not a natural number)" begin
62-
@test_throws DomainError classify(0)
62+
@test_throws DomainError isdeficient(0)
63+
@test_throws DomainError isperfect(0)
64+
@test_throws DomainError isabundant(0)
6365
end
6466

6567
@testset "Negative integer is rejected (not a natural number)" begin
66-
@test_throws DomainError classify(-1)
68+
@test_throws DomainError isdeficient(-1)
69+
@test_throws DomainError isperfect(-1)
70+
@test_throws DomainError isabundant(-1)
6771
end
6872
end

generators/perfect-numbers.jl

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ for i in f["cases"]
3232
global CODE *= """
3333
3434
@testset \"$des\" begin
35-
@test $property($inp) == :$exped
35+
@test is$exped($inp)
3636
end
3737
"""
3838
else
3939
global CODE *= """
4040
4141
@testset \"$des\" begin
42-
@test_throws DomainError classify($inp)
42+
@test_throws DomainError isdeficient($inp)
43+
@test_throws DomainError isperfect($inp)
44+
@test_throws DomainError isabundant($inp)
4345
end
4446
"""
4547
end

0 commit comments

Comments
 (0)