Commit bf92f6ce942baa417c2e65f2a699ad56ec1f8e4a
1 parent
1be798c1
added new functions in library/tools
Showing
2 changed files
with
53 additions
and
0 deletions
Show diff stats
anubis_dev/library/tools/int.anubis
| ... | ... | @@ -63,6 +63,11 @@ public define Int Int x ^ Int y. raises x to the p |
| 63 | 63 | public define Bool odd (Int x). |
| 64 | 64 | public define Bool even (Int x). |
| 65 | 65 | |
| 66 | + *** (7) Greatest common divisor, ... | |
| 67 | + | |
| 68 | +public define Int gcd (Int x, Int y). | |
| 69 | +public define Int lcm (Int x, Int y). least common multiple | |
| 70 | + | |
| 66 | 71 | |
| 67 | 72 | --- That's all for the public part ! ----------------------------------------------- |
| 68 | 73 | |
| ... | ... | @@ -226,6 +231,38 @@ public define Bool |
| 226 | 231 | }. |
| 227 | 232 | |
| 228 | 233 | |
| 234 | +public define Int | |
| 235 | + gcd | |
| 236 | + ( | |
| 237 | + Int x, | |
| 238 | + Int y | |
| 239 | + ) = | |
| 240 | + /* Euclid's algorithm */ | |
| 241 | + if x > y then gcd(y,x) else | |
| 242 | + if x = 0 then abs(y) else | |
| 243 | + if x < 0 then gcd(-x,y) else | |
| 244 | + if y/x is | |
| 245 | + { | |
| 246 | + failure then should_not_happen(0), | |
| 247 | + success(p) then if p is (q,r) then gcd(r,x) | |
| 248 | + }. | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | +public define Int | |
| 253 | + lcm | |
| 254 | + ( | |
| 255 | + Int x, | |
| 256 | + Int y | |
| 257 | + ) = | |
| 258 | + with d = gcd(x,y), | |
| 259 | + if d = 0 | |
| 260 | + then 0 | |
| 261 | + else if (x*y)/d is | |
| 262 | + { | |
| 263 | + failure then should_not_happen(0), | |
| 264 | + success(p) then if p is (q,_) then q | |
| 265 | + }. | |
| 229 | 266 | |
| 230 | 267 | |
| 231 | 268 | ... | ... |
anubis_dev/library/tools/list.anubis
| ... | ... | @@ -910,3 +910,19 @@ public define (List($T1), List($T2)) unzip(List(($T1, $T2)) l) = unzip(reverse(l |
| 910 | 910 | |
| 911 | 911 | |
| 912 | 912 | |
| 913 | + Compute the list of all sublists of a list (beware: for a list of length n, | |
| 914 | + this gives a list of length 2^n). | |
| 915 | + | |
| 916 | +public define List(List($T)) | |
| 917 | + sublists | |
| 918 | + ( | |
| 919 | + List($T) l | |
| 920 | + ) = | |
| 921 | + if l is | |
| 922 | + { | |
| 923 | + [ ] then [[ ]], | |
| 924 | + [h . t] then with p = sublists(t), | |
| 925 | + map((List($T) u) |-> [h . u],p) + p | |
| 926 | + }. | |
| 927 | + | |
| 928 | + | ... | ... |