diff options
| author | Archargelod <archargelod@gmail.com> | 2024-02-21 01:33:32 +0800 |
|---|---|---|
| committer | Archargelod <archargelod@gmail.com> | 2024-02-21 01:33:32 +0800 |
| commit | 77f7f91c73773aa6cfd2713dea32c705a1971b58 (patch) | |
| tree | 368b987ea819a1083930390e2cef19151827c4d6 /challenge-006 | |
| parent | ddd42e0db3017a5ec3108d09efba477f19e7f04b (diff) | |
| download | perlweeklychallenge-club-77f7f91c73773aa6cfd2713dea32c705a1971b58.tar.gz perlweeklychallenge-club-77f7f91c73773aa6cfd2713dea32c705a1971b58.tar.bz2 perlweeklychallenge-club-77f7f91c73773aa6cfd2713dea32c705a1971b58.zip | |
Weeks 5-13, 257 in Nim
Diffstat (limited to 'challenge-006')
| -rw-r--r-- | challenge-006/archargelod/README | 1 | ||||
| -rwxr-xr-x | challenge-006/archargelod/nim/ch_1.nim | 33 | ||||
| -rwxr-xr-x | challenge-006/archargelod/nim/ch_2.nim | 11 |
3 files changed, 45 insertions, 0 deletions
diff --git a/challenge-006/archargelod/README b/challenge-006/archargelod/README new file mode 100644 index 0000000000..6cd57e1074 --- /dev/null +++ b/challenge-006/archargelod/README @@ -0,0 +1 @@ +Solution by archargelod diff --git a/challenge-006/archargelod/nim/ch_1.nim b/challenge-006/archargelod/nim/ch_1.nim new file mode 100755 index 0000000000..0e1c252ea3 --- /dev/null +++ b/challenge-006/archargelod/nim/ch_1.nim @@ -0,0 +1,33 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off + +type + NumberRange = tuple + val, l: int + +func `$`(s: NumberRange): string = + if s.l == 0: + $s.val + elif s.l == 1: + $s.val & ',' & $(s.val + s.l) + else: + $s.val & '-' & $(s.val + s.l) + +func compactNumString*(numbers: openarray[int]): string = + if numbers.len < 1: return result + + var range: NumberRange = (numbers[0], 0) + for i, num in numbers[1..^1]: + if num == range.val + range.l + 1: + inc range.l + else: + result &= $range & ',' + range = (num, 0) + + result &= $range + +when isMainModule: + import std/unittest + + suite "Compact number lists": + test "Example 1": + check compactNumString([1,2,3,4,9,10,14,15,16]) == "1-4,9,10,14-16" diff --git a/challenge-006/archargelod/nim/ch_2.nim b/challenge-006/archargelod/nim/ch_2.nim new file mode 100755 index 0000000000..7f788da601 --- /dev/null +++ b/challenge-006/archargelod/nim/ch_2.nim @@ -0,0 +1,11 @@ +#!/usr/bin/env -S nim r -d:release --verbosity:0 --hints:off +import pkg/mapm # `$ nimble install mapm` + +let approxR* = pow(640_320'M, 3) + 744'M - divide(75'M, 100_000_000_000_000'M, 16) + +when isMainModule: + import std/unittest + + suite "Ramanujan's constant": + test "32-digit approximation": + check $approxR == "262537412640768743.99999999999925" |
