- Commit
- 0dcbc80f01565bfc3adaff579fd56d3aaae7ab5e
- Parent
- 11684a6cca2bd4dc18173a3f2635dc4878a7c0e5
- Author
- Pablo Escobar Gaviria <gark.garcia@protonmail.com>
- Date
Started writing a Racket implementation.
An exercise on polyglossy: the same problem solved on multiple languages
Started writing a Racket implementation.
4 files changed, 25 insertions, 7 deletions
Status | File Name | N° Changes | Insertions | Deletions |
Modified | .gitignore | 1 | 0 | 1 |
Modified | Elixir/main.ex | 4 | 3 | 1 |
Modified | Haskell/Main.hs | 11 | 6 | 5 |
Added | script.rkt | 16 | 16 | 0 |
diff --git a/.gitignore b/.gitignore @@ -10,6 +10,5 @@ build *.swp *.o *.hi -extra.pl Latex Cuda
diff --git a/Elixir/main.ex b/Elixir/main.ex @@ -12,7 +12,9 @@ defmodule Conjecture do :ets.new(:sums_cache, [:set, :public, :named_table]) # Spawn a new process for each starting index from 0 to `max` - f = fn i -> spawn fn -> counterexpl i, max, n_processes, parent_id end end + f = fn i -> + spawn fn -> counterexpl i, max, n_processes, parent_id end + end Enum.map 0..(n_processes - 1), f listen n_processes
diff --git a/Haskell/Main.hs b/Haskell/Main.hs @@ -26,10 +26,11 @@ main = do head' xs = Just (head xs) -- Calculates the sum of the digits of `n`. -sum' :: Int -> Int -sum' n - | n < 10 = n - | otherwise = (n `mod` 10) + sum' (n `div` 10) +sumDigits :: Int -> Int +sumDigits = sumDigitsTail 0 + where sumDigitsTail acc n + | n == 0 = acc + | otherwise = sumDigitsTail (acc + (n `mod` 10)) (n `div` 10) -- Returns `Just updated` if the if the conjecture holds for pair, where -- `updated` is an updated versions of the sums cache provided by `sums`. @@ -47,7 +48,7 @@ test' sums (a, b) = diff `mod` 9 == 0 counterexempl :: Int -> Bool counterexempl max = all (test' sums) [(a, b) | a <- [0..max], b <- [a..max]] - where sums = generate (2 * max + 1) sum' + where sums = generate (2 * max + 1) sumDigits exitInvalidInput :: IO Int exitInvalidInput = exitWith $ ExitFailure 2
diff --git a/script.rkt b/script.rkt @@ -0,0 +1,16 @@ +(define (counterexempl max) + (foldl (lambda (acc a) (and acc (iter a))) (in-range max))) + +(define (iter a) + (foldl (lambda (acc b) (and acc (test a b))) (in-range a))) + +(define (test a b) + (zero? (mod (- (sum-digits (+ a b)) (sum-digits a) (sum-digits b)) 9))) + +(define (sum-digits n) (sum-digits-tail n 0)) + +(define (sum-digits-tail n acc) + (if (zero? n) + acc + (sum-digits-tail (div n 10) (+ acc (mod n 10))))) +