diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f38c24f..b3fef33 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,8 +1,9 @@ name: tasks # Births omc from the 408-byte seed at the aien-dev/omega-mv commit in omega.ref on a GitHub-hosted # ubuntu-24.04-arm runner, compiles and runs every task and exercise, and runs negative controls -# that must fail: a mutated expected.txt, a mutated task source, every exercise stub, and a wrong -# exercise solution. +# that must fail: a mutated expected.txt, a mutated task source, every exercise stub, a wrong +# exercise solution, and every task's expected.txt mutated at once (each task and each oracle +# comparison must catch its own). on: push: branches: [main] @@ -88,6 +89,23 @@ jobs: [ "$rc" = 1 ] || { echo "wrong solution was not caught: rc=$rc"; exit 1; } grep -qx 'EXERCISE FAIL leap failed=3' "$RUNNER_TEMP/wrong.log" echo "negative control 4 failed as required (rc=$rc)" + - name: Negative control 5, every expected.txt mutated (every task and every oracle check must FAIL) + run: | + set -u + t="$RUNNER_TEMP/neg5"; cp -r tasks "$t" + total=0 + for d in "$t"/*/; do + n=$(basename "$d") + printf 'omega-rosetta negative control\n' >> "$d/expected.txt" + if python3 "tasks/$n/oracle.py" | cmp -s - "$d/expected.txt"; then echo "oracle check missed mutated $n"; exit 1; fi + total=$((total + 1)) + done + rc=0; sh run.sh --omc build/omega-mv/seed/out/omc --tasks "$t" > "$RUNNER_TEMP/neg5.log" || rc=$? + cat "$RUNNER_TEMP/neg5.log" + [ "$rc" = 1 ] || { echo "mutated expected.txt files were not caught: rc=$rc"; exit 1; } + [ "$(grep -c '^FAIL .* stdout differs from expected.txt$' "$RUNNER_TEMP/neg5.log")" = "$total" ] + grep -qx "TASKS PASS 0/$total" "$RUNNER_TEMP/neg5.log" + echo "negative control 5: all $total mutated tasks failed as required (rc=$rc)" - name: Keep the logs if: always() uses: actions/upload-artifact@v4 diff --git a/.gitignore b/.gitignore index 567609b..0af7b45 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build/ +dataset/omega-rosetta.parquet diff --git a/.goals.json b/.goals.json index e68b2a4..d4f2f8f 100644 --- a/.goals.json +++ b/.goals.json @@ -4,7 +4,7 @@ { "id": "goal-corpus", "title": "Rosetta corpus checked in CI", - "description": "24 Rosetta Code tasks and 5 exercises compiled by the pinned seeded omc, checked against independent oracles on ubuntu-24.04-arm, with negative controls.", + "description": "216 Rosetta Code tasks and 47 exercises compiled by the pinned seeded omc, checked against independent oracles on ubuntu-24.04-arm, with negative controls.", "status": "Active", "milestones": [ { diff --git a/NOTICE b/NOTICE index 5697655..a3b9f35 100644 --- a/NOTICE +++ b/NOTICE @@ -26,3 +26,29 @@ resolves the GPLv2 patent/indemnity conflict described in the exception text. THIRD-PARTY DATA: tasks/sha-256/sha-256.om carries the FIPS 180-4 SHA-256 round constants and initial hash values, copied from aien-dev/omega-mv om/sha256/sha256-om2.om (Apache-2.0). + +The test cases in each exercises//test.om that names exercism/problem-specifications +(https://github.com/exercism/problem-specifications, canonical-data.json at commit +650bebeb39ad40439175288042aece982b0a90a9) are taken from it, under the MIT License: + + The MIT License (MIT) + + Copyright (c) 2014, 2019, 2021 Exercism + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. diff --git a/README.md b/README.md index eb3b3d8..f79bc8b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # omega-rosetta -24 classic [Rosetta Code](https://rosettacode.org) tasks and 5 Exercism-style exercises, written in +216 classic [Rosetta Code](https://rosettacode.org) tasks and 47 Exercism-style exercises, written in Omega's source language, Omegese, and compiled by `omc`, the compiler that [aien-dev/omega-mv](https://github.com/aien-dev/omega-mv) rebuilds from a 408-byte hand-written seed. I wrote this so you can read real Omega next to the languages you already know, run every program @@ -32,9 +32,10 @@ runs under an emulator such as qemu-user (`qemu-aarch64`); I have not run this r | Check | Level | Where | | --- | --- | --- | | Tasks pass on a GitHub `ubuntu-24.04-arm` runner, compiler born from the seed | EXERCISED | [tasks workflow](.github/workflows/ci.yml) | -| Negative controls (mutated answer, mutated source, unsolved stubs, wrong solution) fail | EXERCISED | same workflow | +| Negative controls (mutated answer, mutated source, unsolved stubs, wrong solution, every answer mutated at once) fail | EXERCISED | same workflow | | Every `expected.txt` is reproduced by its `oracle.py` | EXERCISED | same workflow | | Code posted to rosettacode.org | PROPOSED | [`drafts/rosettacode/`](drafts/rosettacode/) | +| Hugging Face dataset of the corpus | PROPOSED, built locally only | [`dataset/`](dataset/) | This is a host CPU check of compiled programs on Linux AArch64. It says nothing about speed, and it is not a claim that `omc` is correct beyond these programs. @@ -48,40 +49,239 @@ helpers because Omega has no imports (see the notes below). | Program | Rosetta Code task | Lines | Oracle uses | | --- | --- | ---: | --- | +| [`100-doors`](tasks/100-doors/100-doors.om) | [100 doors](https://rosettacode.org/wiki/100_doors) | 50 | plain Python | | [`99-bottles-of-beer`](tasks/99-bottles-of-beer/99-bottles-of-beer.om) | [99 bottles of beer](https://rosettacode.org/wiki/99_bottles_of_beer) | 60 | plain Python | +| [`abc-problem`](tasks/abc-problem/abc-problem.om) | [ABC problem](https://rosettacode.org/wiki/ABC_problem) | 72 | plain Python | +| [`abelian-sandpile-model`](tasks/abelian-sandpile-model/abelian-sandpile-model.om) | [Abelian sandpile model](https://rosettacode.org/wiki/Abelian_sandpile_model) | 88 | plain Python | +| [`abundant-deficient-and-perfect-number-classifications`](tasks/abundant-deficient-and-perfect-number-classifications/abundant-deficient-and-perfect-number-classifications.om) | [Abundant, deficient and perfect number classifications](https://rosettacode.org/wiki/Abundant,_deficient_and_perfect_number_classifications) | 70 | `math` | | [`ackermann-function`](tasks/ackermann-function/ackermann-function.om) | [Ackermann function](https://rosettacode.org/wiki/Ackermann_function) | 52 | `sys` | +| [`additive-primes`](tasks/additive-primes/additive-primes.om) | [Additive primes](https://rosettacode.org/wiki/Additive_primes) | 87 | plain Python | +| [`almost-prime`](tasks/almost-prime/almost-prime.om) | [Almost prime](https://rosettacode.org/wiki/Almost_prime) | 66 | plain Python | +| [`amicable-pairs`](tasks/amicable-pairs/amicable-pairs.om) | [Amicable pairs](https://rosettacode.org/wiki/Amicable_pairs) | 54 | `math` | +| [`anagrams`](tasks/anagrams/anagrams.om) | [Anagrams](https://rosettacode.org/wiki/Anagrams) | 112 | plain Python | +| [`anti-primes`](tasks/anti-primes/anti-primes.om) | [Anti-primes](https://rosettacode.org/wiki/Anti-primes) | 60 | plain Python | +| [`array-concatenation`](tasks/array-concatenation/array-concatenation.om) | [Array concatenation](https://rosettacode.org/wiki/Array_concatenation) | 56 | plain Python | +| [`attractive-numbers`](tasks/attractive-numbers/attractive-numbers.om) | [Attractive numbers](https://rosettacode.org/wiki/Attractive_numbers) | 78 | plain Python | +| [`bacon-cipher`](tasks/bacon-cipher/bacon-cipher.om) | [Bacon cipher](https://rosettacode.org/wiki/Bacon_cipher) | 78 | plain Python | +| [`base64-encode-data`](tasks/base64-encode-data/base64-encode-data.om) | [Base64 encode data](https://rosettacode.org/wiki/Base64_encode_data) | 55 | `base64` | +| [`bead-sort`](tasks/bead-sort/bead-sort.om) | [Sorting algorithms/Bead sort](https://rosettacode.org/wiki/Sorting_algorithms/Bead_sort) | 109 | plain Python | +| [`bell-numbers`](tasks/bell-numbers/bell-numbers.om) | [Bell numbers](https://rosettacode.org/wiki/Bell_numbers) | 104 | `math` | | [`binary-digits`](tasks/binary-digits/binary-digits.om) | [Binary digits](https://rosettacode.org/wiki/Binary_digits) | 35 | plain Python | | [`binary-search`](tasks/binary-search/binary-search.om) | [Binary search](https://rosettacode.org/wiki/Binary_search) | 92 | `bisect` | +| [`bogosort`](tasks/bogosort/bogosort.om) | [Sorting algorithms/Bogosort](https://rosettacode.org/wiki/Sorting_algorithms/Bogosort) | 119 | plain Python | | [`bubble-sort`](tasks/bubble-sort/bubble-sort.om) | [Sorting algorithms/Bubble sort](https://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort) | 78 | plain Python | +| [`caesar-cipher`](tasks/caesar-cipher/caesar-cipher.om) | [Caesar cipher](https://rosettacode.org/wiki/Caesar_cipher) | 38 | plain Python | +| [`camel-case-and-snake-case`](tasks/camel-case-and-snake-case/camel-case-and-snake-case.om) | [Camel case and snake case](https://rosettacode.org/wiki/Camel_case_and_snake_case) | 106 | plain Python | +| [`catalan-numbers`](tasks/catalan-numbers/catalan-numbers.om) | [Catalan numbers](https://rosettacode.org/wiki/Catalan_numbers) | 55 | `math` | +| [`character-codes`](tasks/character-codes/character-codes.om) | [Character codes](https://rosettacode.org/wiki/Character_codes) | 44 | plain Python | +| [`cocktail-sort`](tasks/cocktail-sort/cocktail-sort.om) | [Sorting algorithms/Cocktail sort](https://rosettacode.org/wiki/Sorting_algorithms/Cocktail_sort) | 114 | plain Python | +| [`cocktail-sort-with-shifting-bounds`](tasks/cocktail-sort-with-shifting-bounds/cocktail-sort-with-shifting-bounds.om) | [Sorting algorithms/Cocktail sort with shifting bounds](https://rosettacode.org/wiki/Sorting_algorithms/Cocktail_sort_with_shifting_bounds) | 114 | plain Python | +| [`comb-sort`](tasks/comb-sort/comb-sort.om) | [Sorting algorithms/Comb sort](https://rosettacode.org/wiki/Sorting_algorithms/Comb_sort) | 103 | plain Python | +| [`combinations`](tasks/combinations/combinations.om) | [Combinations](https://rosettacode.org/wiki/Combinations) | 70 | plain Python | +| [`combinations-with-repetitions`](tasks/combinations-with-repetitions/combinations-with-repetitions.om) | [Combinations with repetitions](https://rosettacode.org/wiki/Combinations_with_repetitions) | 82 | `itertools`, `math` | +| [`comma-quibbling`](tasks/comma-quibbling/comma-quibbling.om) | [Comma quibbling](https://rosettacode.org/wiki/Comma_quibbling) | 59 | plain Python | +| [`conways-game-of-life`](tasks/conways-game-of-life/conways-game-of-life.om) | [Conway's Game of Life](https://rosettacode.org/wiki/Conway's_Game_of_Life) | 118 | `collections` | +| [`copy-a-string`](tasks/copy-a-string/copy-a-string.om) | [Copy a string](https://rosettacode.org/wiki/Copy_a_string) | 33 | plain Python | +| [`count-how-many-vowels-and-consonants-occur-in-a-string`](tasks/count-how-many-vowels-and-consonants-occur-in-a-string/count-how-many-vowels-and-consonants-occur-in-a-string.om) | [Count how many vowels and consonants occur in a string](https://rosettacode.org/wiki/Count_how_many_vowels_and_consonants_occur_in_a_string) | 77 | plain Python | +| [`count-in-factors`](tasks/count-in-factors/count-in-factors.om) | [Count in factors](https://rosettacode.org/wiki/Count_in_factors) | 70 | plain Python | +| [`count-occurrences-of-a-substring`](tasks/count-occurrences-of-a-substring/count-occurrences-of-a-substring.om) | [Count occurrences of a substring](https://rosettacode.org/wiki/Count_occurrences_of_a_substring) | 56 | plain Python | +| [`count-the-coins`](tasks/count-the-coins/count-the-coins.om) | [Count the coins](https://rosettacode.org/wiki/Count_the_coins) | 64 | plain Python | +| [`counting-sort`](tasks/counting-sort/counting-sort.om) | [Sorting algorithms/Counting sort](https://rosettacode.org/wiki/Sorting_algorithms/Counting_sort) | 108 | plain Python | | [`crc-32`](tasks/crc-32/crc-32.om) | [CRC-32](https://rosettacode.org/wiki/CRC-32) | 46 | `zlib` | +| [`cuban-primes`](tasks/cuban-primes/cuban-primes.om) | [Cuban primes](https://rosettacode.org/wiki/Cuban_primes) | 129 | plain Python | +| [`cycle-sort`](tasks/cycle-sort/cycle-sort.om) | [Sorting algorithms/Cycle sort](https://rosettacode.org/wiki/Sorting_algorithms/Cycle_sort) | 125 | plain Python | +| [`determinant-and-permanent`](tasks/determinant-and-permanent/determinant-and-permanent.om) | [Determinant and permanent](https://rosettacode.org/wiki/Determinant_and_permanent) | 194 | `fractions` | +| [`determine-if-a-string-has-all-the-same-characters`](tasks/determine-if-a-string-has-all-the-same-characters/determine-if-a-string-has-all-the-same-characters.om) | [Determine if a string has all the same characters](https://rosettacode.org/wiki/Determine_if_a_string_has_all_the_same_characters) | 71 | plain Python | +| [`determine-if-a-string-has-all-unique-characters`](tasks/determine-if-a-string-has-all-unique-characters/determine-if-a-string-has-all-unique-characters.om) | [Determine if a string has all unique characters](https://rosettacode.org/wiki/Determine_if_a_string_has_all_unique_characters) | 73 | plain Python | +| [`determine-if-a-string-is-collapsible`](tasks/determine-if-a-string-is-collapsible/determine-if-a-string-is-collapsible.om) | [Determine if a string is collapsible](https://rosettacode.org/wiki/Determine_if_a_string_is_collapsible) | 66 | `itertools` | +| [`determine-if-a-string-is-numeric`](tasks/determine-if-a-string-is-numeric/determine-if-a-string-is-numeric.om) | [Determine if a string is numeric](https://rosettacode.org/wiki/Determine_if_a_string_is_numeric) | 104 | plain Python | +| [`determine-if-a-string-is-squeezable`](tasks/determine-if-a-string-is-squeezable/determine-if-a-string-is-squeezable.om) | [Determine if a string is squeezable](https://rosettacode.org/wiki/Determine_if_a_string_is_squeezable) | 78 | `re` | +| [`digital-root`](tasks/digital-root/digital-root.om) | [Digital root](https://rosettacode.org/wiki/Digital_root) | 62 | plain Python | +| [`dijkstras-algorithm`](tasks/dijkstras-algorithm/dijkstras-algorithm.om) | [Dijkstra's algorithm](https://rosettacode.org/wiki/Dijkstra's_algorithm) | 133 | plain Python | +| [`dutch-national-flag-problem`](tasks/dutch-national-flag-problem/dutch-national-flag-problem.om) | [Dutch national flag problem](https://rosettacode.org/wiki/Dutch_national_flag_problem) | 121 | plain Python | +| [`elementary-cellular-automaton`](tasks/elementary-cellular-automaton/elementary-cellular-automaton.om) | [Elementary cellular automaton](https://rosettacode.org/wiki/Elementary_cellular_automaton) | 76 | plain Python | +| [`emirp-primes`](tasks/emirp-primes/emirp-primes.om) | [Emirp primes](https://rosettacode.org/wiki/Emirp_primes) | 102 | plain Python | +| [`empty-string`](tasks/empty-string/empty-string.om) | [Empty string](https://rosettacode.org/wiki/Empty_string) | 40 | plain Python | +| [`equilibrium-index`](tasks/equilibrium-index/equilibrium-index.om) | [Equilibrium index](https://rosettacode.org/wiki/Equilibrium_index) | 113 | plain Python | +| [`ethiopian-multiplication`](tasks/ethiopian-multiplication/ethiopian-multiplication.om) | [Ethiopian multiplication](https://rosettacode.org/wiki/Ethiopian_multiplication) | 69 | plain Python | +| [`evaluate-binomial-coefficients`](tasks/evaluate-binomial-coefficients/evaluate-binomial-coefficients.om) | [Evaluate binomial coefficients](https://rosettacode.org/wiki/Evaluate_binomial_coefficients) | 66 | `math` | +| [`extensible-prime-generator`](tasks/extensible-prime-generator/extensible-prime-generator.om) | [Extensible prime generator](https://rosettacode.org/wiki/Extensible_prime_generator) | 96 | `itertools` | | [`factorial`](tasks/factorial/factorial.om) | [Factorial](https://rosettacode.org/wiki/Factorial) | 42 | `math` | +| [`factors-of-an-integer`](tasks/factors-of-an-integer/factors-of-an-integer.om) | [Factors of an integer](https://rosettacode.org/wiki/Factors_of_an_integer) | 66 | plain Python | +| [`fibonacci-n-step-number-sequences`](tasks/fibonacci-n-step-number-sequences/fibonacci-n-step-number-sequences.om) | [Fibonacci n-step number sequences](https://rosettacode.org/wiki/Fibonacci_n-step_number_sequences) | 82 | plain Python | | [`fibonacci-sequence`](tasks/fibonacci-sequence/fibonacci-sequence.om) | [Fibonacci sequence](https://rosettacode.org/wiki/Fibonacci_sequence) | 43 | plain Python | | [`fizzbuzz`](tasks/fizzbuzz/fizzbuzz.om) | [FizzBuzz](https://rosettacode.org/wiki/FizzBuzz) | 49 | plain Python | +| [`floyd-warshall-algorithm`](tasks/floyd-warshall-algorithm/floyd-warshall-algorithm.om) | [Floyd-Warshall algorithm](https://rosettacode.org/wiki/Floyd-Warshall_algorithm) | 125 | plain Python | +| [`floyds-triangle`](tasks/floyds-triangle/floyds-triangle.om) | [Floyd's triangle](https://rosettacode.org/wiki/Floyd's_triangle) | 74 | plain Python | +| [`fusc-sequence`](tasks/fusc-sequence/fusc-sequence.om) | [Fusc sequence](https://rosettacode.org/wiki/Fusc_sequence) | 73 | `functools` | +| [`generate-lower-case-ascii-alphabet`](tasks/generate-lower-case-ascii-alphabet/generate-lower-case-ascii-alphabet.om) | [Generate lower case ASCII alphabet](https://rosettacode.org/wiki/Generate_lower_case_ASCII_alphabet) | 21 | `string` | +| [`gnome-sort`](tasks/gnome-sort/gnome-sort.om) | [Sorting algorithms/Gnome sort](https://rosettacode.org/wiki/Sorting_algorithms/Gnome_sort) | 101 | plain Python | +| [`gray-code`](tasks/gray-code/gray-code.om) | [Gray code](https://rosettacode.org/wiki/Gray_code) | 86 | plain Python | | [`greatest-common-divisor`](tasks/greatest-common-divisor/greatest-common-divisor.om) | [Greatest common divisor](https://rosettacode.org/wiki/Greatest_common_divisor) | 57 | `math` | +| [`greatest-subsequential-sum`](tasks/greatest-subsequential-sum/greatest-subsequential-sum.om) | [Greatest subsequential sum](https://rosettacode.org/wiki/Greatest_subsequential_sum) | 127 | plain Python | | [`hailstone-sequence`](tasks/hailstone-sequence/hailstone-sequence.om) | [Hailstone sequence](https://rosettacode.org/wiki/Hailstone_sequence) | 81 | plain Python | +| [`hamming-numbers`](tasks/hamming-numbers/hamming-numbers.om) | [Hamming numbers](https://rosettacode.org/wiki/Hamming_numbers) | 78 | `heapq` | +| [`happy-numbers`](tasks/happy-numbers/happy-numbers.om) | [Happy numbers](https://rosettacode.org/wiki/Happy_numbers) | 62 | plain Python | +| [`harshad-or-niven-series`](tasks/harshad-or-niven-series/harshad-or-niven-series.om) | [Harshad or Niven series](https://rosettacode.org/wiki/Harshad_or_Niven_series) | 60 | `itertools` | +| [`heapsort`](tasks/heapsort/heapsort.om) | [Sorting algorithms/Heapsort](https://rosettacode.org/wiki/Sorting_algorithms/Heapsort) | 117 | plain Python | | [`hello-world`](tasks/hello-world/hello-world.om) | [Hello world/Text](https://rosettacode.org/wiki/Hello_world/Text) | 16 | plain Python | +| [`hello-world-newline-omission`](tasks/hello-world-newline-omission/hello-world-newline-omission.om) | [Hello world/Newline omission](https://rosettacode.org/wiki/Hello_world/Newline_omission) | 16 | plain Python | +| [`hofstadter-figure-figure-sequences`](tasks/hofstadter-figure-figure-sequences/hofstadter-figure-figure-sequences.om) | [Hofstadter Figure-Figure sequences](https://rosettacode.org/wiki/Hofstadter_Figure-Figure_sequences) | 95 | `itertools` | +| [`hofstadter-q-sequence`](tasks/hofstadter-q-sequence/hofstadter-q-sequence.om) | [Hofstadter Q sequence](https://rosettacode.org/wiki/Hofstadter_Q_sequence) | 61 | plain Python | +| [`identity-matrix`](tasks/identity-matrix/identity-matrix.om) | [Identity matrix](https://rosettacode.org/wiki/Identity_matrix) | 51 | plain Python | | [`insertion-sort`](tasks/insertion-sort/insertion-sort.om) | [Sorting algorithms/Insertion sort](https://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort) | 76 | plain Python | +| [`isbn13-check-digit`](tasks/isbn13-check-digit/isbn13-check-digit.om) | [ISBN13 check digit](https://rosettacode.org/wiki/ISBN13_check_digit) | 49 | plain Python | +| [`isqrt-integer-square-root-of-x`](tasks/isqrt-integer-square-root-of-x/isqrt-integer-square-root-of-x.om) | [Isqrt (integer square root) of X](https://rosettacode.org/wiki/Isqrt_(integer_square_root)_of_X) | 68 | `math` | +| [`jewels-and-stones`](tasks/jewels-and-stones/jewels-and-stones.om) | [Jewels and stones](https://rosettacode.org/wiki/Jewels_and_stones) | 56 | plain Python | +| [`josephus-problem`](tasks/josephus-problem/josephus-problem.om) | [Josephus problem](https://rosettacode.org/wiki/Josephus_problem) | 61 | plain Python | +| [`kaprekar-numbers`](tasks/kaprekar-numbers/kaprekar-numbers.om) | [Kaprekar numbers](https://rosettacode.org/wiki/Kaprekar_numbers) | 65 | plain Python | +| [`knapsack-problem-0-1`](tasks/knapsack-problem-0-1/knapsack-problem-0-1.om) | [Knapsack problem/0-1](https://rosettacode.org/wiki/Knapsack_problem/0-1) | 131 | `itertools` | +| [`knapsack-problem-unbounded`](tasks/knapsack-problem-unbounded/knapsack-problem-unbounded.om) | [Knapsack problem/Unbounded](https://rosettacode.org/wiki/Knapsack_problem/Unbounded) | 80 | `fractions`, `itertools` | +| [`knights-tour`](tasks/knights-tour/knights-tour.om) | [Knight's tour](https://rosettacode.org/wiki/Knight's_tour) | 125 | plain Python | +| [`kronecker-product`](tasks/kronecker-product/kronecker-product.om) | [Kronecker product](https://rosettacode.org/wiki/Kronecker_product) | 137 | plain Python | +| [`langtons-ant`](tasks/langtons-ant/langtons-ant.om) | [Langton's ant](https://rosettacode.org/wiki/Langton's_ant) | 130 | plain Python | | [`leap-year`](tasks/leap-year/leap-year.om) | [Leap year](https://rosettacode.org/wiki/Leap_year) | 54 | `calendar` | +| [`least-common-multiple`](tasks/least-common-multiple/least-common-multiple.om) | [Least common multiple](https://rosettacode.org/wiki/Least_common_multiple) | 78 | `functools`, `math` | +| [`letter-frequency`](tasks/letter-frequency/letter-frequency.om) | [Letter frequency](https://rosettacode.org/wiki/Letter_frequency) | 52 | `collections`, `string` | +| [`levenshtein-distance`](tasks/levenshtein-distance/levenshtein-distance.om) | [Levenshtein distance](https://rosettacode.org/wiki/Levenshtein_distance) | 76 | plain Python | +| [`longest-common-prefix`](tasks/longest-common-prefix/longest-common-prefix.om) | [Longest common prefix](https://rosettacode.org/wiki/Longest_common_prefix) | 80 | `os.path` | +| [`longest-common-subsequence`](tasks/longest-common-subsequence/longest-common-subsequence.om) | [Longest common subsequence](https://rosettacode.org/wiki/Longest_common_subsequence) | 72 | plain Python | +| [`longest-increasing-subsequence`](tasks/longest-increasing-subsequence/longest-increasing-subsequence.om) | [Longest increasing subsequence](https://rosettacode.org/wiki/Longest_increasing_subsequence) | 137 | `itertools` | +| [`look-and-say-sequence`](tasks/look-and-say-sequence/look-and-say-sequence.om) | [Look-and-say sequence](https://rosettacode.org/wiki/Look-and-say_sequence) | 63 | `itertools` | +| [`loops-break`](tasks/loops-break/loops-break.om) | [Loops/Break](https://rosettacode.org/wiki/Loops/Break) | 42 | plain Python | +| [`loops-continue`](tasks/loops-continue/loops-continue.om) | [Loops/Continue](https://rosettacode.org/wiki/Loops/Continue) | 39 | plain Python | +| [`loops-do-while`](tasks/loops-do-while/loops-do-while.om) | [Loops/Do-while](https://rosettacode.org/wiki/Loops/Do-while) | 38 | plain Python | +| [`loops-downward-for`](tasks/loops-downward-for/loops-downward-for.om) | [Loops/Downward for](https://rosettacode.org/wiki/Loops/Downward_for) | 33 | plain Python | +| [`loops-for`](tasks/loops-for/loops-for.om) | [Loops/For](https://rosettacode.org/wiki/Loops/For) | 22 | plain Python | +| [`loops-for-with-a-specified-step`](tasks/loops-for-with-a-specified-step/loops-for-with-a-specified-step.om) | [Loops/For with a specified step](https://rosettacode.org/wiki/Loops/For_with_a_specified_step) | 38 | plain Python | +| [`loops-foreach`](tasks/loops-foreach/loops-foreach.om) | [Loops/Foreach](https://rosettacode.org/wiki/Loops/Foreach) | 23 | plain Python | +| [`loops-n-plus-one-half`](tasks/loops-n-plus-one-half/loops-n-plus-one-half.om) | [Loops/N plus one half](https://rosettacode.org/wiki/Loops/N_plus_one_half) | 40 | plain Python | +| [`loops-while`](tasks/loops-while/loops-while.om) | [Loops/While](https://rosettacode.org/wiki/Loops/While) | 35 | plain Python | +| [`lucas-lehmer-test`](tasks/lucas-lehmer-test/lucas-lehmer-test.om) | [Lucas-Lehmer test](https://rosettacode.org/wiki/Lucas-Lehmer_test) | 100 | plain Python | +| [`ludic-numbers`](tasks/ludic-numbers/ludic-numbers.om) | [Ludic numbers](https://rosettacode.org/wiki/Ludic_numbers) | 98 | plain Python | +| [`luhn-test-of-credit-card-numbers`](tasks/luhn-test-of-credit-card-numbers/luhn-test-of-credit-card-numbers.om) | [Luhn test of credit card numbers](https://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers) | 48 | plain Python | +| [`magic-squares-of-odd-order`](tasks/magic-squares-of-odd-order/magic-squares-of-odd-order.om) | [Magic squares of odd order](https://rosettacode.org/wiki/Magic_squares_of_odd_order) | 87 | plain Python | | [`mandelbrot-set`](tasks/mandelbrot-set/mandelbrot-set.om) | [Mandelbrot set](https://rosettacode.org/wiki/Mandelbrot_set) | 46 | plain Python | +| [`matrix-multiplication`](tasks/matrix-multiplication/matrix-multiplication.om) | [Matrix multiplication](https://rosettacode.org/wiki/Matrix_multiplication) | 103 | plain Python | +| [`matrix-transposition`](tasks/matrix-transposition/matrix-transposition.om) | [Matrix transposition](https://rosettacode.org/wiki/Matrix_transposition) | 85 | plain Python | +| [`matrix-with-two-diagonals`](tasks/matrix-with-two-diagonals/matrix-with-two-diagonals.om) | [Matrix with two diagonals](https://rosettacode.org/wiki/Matrix_with_two_diagonals) | 49 | plain Python | +| [`maximum-triangle-path-sum`](tasks/maximum-triangle-path-sum/maximum-triangle-path-sum.om) | [Maximum triangle path sum](https://rosettacode.org/wiki/Maximum_triangle_path_sum) | 101 | plain Python | +| [`maze-generation`](tasks/maze-generation/maze-generation.om) | [Maze generation](https://rosettacode.org/wiki/Maze_generation) | 148 | plain Python | +| [`maze-solving`](tasks/maze-solving/maze-solving.om) | [Maze solving](https://rosettacode.org/wiki/Maze_solving) | 208 | plain Python | +| [`md5`](tasks/md5/md5.om) | [MD5](https://rosettacode.org/wiki/MD5) | 144 | `hashlib` | +| [`merge-sort`](tasks/merge-sort/merge-sort.om) | [Sorting algorithms/Merge sort](https://rosettacode.org/wiki/Sorting_algorithms/Merge_sort) | 119 | plain Python | +| [`mersenne-primes`](tasks/mersenne-primes/mersenne-primes.om) | [Mersenne primes](https://rosettacode.org/wiki/Mersenne_primes) | 125 | plain Python | +| [`miller-rabin-primality-test`](tasks/miller-rabin-primality-test/miller-rabin-primality-test.om) | [Miller-Rabin primality test](https://rosettacode.org/wiki/Miller-Rabin_primality_test) | 143 | `math` | +| [`mobius-function`](tasks/mobius-function/mobius-function.om) | [Möbius function](https://rosettacode.org/wiki/M%C3%B6bius_function) | 60 | plain Python | +| [`modular-exponentiation`](tasks/modular-exponentiation/modular-exponentiation.om) | [Modular exponentiation](https://rosettacode.org/wiki/Modular_exponentiation) | 113 | plain Python | +| [`modular-inverse`](tasks/modular-inverse/modular-inverse.om) | [Modular inverse](https://rosettacode.org/wiki/Modular_inverse) | 86 | plain Python | | [`n-queens-problem`](tasks/n-queens-problem/n-queens-problem.om) | [N-queens problem](https://rosettacode.org/wiki/N-queens_problem) | 65 | `itertools` | +| [`narcissistic-decimal-number`](tasks/narcissistic-decimal-number/narcissistic-decimal-number.om) | [Narcissistic decimal number](https://rosettacode.org/wiki/Narcissistic_decimal_number) | 66 | `itertools` | +| [`number-names`](tasks/number-names/number-names.om) | [Number names](https://rosettacode.org/wiki/Number_names) | 120 | plain Python | +| [`one-dimensional-cellular-automata`](tasks/one-dimensional-cellular-automata/one-dimensional-cellular-automata.om) | [One-dimensional cellular automata](https://rosettacode.org/wiki/One-dimensional_cellular_automata) | 88 | plain Python | +| [`ordered-partitions`](tasks/ordered-partitions/ordered-partitions.om) | [Ordered partitions](https://rosettacode.org/wiki/Ordered_partitions) | 107 | `itertools`, `math` | +| [`ordered-words`](tasks/ordered-words/ordered-words.om) | [Ordered words](https://rosettacode.org/wiki/Ordered_words) | 51 | plain Python | +| [`palindrome-detection`](tasks/palindrome-detection/palindrome-detection.om) | [Palindrome detection](https://rosettacode.org/wiki/Palindrome_detection) | 70 | plain Python | +| [`pancake-sort`](tasks/pancake-sort/pancake-sort.om) | [Sorting algorithms/Pancake sort](https://rosettacode.org/wiki/Sorting_algorithms/Pancake_sort) | 116 | plain Python | +| [`pangram-checker`](tasks/pangram-checker/pangram-checker.om) | [Pangram checker](https://rosettacode.org/wiki/Pangram_checker) | 51 | `string` | +| [`pascals-triangle`](tasks/pascals-triangle/pascals-triangle.om) | [Pascal's triangle](https://rosettacode.org/wiki/Pascal's_triangle) | 53 | `math` | +| [`patience-sort`](tasks/patience-sort/patience-sort.om) | [Sorting algorithms/Patience sort](https://rosettacode.org/wiki/Sorting_algorithms/Patience_sort) | 126 | `heapq` | | [`perfect-numbers`](tasks/perfect-numbers/perfect-numbers.om) | [Perfect numbers](https://rosettacode.org/wiki/Perfect_numbers) | 52 | plain Python | +| [`perfect-totient-numbers`](tasks/perfect-totient-numbers/perfect-totient-numbers.om) | [Perfect totient numbers](https://rosettacode.org/wiki/Perfect_totient_numbers) | 68 | plain Python | +| [`permutation-sort`](tasks/permutation-sort/permutation-sort.om) | [Sorting algorithms/Permutation sort](https://rosettacode.org/wiki/Sorting_algorithms/Permutation_sort) | 145 | `itertools` | +| [`permutations`](tasks/permutations/permutations.om) | [Permutations](https://rosettacode.org/wiki/Permutations) | 83 | plain Python | +| [`permutations-by-swapping`](tasks/permutations-by-swapping/permutations-by-swapping.om) | [Permutations by swapping](https://rosettacode.org/wiki/Permutations_by_swapping) | 101 | plain Python | +| [`permutations-derangements`](tasks/permutations-derangements/permutations-derangements.om) | [Permutations/Derangements](https://rosettacode.org/wiki/Permutations/Derangements) | 144 | `itertools`, `math` | +| [`pernicious-numbers`](tasks/pernicious-numbers/pernicious-numbers.om) | [Pernicious numbers](https://rosettacode.org/wiki/Pernicious_numbers) | 75 | plain Python | +| [`phrase-reversals`](tasks/phrase-reversals/phrase-reversals.om) | [Phrase reversals](https://rosettacode.org/wiki/Phrase_reversals) | 64 | plain Python | +| [`population-count`](tasks/population-count/population-count.om) | [Population count](https://rosettacode.org/wiki/Population_count) | 67 | `itertools` | +| [`power-set`](tasks/power-set/power-set.om) | [Power set](https://rosettacode.org/wiki/Power_set) | 56 | plain Python | +| [`primality-by-trial-division`](tasks/primality-by-trial-division/primality-by-trial-division.om) | [Primality by trial division](https://rosettacode.org/wiki/Primality_by_trial_division) | 74 | `math` | | [`prime-decomposition`](tasks/prime-decomposition/prime-decomposition.om) | [Prime decomposition](https://rosettacode.org/wiki/Prime_decomposition) | 64 | plain Python | +| [`priority-queue`](tasks/priority-queue/priority-queue.om) | [Priority queue](https://rosettacode.org/wiki/Priority_queue) | 115 | `heapq` | +| [`proper-divisors`](tasks/proper-divisors/proper-divisors.om) | [Proper divisors](https://rosettacode.org/wiki/Proper_divisors) | 69 | `math` | +| [`pythagorean-triples`](tasks/pythagorean-triples/pythagorean-triples.om) | [Pythagorean triples](https://rosettacode.org/wiki/Pythagorean_triples) | 78 | `math` | +| [`queue-definition`](tasks/queue-definition/queue-definition.om) | [Queue/Definition](https://rosettacode.org/wiki/Queue/Definition) | 97 | `collections` | +| [`queue-usage`](tasks/queue-usage/queue-usage.om) | [Queue/Usage](https://rosettacode.org/wiki/Queue/Usage) | 95 | plain Python | +| [`quicksort`](tasks/quicksort/quicksort.om) | [Sorting algorithms/Quicksort](https://rosettacode.org/wiki/Sorting_algorithms/Quicksort) | 105 | plain Python | +| [`radix-sort`](tasks/radix-sort/radix-sort.om) | [Sorting algorithms/Radix sort](https://rosettacode.org/wiki/Sorting_algorithms/Radix_sort) | 133 | plain Python | +| [`recamans-sequence`](tasks/recamans-sequence/recamans-sequence.om) | [Recaman's sequence](https://rosettacode.org/wiki/Recaman%27s_sequence) | 76 | plain Python | +| [`remove-duplicate-elements`](tasks/remove-duplicate-elements/remove-duplicate-elements.om) | [Remove duplicate elements](https://rosettacode.org/wiki/Remove_duplicate_elements) | 102 | plain Python | +| [`remove-vowels-from-a-string`](tasks/remove-vowels-from-a-string/remove-vowels-from-a-string.om) | [Remove vowels from a string](https://rosettacode.org/wiki/Remove_vowels_from_a_string) | 39 | plain Python | +| [`repeat-a-string`](tasks/repeat-a-string/repeat-a-string.om) | [Repeat a string](https://rosettacode.org/wiki/Repeat_a_string) | 31 | plain Python | | [`reverse-a-string`](tasks/reverse-a-string/reverse-a-string.om) | [Reverse a string](https://rosettacode.org/wiki/Reverse_a_string) | 28 | plain Python | +| [`reverse-words-in-a-string`](tasks/reverse-words-in-a-string/reverse-words-in-a-string.om) | [Reverse words in a string](https://rosettacode.org/wiki/Reverse_words_in_a_string) | 55 | plain Python | | [`roman-numerals-encode`](tasks/roman-numerals-encode/roman-numerals-encode.om) | [Roman numerals/Encode](https://rosettacode.org/wiki/Roman_numerals/Encode) | 70 | plain Python | | [`rot-13`](tasks/rot-13/rot-13.om) | [Rot-13](https://rosettacode.org/wiki/Rot-13) | 35 | `codecs` | +| [`run-length-encoding`](tasks/run-length-encoding/run-length-encoding.om) | [Run-length encoding](https://rosettacode.org/wiki/Run-length_encoding) | 79 | `itertools`, `re` | +| [`selection-sort`](tasks/selection-sort/selection-sort.om) | [Sorting algorithms/Selection sort](https://rosettacode.org/wiki/Sorting_algorithms/Selection_sort) | 98 | plain Python | +| [`semiprime`](tasks/semiprime/semiprime.om) | [Semiprime](https://rosettacode.org/wiki/Semiprime) | 79 | plain Python | +| [`sha-1`](tasks/sha-1/sha-1.om) | [SHA-1](https://rosettacode.org/wiki/SHA-1) | 131 | `hashlib` | | [`sha-256`](tasks/sha-256/sha-256.om) | [SHA-256](https://rosettacode.org/wiki/SHA-256) | 208 | `hashlib` | +| [`shell-sort`](tasks/shell-sort/shell-sort.om) | [Sorting algorithms/Shell sort](https://rosettacode.org/wiki/Sorting_algorithms/Shell_sort) | 103 | plain Python | | [`sieve-of-eratosthenes`](tasks/sieve-of-eratosthenes/sieve-of-eratosthenes.om) | [Sieve of Eratosthenes](https://rosettacode.org/wiki/Sieve_of_Eratosthenes) | 48 | plain Python | +| [`smith-numbers`](tasks/smith-numbers/smith-numbers.om) | [Smith numbers](https://rosettacode.org/wiki/Smith_numbers) | 89 | plain Python | +| [`sort-an-integer-array`](tasks/sort-an-integer-array/sort-an-integer-array.om) | [Sort an integer array](https://rosettacode.org/wiki/Sort_an_integer_array) | 104 | plain Python | +| [`sort-disjoint-sublist`](tasks/sort-disjoint-sublist/sort-disjoint-sublist.om) | [Sort disjoint sublist](https://rosettacode.org/wiki/Sort_disjoint_sublist) | 124 | plain Python | +| [`sort-three-variables`](tasks/sort-three-variables/sort-three-variables.om) | [Sort three variables](https://rosettacode.org/wiki/Sort_three_variables) | 162 | plain Python | +| [`soundex`](tasks/soundex/soundex.om) | [Soundex](https://rosettacode.org/wiki/Soundex) | 73 | plain Python | +| [`spiral-matrix`](tasks/spiral-matrix/spiral-matrix.om) | [Spiral matrix](https://rosettacode.org/wiki/Spiral_matrix) | 91 | plain Python | +| [`square-free-integers`](tasks/square-free-integers/square-free-integers.om) | [Square-free integers](https://rosettacode.org/wiki/Square-free_integers) | 98 | `math` | +| [`stack`](tasks/stack/stack.om) | [Stack](https://rosettacode.org/wiki/Stack) | 102 | plain Python | +| [`stern-brocot-sequence`](tasks/stern-brocot-sequence/stern-brocot-sequence.om) | [Stern-Brocot sequence](https://rosettacode.org/wiki/Stern-Brocot_sequence) | 97 | `math` | +| [`stooge-sort`](tasks/stooge-sort/stooge-sort.om) | [Sorting algorithms/Stooge sort](https://rosettacode.org/wiki/Sorting_algorithms/Stooge_sort) | 97 | plain Python | +| [`strand-sort`](tasks/strand-sort/strand-sort.om) | [Sorting algorithms/Strand sort](https://rosettacode.org/wiki/Sorting_algorithms/Strand_sort) | 155 | plain Python | +| [`string-append`](tasks/string-append/string-append.om) | [String append](https://rosettacode.org/wiki/String_append) | 28 | plain Python | +| [`string-case`](tasks/string-case/string-case.om) | [String case](https://rosettacode.org/wiki/String_case) | 37 | plain Python | +| [`string-comparison`](tasks/string-comparison/string-comparison.om) | [String comparison](https://rosettacode.org/wiki/String_comparison) | 83 | plain Python | +| [`string-concatenation`](tasks/string-concatenation/string-concatenation.om) | [String concatenation](https://rosettacode.org/wiki/String_concatenation) | 30 | plain Python | +| [`string-length`](tasks/string-length/string-length.om) | [String length](https://rosettacode.org/wiki/String_length) | 51 | `sys` | +| [`string-matching`](tasks/string-matching/string-matching.om) | [String matching](https://rosettacode.org/wiki/String_matching) | 69 | plain Python | +| [`string-prepend`](tasks/string-prepend/string-prepend.om) | [String prepend](https://rosettacode.org/wiki/String_prepend) | 37 | plain Python | +| [`strip-a-set-of-characters-from-a-string`](tasks/strip-a-set-of-characters-from-a-string/strip-a-set-of-characters-from-a-string.om) | [Strip a set of characters from a string](https://rosettacode.org/wiki/Strip_a_set_of_characters_from_a_string) | 38 | plain Python | +| [`strip-comments-from-a-string`](tasks/strip-comments-from-a-string/strip-comments-from-a-string.om) | [Strip comments from a string](https://rosettacode.org/wiki/Strip_comments_from_a_string) | 51 | `re` | +| [`strip-whitespace-from-a-string`](tasks/strip-whitespace-from-a-string/strip-whitespace-from-a-string.om) | [Strip whitespace from a string/Top and tail](https://rosettacode.org/wiki/Strip_whitespace_from_a_string/Top_and_tail) | 49 | plain Python | +| [`substitution-cipher`](tasks/substitution-cipher/substitution-cipher.om) | [Substitution cipher](https://rosettacode.org/wiki/Substitution_cipher) | 50 | `string` | +| [`substring`](tasks/substring/substring.om) | [Substring](https://rosettacode.org/wiki/Substring) | 43 | plain Python | +| [`sudoku`](tasks/sudoku/sudoku.om) | [Sudoku](https://rosettacode.org/wiki/Sudoku) | 103 | plain Python | +| [`sum-and-product-of-an-array`](tasks/sum-and-product-of-an-array/sum-and-product-of-an-array.om) | [Sum and product of an array](https://rosettacode.org/wiki/Sum_and_product_of_an_array) | 47 | `math` | +| [`sum-digits-of-an-integer`](tasks/sum-digits-of-an-integer/sum-digits-of-an-integer.om) | [Sum digits of an integer](https://rosettacode.org/wiki/Sum_digits_of_an_integer) | 76 | `string` | +| [`sum-multiples-of-3-and-5`](tasks/sum-multiples-of-3-and-5/sum-multiples-of-3-and-5.om) | [Sum multiples of 3 and 5](https://rosettacode.org/wiki/Sum_multiples_of_3_and_5) | 54 | plain Python | +| [`temperature-conversion`](tasks/temperature-conversion/temperature-conversion.om) | [Temperature conversion](https://rosettacode.org/wiki/Temperature_conversion) | 71 | `fractions` | +| [`the-twelve-days-of-christmas`](tasks/the-twelve-days-of-christmas/the-twelve-days-of-christmas.om) | [The Twelve Days of Christmas](https://rosettacode.org/wiki/The_Twelve_Days_of_Christmas) | 55 | plain Python | +| [`thue-morse`](tasks/thue-morse/thue-morse.om) | [Thue-Morse](https://rosettacode.org/wiki/Thue-Morse) | 33 | plain Python | +| [`tokenize-a-string`](tasks/tokenize-a-string/tokenize-a-string.om) | [Tokenize a string](https://rosettacode.org/wiki/Tokenize_a_string) | 36 | plain Python | +| [`topological-sort`](tasks/topological-sort/topological-sort.om) | [Topological sort](https://rosettacode.org/wiki/Topological_sort) | 149 | `heapq` | +| [`topswops`](tasks/topswops/topswops.om) | [Topswops](https://rosettacode.org/wiki/Topswops) | 121 | `itertools` | +| [`totient-function`](tasks/totient-function/totient-function.om) | [Totient function](https://rosettacode.org/wiki/Totient_function) | 79 | `math` | | [`towers-of-hanoi`](tasks/towers-of-hanoi/towers-of-hanoi.om) | [Towers of Hanoi](https://rosettacode.org/wiki/Towers_of_Hanoi) | 47 | plain Python | +| [`tree-traversal`](tasks/tree-traversal/tree-traversal.om) | [Tree traversal](https://rosettacode.org/wiki/Tree_traversal) | 105 | `collections` | +| [`twin-primes`](tasks/twin-primes/twin-primes.om) | [Twin primes](https://rosettacode.org/wiki/Twin_primes) | 79 | `bisect`, `itertools` | +| [`url-decoding`](tasks/url-decoding/url-decoding.om) | [URL decoding](https://rosettacode.org/wiki/URL_decoding) | 51 | `sys`, `urllib.parse` | +| [`url-encoding`](tasks/url-encoding/url-encoding.om) | [URL encoding](https://rosettacode.org/wiki/URL_encoding) | 50 | `urllib.parse` | +| [`van-eck-sequence`](tasks/van-eck-sequence/van-eck-sequence.om) | [Van Eck sequence](https://rosettacode.org/wiki/Van_Eck_sequence) | 54 | plain Python | +| [`vigenere-cipher`](tasks/vigenere-cipher/vigenere-cipher.om) | [Vigenère cipher](https://rosettacode.org/wiki/Vigen%C3%A8re_cipher) | 55 | plain Python | +| [`water-collected-between-towers`](tasks/water-collected-between-towers/water-collected-between-towers.om) | [Water collected between towers](https://rosettacode.org/wiki/Water_collected_between_towers) | 146 | plain Python | +| [`wireworld`](tasks/wireworld/wireworld.om) | [Wireworld](https://rosettacode.org/wiki/Wireworld) | 96 | plain Python | +| [`word-wrap`](tasks/word-wrap/word-wrap.om) | [Word wrap](https://rosettacode.org/wiki/Word_wrap) | 53 | `textwrap` | +| [`zig-zag-matrix`](tasks/zig-zag-matrix/zig-zag-matrix.om) | [Zig-zag matrix](https://rosettacode.org/wiki/Zig-zag_matrix) | 77 | plain Python | The SHA-256 round constants and initial values in `tasks/sha-256/sha-256.om` were copied mechanically from omega-mv's [`om/sha256/sha256-om2.om`](https://github.com/aien-dev/omega-mv/blob/e5b32567a7aff3ce594e3254a622a846a6cd190b/om/sha256/sha256-om2.om); the hashes are checked against Python's `hashlib`, and "Rosetta code" gives Rosetta Code's published value `764faf5c…cdfcf`. +The 49 number-theory tasks that carry a `wolfram.txt` were also checked a second, independent way: +the file records a Wolfram Language query that rebuilds the whole expected output from Wolfram +built-ins, and whether that text's SHA-256 matched `expected.txt` (all 49 record a match). + ## Exercises -Five small problems in the Exercism style. Each `exercises//.om` is a stub that traps -until you write the function; `test.om` holds the cases. +47 small problems in the Exercism style. Each `exercises//.om` is a stub that traps +until you write the function; `test.om` holds the cases. For 42 of them the cases come from +[exercism/problem-specifications](https://github.com/exercism/problem-specifications) +`canonical-data.json` at commit `650bebeb` (MIT; see [NOTICE](NOTICE)); each `test.om` notes every +case's uuid and how many cases it kept, skipped and left out as superseded. ```sh sh run.sh # once, to birth omc into build/ @@ -94,11 +294,53 @@ all pass. Reference solutions are in `exercises//.meta/example.om`. | Stub | Problem | | --- | --- | +| [`acronym`](exercises/acronym/acronym.om) | [acronym](https://exercism.org/tracks/python/exercises/acronym) | +| [`all-your-base`](exercises/all-your-base/all-your-base.om) | [all-your-base](https://exercism.org/tracks/python/exercises/all-your-base) | +| [`allergies`](exercises/allergies/allergies.om) | [allergies](https://exercism.org/tracks/python/exercises/allergies) | | [`armstrong-numbers`](exercises/armstrong-numbers/armstrong-numbers.om) | [armstrong-numbers](https://exercism.org/tracks/python/exercises/armstrong-numbers) | +| [`atbash-cipher`](exercises/atbash-cipher/atbash-cipher.om) | [atbash-cipher](https://exercism.org/tracks/python/exercises/atbash-cipher) | +| [`binary-search`](exercises/binary-search/binary-search.om) | [binary-search](https://exercism.org/tracks/python/exercises/binary-search) | +| [`bob`](exercises/bob/bob.om) | [bob](https://exercism.org/tracks/python/exercises/bob) | +| [`change`](exercises/change/change.om) | [change](https://exercism.org/tracks/python/exercises/change) | | [`collatz-conjecture`](exercises/collatz-conjecture/collatz-conjecture.om) | [collatz-conjecture](https://exercism.org/tracks/python/exercises/collatz-conjecture) | +| [`darts`](exercises/darts/darts.om) | [darts](https://exercism.org/tracks/python/exercises/darts) | | [`difference-of-squares`](exercises/difference-of-squares/difference-of-squares.om) | [difference-of-squares](https://exercism.org/tracks/python/exercises/difference-of-squares) | +| [`eliuds-eggs`](exercises/eliuds-eggs/eliuds-eggs.om) | [eliuds-eggs](https://exercism.org/tracks/python/exercises/eliuds-eggs) | | [`grains`](exercises/grains/grains.om) | [grains](https://exercism.org/tracks/python/exercises/grains) | +| [`hamming`](exercises/hamming/hamming.om) | [hamming](https://exercism.org/tracks/python/exercises/hamming) | +| [`hello-world`](exercises/hello-world/hello-world.om) | [hello-world](https://exercism.org/tracks/python/exercises/hello-world) | +| [`isbn-verifier`](exercises/isbn-verifier/isbn-verifier.om) | [isbn-verifier](https://exercism.org/tracks/python/exercises/isbn-verifier) | +| [`isogram`](exercises/isogram/isogram.om) | [isogram](https://exercism.org/tracks/python/exercises/isogram) | +| [`knapsack`](exercises/knapsack/knapsack.om) | [knapsack](https://exercism.org/tracks/python/exercises/knapsack) | +| [`largest-series-product`](exercises/largest-series-product/largest-series-product.om) | [largest-series-product](https://exercism.org/tracks/python/exercises/largest-series-product) | | [`leap`](exercises/leap/leap.om) | [leap](https://exercism.org/tracks/python/exercises/leap) | +| [`luhn`](exercises/luhn/luhn.om) | [luhn](https://exercism.org/tracks/python/exercises/luhn) | +| [`nth-prime`](exercises/nth-prime/nth-prime.om) | [nth-prime](https://exercism.org/tracks/python/exercises/nth-prime) | +| [`nucleotide-count`](exercises/nucleotide-count/nucleotide-count.om) | [nucleotide-count](https://exercism.org/tracks/python/exercises/nucleotide-count) | +| [`pangram`](exercises/pangram/pangram.om) | [pangram](https://exercism.org/tracks/python/exercises/pangram) | +| [`pascals-triangle`](exercises/pascals-triangle/pascals-triangle.om) | [pascals-triangle](https://exercism.org/tracks/python/exercises/pascals-triangle) | +| [`perfect-numbers`](exercises/perfect-numbers/perfect-numbers.om) | [perfect-numbers](https://exercism.org/tracks/python/exercises/perfect-numbers) | +| [`prime-factors`](exercises/prime-factors/prime-factors.om) | [prime-factors](https://exercism.org/tracks/python/exercises/prime-factors) | +| [`protein-translation`](exercises/protein-translation/protein-translation.om) | [protein-translation](https://exercism.org/tracks/python/exercises/protein-translation) | +| [`queen-attack`](exercises/queen-attack/queen-attack.om) | [queen-attack](https://exercism.org/tracks/python/exercises/queen-attack) | +| [`raindrops`](exercises/raindrops/raindrops.om) | [raindrops](https://exercism.org/tracks/python/exercises/raindrops) | +| [`resistor-color`](exercises/resistor-color/resistor-color.om) | [resistor-color](https://exercism.org/tracks/python/exercises/resistor-color) | +| [`resistor-color-duo`](exercises/resistor-color-duo/resistor-color-duo.om) | [resistor-color-duo](https://exercism.org/tracks/python/exercises/resistor-color-duo) | +| [`resistor-color-trio`](exercises/resistor-color-trio/resistor-color-trio.om) | [resistor-color-trio](https://exercism.org/tracks/python/exercises/resistor-color-trio) | +| [`reverse-string`](exercises/reverse-string/reverse-string.om) | [reverse-string](https://exercism.org/tracks/python/exercises/reverse-string) | +| [`rna-transcription`](exercises/rna-transcription/rna-transcription.om) | [rna-transcription](https://exercism.org/tracks/python/exercises/rna-transcription) | +| [`rotational-cipher`](exercises/rotational-cipher/rotational-cipher.om) | [rotational-cipher](https://exercism.org/tracks/python/exercises/rotational-cipher) | +| [`run-length-encoding`](exercises/run-length-encoding/run-length-encoding.om) | [run-length-encoding](https://exercism.org/tracks/python/exercises/run-length-encoding) | +| [`saddle-points`](exercises/saddle-points/saddle-points.om) | [saddle-points](https://exercism.org/tracks/python/exercises/saddle-points) | +| [`scrabble-score`](exercises/scrabble-score/scrabble-score.om) | [scrabble-score](https://exercism.org/tracks/python/exercises/scrabble-score) | +| [`secret-handshake`](exercises/secret-handshake/secret-handshake.om) | [secret-handshake](https://exercism.org/tracks/python/exercises/secret-handshake) | +| [`series`](exercises/series/series.om) | [series](https://exercism.org/tracks/python/exercises/series) | +| [`sieve`](exercises/sieve/sieve.om) | [sieve](https://exercism.org/tracks/python/exercises/sieve) | +| [`space-age`](exercises/space-age/space-age.om) | [space-age](https://exercism.org/tracks/python/exercises/space-age) | +| [`square-root`](exercises/square-root/square-root.om) | [square-root](https://exercism.org/tracks/python/exercises/square-root) | +| [`sum-of-multiples`](exercises/sum-of-multiples/sum-of-multiples.om) | [sum-of-multiples](https://exercism.org/tracks/python/exercises/sum-of-multiples) | +| [`triangle`](exercises/triangle/triangle.om) | [triangle](https://exercism.org/tracks/python/exercises/triangle) | +| [`two-fer`](exercises/two-fer/two-fer.om) | [two-fer](https://exercism.org/tracks/python/exercises/two-fer) | ## Reading Omega in ten minutes diff --git a/dataset/README.md b/dataset/README.md new file mode 100644 index 0000000..a69a7fd --- /dev/null +++ b/dataset/README.md @@ -0,0 +1,83 @@ +--- +license: apache-2.0 +language: +- en +- code +pretty_name: omega-rosetta +size_categories: +- n<1K +task_categories: +- text-generation +tags: +- code +- omega +- omegese +- rosetta-code +- exercism +configs: +- config_name: default + data_files: omega-rosetta.parquet +--- + +# omega-rosetta + +263 programs written in Omegese (the OM-2 profile of the Omega language) and compiled by `omc`, the +compiler that [aien-dev/omega-mv](https://github.com/aien-dev/omega-mv) rebuilds from a 408-byte +hand-written seed: 216 [Rosetta Code](https://rosettacode.org) tasks and 47 Exercism-style exercises. +Each task's expected output was written by an independent Python 3 oracle, never by `omc`. + +The file is built from the repository by `python3 dataset/export.py` (needs `pyarrow`). It is not +published; this card describes the local build. + +## Fields + +One row per program, tasks first, each kind sorted by name. + +| Column | Type | Meaning | +| --- | --- | --- | +| `name` | string | directory name, `tasks/` or `exercises/` | +| `kind` | string | `task` or `exercise` | +| `rosetta_url` | string | the Rosetta Code task page (tasks; null for exercises) | +| `exercism_url` | string | the Exercism problem page (exercises; null for tasks) | +| `title` | string | the Rosetta Code page title (tasks) or the exercise slug | +| `source_om` | string | the program: `tasks//.om`, or for an exercise its reference solution `.meta/example.om` | +| `stub_om` | string | the exercise stub that traps (exit 113) until solved; null for tasks | +| `test_om` | string | the exercise's test cases, appended to a solution to form one unit; null for tasks | +| `expected_stdout` | string | `tasks//expected.txt` byte for byte; null for exercises, whose test unit prints `ok N - ...` lines | +| `expected_exit` | int32 | 0 for every row: a task exits 0, and `source_om` + `test_om` exits 0 when every case passes | +| `oracle_py` | string | the Python 3 standard-library program that wrote `expected_stdout`; null for exercises | +| `omc_sha256` | string | sha256 of the `omc` binary every row was checked with | +| `omega_mv_commit` | string | the aien-dev/omega-mv commit that births that `omc` | +| `lines` | int32 | lines of `source_om` that are not `note` comments | + +## Provenance + +- Repository: [aien-dev/omega-rosetta](https://github.com/aien-dev/omega-rosetta), branch `corpus/200`. +- Compiler pin ([`omega.ref`](../omega.ref)): omega-mv `e5b32567a7aff3ce594e3254a622a846a6cd190b`, + `omc` sha256 `2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f`. +- Gate, Linux AArch64 (DGX Spark, Cortex-A725 cores 10-14), 2026-10-27, prebuilt pinned `omc`: + `sh run.sh --omc ` printed `TASKS PASS 216/216`; all 216 oracles reproduced their + `expected.txt`; all 47 exercise examples passed through `exercise.sh` and all 47 stubs failed with + trap 13. Negative controls failed as required: a mutated FizzBuzz answer (215/216), a one-bit + change to the CRC-32 polynomial (215/216), a wrong leap solution (`failed=3`), and every + `expected.txt` mutated at once (0/216, and 216/216 oracle comparisons caught it). +- Test cases of 42 exercises come from + [exercism/problem-specifications](https://github.com/exercism/problem-specifications) + `canonical-data.json` at commit `650bebeb39ad40439175288042aece982b0a90a9` (MIT; see + [NOTICE](../NOTICE)). 49 number-theory tasks carry a `wolfram.txt` recording an independent + Wolfram Language check of their expected output; that file is not a column here. + +## Known limits + +- One compiler at one commit. These programs say nothing about other `omc` versions, and passing + them is not a proof that `omc` is correct. +- AArch64 Linux only: `omc` and the programs it writes are static AArch64 Linux ELF files. +- Every task's input is fixed in its source; Omega has no imports, so each program repeats its own + output helpers, which inflates `lines` and makes rows look alike. +- Rosetta Code's `anagrams` and `ordered-words` tasks read `unixdict.txt`; these programs embed a small fixed sample instead, + so their output differs from the full-list answers on rosettacode.org. +- Exercises carry no expected stdout; their check is the exit code of solution + tests. +- 3 non-ASCII cases of `reverse-string` were skipped; Omegese strings here are bytes. +- Rosetta Code publishes content under the GNU FDL 1.3; this dataset is the repository's own code + under Apache-2.0 WITH LLVM-exception. It copies no Rosetta Code page prose; sample inputs that a + task statement names (word lists, test strings) are reused as data. diff --git a/dataset/export.py b/dataset/export.py new file mode 100644 index 0000000..40f71bc --- /dev/null +++ b/dataset/export.py @@ -0,0 +1,117 @@ +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +"""Export the omega-rosetta corpus as one Parquet file for a Hugging Face dataset. + + python3 dataset/export.py [--out dataset/omega-rosetta.parquet] + +Needs pyarrow (use a virtual environment). Reads only files in this repository; it does not run +omc or any oracle. Run `sh run.sh` first: the dataset card records that gate's result, and this +script refuses a task whose expected.txt is missing. + +One row per task (tasks/) and per exercise (exercises/), sorted by kind then name. +""" +import argparse +import hashlib +import os +import re +import sys + +import pyarrow as pa +import pyarrow.parquet as pq + +HERE = os.path.dirname(os.path.abspath(__file__)) +ROOT = os.path.dirname(HERE) + +SCHEMA = pa.schema([ + ("name", pa.string()), + ("kind", pa.string()), + ("rosetta_url", pa.string()), + ("exercism_url", pa.string()), + ("title", pa.string()), + ("source_om", pa.string()), + ("stub_om", pa.string()), + ("test_om", pa.string()), + ("expected_stdout", pa.string()), + ("expected_exit", pa.int32()), + ("oracle_py", pa.string()), + ("omc_sha256", pa.string()), + ("omega_mv_commit", pa.string()), + ("lines", pa.int32()), +]) + +HEADER = re.compile(r'^note Rosetta Code task "(.+)": (https://rosettacode\.org/wiki/\S+)$') + + +def read(path): + with open(path, encoding="utf-8") as f: + return f.read() + + +def pin(): + ref = {} + for line in read(os.path.join(ROOT, "omega.ref")).splitlines(): + parts = line.split() + if len(parts) == 2 and not line.startswith("#"): + ref[parts[0]] = parts[1] + return ref["commit"], ref["omc_sha256"] + + +def code_lines(src): + """Lines that are not note comments, as the README table counts them.""" + return sum(1 for l in src.splitlines() if not (l.startswith("note ") or l == "note")) + + +def exercism_url(readme, name): + m = re.search(r"\| \[`%s`\]\([^)]*\) \| \[[^\]]*\]\((https://exercism\.org/[^)]+)\) \|" % re.escape(name), readme) + if not m: + sys.exit("export.py: no README row with an exercism.org link for exercise " + name) + return m.group(1) + + +def rows(): + commit, sha = pin() + readme = read(os.path.join(ROOT, "README.md")) + out = [] + tasks = os.path.join(ROOT, "tasks") + for name in sorted(os.listdir(tasks)): + d = os.path.join(tasks, name) + src = read(os.path.join(d, name + ".om")) + m = HEADER.match(src.splitlines()[0]) + if not m: + sys.exit("export.py: %s.om does not start with the Rosetta Code task note" % name) + out.append({ + "name": name, "kind": "task", "rosetta_url": m.group(2), "exercism_url": None, + "title": m.group(1), "source_om": src, "stub_om": None, "test_om": None, + "expected_stdout": read(os.path.join(d, "expected.txt")), "expected_exit": 0, + "oracle_py": read(os.path.join(d, "oracle.py")), "omc_sha256": sha, + "omega_mv_commit": commit, "lines": code_lines(src), + }) + exercises = os.path.join(ROOT, "exercises") + for name in sorted(os.listdir(exercises)): + d = os.path.join(exercises, name) + src = read(os.path.join(d, ".meta", "example.om")) + out.append({ + "name": name, "kind": "exercise", "rosetta_url": None, + "exercism_url": exercism_url(readme, name), "title": name, "source_om": src, + "stub_om": read(os.path.join(d, name + ".om")), "test_om": read(os.path.join(d, "test.om")), + "expected_stdout": None, "expected_exit": 0, "oracle_py": None, "omc_sha256": sha, + "omega_mv_commit": commit, "lines": code_lines(src), + }) + return out + + +def main(): + ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + ap.add_argument("--out", default=os.path.join(HERE, "omega-rosetta.parquet")) + args = ap.parse_args() + data = rows() + table = pa.Table.from_pylist(data, schema=SCHEMA) + pq.write_table(table, args.out, compression="zstd") + h = hashlib.sha256() + with open(args.out, "rb") as f: + h.update(f.read()) + kinds = {k: sum(1 for r in data if r["kind"] == k) for k in ("task", "exercise")} + print("%s rows=%d tasks=%d exercises=%d sha256=%s" % (args.out, len(data), kinds["task"], kinds["exercise"], h.hexdigest())) + + +if __name__ == "__main__": + main() diff --git a/drafts/rosettacode/100-doors.wiki b/drafts/rosettacode/100-doors.wiki new file mode 100644 index 0000000..71f374b --- /dev/null +++ b/drafts/rosettacode/100-doors.wiki @@ -0,0 +1,62 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note pass p (1..100) toggles every p-th door; afterwards print the open doors +fn main to int +d = new uchar 101 0 +for k ulong 100 +p = add k 1 +i = p +while le i 100 bound 101 +put d i xor get d i 1 +i = add i p +end +end +f = true +for i ulong 101 +if eq get d i 1 +if not f +emit sp +end +emitu i +f = false +end +end +emit nl +ret 0 +end + +{{out}} +
+1 4 9 16 25 36 49 64 81 100
+
diff --git a/drafts/rosettacode/abc-problem.wiki b/drafts/rosettacode/abc-problem.wiki new file mode 100644 index 0000000..6950355 --- /dev/null +++ b/drafts/rosettacode/abc-problem.wiki @@ -0,0 +1,92 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text tyes "yes" +text tno "no" +note the 20 blocks, two letters each, separated by spaces +text blocks "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM" +text w0 "A" +text w1 "BARK" +text w2 "BOOK" +text w3 "TREAT" +text w4 "COMMON" +text w5 "SQUAD" +text w6 "CONFUSE" +global used uchar 20 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +fn emitb b bool to void +if b +emit tyes +else +emit tno +end +end +note onblock: block b carries letter c +fn onblock b ulong c uchar to bool +x = mul b 3 +ret or eq get blocks x c eq get blocks add x 1 c +end +note can: letters i.. of w can be spelled with the unused blocks (depth-first, a block is taken +note back when the rest fails) +fn can w view uchar * i ulong to bool +if eq i len w +ret true +end +c = get w i +for b ulong 20 +if eq get used b 0 +if onblock b c +put used b 1 +if can w add i 1 +ret true +end +put used b 0 +end +end +end +ret false +end +fn show w view uchar * to void +for b ulong 20 +put used b 0 +end +emit w +emit sp +emitb can w 0 +emit nl +end +fn main to int +show w0 +show w1 +show w2 +show w3 +show w4 +show w5 +show w6 +ret 0 +end + +{{out}} +
+A yes
+BARK yes
+BOOK no
+TREAT yes
+COMMON no
+SQUAD yes
+CONFUSE yes
+
diff --git a/drafts/rosettacode/abelian-sandpile-model.wiki b/drafts/rosettacode/abelian-sandpile-model.wiki new file mode 100644 index 0000000..fa0ff8d --- /dev/null +++ b/drafts/rosettacode/abelian-sandpile-model.wiki @@ -0,0 +1,127 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text ttop "topplings: " +global grid ulong 81 +global tops ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a cell holding 4 or more grains topples: it loses 4 and each of its 4 neighbours gains 1; grains +note pushed past the edge are lost; the end state and the number of topplings do not depend on order +fn give k ulong r long c long n ulong to void +if and and ge r 0 lt r long n and ge c 0 lt c long n +j = add mul ulong r n ulong c +put grid j add get grid j 1 +end +end +fn topple r ulong c ulong n ulong to void +k = add mul r n c +put grid k sub get grid k 4 +put tops 0 add get tops 0 1 +give k sub long r 1 long c n +give k add long r 1 long c n +give k long r sub long c 1 n +give k long r add long c 1 n +end +note sweep topples every full cell once and says whether any toppled +fn sweep n ulong to bool +any = false +for r ulong n +for c ulong n +if ge get grid add mul r n c 4 +topple r c n +any = true +end +end +end +ret any +end +note sweeping in this order takes 6, 25 and 91 sweeps for the three cases (counted on the host); +note 10000 bounds them with room to spare +fn pile n ulong grains ulong to void +for i ulong mul n n +put grid i 0 +end +put tops 0 0 +m = div n 2 +put grid add mul m n m grains +while sweep n bound 10000 +end +for r ulong n +for c ulong n +if gt c 0 +emit sp +end +emitu get grid add mul r n c +end +emit nl +end +emit ttop +emitu get tops 0 +emit nl +end +fn main to int +pile 5 16 +emit nl +pile 7 64 +emit nl +pile 9 200 +ret 0 +end + +{{out}} +
+0 0 1 0 0
+0 2 1 2 0
+1 1 0 1 1
+0 2 1 2 0
+0 0 1 0 0
+topplings: 9
+
+0 0 1 2 1 0 0
+0 2 2 2 2 2 0
+1 2 2 2 2 2 1
+2 2 2 0 2 2 2
+1 2 2 2 2 2 1
+0 2 2 2 2 2 0
+0 0 1 2 1 0 0
+topplings: 88
+...
+2 3 2 2 3 2 2 3 2
+3 2 2 2 3 2 2 2 3
+1 0 3 3 0 3 3 0 1
+3 2 2 2 3 2 2 2 3
+2 3 2 2 3 2 2 3 2
+3 0 3 2 0 2 3 0 3
+0 3 2 3 1 3 2 3 0
+topplings: 758
+
diff --git a/drafts/rosettacode/abundant-deficient-and-perfect-number-classifications.wiki b/drafts/rosettacode/abundant-deficient-and-perfect-number-classifications.wiki new file mode 100644 index 0000000..0327c9a --- /dev/null +++ b/drafts/rosettacode/abundant-deficient-and-perfect-number-classifications.wiki @@ -0,0 +1,86 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text tdef "deficient: " +text tper "perfect: " +text tabu "abundant: " +note ds n is the sum of the proper divisors of n, for n up to 20000 +global ds ulong 20001 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note each i adds itself to its multiples 2i, 3i, ... up to 20000; fewer than 20000 multiples per i +fn sieve to void +i = ulong 1 +while le i 10000 bound 10000 +j = mul i 2 +while le j 20000 bound 20000 +put ds j add get ds j i +j = add j i +end +i = add i 1 +end +end +note 1..20000 sorted by comparing each n with the sum of its proper divisors +fn main to int +sieve +d = ulong 0 +p = ulong 0 +a = ulong 0 +for i ulong 20000 +n = add i 1 +s = get ds n +if lt s n +d = add d 1 +end +if eq s n +p = add p 1 +end +if gt s n +a = add a 1 +end +end +emit tdef +emitu d +emit nl +emit tper +emitu p +emit nl +emit tabu +emitu a +emit nl +ret 0 +end + +{{out}} +
+deficient: 15043
+perfect:   4
+abundant:  4953
+
diff --git a/drafts/rosettacode/additive-primes.wiki b/drafts/rosettacode/additive-primes.wiki new file mode 100644 index 0000000..142c501 --- /dev/null +++ b/drafts/rosettacode/additive-primes.wiki @@ -0,0 +1,106 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text hdr "additive primes below 500 (prime, and the digit sum is prime):\n" +text cnt "count: " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n in decimal right-aligned in w columns (w at most 20) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt i sub 20 w +i = sub 20 w +end +emit part d i sub 20 i +end +fn isprime n ulong to bool +if lt n 2 +ret false +end +d = ulong 2 +while le mul d d n bound 32 +if eq rem n d 0 +ret false +end +d = add d 1 +end +ret true +end +fn dsum n ulong to ulong +s = ulong 0 +v = n +while gt v 0 bound 4 +s = add s rem v 10 +v = div v 10 +end +ret s +end +fn main to int +emit hdr +c = ulong 0 +for n ulong 500 +if isprime n +if isprime dsum n +emitw n 4 +c = add c 1 +if eq rem c 10 0 +emit nl +end +end +end +end +emit nl +emit cnt +emitu c +emit nl +ret 0 +end + +{{out}} +
+additive primes below 500 (prime, and the digit sum is prime):
+   2   3   5   7  11  23  29  41  43  47
+  61  67  83  89 101 113 131 137 139 151
+ 157 173 179 191 193 197 199 223 227 229
+ 241 263 269 281 283 311 313 317 331 337
+ 353 359 373 379 397 401 409 421 443 449
+ 461 463 467 487
+count: 54
+
diff --git a/drafts/rosettacode/almost-prime.wiki b/drafts/rosettacode/almost-prime.wiki new file mode 100644 index 0000000..f440108 --- /dev/null +++ b/drafts/rosettacode/almost-prime.wiki @@ -0,0 +1,83 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text kk "k = " +text col ":" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a k-almost prime has exactly k prime factors counted with multiplicity +fn bigo n ulong to ulong +v = n +d = ulong 2 +k = ulong 0 +while le mul d d v bound 64 +if eq rem v d 0 +k = add k 1 +v = div v d +else +d = add d 1 +end +end +if gt v 1 +k = add k 1 +end +ret k +end +note bound: the 10th 5-almost prime is 176, so no list needs more than 200 candidates +fn main to int +for j ulong 5 +k = add j 1 +emit kk +emitu k +emit col +c = ulong 0 +n = ulong 2 +while lt c 10 bound 200 +if eq bigo n k +emit sp +emitu n +c = add c 1 +end +n = add n 1 +end +emit nl +end +ret 0 +end + +{{out}} +
+k = 1: 2 3 5 7 11 13 17 19 23 29
+k = 2: 4 6 9 10 14 15 21 22 25 26
+k = 3: 8 12 18 20 27 28 30 42 44 45
+k = 4: 16 24 36 40 54 56 60 81 84 88
+k = 5: 32 48 72 80 108 112 120 162 168 176
+
diff --git a/drafts/rosettacode/amicable-pairs.wiki b/drafts/rosettacode/amicable-pairs.wiki new file mode 100644 index 0000000..1ccbacb --- /dev/null +++ b/drafts/rosettacode/amicable-pairs.wiki @@ -0,0 +1,74 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +note ds n is the sum of the proper divisors of n, for n below 20000 +global ds ulong 20000 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note each i adds itself to its multiples 2i, 3i, ... below 20000; fewer than 20000 multiples per i +fn sieve to void +i = ulong 1 +while lt i 10000 bound 10000 +j = mul i 2 +while lt j 20000 bound 20000 +put ds j add get ds j i +j = add j i +end +i = add i 1 +end +end +fn main to int +sieve +for n ulong 20000 +m = get ds n +if and gt m n lt m 20000 +if eq get ds m n +emitu n +emit sp +emitu m +emit nl +end +end +end +ret 0 +end + +{{out}} +
+220 284
+1184 1210
+2620 2924
+5020 5564
+6232 6368
+10744 10856
+12285 14595
+17296 18416
+
diff --git a/drafts/rosettacode/anagrams.wiki b/drafts/rosettacode/anagrams.wiki new file mode 100644 index 0000000..f457873 --- /dev/null +++ b/drafts/rosettacode/anagrams.wiki @@ -0,0 +1,133 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +note the word list (Rosetta Code reads unixdict.txt; this is a fixed sample of it), newline-terminated +text dict "abel\nable\nbale\nbela\nelba\nalger\nglare\nlager\nlarge\nregal\nangel\nangle\ngalen\nglean\nlange\ncaret\ncarte\ncater\ncrate\ntrace\nevil\nlevi\nlive\nveil\nvile\nelan\nlane\nlean\nlena\nneal\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note key writes the letters of a sorted (insertion sort) into k; words are at most 16 letters +fn key a view uchar * k edit uchar 16 to void +for i ulong 16 +put k i 0 +end +for i ulong len a +c = get a i +j = i +while gt j 0 bound 16 +if le get k sub j 1 c +break +end +put k j get k sub j 1 +j = sub j 1 +end +put k j c +end +end +fn same x view uchar 16 y view uchar 16 to bool +for i ulong 16 +if ne get x i get y i +ret false +end +end +ret true +end +note index records each word's (start, length) and returns how many words there are +fn index st edit ulong 64 ln edit ulong 64 to ulong +k = ulong 0 +s = ulong 0 +for i ulong len dict +if eq get dict i 10 +put st k s +put ln k sub i s +k = add k 1 +s = add i 1 +end +end +ret k +end +note count: how many words share word w's key +fn count st view ulong 64 ln view ulong 64 n ulong w ulong to ulong +a = new uchar 16 0 +b = new uchar 16 0 +key part dict get st w get ln w a +c = ulong 0 +for v ulong n +key part dict get st v get ln v b +if same a b +c = add c 1 +end +end +ret c +end +note first: no earlier word has word w's key +fn first st view ulong 64 ln view ulong 64 w ulong to bool +a = new uchar 16 0 +b = new uchar 16 0 +key part dict get st w get ln w a +for v ulong w +key part dict get st v get ln v b +if same a b +ret false +end +end +ret true +end +note print every group of the largest size, groups by first appearance, words in list order +fn show st view ulong 64 ln view ulong 64 n ulong best ulong to void +a = new uchar 16 0 +b = new uchar 16 0 +for w ulong n +if and first st ln w eq count st ln n w best +key part dict get st w get ln w a +f = true +for v ulong n +key part dict get st v get ln v b +if same a b +if not f +emit sp +end +emit part dict get st v get ln v +f = false +end +end +emit nl +end +end +end +fn main to int +st = new ulong 64 0 +ln = new ulong 64 0 +n = index st ln +best = ulong 0 +for w ulong n +c = count st ln n w +if gt c best +best = c +end +end +show st ln n best +ret 0 +end + +{{out}} +
+abel able bale bela elba
+alger glare lager large regal
+angel angle galen glean lange
+caret carte cater crate trace
+evil levi live veil vile
+elan lane lean lena neal
+
diff --git a/drafts/rosettacode/anti-primes.wiki b/drafts/rosettacode/anti-primes.wiki new file mode 100644 index 0000000..69eee2f --- /dev/null +++ b/drafts/rosettacode/anti-primes.wiki @@ -0,0 +1,73 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text first "first 20 anti-primes:" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note divisor count by pairing d with n / d for d * d <= n +fn ndiv n ulong to ulong +c = ulong 0 +d = ulong 1 +while le mul d d n bound 100 +if eq rem n d 0 +c = add c 2 +if eq mul d d n +c = sub c 1 +end +end +d = add d 1 +end +ret c +end +note an anti-prime has more divisors than every smaller positive integer; the 20th is 7560 +fn main to int +emit first +best = ulong 0 +c = ulong 0 +n = ulong 1 +while lt c 20 bound 7560 +k = ndiv n +if gt k best +best = k +emit sp +emitu n +c = add c 1 +end +n = add n 1 +end +emit nl +ret 0 +end + +{{out}} +
+first 20 anti-primes: 1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
+
diff --git a/drafts/rosettacode/array-concatenation.wiki b/drafts/rosettacode/array-concatenation.wiki new file mode 100644 index 0000000..aaa4280 --- /dev/null +++ b/drafts/rosettacode/array-concatenation.wiki @@ -0,0 +1,68 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note cat copies x then y into r and returns the length +fn cat x view ulong * y view ulong * r edit ulong * to ulong +for i ulong len x +put r i get x i +end +for i ulong len y +put r add len x i get y i +end +ret add len x len y +end +fn main to int +a = new ulong 3 0 +b = new ulong 4 0 +c = new ulong 7 0 +for i ulong 3 +put a i add i 1 +end +for i ulong 4 +put b i add i 4 +end +n = cat a b c +for i ulong n +if gt i 0 +emit sp +end +emitu get c i +end +emit nl +ret 0 +end + +{{out}} +
+1 2 3 4 5 6 7
+
diff --git a/drafts/rosettacode/attractive-numbers.wiki b/drafts/rosettacode/attractive-numbers.wiki new file mode 100644 index 0000000..a5214e9 --- /dev/null +++ b/drafts/rosettacode/attractive-numbers.wiki @@ -0,0 +1,96 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text hdr "attractive numbers up to 120 (the count of prime factors, with multiplicity, is prime):\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n in decimal right-aligned in w columns (w at most 20) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt i sub 20 w +i = sub 20 w +end +emit part d i sub 20 i +end +note Omega(n): the number of prime factors of n counted with multiplicity +fn bigo n ulong to ulong +v = n +d = ulong 2 +k = ulong 0 +while le mul d d v bound 64 +if eq rem v d 0 +k = add k 1 +v = div v d +else +d = add d 1 +end +end +if gt v 1 +k = add k 1 +end +ret k +end +note Omega(n) <= 6 for n <= 120, so the primes to test are 2, 3 and 5 +fn main to int +emit hdr +c = ulong 0 +for i ulong 120 +n = add i 1 +k = bigo n +if or or eq k 2 eq k 3 eq k 5 +emitw n 4 +c = add c 1 +if eq rem c 20 0 +emit nl +end +end +end +emit nl +ret 0 +end + +{{out}} +
+attractive numbers up to 120 (the count of prime factors, with multiplicity, is prime):
+   4   6   8   9  10  12  14  15  18  20  21  22  25  26  27  28  30  32  33  34
+  35  38  39  42  44  45  46  48  49  50  51  52  55  57  58  62  63  65  66  68
+  69  70  72  74  75  76  77  78  80  82  85  86  87  91  92  93  94  95  98  99
+ 102 105 106 108 110 111 112 114 115 116 117 118 119 120
+
diff --git a/drafts/rosettacode/bacon-cipher.wiki b/drafts/rosettacode/bacon-cipher.wiki new file mode 100644 index 0000000..46730db --- /dev/null +++ b/drafts/rosettacode/bacon-cipher.wiki @@ -0,0 +1,93 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text secret "rosetta code" +text cover "bacon's cipher is a method of steganography devised by francis bacon. a message is concealed in the presentation of text rather than its content." +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note 26-letter variant: letter n (a = 0) is 5 bits, most significant first; a space is 26 (11010). +note The bits ride on the cover text's letters: 0 lower case, 1 upper case; other bytes are copied, +note letters after the last bit stay lower case. Decoding reads the case of the letters 5 at a time. +fn code c uchar to ulong +if eq c 32 +ret 26 +end +ret ulong sub c 97 +end +fn isl c uchar to bool +ret and ge c 97 le c 122 +end +fn enc o edit uchar * to void +nb = mul len secret 5 +b = ulong 0 +for i ulong len cover +c = get cover i +if and isl c lt b nb +v = code get secret div b 5 +if eq and shr v sub 4 rem b 5 1 1 +c = sub c 32 +end +b = add b 1 +end +put o i c +end +end +fn dec e view uchar * n ulong to void +v = ulong 0 +k = ulong 0 +m = ulong 0 +for i ulong len e +c = get e i +if and lt m n or isl c and ge c 65 le c 90 +v = shl v 1 +if le c 90 +v = or v 1 +end +k = add k 1 +if eq k 5 +if eq v 26 +emitc 32 +else +emitc add uchar v 97 +end +v = 0 +k = 0 +m = add m 1 +end +end +end +emit nl +end +fn main to int +o = new uchar 256 0 +n = len cover +enc part o 0 n +emit part o 0 n +emit nl +dec part o 0 n len secret +ret 0 +end + +{{out}} +
+BacoN's CIPhEr iS a meThoD of STEgaNOgraphY DeVised By fRANcis bACon. A message is concealed in the presentation of text rather than its content.
+rosetta code
+
diff --git a/drafts/rosettacode/base64-encode-data.wiki b/drafts/rosettacode/base64-encode-data.wiki new file mode 100644 index 0000000..7a8bcea --- /dev/null +++ b/drafts/rosettacode/base64-encode-data.wiki @@ -0,0 +1,71 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text abc "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" +text m0 "M" +text m1 "Ma" +text m2 "Man" +text m3 "Hello, World!" +text m4 "To err is human, but to really foul things up you need a computer.\n -- Paul R. Ehrlich" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note b64 takes the input 3 bytes (24 bits) at a time and writes 4 alphabet characters, '=' padding +note a short last group; output up to 512 bytes (input up to 384, longer traps 3) +fn b64 a view uchar * to void +o = new uchar 512 0 +p = ulong 0 +i = ulong 0 +while lt i len a bound 200 +r = sub len a i +v = shl ulong get a i 16 +if gt r 1 +v = or v shl ulong get a add i 1 8 +end +if gt r 2 +v = or v ulong get a add i 2 +end +put o p get abc shr v 18 +put o add p 1 get abc and shr v 12 63 +put o add p 2 61 +put o add p 3 61 +if gt r 1 +put o add p 2 get abc and shr v 6 63 +end +if gt r 2 +put o add p 3 get abc and v 63 +end +p = add p 4 +i = add i 3 +end +emit part o 0 p +emit nl +end +fn main to int +b64 m0 +b64 m1 +b64 m2 +b64 m3 +b64 m4 +ret 0 +end + +{{out}} +
+TQ==
+TWE=
+TWFu
+SGVsbG8sIFdvcmxkIQ==
+VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
+
diff --git a/drafts/rosettacode/bead-sort.wiki b/drafts/rosettacode/bead-sort.wiki new file mode 100644 index 0000000..5257681 --- /dev/null +++ b/drafts/rosettacode/bead-sort.wiki @@ -0,0 +1,129 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "5 3 1 7 4 1 1 20 0 9" +global vals long 16 +global grid uchar 512 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note the abacus has one row per value (at most 16) and one column per unit (at most 32): grid[r*32+c] +note holds a bead when c < vals[r]; the values here are 0 .. 20 +fn bdsort n ulong to void +for r ulong n +for c ulong ulong get vals r +put grid add mul r 32 c 1 +end +end +note gravity: each column's beads fall to the bottom rows +for c ulong 32 +h = ulong 0 +for r ulong n +h = add h ulong get grid add mul r 32 c +put grid add mul r 32 c 0 +end +for x ulong h +put grid add mul sub sub n x 1 32 c 1 +end +end +note row r now holds the r-th smallest value as its bead count, read top to bottom +for r ulong n +v = long 0 +for c ulong 32 +v = add v long get grid add mul r 32 c +end +put vals r v +end +end +fn main to int +n = parse data +show n +bdsort n +show n +ret 0 +end + +{{out}} +
+5 3 1 7 4 1 1 20 0 9
+0 1 1 1 3 4 5 7 9 20
+
diff --git a/drafts/rosettacode/bell-numbers.wiki b/drafts/rosettacode/bell-numbers.wiki new file mode 100644 index 0000000..beb62f2 --- /dev/null +++ b/drafts/rosettacode/bell-numbers.wiki @@ -0,0 +1,132 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text tri "first 10 rows of the Bell triangle:\n" +text first "first 15 Bell numbers:" +text b49 "B(49), the 50th Bell number: " +note a triangle row of up to 50 entries; entry e is 6 limbs base 10^9 at 6e..6e+5, least significant first +note 6 limbs hold below 10^54; the largest entry needed, in row 49, is below B(50) < 2 * 10^47 +global pv ulong 300 +global cu ulong 300 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note row i of the Bell triangle starts with the last entry of row i - 1; each next entry adds the entry +note to its left and the entry above that one. Row i - 1 sits in pv and row i is built in cu, then copied +fn bellrow i ulong to void +for l ulong 6 +put cu l get pv add mul sub i 1 6 l +end +for j ulong i +c = ulong 0 +for l ulong 6 +o = add mul j 6 l +s = add get cu o c +s = add s get pv o +put cu add o 6 rem s 1000000000 +c = div s 1000000000 +end +end +for t ulong mul add i 1 6 +put pv t get cu t +end +end +note pv entry 0 in decimal: the top non-zero limb, then each lower limb padded to 9 digits +fn emitb to void +k = ulong 5 +while and gt k 0 eq get pv k 0 bound 6 +k = sub k 1 +end +emitu get pv k +for t ulong k +v = get pv sub sub k 1 t +d = ulong 100000000 +while gt d 0 bound 9 +emitu rem div v d 10 +d = div d 10 +end +end +end +fn main to int +bs = new ulong 15 0 +put pv 0 1 +put bs 0 1 +emit tri +emitu 1 +emit nl +i = ulong 1 +while le i 49 bound 49 +bellrow i +if lt i 10 +for j ulong add i 1 +if gt j 0 +emit sp +end +emitu get pv mul j 6 +end +emit nl +end +if lt i 15 +put bs i get pv 0 +end +if lt i 49 +i = add i 1 +else +break +end +end +emit first +for j ulong 15 +emit sp +emitu get bs j +end +emit nl +emit b49 +emitb +emit nl +ret 0 +end + +{{out}} +
+first 10 rows of the Bell triangle:
+1
+1 2
+2 3 5
+5 7 10 15
+15 20 27 37 52
+52 67 87 114 151 203
+203 255 322 409 523 674 877
+877 1080 1335 1657 2066 2589 3263 4140
+4140 5017 6097 7432 9089 11155 13744 17007 21147
+21147 25287 30304 36401 43833 52922 64077 77821 94828 115975
+first 15 Bell numbers: 1 1 2 5 15 52 203 877 4140 21147 115975 678570 4213597 27644437 190899322
+B(49), the 50th Bell number: 10726137154573358400342215518590002633917247281
+
diff --git a/drafts/rosettacode/bogosort.wiki b/drafts/rosettacode/bogosort.wiki new file mode 100644 index 0000000..44b850a --- /dev/null +++ b/drafts/rosettacode/bogosort.wiki @@ -0,0 +1,141 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "3 -1 4 1 5 9 2" +text tshuf "shuffles: " +global vals long 8 +global seed ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note rnd is Knuth's MMIX linear congruential generator, x = x*6364136223846793005 + 1442695040888963407 +note mod 2^64 (ulong arithmetic wraps), starting from seed 0; it returns the top 31 bits, x >> 33 +fn rnd to ulong +x = add mul get seed 0 6364136223846793005 1442695040888963407 +put seed 0 x +ret shr x 33 +end +note shuffle is Fisher-Yates: for i = n-1 .. 1 swap vals[i] with vals[rnd mod (i+1)] +fn shuffle n ulong to void +for x ulong sub n 1 +i = sub sub n x 1 +j = rem rnd add i 1 +t = get vals i +put vals i get vals j +put vals j t +end +end +fn inorder n ulong to bool +for i ulong sub n 1 +if gt get vals i get vals add i 1 +ret false +end +end +ret true +end +note bgsort shuffles until sorted; with this generator and input it takes the count printed, well under +note the bound of 200000 (7! = 5040 orders, so a working generator lands far inside it) +fn bgsort n ulong to ulong +c = ulong 0 +while not inorder n bound 200000 +shuffle n +c = add c 1 +end +ret c +end +fn main to int +n = parse data +show n +c = bgsort n +show n +emit tshuf +emitu c +emit nl +ret 0 +end + +{{out}} +
+3 -1 4 1 5 9 2
+-1 1 2 3 4 5 9
+shuffles: 2314
+
diff --git a/drafts/rosettacode/caesar-cipher.wiki b/drafts/rosettacode/caesar-cipher.wiki new file mode 100644 index 0000000..9c5bbf3 --- /dev/null +++ b/drafts/rosettacode/caesar-cipher.wiki @@ -0,0 +1,51 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text msg "The quick brown fox Jumped over the lazy Dog" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note shift rotates ASCII letters by k places (0..25) into r; other bytes are copied +fn shift a view uchar * r edit uchar * k uchar to void +for i ulong len a +c = get a i +if and ge c 65 le c 90 +c = add rem add sub c 65 k 26 65 +else +if and ge c 97 le c 122 +c = add rem add sub c 97 k 26 97 +end +end +put r i c +end +end +fn main to int +n = len msg +e = new uchar 256 0 +d = new uchar 256 0 +shift msg part e 0 n 11 +emit part e 0 n +emit nl +note decoding is a shift by 26 - 11 +shift part e 0 n part d 0 n 15 +emit part d 0 n +emit nl +ret 0 +end + +{{out}} +
+Esp bftnv mczhy qzi Ufxapo zgpc esp wlkj Ozr
+The quick brown fox Jumped over the lazy Dog
+
diff --git a/drafts/rosettacode/camel-case-and-snake-case.wiki b/drafts/rosettacode/camel-case-and-snake-case.wiki new file mode 100644 index 0000000..8c51a68 --- /dev/null +++ b/drafts/rosettacode/camel-case-and-snake-case.wiki @@ -0,0 +1,128 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text tsn " -> snake: " +text tcm " -> camel: " +text c0 "snakeCase" +text c1 "snake_case" +text c2 "variable_10_case" +text c3 "variable10Case" +text c4 "hurry-up-joe!" +text c5 "c://my-docs/happy_Flag-Day/12.doc" +text c6 " spaces " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note leading and trailing spaces are kept as they are; inside the text, space and '-' count as +note separators like '_' +fn issep c uchar to bool +ret or or eq c 95 eq c 45 eq c 32 +end +fn lead a view uchar * to ulong +for i ulong len a +if ne get a i 32 +ret i +end +end +ret len a +end +fn trail a view uchar * to ulong +for i ulong len a +if ne get a sub sub len a 1 i 32 +ret sub len a i +end +end +ret 0 +end +note snake: a separator becomes '_', an upper-case letter becomes '_' and its lower case (no '_' first +note when it is at the start or follows a separator) +fn snake a view uchar * to void +l = lead a +t = trail a +emit part a 0 l +for i ulong sub t l +k = add l i +c = get a k +if issep c +emitc 95 +else +if and ge c 65 le c 90 +if gt i 0 +if not issep get a sub k 1 +emitc 95 +end +end +emitc add c 32 +else +emitc c +end +end +end +emit part a t sub len a t +end +note camel: separators are dropped and the letter after one is upper-cased +fn camel a view uchar * to void +l = lead a +t = trail a +emit part a 0 l +up = false +for i ulong sub t l +c = get a add l i +if issep c +up = true +else +if and up and ge c 97 le c 122 +c = sub c 32 +end +emitc c +up = false +end +end +emit part a t sub len a t +end +fn show a view uchar * to void +emit a +emit tsn +snake a +emit tcm +camel a +emit nl +end +fn main to int +show c0 +show c1 +show c2 +show c3 +show c4 +show c5 +show c6 +ret 0 +end + +{{out}} +
+snakeCase -> snake: snake_case -> camel: snakeCase
+snake_case -> snake: snake_case -> camel: snakeCase
+variable_10_case -> snake: variable_10_case -> camel: variable10Case
+variable10Case -> snake: variable10_case -> camel: variable10Case
+hurry-up-joe! -> snake: hurry_up_joe! -> camel: hurryUpJoe!
+c://my-docs/happy_Flag-Day/12.doc -> snake: c://my_docs/happy_flag_day/12.doc -> camel: c://myDocs/happyFlagDay/12.doc
+  spaces   -> snake:   spaces   -> camel:   spaces  
+
diff --git a/drafts/rosettacode/catalan-numbers.wiki b/drafts/rosettacode/catalan-numbers.wiki new file mode 100644 index 0000000..c86c5af --- /dev/null +++ b/drafts/rosettacode/catalan-numbers.wiki @@ -0,0 +1,82 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text cc "C(" +text eqs ") = " +text by " by the recurrence, " +text sum " by the convolution sum\n" +text hdr "n, C(n) by C(n+1) = 2(2n+1)/(n+2) C(n), and again by C(n+1) = sum C(i) C(n-i):\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note cs holds C(0)..C(14) from the convolution; all stay below 2^32, so every product fits +fn main to int +cs = new ulong 15 0 +put cs 0 1 +for n ulong 14 +s = ulong 0 +for i ulong add n 1 +s = add s mul get cs i get cs sub n i +end +put cs add n 1 s +end +emit hdr +c = ulong 1 +for n ulong 15 +emit cc +emitu n +emit eqs +emitu c +emit by +emitu get cs n +emit sum +c = div mul c mul 2 add mul 2 n 1 add n 2 +end +ret 0 +end + +{{out}} +
+n, C(n) by C(n+1) = 2(2n+1)/(n+2) C(n), and again by C(n+1) = sum C(i) C(n-i):
+C(0) = 1 by the recurrence, 1 by the convolution sum
+C(1) = 1 by the recurrence, 1 by the convolution sum
+C(2) = 2 by the recurrence, 2 by the convolution sum
+C(3) = 5 by the recurrence, 5 by the convolution sum
+C(4) = 14 by the recurrence, 14 by the convolution sum
+C(5) = 42 by the recurrence, 42 by the convolution sum
+C(6) = 132 by the recurrence, 132 by the convolution sum
+C(7) = 429 by the recurrence, 429 by the convolution sum
+C(8) = 1430 by the recurrence, 1430 by the convolution sum
+C(9) = 4862 by the recurrence, 4862 by the convolution sum
+C(10) = 16796 by the recurrence, 16796 by the convolution sum
+C(11) = 58786 by the recurrence, 58786 by the convolution sum
+C(12) = 208012 by the recurrence, 208012 by the convolution sum
+C(13) = 742900 by the recurrence, 742900 by the convolution sum
+C(14) = 2674440 by the recurrence, 2674440 by the convolution sum
+
diff --git a/drafts/rosettacode/character-codes.wiki b/drafts/rosettacode/character-codes.wiki new file mode 100644 index 0000000..21ef947 --- /dev/null +++ b/drafts/rosettacode/character-codes.wiki @@ -0,0 +1,58 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text ch "a" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +fn main to int +note a byte is a number: the code of a is get ch 0, and the number 97 printed as a byte is a +emit ch +emit sp +emitu ulong get ch 0 +emit nl +emitu 97 +emit sp +emitc 97 +emit nl +ret 0 +end + +{{out}} +
+a 97
+97 a
+
diff --git a/drafts/rosettacode/cocktail-sort-with-shifting-bounds.wiki b/drafts/rosettacode/cocktail-sort-with-shifting-bounds.wiki new file mode 100644 index 0000000..0cf9d4f --- /dev/null +++ b/drafts/rosettacode/cocktail-sort-with-shifting-bounds.wiki @@ -0,0 +1,133 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "21 -4 7 7 0 88 -15 3 42 9 1 -4 60" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note cssort runs forward and backward passes over vals[b..e]; after a forward pass everything past the +note last swap is in place, so e moves to it, and b likewise after a backward pass; each round moves +note e down or b up by at least one, so n rounds (at most 16) suffice +fn cssort n ulong to void +b = ulong 0 +e = sub n 1 +while lt b e bound 16 +ls = b +i = b +while lt i e bound 16 +if gt get vals i get vals add i 1 +t = get vals i +put vals i get vals add i 1 +put vals add i 1 t +ls = i +end +i = add i 1 +end +e = ls +fs = e +i = e +while gt i b bound 16 +if gt get vals sub i 1 get vals i +t = get vals i +put vals i get vals sub i 1 +put vals sub i 1 t +fs = i +end +i = sub i 1 +end +b = fs +end +end +fn main to int +n = parse data +show n +cssort n +show n +ret 0 +end + +{{out}} +
+21 -4 7 7 0 88 -15 3 42 9 1 -4 60
+-15 -4 -4 0 1 3 7 7 9 21 42 60 88
+
diff --git a/drafts/rosettacode/cocktail-sort.wiki b/drafts/rosettacode/cocktail-sort.wiki new file mode 100644 index 0000000..f42a5c7 --- /dev/null +++ b/drafts/rosettacode/cocktail-sort.wiki @@ -0,0 +1,133 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "5 1 -4 2 8 0 2 -9 13 7 3" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note cswap swaps vals[i] and vals[i+1] when they are out of order and says whether it did +fn cswap i ulong to bool +if gt get vals i get vals add i 1 +t = get vals i +put vals i get vals add i 1 +put vals add i 1 t +ret true +end +ret false +end +note csort passes forward then backward until a pass swaps nothing; each round puts at least the +note largest remaining element in place, so n rounds (at most 16) suffice +fn csort n ulong to void +while true bound 16 +done = true +for i ulong sub n 1 +if cswap i +done = false +end +end +if done +break +end +for x ulong sub n 1 +if cswap sub sub n 2 x +done = false +end +end +if done +break +end +end +end +fn main to int +n = parse data +show n +csort n +show n +ret 0 +end + +{{out}} +
+5 1 -4 2 8 0 2 -9 13 7 3
+-9 -4 0 1 2 2 3 5 7 8 13
+
diff --git a/drafts/rosettacode/comb-sort.wiki b/drafts/rosettacode/comb-sort.wiki new file mode 100644 index 0000000..804c94f --- /dev/null +++ b/drafts/rosettacode/comb-sort.wiki @@ -0,0 +1,121 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "63 -2 18 7 0 99 41 -30 5 5 72 11 -8 26" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note cbsort shrinks the gap by 10/13 each pass and finishes with gap-1 passes until nothing swaps; +note gap 1 then behaves as bubble sort, which needs at most n (16) passes, plus at most 10 shrink passes +fn cbsort n ulong to void +g = n +done = false +while or gt g 1 not done bound 26 +g = div mul g 10 13 +if eq g 0 +g = 1 +end +done = true +for x ulong sub n g +if gt get vals x get vals add x g +t = get vals x +put vals x get vals add x g +put vals add x g t +done = false +end +end +end +end +fn main to int +n = parse data +show n +cbsort n +show n +ret 0 +end + +{{out}} +
+63 -2 18 7 0 99 41 -30 5 5 72 11 -8 26
+-30 -8 -2 0 5 5 7 11 18 26 41 63 72 99
+
diff --git a/drafts/rosettacode/combinations-with-repetitions.wiki b/drafts/rosettacode/combinations-with-repetitions.wiki new file mode 100644 index 0000000..868d650 --- /dev/null +++ b/drafts/rosettacode/combinations-with-repetitions.wiki @@ -0,0 +1,103 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text t0 "iced" +text t1 "jam" +text t2 "plain" +text tcount "choices of 3 from 10 kinds: " +global pick ulong 4 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note nextm steps pick[0..k-1], a non-decreasing list of kinds 0..n-1, to the next one in lexicographic +note order: raise the last pick below n-1 and set every pick after it to the same kind +fn nextm k ulong n ulong to bool +i = k +while gt i 0 bound 4 +i = sub i 1 +if lt get pick i sub n 1 +v = add get pick i 1 +for x ulong sub k i +put pick add i x v +end +ret true +end +end +ret false +end +fn kind d ulong to void +if eq d 0 +emit t0 +end +if eq d 1 +emit t1 +end +if eq d 2 +emit t2 +end +end +fn main to int +note the two-doughnut choices from three kinds: C(3+2-1, 2) = 6 +put pick 0 0 +put pick 1 0 +while true bound 6 +kind get pick 0 +emit sp +kind get pick 1 +emit nl +if not nextm 2 3 +break +end +end +note counted, not computed: C(10+3-1, 3) = 220 choices bound the loop +put pick 0 0 +put pick 1 0 +put pick 2 0 +c = ulong 1 +while nextm 3 10 bound 220 +c = add c 1 +end +emit tcount +emitu c +emit nl +ret 0 +end + +{{out}} +
+iced iced
+iced jam
+iced plain
+jam jam
+jam plain
+plain plain
+choices of 3 from 10 kinds: 220
+
diff --git a/drafts/rosettacode/combinations.wiki b/drafts/rosettacode/combinations.wiki new file mode 100644 index 0000000..67eac92 --- /dev/null +++ b/drafts/rosettacode/combinations.wiki @@ -0,0 +1,94 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tcount "combinations: " +global comb ulong 8 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note nextc steps comb[0..k-1] to the next k-subset of 0..n-1 in lexicographic order: raise the last +note element that can still rise and reset the ones after it; false after the last subset +fn nextc k ulong n ulong to bool +i = k +while gt i 0 bound 8 +i = sub i 1 +if lt get comb i add sub n k i +put comb i add get comb i 1 +for x ulong sub sub k i 1 +j = add add i x 1 +put comb j add get comb sub j 1 1 +end +ret true +end +end +ret false +end +fn show k ulong to void +for i ulong k +if gt i 0 +emit sp +end +emitu get comb i +end +emit nl +end +note 5 choose 3 is 10, which bounds the loop +fn main to int +k = ulong 3 +for i ulong k +put comb i i +end +c = ulong 1 +show k +while nextc k 5 bound 10 +show k +c = add c 1 +end +emit tcount +emitu c +emit nl +ret 0 +end + +{{out}} +
+0 1 2
+0 1 3
+0 1 4
+0 2 3
+0 2 4
+0 3 4
+1 2 3
+1 2 4
+1 3 4
+2 3 4
+combinations: 10
+
diff --git a/drafts/rosettacode/comma-quibbling.wiki b/drafts/rosettacode/comma-quibbling.wiki new file mode 100644 index 0000000..b24c82d --- /dev/null +++ b/drafts/rosettacode/comma-quibbling.wiki @@ -0,0 +1,75 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text lb "{" +text rb "}" +text sep ", " +text tand " and " +text w1 "ABC" +text w2 "ABC DEF" +text w3 "ABC DEF G H" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note quib takes the words of a (single spaces between them): all but the last two are followed by a +note comma, the last two are joined by "and", inside braces +fn quib a view uchar * to void +st = new ulong 16 0 +ln = new ulong 16 0 +k = ulong 0 +s = ulong 0 +for i ulong add len a 1 +e = eq i len a +if not e +e = eq get a i 32 +end +if and e gt i s +put st k s +put ln k sub i s +k = add k 1 +end +if e +s = add i 1 +end +end +emit lb +for w ulong k +if gt w 0 +if eq w sub k 1 +emit tand +else +emit sep +end +end +emit part a get st w get ln w +end +emit rb +emit nl +end +fn main to int +note the empty list, as a slice of length 0 +quib part w1 0 0 +quib w1 +quib w2 +quib w3 +ret 0 +end + +{{out}} +
+{}
+{ABC}
+{ABC and DEF}
+{ABC, DEF, G and H}
+
diff --git a/drafts/rosettacode/conways-game-of-life.wiki b/drafts/rosettacode/conways-game-of-life.wiki new file mode 100644 index 0000000..b658a25 --- /dev/null +++ b/drafts/rosettacode/conways-game-of-life.wiki @@ -0,0 +1,156 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tgen "generation " +text tdot "." +text thash "#" +global cur uchar 64 +global nxt uchar 64 +global dim ulong 2 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note the board is dim[0] rows by dim[1] columns, row by row, with dead cells beyond the edges; a live +note cell with 2 or 3 live neighbours lives on, a dead one with exactly 3 comes alive +fn alive r long c long to ulong +if or or lt r 0 ge r long get dim 0 or lt c 0 ge c long get dim 1 +ret 0 +end +ret ulong get cur add mul ulong r get dim 1 ulong c +end +fn around r long c long to ulong +n = ulong 0 +for a ulong 3 +for b ulong 3 +if or ne a 1 ne b 1 +n = add n alive sub add r long a 1 sub add c long b 1 +end +end +end +ret n +end +fn step to void +w = get dim 1 +for r ulong get dim 0 +for c ulong w +n = around long r long c +v = uchar 0 +if eq n 3 +v = 1 +end +if and eq n 2 eq get cur add mul r w c 1 +v = 1 +end +put nxt add mul r w c v +end +end +for i ulong mul get dim 0 w +put cur i get nxt i +end +end +fn show g ulong to void +emit tgen +emitu g +emit nl +w = get dim 1 +for r ulong get dim 0 +for c ulong w +if eq get cur add mul r w c 1 +emit thash +else +emit tdot +end +end +emit nl +end +end +fn run gens ulong to void +for g ulong add gens 1 +show g +if lt g gens +step +end +end +end +fn main to int +note a blinker on a 3 x 3 board for 3 generations +put dim 0 3 +put dim 1 3 +for i ulong 9 +put cur i 0 +end +put cur 3 1 +put cur 4 1 +put cur 5 1 +run 2 +emit nl +note a glider on an 8 x 8 board; after 4 generations it has moved one cell down and right +put dim 0 8 +put dim 1 8 +for i ulong 64 +put cur i 0 +end +put cur 1 1 +put cur 10 1 +put cur 16 1 +put cur 17 1 +put cur 18 1 +run 4 +ret 0 +end + +{{out}} +
+generation 0
+...
+###
+...
+generation 1
+.#.
+.#.
+.#.
+generation 2
+...
+###
+...
+
+generation 0
+.#......
+...
+........
+..#.....
+...#....
+.###....
+........
+........
+........
+........
+
diff --git a/drafts/rosettacode/copy-a-string.wiki b/drafts/rosettacode/copy-a-string.wiki new file mode 100644 index 0000000..d9ebe76 --- /dev/null +++ b/drafts/rosettacode/copy-a-string.wiki @@ -0,0 +1,46 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text src "Hello" +text tsrc "source: " +text tcp "copy: " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note copyto copies s into d at offset off and returns the offset after it (past the end traps 3) +fn copyto d edit uchar * off ulong s view uchar * to ulong +for i ulong len s +put d add off i get s i +end +ret add off len s +end +fn main to int +note a text is read-only; copying it into a local array gives an independent, writable string +c = new uchar 16 0 +n = copyto c 0 src +put c 0 74 +emit tsrc +emit src +emit nl +emit tcp +emit part c 0 n +emit nl +ret 0 +end + +{{out}} +
+source: Hello
+copy:   Jello
+
diff --git a/drafts/rosettacode/count-how-many-vowels-and-consonants-occur-in-a-string.wiki b/drafts/rosettacode/count-how-many-vowels-and-consonants-occur-in-a-string.wiki new file mode 100644 index 0000000..1eaf673 --- /dev/null +++ b/drafts/rosettacode/count-how-many-vowels-and-consonants-occur-in-a-string.wiki @@ -0,0 +1,90 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text tv "vowels " +text tc ", consonants " +text td "distinct vowels " +text msg "Now is the time for all good men to come to the aid of their country." +text vow "aeiou" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note letters are ASCII a..z either case; vowels a e i o u, the other letters are consonants +fn main to int +seen = new uchar 26 0 +v = ulong 0 +c = ulong 0 +for i ulong len msg +x = get msg i +if and ge x 65 le x 90 +x = add x 32 +end +if and ge x 97 le x 122 +put seen sub x 97 1 +isv = false +for j ulong len vow +if eq x get vow j +isv = true +end +end +if isv +v = add v 1 +else +c = add c 1 +end +end +end +emit tv +emitu v +emit tc +emitu c +emit nl +dv = ulong 0 +dc = ulong 0 +for j ulong 26 +if eq get seen j 1 +dc = add dc 1 +end +end +for j ulong len vow +dv = add dv ulong get seen sub get vow j 97 +end +emit td +emitu dv +emit tc +emitu sub dc dv +emit nl +ret 0 +end + +{{out}} +
+vowels 22, consonants 31
+distinct vowels 5, consonants 13
+
diff --git a/drafts/rosettacode/count-in-factors.wiki b/drafts/rosettacode/count-in-factors.wiki new file mode 100644 index 0000000..642052c --- /dev/null +++ b/drafts/rosettacode/count-in-factors.wiki @@ -0,0 +1,106 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text x " x " +text eqs " = " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note prime factors in ascending order by trial division; 1 is shown as itself +note bound: each pass divides v or steps d, and d * d <= v; for the inputs here d never passes 6857 +fn tell n ulong to void +emitu n +emit eqs +if eq n 1 +emitu 1 +emit nl +ret +end +v = n +d = ulong 2 +f = ulong 0 +while le mul d d v bound 999999 +if eq rem v d 0 +if gt f 0 +emit x +end +emitu d +f = add f 1 +v = div v d +else +d = add d 1 +end +end +if gt v 1 +if gt f 0 +emit x +end +emitu v +end +emit nl +end +fn main to int +for i ulong 25 +tell add i 1 +end +for i ulong 13 +tell add 2144 i +end +tell 600851475143 +tell 1000000000000 +ret 0 +end + +{{out}} +
+1 = 1
+2 = 2
+3 = 3
+4 = 2 x 2
+5 = 5
+6 = 2 x 3
+7 = 7
+8 = 2 x 2 x 2
+9 = 3 x 3
+10 = 2 x 5
+11 = 11
+12 = 2 x 2 x 3
+13 = 13
+14 = 2 x 7
+15 = 3 x 5
+...
+2151 = 3 x 3 x 239
+2152 = 2 x 2 x 2 x 269
+2153 = 2153
+2154 = 2 x 3 x 359
+2155 = 5 x 431
+2156 = 2 x 2 x 7 x 7 x 11
+600851475143 = 71 x 839 x 1471 x 6857
+1000000000000 = 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5
+
diff --git a/drafts/rosettacode/count-occurrences-of-a-substring.wiki b/drafts/rosettacode/count-occurrences-of-a-substring.wiki new file mode 100644 index 0000000..2d42d88 --- /dev/null +++ b/drafts/rosettacode/count-occurrences-of-a-substring.wiki @@ -0,0 +1,69 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text h1 "the three truths" +text n1 "th" +text h2 "ababababab" +text n2 "abab" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note count scans left to right and skips past each match, so matches never overlap +fn count s view uchar * t view uchar * to ulong +c = ulong 0 +i = ulong 0 +while le add i len t len s bound 4096 +m = true +for j ulong len t +if ne get s add i j get t j +m = false +end +end +if m +c = add c 1 +i = add i len t +else +i = add i 1 +end +end +ret c +end +fn main to int +emitu count h1 n1 +emit nl +emitu count h2 n2 +emit nl +ret 0 +end + +{{out}} +
+3
+2
+
diff --git a/drafts/rosettacode/count-the-coins.wiki b/drafts/rosettacode/count-the-coins.wiki new file mode 100644 index 0000000..b7a5819 --- /dev/null +++ b/drafts/rosettacode/count-the-coins.wiki @@ -0,0 +1,79 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text t1 "ways to make 100 cents from 1, 5, 10, 25: " +text t2 "ways to make 100000 cents from 1, 5, 10, 25, 50, 100: " +global ways ulong 100001 +global coins ulong 6 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note addcoin lets every amount from c up to amt also end with a coin c +fn addcoin amt ulong c ulong to void +for x ulong add sub amt c 1 +y = add x c +w = add get ways y get ways x +put ways y w +end +end +note count is the classic change-making dynamic program: ways[a] counts the multisets of the first k +note coins that sum to a; adding coin c lets every amount a >= c also end with a c +fn count amt ulong k ulong to ulong +for a ulong add amt 1 +put ways a 0 +end +put ways 0 1 +for i ulong k +addcoin amt get coins i +end +ret get ways amt +end +fn main to int +put coins 0 1 +put coins 1 5 +put coins 2 10 +put coins 3 25 +put coins 4 50 +put coins 5 100 +emit t1 +emitu count 100 4 +emit nl +emit t2 +emitu count 100000 6 +emit nl +ret 0 +end + +{{out}} +
+ways to make 100 cents from 1, 5, 10, 25: 242
+ways to make 100000 cents from 1, 5, 10, 25, 50, 100: 13398445413854501
+
diff --git a/drafts/rosettacode/counting-sort.wiki b/drafts/rosettacode/counting-sort.wiki new file mode 100644 index 0000000..4815a8f --- /dev/null +++ b/drafts/rosettacode/counting-sort.wiki @@ -0,0 +1,125 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "7 -3 12 0 7 4 -3 9 15 1 -5 4 12 0 8" +global vals long 16 +global cnt ulong 64 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note cntsort counts each value in min..max (the range must fit the 64 counters), then rewrites vals +fn cntsort n ulong to void +lo = get vals 0 +hi = get vals 0 +for i ulong n +if lt get vals i lo +lo = get vals i +end +if gt get vals i hi +hi = get vals i +end +end +for i ulong n +c = ulong sub get vals i lo +put cnt c add get cnt c 1 +end +k = ulong 0 +for x ulong add ulong sub hi lo 1 +for y ulong get cnt x +put vals k add lo long x +k = add k 1 +end +end +end +fn main to int +n = parse data +show n +cntsort n +show n +ret 0 +end + +{{out}} +
+7 -3 12 0 7 4 -3 9 15 1 -5 4 12 0 8
+-5 -3 -3 0 0 1 4 4 7 7 8 9 12 12 15
+
diff --git a/drafts/rosettacode/cuban-primes.wiki b/drafts/rosettacode/cuban-primes.wiki new file mode 100644 index 0000000..9d8657f --- /dev/null +++ b/drafts/rosettacode/cuban-primes.wiki @@ -0,0 +1,165 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text first "first 200 cuban primes:\n" +text th "the 100000th cuban prime: " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note cuban primes here are the primes (n+1)^3 - n^3 = 3n^2 + 3n + 1; the 100000th, 1792617147127, is below 2^41 +note mulm is a * b mod m for m < 2^42: b splits into 21-bit halves so each product stays below 2^63 +fn mulm a ulong b ulong m ulong to ulong +h = mul a shr b 21 +h = rem h m +h = rem shl h 21 m +l = rem mul a and b 0x1fffff m +r = add h l +if ge r m +r = sub r m +end +ret r +end +fn powm b ulong e ulong m ulong to ulong +r = ulong 1 +x = rem b m +v = e +while gt v 0 bound 64 +if eq and v 1 1 +r = mulm r x m +end +x = mulm x x m +v = shr v 1 +end +ret r +end +fn sprp n ulong a ulong to bool +d = sub n 1 +s = ulong 0 +while eq and d 1 0 bound 64 +d = shr d 1 +s = add s 1 +end +x = powm a d n +if or eq x 1 eq x sub n 1 +ret true +end +for i ulong sub s 1 +x = mulm x x n +if eq x sub n 1 +ret true +end +end +ret false +end +note trial division by 2..49 first, then Miller-Rabin to bases 2, 3, 5, 7, 11, 13, which is exact below 3.47 * 10^12 +fn isprime n ulong to bool +p = ulong 2 +while lt p 50 bound 50 +if eq rem n p 0 +ret eq n p +end +p = add p 1 +end +for k ulong 6 +a = ulong 13 +if eq k 0 +a = 2 +end +if eq k 1 +a = 3 +end +if eq k 2 +a = 5 +end +if eq k 3 +a = 7 +end +if eq k 4 +a = 11 +end +if not sprp n a +ret false +end +end +ret true +end +note bound: the 100000th cuban prime is reached at n = 773019 +fn main to int +emit first +c = ulong 0 +n = ulong 1 +while lt c 100000 bound 999999 +v = add mul mul 3 n add n 1 1 +if isprime v +c = add c 1 +if le c 200 +emitu v +if eq rem c 10 0 +emit nl +else +emit sp +end +end +if eq c 100000 +emit th +emitu v +emit nl +end +end +n = add n 1 +end +ret 0 +end + +{{out}} +
+first 200 cuban primes:
+7 19 37 61 127 271 331 397 547 631
+919 1657 1801 1951 2269 2437 2791 3169 3571 4219
+4447 5167 5419 6211 7057 7351 8269 9241 10267 11719
+12097 13267 13669 16651 19441 19927 22447 23497 24571 25117
+26227 27361 33391 35317 42841 45757 47251 49537 50311 55897
+59221 60919 65269 70687 73477 74419 75367 81181 82171 87211
+88237 89269 92401 96661 102121 103231 104347 110017 112327 114661
+115837 126691 129169 131671 135469 140617 144541 145861 151201 155269
+163567 169219 170647 176419 180811 189757 200467 202021 213067 231019
+234361 241117 246247 251431 260191 263737 267307 276337 279991 283669
+285517 292969 296731 298621 310087 329677 333667 337681 347821 351919
+360187 368551 372769 374887 377011 383419 387721 398581 407377 423001
+436627 452797 459817 476407 478801 493291 522919 527941 553411 574219
+584767 590077 592741 595411 603457 608851 611557 619711 627919 650071
+658477 666937 689761 692641 698419 707131 733591 742519 760537 769627
+772669 784897 791047 812761 825301 837937 847477 863497 879667 886177
+895987 909151 915769 925741 929077 932419 939121 952597 972991 976411
+986707 990151 997057 1021417 1024921 1035469 1074607 1085407 1110817 1114471
+1125469 1155061 1177507 1181269 1215397 1253887 1281187 1285111 1324681 1328671
+1372957 1409731 1422097 1426231 1442827 1451161 1480519 1484737 1527247 1570357
+the 100000th cuban prime: 1792617147127
+
diff --git a/drafts/rosettacode/cycle-sort.wiki b/drafts/rosettacode/cycle-sort.wiki new file mode 100644 index 0000000..1cb58ad --- /dev/null +++ b/drafts/rosettacode/cycle-sort.wiki @@ -0,0 +1,145 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "5 0 1 2 2 3 5 1 1 0 5 6 9 8 0 1" +text twr "writes: " +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note place counts the elements after vals[c] that are smaller than item: that is item's sorted position +fn place c ulong n ulong item long to ulong +p = c +for x ulong sub sub n c 1 +if lt get vals add add c x 1 item +p = add p 1 +end +end +ret p +end +note cysort rotates each cycle into place, writing every misplaced element exactly once, and returns +note the number of writes; each rotation pass writes one element, so n passes bound one cycle +fn cysort n ulong to ulong +w = ulong 0 +for c ulong sub n 1 +item = get vals c +p = place c n item +if eq p c +continue +end +while eq item get vals p bound 16 +p = add p 1 +end +t = get vals p +put vals p item +item = t +w = add w 1 +while ne p c bound 16 +p = place c n item +while eq item get vals p bound 16 +p = add p 1 +end +t = get vals p +put vals p item +item = t +w = add w 1 +end +end +ret w +end +fn main to int +n = parse data +show n +w = cysort n +show n +emit twr +emitu w +emit nl +ret 0 +end + +{{out}} +
+5 0 1 2 2 3 5 1 1 0 5 6 9 8 0 1
+0 0 0 1 1 1 1 2 2 3 5 5 5 6 8 9
+writes: 14
+
diff --git a/drafts/rosettacode/determinant-and-permanent.wiki b/drafts/rosettacode/determinant-and-permanent.wiki new file mode 100644 index 0000000..b482051 --- /dev/null +++ b/drafts/rosettacode/determinant-and-permanent.wiki @@ -0,0 +1,237 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tm "matrix:\n" +text tdet "determinant: " +text tper "permanent: " +text tdata "1 2 3 4 2 9 4 7 5 3 6 1 8 1 2 3 4 4 5 6 7 7 8 9 10 10 11 12 13" +global vals long 64 +global mat long 25 +global perm ulong 8 +global res long 2 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note emitwi writes a signed n right-aligned in w columns (w at most 20) +fn emitwi n long w ulong to void +d = new uchar 21 32 +i = ulong 21 +v = n +if lt n 0 +v = neg n +end +while true bound 20 +i = sub i 1 +put d i uchar add long 48 rem v 10 +v = div v 10 +if eq v 0 +break +end +end +if lt n 0 +i = sub i 1 +put d i 45 +end +if gt w sub 21 i +i = sub 21 w +end +emit part d i sub 21 i +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note both are sums over all n! permutations p of the products m[0][p0] * .. * m[n-1][p(n-1)]; the +note determinant gives each the sign of p (its inversion count parity), the permanent adds them all +note nextp: the next permutation in lexicographic order, false after the last +fn nextp n ulong to bool +if lt n 2 +ret false +end +i = sub n 1 +while gt i 0 bound 8 +if lt get perm sub i 1 get perm i +break +end +i = sub i 1 +end +if eq i 0 +ret false +end +j = sub n 1 +while le get perm j get perm sub i 1 bound 8 +j = sub j 1 +end +t = get perm sub i 1 +put perm sub i 1 get perm j +put perm j t +k = sub n 1 +while lt i k bound 8 +t2 = get perm i +put perm i get perm k +put perm k t2 +i = add i 1 +k = sub k 1 +end +ret true +end +fn odd n ulong to bool +c = ulong 0 +for i ulong n +for j ulong n +if and lt i j gt get perm i get perm j +c = add c 1 +end +end +end +ret eq rem c 2 1 +end +fn term n ulong to long +p = long 1 +for i ulong n +p = mul p get mat add mul i n get perm i +end +ret p +end +note detper fills res[0] with the determinant and res[1] with the permanent; 5! = 120 bounds the loop +fn detper n ulong to void +for i ulong n +put perm i i +end +put res 0 0 +put res 1 0 +while true bound 120 +t = term n +put res 1 add get res 1 t +if odd n +put res 0 sub get res 0 t +else +put res 0 add get res 0 t +end +if not nextp n +break +end +end +end +fn case o ulong n ulong to void +emit tm +for i ulong n +for j ulong n +put mat add mul i n j get vals add o add mul i n j +emitwi get mat add mul i n j 4 +end +emit nl +end +detper n +emit tdet +emiti get res 0 +emit nl +emit tper +emiti get res 1 +emit nl +end +note the 5 x 5 case is 0 .. 24, filled in place rather than parsed +fn main to int +n = parse tdata +case 0 2 +case 4 3 +case 13 4 +for i ulong 25 +put vals add 29 i long i +end +case 29 5 +ret 0 +end + +{{out}} +
+matrix:
+   1   2
+   3   4
+determinant: -2
+permanent: 10
+matrix:
+   2   9   4
+   7   5   3
+   6   1   8
+determinant: -360
+permanent: 900
+matrix:
+   1   2   3   4
+   4   5   6   7
+   7   8   9  10
+...
+matrix:
+   0   1   2   3   4
+   5   6   7   8   9
+  10  11  12  13  14
+  15  16  17  18  19
+  20  21  22  23  24
+determinant: 0
+permanent: 6778800
+
diff --git a/drafts/rosettacode/determine-if-a-string-has-all-the-same-characters.wiki b/drafts/rosettacode/determine-if-a-string-has-all-the-same-characters.wiki new file mode 100644 index 0000000..4133acc --- /dev/null +++ b/drafts/rosettacode/determine-if-a-string-has-all-the-same-characters.wiki @@ -0,0 +1,90 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text lq "<<<" +text rq ">>>" +text tlen " length " +text tsame ": all the same\n" +text tdiff ": first different '" +text tat "' at " +text u0 " " +text u1 "2" +text u2 "333" +text u3 ".55" +text u4 "tttTTT" +text u5 "4444 444k" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +fn show a view uchar * to void +emit lq +emit a +emit rq +emit tlen +emitu len a +for i ulong len a +if ne get a i get a 0 +emit tdiff +emitc get a i +emit tat +emitu i +emit nl +ret +end +end +emit tsame +end +fn main to int +note the empty string, as a slice of length 0 +show part u0 0 0 +show u0 +show u1 +show u2 +show u3 +show u4 +show u5 +ret 0 +end + +{{out}} +
+<<<>>> length 0: all the same
+<<<   >>> length 3: all the same
+<<<2>>> length 1: all the same
+<<<333>>> length 3: all the same
+<<<.55>>> length 3: first different '5' at 1
+<<<tttTTT>>> length 6: first different 'T' at 3
+<<<4444 444k>>> length 9: first different ' ' at 4
+
diff --git a/drafts/rosettacode/determine-if-a-string-has-all-unique-characters.wiki b/drafts/rosettacode/determine-if-a-string-has-all-unique-characters.wiki new file mode 100644 index 0000000..3cd4c83 --- /dev/null +++ b/drafts/rosettacode/determine-if-a-string-has-all-unique-characters.wiki @@ -0,0 +1,91 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text lq "<<<" +text rq ">>>" +text tlen " length " +text tuni ": all unique\n" +text tdup ": duplicate '" +text tat "' at " +text tand " and " +text u0 "." +text u1 "abcABC" +text u2 "XYZ ZYX" +text u3 "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note the first byte (smallest i) that occurs again later, with the place j of its next occurrence +fn show a view uchar * to void +emit lq +emit a +emit rq +emit tlen +emitu len a +for i ulong len a +for j ulong sub sub len a i 1 +k = add add i j 1 +if eq get a i get a k +emit tdup +emitc get a i +emit tat +emitu i +emit tand +emitu k +emit nl +ret +end +end +end +emit tuni +end +fn main to int +note the empty string, as a slice of length 0 +show part u0 0 0 +show u0 +show u1 +show u2 +show u3 +ret 0 +end + +{{out}} +
+<<<>>> length 0: all unique
+<<<.>>> length 1: all unique
+<<<abcABC>>> length 6: all unique
+<<<XYZ ZYX>>> length 7: duplicate 'X' at 0 and 6
+<<<1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ>>> length 36: duplicate '0' at 9 and 24
+
diff --git a/drafts/rosettacode/determine-if-a-string-is-collapsible.wiki b/drafts/rosettacode/determine-if-a-string-is-collapsible.wiki new file mode 100644 index 0000000..d59b004 --- /dev/null +++ b/drafts/rosettacode/determine-if-a-string-is-collapsible.wiki @@ -0,0 +1,88 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text lq "<<<" +text rq ">>>" +text tlen " length " +text c0 "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln " +text c1 "..1111111111111111111111111111111111111111111111111111111111111117777888" +text c2 "I never give 'em hell, I just tell the truth, and they think it's hell. " +text c3 " --- Harry S Truman " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn line a view uchar * to void +emit lq +emit a +emit rq +emit tlen +emitu len a +emit nl +end +note collapse keeps a byte only when it differs from the byte before it +fn collapse a view uchar * to void +o = new uchar 256 0 +n = ulong 0 +for i ulong len a +c = get a i +k = true +if gt i 0 +k = ne c get a sub i 1 +end +if k +put o n c +n = add n 1 +end +end +line a +line part o 0 n +end +fn main to int +note the empty string, as a slice of length 0 +collapse part c0 0 0 +collapse c0 +collapse c1 +collapse c2 +collapse c3 +ret 0 +end + +{{out}} +
+<<<>>> length 0
+<<<>>> length 0
+<<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> length 72
+<<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> length 70
+<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> length 72
+<<<.178>>> length 4
+<<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> length 72
+<<<I never give 'em hel, I just tel the truth, and they think it's hel. >>> length 69
+<<<                                                    --- Harry S Truman  >>> length 72
+<<< - Hary S Truman >>> length 17
+
diff --git a/drafts/rosettacode/determine-if-a-string-is-numeric.wiki b/drafts/rosettacode/determine-if-a-string-is-numeric.wiki new file mode 100644 index 0000000..d660faf --- /dev/null +++ b/drafts/rosettacode/determine-if-a-string-is-numeric.wiki @@ -0,0 +1,134 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text tq "\"" +text tyes "\" numeric\n" +text tno "\" not numeric\n" +text s0 "0" +text s1 "-12" +text s2 "3.14" +text s3 "+1e5" +text s4 "1e-3" +text s5 ".5" +text s6 "5." +text s7 "abc" +text s9 "12a" +text s10 "-" +text s11 "1.2.3" +text s12 "e5" +text s13 "1e" +text s14 "--1" +text s15 "6.02E+23" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note digits returns the offset after a run of decimal digits starting at i +fn digits a view uchar * i ulong to ulong +j = i +while lt j len a bound 4096 +c = get a j +if or lt c 48 gt c 57 +ret j +end +j = add j 1 +end +ret j +end +note sign returns the offset after an optional + or - at i +fn sign a view uchar * i ulong to ulong +if lt i len a +c = get a i +if or eq c 43 eq c 45 +ret add i 1 +end +end +ret i +end +note numeric accepts [+-] then digits with an optional fraction (at least one digit in all), then an +note optional exponent e or E, [+-], at least one digit; nothing else, no spaces +fn numeric a view uchar * to bool +i = sign a 0 +j = digits a i +m = sub j i +if lt j len a +if eq get a j 46 +i = add j 1 +j = digits a i +m = add m sub j i +end +end +if eq m 0 +ret false +end +if lt j len a +c = get a j +if or eq c 101 eq c 69 +i = sign a add j 1 +j = digits a i +if eq j i +ret false +end +end +end +ret eq j len a +end +fn show a view uchar * to void +emit tq +emit a +if numeric a +emit tyes +else +emit tno +end +end +fn main to int +show s0 +show s1 +show s2 +show s3 +show s4 +show s5 +show s6 +show s7 +note the empty string: an empty text literal would read as unsized at this pin, so slice one +show part s7 0 0 +show s9 +show s10 +show s11 +show s12 +show s13 +show s14 +show s15 +ret 0 +end + +{{out}} +
+"0" numeric
+"-12" numeric
+"3.14" numeric
+"+1e5" numeric
+"1e-3" numeric
+".5" numeric
+"5." numeric
+"abc" not numeric
+"" not numeric
+"12a" not numeric
+"-" not numeric
+"1.2.3" not numeric
+"e5" not numeric
+"1e" not numeric
+"--1" not numeric
+"6.02E+23" numeric
+
diff --git a/drafts/rosettacode/determine-if-a-string-is-squeezable.wiki b/drafts/rosettacode/determine-if-a-string-is-squeezable.wiki new file mode 100644 index 0000000..94d540a --- /dev/null +++ b/drafts/rosettacode/determine-if-a-string-is-squeezable.wiki @@ -0,0 +1,108 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text lq "<<<" +text rq ">>>" +text tlen " length " +text tch "squeeze '" +text tq "'\n" +text c0 "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln " +text c1 "..1111111111111111111111111111111111111111111111111111111111111117777888" +text c2 "I never give 'em hell, I just tell the truth, and they think it's hell. " +text c3 " --- Harry S Truman " +text chars "-7. -r" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +fn line a view uchar * to void +emit lq +emit a +emit rq +emit tlen +emitu len a +emit nl +end +note squeeze drops a byte equal to ch when the byte before it is also ch +fn squeeze a view uchar * ch uchar to void +o = new uchar 256 0 +n = ulong 0 +for i ulong len a +c = get a i +k = true +if and gt i 0 eq c ch +k = ne get a sub i 1 ch +end +if k +put o n c +n = add n 1 +end +end +emit tch +emitc ch +emit tq +line a +line part o 0 n +end +fn main to int +squeeze c1 get chars 0 +squeeze c1 get chars 1 +squeeze c2 get chars 2 +squeeze c3 get chars 3 +squeeze c3 get chars 4 +squeeze c3 get chars 5 +ret 0 +end + +{{out}} +
+squeeze '-'
+<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> length 72
+<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> length 72
+squeeze '7'
+<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> length 72
+<<<..1111111111111111111111111111111111111111111111111111111111111117888>>> length 69
+squeeze '.'
+<<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> length 72
+<<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> length 72
+squeeze ' '
+<<<                                                    --- Harry S Truman  >>> length 72
+<<< --- Harry S Truman >>> length 20
+squeeze '-'
+<<<                                                    --- Harry S Truman  >>> length 72
+<<<                                                    - Harry S Truman  >>> length 70
+squeeze 'r'
+<<<                                                    --- Harry S Truman  >>> length 72
+<<<                                                    --- Hary S Truman  >>> length 71
+
diff --git a/drafts/rosettacode/digital-root.wiki b/drafts/rosettacode/digital-root.wiki new file mode 100644 index 0000000..6b99949 --- /dev/null +++ b/drafts/rosettacode/digital-root.wiki @@ -0,0 +1,82 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text per ": additive persistence " +text dr ", digital root " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a ulong has at most 20 digits +fn dsum n ulong to ulong +s = ulong 0 +v = n +while gt v 0 bound 20 +s = add s rem v 10 +v = div v 10 +end +ret s +end +note bound: one digit sum takes any ulong below 181, the next below 19, then a single digit, so 4 rounds suffice +fn tell n ulong to void +v = n +p = ulong 0 +while gt v 9 bound 4 +v = dsum v +p = add p 1 +end +emitu n +emit per +emitu p +emit dr +emitu v +emit nl +end +fn main to int +tell 627615 +tell 39390 +tell 588225 +tell 393900588225 +tell 0 +tell 9 +tell 199 +tell 18446744073709551615 +ret 0 +end + +{{out}} +
+627615: additive persistence 2, digital root 9
+39390: additive persistence 2, digital root 6
+588225: additive persistence 2, digital root 3
+393900588225: additive persistence 2, digital root 9
+0: additive persistence 0, digital root 0
+9: additive persistence 0, digital root 9
+199: additive persistence 3, digital root 1
+18446744073709551615: additive persistence 3, digital root 6
+
diff --git a/drafts/rosettacode/dijkstras-algorithm.wiki b/drafts/rosettacode/dijkstras-algorithm.wiki new file mode 100644 index 0000000..e4dfa39 --- /dev/null +++ b/drafts/rosettacode/dijkstras-algorithm.wiki @@ -0,0 +1,154 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tarrow " -> " +text tcost " cost " +text tpath " path" +text tnone " unreachable" +global wt long 36 +global dist long 6 +global prev ulong 6 +global done uchar 6 +global stk ulong 6 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note emitb writes the single byte c +fn emitb c ulong to void +d = new uchar 1 0 +put d 0 uchar c +emit d +end +note edge records a directed edge u -> v of weight w; vertices a..f are 0..5, weight 0 means no edge +fn edge u ulong v ulong w long to void +put wt add mul u 6 v w +end +note dijkstra from s: repeatedly settle the unsettled vertex of least known distance (-1 is infinity) +note and relax its out-edges; 6 rounds settle every reachable vertex +fn dijkstra s ulong to void +for i ulong 6 +put dist i -1 +put prev i 6 +put done i 0 +end +put dist s 0 +for r ulong 6 +u = ulong 6 +for i ulong 6 +if and eq get done i 0 ge get dist i 0 +if eq u 6 +u = i +else +if lt get dist i get dist u +u = i +end +end +end +end +if eq u 6 +ret +end +put done u 1 +for v ulong 6 +relax u v +end +end +end +fn relax u ulong v ulong to void +w = get wt add mul u 6 v +if gt w 0 +d = add get dist u w +if or lt get dist v 0 lt d get dist v +put dist v d +put prev v u +end +end +end +note route prints the path to t by following prev back to the source through a small stack +fn route t ulong to void +k = ulong 0 +v = t +while lt v 6 bound 6 +put stk k v +k = add k 1 +v = get prev v +end +emit tpath +for x ulong k +emit sp +emitb add 97 get stk sub sub k x 1 +end +end +fn main to int +edge 0 1 7 +edge 0 2 9 +edge 0 5 14 +edge 1 2 10 +edge 1 3 15 +edge 2 3 11 +edge 2 5 2 +edge 3 4 6 +edge 4 5 9 +dijkstra 0 +for t ulong 5 +v = add t 1 +emitb 97 +emit tarrow +emitb add 97 v +if lt get dist v 0 +emit tnone +else +emit tcost +emiti get dist v +route v +end +emit nl +end +ret 0 +end + +{{out}} +
+a -> b  cost 7  path a b
+a -> c  cost 9  path a c
+a -> d  cost 20  path a c d
+a -> e  cost 26  path a c d e
+a -> f  cost 11  path a c f
+
diff --git a/drafts/rosettacode/dutch-national-flag-problem.wiki b/drafts/rosettacode/dutch-national-flag-problem.wiki new file mode 100644 index 0000000..774922c --- /dev/null +++ b/drafts/rosettacode/dutch-national-flag-problem.wiki @@ -0,0 +1,141 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tred "red" +text twhite "white" +text tblue "blue" +text tok "in flag order: " +text tyes "yes\n" +text tno "no\n" +global vals long 16 +global seed ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note rnd is Knuth's MMIX linear congruential generator, x = x*6364136223846793005 + 1442695040888963407 +note mod 2^64 (ulong arithmetic wraps), starting from seed 0; it returns the top 31 bits, x >> 33 +fn rnd to ulong +x = add mul get seed 0 6364136223846793005 1442695040888963407 +put seed 0 x +ret shr x 33 +end +fn ball c long to void +if eq c 0 +emit tred +end +if eq c 1 +emit twhite +end +if eq c 2 +emit tblue +end +end +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +ball get vals i +end +emit nl +end +fn inorder n ulong to bool +for i ulong sub n 1 +if gt get vals i get vals add i 1 +ret false +end +end +ret true +end +fn swap i ulong j ulong to void +t = get vals i +put vals i get vals j +put vals j t +end +note Dijkstra's three-way partition: [0,lo) red, [lo,mid) white, (hi,n) blue; every pass moves mid up +note or hi down, so n passes suffice +fn flag n ulong to void +lo = ulong 0 +mid = ulong 0 +hi = n +while lt mid hi bound 16 +c = get vals mid +if eq c 0 +swap lo mid +lo = add lo 1 +mid = add mid 1 +else +if eq c 1 +mid = add mid 1 +else +hi = sub hi 1 +swap mid hi +end +end +end +end +note draws 12 random balls (0 red, 1 white, 2 blue), drawing again while they happen to be in order; +note with this generator the first draw is already out of order, and 100 draws bound it +fn main to int +n = ulong 12 +while true bound 100 +for i ulong n +put vals i long rem rnd 3 +end +if not inorder n +break +end +end +show n +emit tok +if inorder n +emit tyes +else +emit tno +end +flag n +show n +emit tok +if inorder n +emit tyes +else +emit tno +end +ret 0 +end + +{{out}} +
+blue red white white blue blue blue blue red red white red
+in flag order: no
+red red red red white white white blue blue blue blue blue
+in flag order: yes
+
diff --git a/drafts/rosettacode/elementary-cellular-automaton.wiki b/drafts/rosettacode/elementary-cellular-automaton.wiki new file mode 100644 index 0000000..4c88e50 --- /dev/null +++ b/drafts/rosettacode/elementary-cellular-automaton.wiki @@ -0,0 +1,111 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text trule "rule " +text tdot "." +text thash "#" +global cur uchar 32 +global nxt uchar 32 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note 31 cells on a ring; a cell's next state is bit (4*left + 2*self + right) of the rule number +fn line to void +for i ulong 31 +if eq get cur i 1 +emit thash +else +emit tdot +end +end +emit nl +end +fn step rule ulong to void +for i ulong 31 +l = ulong get cur rem add i 30 31 +c = ulong get cur i +r = ulong get cur rem add i 1 31 +k = add add mul l 4 mul c 2 r +put nxt i uchar and shr rule k 1 +end +for i ulong 31 +put cur i get nxt i +end +end +fn run rule ulong to void +emit trule +emitu rule +emit nl +for i ulong 31 +put cur i 0 +end +put cur 15 1 +for g ulong 16 +line +step rule +end +end +fn main to int +run 90 +emit nl +run 30 +emit nl +run 110 +ret 0 +end + +{{out}} +
+rule 90
+...............#...............
+..............#.#..............
+.............#...#.............
+............#.#.#.#............
+...........#.......#...........
+..........#.#.....#.#..........
+.........#...#...#...#.........
+........#.#.#.#.#.#.#.#........
+.......#...............#.......
+......#.#.............#.#......
+.....#...#...........#...#.....
+....#.#.#.#.........#.#.#.#....
+...#.......#.......#.......#...
+..#.#.....#.#.....#.#.....#.#..
+...
+.......#######.#...............
+......##.....###...............
+.....###....##.#...............
+....##.#...#####...............
+...#####..##...#...............
+..##...#.###..##...............
+.###..####.#.###...............
+##.#.##..#####.#...............
+
diff --git a/drafts/rosettacode/emirp-primes.wiki b/drafts/rosettacode/emirp-primes.wiki new file mode 100644 index 0000000..07e38d0 --- /dev/null +++ b/drafts/rosettacode/emirp-primes.wiki @@ -0,0 +1,119 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text first "first 20 emirps:" +text betw "emirps between 7700 and 8000:" +text nth "the 10000th emirp: " +note cp i is 1 when i is composite; the sieve covers every number below 10^6, which holds each 6-digit reversal +global cp uchar 1000000 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note bound: i * i < 10^6 needs i < 1000; the multiples of one i below 10^6 are fewer than 500000 +fn sieve to void +put cp 0 1 +put cp 1 1 +i = ulong 2 +while lt mul i i 1000000 bound 1000 +if eq get cp i 0 +j = mul i i +while lt j 1000000 bound 999999 +put cp j 1 +j = add j i +end +end +i = add i 1 +end +end +note rev reverses the decimal digits of n; a ulong has at most 20 digits +fn rev n ulong to ulong +r = ulong 0 +v = n +while gt v 0 bound 20 +r = add mul r 10 rem v 10 +v = div v 10 +end +ret r +end +note an emirp is a prime whose reversal is a different prime; n stays below 10^6 here +fn emirp n ulong to bool +if ne get cp n 0 +ret false +end +r = rev n +if eq r n +ret false +end +ret eq get cp r 0 +end +fn main to int +sieve +emit first +n = ulong 0 +k = ulong 0 +while lt k 20 bound 1000 +n = add n 1 +if emirp n +k = add k 1 +emit sp +emitu n +end +end +emit nl +emit betw +n = ulong 7700 +while le n 8000 bound 1000 +if emirp n +emit sp +emitu n +end +n = add n 1 +end +emit nl +n = ulong 0 +k = ulong 0 +while lt k 10000 bound 999999 +n = add n 1 +if emirp n +k = add k 1 +end +end +emit nth +emitu n +emit nl +ret 0 +end + +{{out}} +
+first 20 emirps: 13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389
+emirps between 7700 and 8000: 7717 7757 7817 7841 7867 7879 7901 7927 7949 7951 7963
+the 10000th emirp: 948349
+
diff --git a/drafts/rosettacode/empty-string.wiki b/drafts/rosettacode/empty-string.wiki new file mode 100644 index 0000000..7036250 --- /dev/null +++ b/drafts/rosettacode/empty-string.wiki @@ -0,0 +1,54 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text tempty "s is empty\n" +text tfull "s is not empty: " +text word "abc" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note copyto copies s into d at offset off and returns the offset after it (past the end traps 3) +fn copyto d edit uchar * off ulong s view uchar * to ulong +for i ulong len s +put d add off i get s i +end +ret add off len s +end +note a string here is a buffer b and its length n; the empty string is length 0 +fn check b view uchar * to void +if eq len b 0 +emit tempty +else +emit tfull +emit b +emit nl +end +end +fn main to int +b = new uchar 16 0 +n = ulong 0 +check part b 0 n +n = copyto b 0 word +check part b 0 n +n = 0 +check part b 0 n +ret 0 +end + +{{out}} +
+s is empty
+s is not empty: abc
+s is empty
+
diff --git a/drafts/rosettacode/equilibrium-index.wiki b/drafts/rosettacode/equilibrium-index.wiki new file mode 100644 index 0000000..2fd2ccc --- /dev/null +++ b/drafts/rosettacode/equilibrium-index.wiki @@ -0,0 +1,136 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text d1 "-7 1 5 2 -4 3 0" +text d2 "2 4 6" +text d3 "2 9 2" +text d4 "1 -1 1 -1 1 -1 1" +text tidx "equilibrium indices:" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note eqidx keeps the sum to the left and the sum to the right of i in one pass after the total +fn eqidx n ulong to void +tot = long 0 +for i ulong n +tot = add tot get vals i +end +lsum = long 0 +emit tidx +for i ulong n +rsum = sub sub tot lsum get vals i +if eq lsum rsum +emit sp +emitu i +end +lsum = add lsum get vals i +end +emit nl +end +fn main to int +n = parse d1 +show n +eqidx n +n = parse d2 +show n +eqidx n +n = parse d3 +show n +eqidx n +n = parse d4 +show n +eqidx n +ret 0 +end + +{{out}} +
+-7 1 5 2 -4 3 0
+equilibrium indices: 3 6
+2 4 6
+equilibrium indices:
+2 9 2
+equilibrium indices: 1
+1 -1 1 -1 1 -1 1
+equilibrium indices: 0 1 2 3 4 5 6
+
diff --git a/drafts/rosettacode/ethiopian-multiplication.wiki b/drafts/rosettacode/ethiopian-multiplication.wiki new file mode 100644 index 0000000..1dc5211 --- /dev/null +++ b/drafts/rosettacode/ethiopian-multiplication.wiki @@ -0,0 +1,87 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text kept " KEPT\n" +text struck " STRUCK\n" +text tx " x " +text teq " = " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn halve n ulong to ulong +ret shr n 1 +end +fn double n ulong to ulong +ret shl n 1 +end +fn iseven n ulong to bool +ret eq and n 1 0 +end +note left column halves to 1, right column doubles; rows with an even left number are struck out and +note the kept right numbers add up to the product +fn ethiop a ulong b ulong to ulong +s = ulong 0 +l = a +r = b +while ge l 1 bound 64 +emitu l +emit sp +emitu r +if iseven l +emit struck +else +emit kept +s = add s r +end +l = halve l +r = double r +end +ret s +end +fn main to int +p = ethiop 17 34 +emitu 17 +emit tx +emitu 34 +emit teq +emitu p +emit nl +ret 0 +end + +{{out}} +
+17 34 KEPT
+8 68 STRUCK
+4 136 STRUCK
+2 272 STRUCK
+1 544 KEPT
+17 x 34 = 578
+
diff --git a/drafts/rosettacode/evaluate-binomial-coefficients.wiki b/drafts/rosettacode/evaluate-binomial-coefficients.wiki new file mode 100644 index 0000000..88056c0 --- /dev/null +++ b/drafts/rosettacode/evaluate-binomial-coefficients.wiki @@ -0,0 +1,84 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text cc "C(" +text cm ", " +text eqs ") = " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn gcd a ulong b ulong to ulong +x = a +y = b +while ne y 0 bound 128 +t = rem x y +x = y +y = t +end +ret x +end +note r runs through C(n-k+i, i) for i = 1..k; with g = gcd(r, i), i / g divides n - k + i, so +note r / g * ((n-k+i) / (i / g)) is exact and never exceeds the final value, which must fit 64 bits +fn binom n ulong k ulong to ulong +r = ulong 1 +for j ulong k +i = add j 1 +g = gcd r i +r = mul div r g div add sub n k i div i g +end +ret r +end +fn tell n ulong k ulong to void +emit cc +emitu n +emit cm +emitu k +emit eqs +emitu binom n k +emit nl +end +fn main to int +tell 5 3 +tell 10 0 +tell 10 10 +tell 33 17 +tell 60 30 +tell 67 33 +ret 0 +end + +{{out}} +
+C(5, 3) = 10
+C(10, 0) = 1
+C(10, 10) = 1
+C(33, 17) = 1166803110
+C(60, 30) = 118264581564861424
+C(67, 33) = 14226520737620288370
+
diff --git a/drafts/rosettacode/extensible-prime-generator.wiki b/drafts/rosettacode/extensible-prime-generator.wiki new file mode 100644 index 0000000..3d05885 --- /dev/null +++ b/drafts/rosettacode/extensible-prime-generator.wiki @@ -0,0 +1,114 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text first "first 20 primes:" +text betw "primes between 100 and 150:" +text cnt "number of primes between 7700 and 8000: " +text nth "the 10000th prime: " +note the generator's state: pr holds every prime found so far, st 0 their count, st 1 the last candidate +note pr has room for 20000 primes; asking for more traps 3 at put, never a wild write +global pr ulong 20000 +global st ulong 2 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note nextp extends the stored list by one prime: the next candidate is tried against stored primes p while p * p <= c +note bound: a prime gap below 2^32 is far below 999999 candidates; the stored primes checked are at most st 0 +fn nextp to ulong +c = get st 1 +while true bound 999999 +c = add c 1 +ok = true +j = ulong 0 +while lt j get st 0 bound 20000 +p = get pr j +if gt mul p p c +break +end +if eq rem c p 0 +ok = false +break +end +j = add j 1 +end +if ok +put pr get st 0 c +put st 0 add get st 0 1 +put st 1 c +ret c +end +end +trap 4 +end +fn main to int +put st 1 1 +emit first +for i ulong 20 +emit sp +emitu nextp +end +emit nl +emit betw +p = nextp +while le p 150 bound 1000 +if ge p 100 +emit sp +emitu p +end +p = nextp +end +emit nl +while lt p 7700 bound 10000 +p = nextp +end +n = ulong 0 +while le p 8000 bound 10000 +n = add n 1 +p = nextp +end +emit cnt +emitu n +emit nl +while lt get st 0 10000 bound 10000 +p = nextp +end +emit nth +emitu get pr 9999 +emit nl +ret 0 +end + +{{out}} +
+first 20 primes: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
+primes between 100 and 150: 101 103 107 109 113 127 131 137 139 149
+number of primes between 7700 and 8000: 30
+the 10000th prime: 104729
+
diff --git a/drafts/rosettacode/factors-of-an-integer.wiki b/drafts/rosettacode/factors-of-an-integer.wiki new file mode 100644 index 0000000..0a01212 --- /dev/null +++ b/drafts/rosettacode/factors-of-an-integer.wiki @@ -0,0 +1,85 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text pre "factors of " +text col ":" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note divisors d with d * d <= n come out ascending; their partners n / d, read back in reverse, finish the list +note bound: the largest n here is 600851475143, so d stays below 775147, and no n has more than 64 small divisors +fn tell n ulong to void +lo = new ulong 64 0 +k = ulong 0 +d = ulong 1 +while le mul d d n bound 999999 +if eq rem n d 0 +put lo k d +k = add k 1 +end +d = add d 1 +end +emit pre +emitu n +emit col +for i ulong k +emit sp +emitu get lo i +end +for i ulong k +e = get lo sub sub k 1 i +if ne mul e e n +emit sp +emitu div n e +end +end +emit nl +end +fn main to int +tell 1 +tell 45 +tell 53 +tell 64 +tell 720 +tell 1000000 +tell 600851475143 +ret 0 +end + +{{out}} +
+factors of 1: 1
+factors of 45: 1 3 5 9 15 45
+factors of 53: 1 53
+factors of 64: 1 2 4 8 16 32 64
+factors of 720: 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 30 36 40 45 48 60 72 80 90 120 144 180 240 360 720
+factors of 1000000: 1 2 4 5 8 10 16 20 25 32 40 50 64 80 100 125 160 200 250 320 400 500 625 800 1000 1250 1600 2000 2500 3125 4000 5000 6250 8000 10000 12500 15625 20000 25000 31250 40000 50000 62500 100000 125000 200000 250000 500000 1000000
+factors of 600851475143: 1 71 839 1471 6857 59569 104441 486847 1234169 5753023 10086647 87625999 408464633 716151937 8462696833 600851475143
+
diff --git a/drafts/rosettacode/fibonacci-n-step-number-sequences.wiki b/drafts/rosettacode/fibonacci-n-step-number-sequences.wiki new file mode 100644 index 0000000..d6500ae --- /dev/null +++ b/drafts/rosettacode/fibonacci-n-step-number-sequences.wiki @@ -0,0 +1,104 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text t2 "fibonacci: " +text t3 "tribonacci:" +text t4 "tetranacci:" +text t5 "pentanacci:" +text t6 "hexanacci: " +text t7 "heptanacci:" +text t8 "octonacci: " +text t9 "nonanacci: " +text t10 "decanacci: " +text tl "lucas: " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note the n-step sequence starts 1, 1, 2, 4, ..., 2^(n-2) (the n-step seeds) and then each term sums the +note previous n; the Lucas numbers are the 2-step sequence seeded 2, 1. 15 terms, all below 2^15 * 15 +fn seq n ulong s0 ulong s1 ulong to void +a = new ulong 15 0 +put a 0 s0 +put a 1 s1 +for i ulong 13 +k = add i 2 +s = ulong 0 +if le k sub n 1 +s = shl ulong 1 sub k 1 +else +for j ulong n +s = add s get a sub sub k 1 j +end +end +put a k s +end +for i ulong 15 +emit sp +emitu get a i +end +emit nl +end +fn main to int +emit t2 +seq 2 1 1 +emit t3 +seq 3 1 1 +emit t4 +seq 4 1 1 +emit t5 +seq 5 1 1 +emit t6 +seq 6 1 1 +emit t7 +seq 7 1 1 +emit t8 +seq 8 1 1 +emit t9 +seq 9 1 1 +emit t10 +seq 10 1 1 +emit tl +seq 2 2 1 +ret 0 +end + +{{out}} +
+fibonacci:  1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
+tribonacci: 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136
+tetranacci: 1 1 2 4 8 15 29 56 108 208 401 773 1490 2872 5536
+pentanacci: 1 1 2 4 8 16 31 61 120 236 464 912 1793 3525 6930
+hexanacci:  1 1 2 4 8 16 32 63 125 248 492 976 1936 3840 7617
+heptanacci: 1 1 2 4 8 16 32 64 127 253 504 1004 2000 3984 7936
+octonacci:  1 1 2 4 8 16 32 64 128 255 509 1016 2028 4048 8080
+nonanacci:  1 1 2 4 8 16 32 64 128 256 511 1021 2040 4076 8144
+decanacci:  1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172
+lucas:      2 1 3 4 7 11 18 29 47 76 123 199 322 521 843
+
diff --git a/drafts/rosettacode/floyd-warshall-algorithm.wiki b/drafts/rosettacode/floyd-warshall-algorithm.wiki new file mode 100644 index 0000000..04cbdd4 --- /dev/null +++ b/drafts/rosettacode/floyd-warshall-algorithm.wiki @@ -0,0 +1,153 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text thead "pair dist path\n" +text tarrow " -> " +global dist long 16 +global nxt ulong 16 +global has uchar 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note emitwi writes a signed n right-aligned in w columns (w at most 20) +fn emitwi n long w ulong to void +d = new uchar 21 32 +i = ulong 21 +v = n +if lt n 0 +v = neg n +end +while true bound 20 +i = sub i 1 +put d i uchar add long 48 rem v 10 +v = div v 10 +if eq v 0 +break +end +end +if lt n 0 +i = sub i 1 +put d i 45 +end +if gt w sub 21 i +i = sub 21 w +end +emit part d i sub 21 i +end +note vertices 1..4 are stored as 0..3; has[i*4+j] marks a known distance, nxt the first hop +fn edge u ulong v ulong w long to void +i = add mul sub u 1 4 sub v 1 +put dist i w +put has i 1 +put nxt i sub v 1 +end +note try k as an intermediate between i and j +fn via i ulong k ulong j ulong to void +ik = add mul i 4 k +kj = add mul k 4 j +ij = add mul i 4 j +if and eq get has ik 1 eq get has kj 1 +d = add get dist ik get dist kj +if or eq get has ij 0 lt d get dist ij +put dist ij d +put has ij 1 +put nxt ij get nxt ik +end +end +end +note path follows the next-hop table from i to j; a shortest path visits at most the 4 vertices +fn path i ulong j ulong to void +emitu add i 1 +v = i +while ne v j bound 4 +v = get nxt add mul v 4 j +emit tarrow +emitu add v 1 +end +end +fn main to int +edge 1 3 -2 +edge 2 1 4 +edge 2 3 3 +edge 3 4 2 +edge 4 2 -1 +for k ulong 4 +for i ulong 4 +for j ulong 4 +via i k j +end +end +end +emit thead +for i ulong 4 +for j ulong 4 +if ne i j +emitu add i 1 +emit tarrow +emitu add j 1 +emitwi get dist add mul i 4 j 6 +emit sp +emit sp +emit sp +emit sp +path i j +emit nl +end +end +end +ret 0 +end + +{{out}} +
+pair     dist    path
+1 -> 2    -1    1 -> 3 -> 4 -> 2
+1 -> 3    -2    1 -> 3
+1 -> 4     0    1 -> 3 -> 4
+2 -> 1     4    2 -> 1
+2 -> 3     2    2 -> 1 -> 3
+2 -> 4     4    2 -> 1 -> 3 -> 4
+3 -> 1     5    3 -> 4 -> 2 -> 1
+3 -> 2     1    3 -> 4 -> 2
+3 -> 4     2    3 -> 4
+4 -> 1     3    4 -> 2 -> 1
+4 -> 2    -1    4 -> 2
+4 -> 3     1    4 -> 2 -> 1 -> 3
+
diff --git a/drafts/rosettacode/floyds-triangle.wiki b/drafts/rosettacode/floyds-triangle.wiki new file mode 100644 index 0000000..11b466b --- /dev/null +++ b/drafts/rosettacode/floyds-triangle.wiki @@ -0,0 +1,107 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt w sub 20 i +i = sub 20 w +end +emit part d i sub 20 i +end +note row r (from 0) holds r+1 consecutive numbers; column j is as wide as the last row's entry in +note column j, which is the widest in that column +fn digits v ulong to ulong +d = ulong 1 +while ge v 10 bound 20 +v = div v 10 +d = add d 1 +end +ret d +end +fn floyd n ulong to void +last = add div mul sub n 1 n 2 1 +k = ulong 1 +for r ulong n +for j ulong add r 1 +w = digits add last j +if gt j 0 +emit sp +end +emitw k w +k = add k 1 +end +emit nl +end +end +fn main to int +floyd 5 +emit nl +floyd 14 +ret 0 +end + +{{out}} +
+ 1
+ 2  3
+ 4  5  6
+ 7  8  9 10
+11 12 13 14 15
+
+ 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 57 58 59 60 61 62 63  64  65  66
+67 68 69 70 71 72 73 74  75  76  77  78
+79 80 81 82 83 84 85 86  87  88  89  90  91
+92 93 94 95 96 97 98 99 100 101 102 103 104 105
+
diff --git a/drafts/rosettacode/fusc-sequence.wiki b/drafts/rosettacode/fusc-sequence.wiki new file mode 100644 index 0000000..80d0f83 --- /dev/null +++ b/drafts/rosettacode/fusc-sequence.wiki @@ -0,0 +1,92 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text first "first 61 terms:" +text rec "first term of each decimal length, below index 1000000:\n" +text fu "fusc(" +text eqs ") = " +note fu n is fusc(n) for n below 10^6 +global fs ulong 1000000 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn digits n ulong to ulong +k = ulong 1 +v = div n 10 +while gt v 0 bound 20 +k = add k 1 +v = div v 10 +end +ret k +end +note fusc(0) = 0, fusc(1) = 1, fusc(2n) = fusc(n), fusc(2n+1) = fusc(n) + fusc(n+1); each needs only smaller indices +fn main to int +put fs 1 1 +for i ulong 999998 +n = add i 2 +h = shr n 1 +if eq rem n 2 0 +put fs n get fs h +else +put fs n add get fs h get fs add h 1 +end +end +emit first +for i ulong 61 +emit sp +emitu get fs i +end +emit nl +emit rec +w = ulong 0 +for n ulong 1000000 +d = digits get fs n +if gt d w +w = d +emit fu +emitu n +emit eqs +emitu get fs n +emit nl +end +end +ret 0 +end + +{{out}} +
+first 61 terms: 0 1 1 2 1 3 2 3 1 4 3 5 2 5 3 4 1 5 4 7 3 8 5 7 2 7 5 8 3 7 4 5 1 6 5 9 4 11 7 10 3 11 8 13 5 12 7 9 2 9 7 12 5 13 8 11 3 10 7 11 4
+first term of each decimal length, below index 1000000:
+fusc(0) = 0
+fusc(37) = 11
+fusc(1173) = 108
+fusc(35499) = 1076
+fusc(699051) = 10946
+
diff --git a/drafts/rosettacode/generate-lower-case-ascii-alphabet.wiki b/drafts/rosettacode/generate-lower-case-ascii-alphabet.wiki new file mode 100644 index 0000000..b84cd8b --- /dev/null +++ b/drafts/rosettacode/generate-lower-case-ascii-alphabet.wiki @@ -0,0 +1,31 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +fn main to int +a = new uchar 26 0 +for i ulong 26 +put a i add uchar i 97 +end +emit a +emit nl +ret 0 +end + +{{out}} +
+abcdefghijklmnopqrstuvwxyz
+
diff --git a/drafts/rosettacode/gnome-sort.wiki b/drafts/rosettacode/gnome-sort.wiki new file mode 100644 index 0000000..7cbaded --- /dev/null +++ b/drafts/rosettacode/gnome-sort.wiki @@ -0,0 +1,120 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "34 2 -17 9 9 0 51 -3 28 6" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note gnsort walks forward over ordered pairs and steps back after each swap (and is put back to 1 at 0); +note a pass moves forward, removes one inversion or leaves 0, so fewer than 3*(n + n*(n-1)/2) passes +note suffice: 165 for n = 10, bound 300; and/or evaluate both sides, so i = 0 is tested on its own +fn gnsort n ulong to void +i = ulong 1 +while lt i n bound 300 +if eq i 0 +i = 1 +continue +end +if le get vals sub i 1 get vals i +i = add i 1 +else +t = get vals i +put vals i get vals sub i 1 +put vals sub i 1 t +i = sub i 1 +end +end +end +fn main to int +n = parse data +show n +gnsort n +show n +ret 0 +end + +{{out}} +
+34 2 -17 9 9 0 51 -3 28 6
+-17 -3 0 2 6 9 9 28 34 51
+
diff --git a/drafts/rosettacode/gray-code.wiki b/drafts/rosettacode/gray-code.wiki new file mode 100644 index 0000000..cd3f9bf --- /dev/null +++ b/drafts/rosettacode/gray-code.wiki @@ -0,0 +1,124 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text thead "number binary gray decoded\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt w sub 20 i +i = sub 20 w +end +emit part d i sub 20 i +end +note bin5 writes the low 5 bits of v, most significant first +fn bin5 v ulong to void +d = new uchar 5 48 +for i ulong 5 +if ne and shr v sub 4 i 1 0 +put d i 49 +end +end +emit d +end +note encode is n xor (n >> 1); decode undoes it by xoring in every right shift of g +fn encode n ulong to ulong +ret xor n shr n 1 +end +note one xor per bit of g: a ulong has 64, so 64 shifts empty s +fn decode g ulong to ulong +n = g +s = shr g 1 +while ne s 0 bound 64 +n = xor n s +s = shr s 1 +end +ret n +end +fn main to int +emit thead +for n ulong 32 +g = encode n +emitw n 6 +emit sp +emit sp +bin5 n +emit sp +emit sp +emit sp +bin5 g +emit sp +emit sp +emitw decode g 7 +emit nl +end +ret 0 +end + +{{out}} +
+number  binary  gray   decoded
+     0  00000   00000        0
+     1  00001   00001        1
+     2  00010   00011        2
+     3  00011   00010        3
+     4  00100   00110        4
+     5  00101   00111        5
+     6  00110   00101        6
+     7  00111   00100        7
+     8  01000   01100        8
+     9  01001   01101        9
+    10  01010   01111       10
+    11  01011   01110       11
+    12  01100   01010       12
+    13  01101   01011       13
+...
+    24  11000   10100       24
+    25  11001   10101       25
+    26  11010   10111       26
+    27  11011   10110       27
+    28  11100   10010       28
+    29  11101   10011       29
+    30  11110   10001       30
+    31  11111   10000       31
+
diff --git a/drafts/rosettacode/greatest-subsequential-sum.wiki b/drafts/rosettacode/greatest-subsequential-sum.wiki new file mode 100644 index 0000000..44a2e12 --- /dev/null +++ b/drafts/rosettacode/greatest-subsequential-sum.wiki @@ -0,0 +1,155 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text d0 "-1 -2 3 5 6 -2 -1 4 -4 2 -1" +text d1 "-1 -2 -3 -4" +text d2 "4 -1 2 1 -5 4 0 3" +text d3 "2 -3 2 -3 2" +text tsum "sum: " +text tsub "subsequence:" +global vals long 16 +global pre long 17 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note gss tries every start i and end j using prefix sums; the empty subsequence (sum 0) is allowed, +note and on a tie the earliest start wins, then the shortest run, so the answer is unique +fn gss n ulong to void +put pre 0 0 +for i ulong n +put pre add i 1 add get pre i get vals i +end +bs = long 0 +bi = ulong 0 +bj = ulong 0 +for i ulong n +for x ulong sub n i +j = add add i x 1 +s = sub get pre j get pre i +if gt s bs +bs = s +bi = i +bj = j +end +end +end +emit tsum +emiti bs +emit nl +emit tsub +for k ulong sub bj bi +emit sp +emiti get vals add bi k +end +emit nl +end +fn main to int +n = parse d0 +show n +gss n +n = parse d1 +show n +gss n +n = parse d2 +show n +gss n +n = parse d3 +show n +gss n +ret 0 +end + +{{out}} +
+-1 -2 3 5 6 -2 -1 4 -4 2 -1
+sum: 15
+subsequence: 3 5 6 -2 -1 4
+-1 -2 -3 -4
+sum: 0
+subsequence:
+4 -1 2 1 -5 4 0 3
+sum: 8
+subsequence: 4 -1 2 1 -5 4 0 3
+2 -3 2 -3 2
+sum: 2
+subsequence: 2
+
diff --git a/drafts/rosettacode/hamming-numbers.wiki b/drafts/rosettacode/hamming-numbers.wiki new file mode 100644 index 0000000..e3b6afd --- /dev/null +++ b/drafts/rosettacode/hamming-numbers.wiki @@ -0,0 +1,92 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tfirst "first 20: " +text tnth "1691st: " +global ham ulong 1691 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note Dijkstra's method: three indices into the list already built point at the smallest numbers whose +note double, triple and quintuple are not yet listed; the next number is the least of the three +fn min3 a ulong b ulong c ulong to ulong +m = a +if lt b m +m = b +end +if lt c m +m = c +end +ret m +end +fn build n ulong to void +put ham 0 1 +i = ulong 0 +j = ulong 0 +k = ulong 0 +for x ulong sub n 1 +a = mul get ham i 2 +b = mul get ham j 3 +c = mul get ham k 5 +m = min3 a b c +put ham add x 1 m +if eq a m +i = add i 1 +end +if eq b m +j = add j 1 +end +if eq c m +k = add k 1 +end +end +end +fn main to int +build 1691 +emit tfirst +for i ulong 20 +if gt i 0 +emit sp +end +emitu get ham i +end +emit nl +emit tnth +emitu get ham 1690 +emit nl +ret 0 +end + +{{out}} +
+first 20: 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36
+1691st: 2125764000
+
diff --git a/drafts/rosettacode/happy-numbers.wiki b/drafts/rosettacode/happy-numbers.wiki new file mode 100644 index 0000000..fb40d52 --- /dev/null +++ b/drafts/rosettacode/happy-numbers.wiki @@ -0,0 +1,75 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text hdr "first 8 happy numbers:" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn step n ulong to ulong +s = ulong 0 +v = n +while gt v 0 bound 20 +d = rem v 10 +s = add s mul d d +v = div v 10 +end +ret s +end +note Floyd's tortoise and hare: the iteration is eventually periodic; n is happy when the cycle is 1 -> 1 +note bound: from n < 1000 the values stay below 1000 (a 3-digit square sum is at most 243), so the meeting comes within 1000 steps +fn happy n ulong to bool +s = n +f = step n +while ne s f bound 1000 +s = step s +f = step step f +end +ret eq s 1 +end +fn main to int +emit hdr +n = ulong 0 +k = ulong 0 +while lt k 8 bound 1000 +n = add n 1 +if happy n +emit sp +emitu n +k = add k 1 +end +end +emit nl +ret 0 +end + +{{out}} +
+first 8 happy numbers: 1 7 10 13 19 23 28 31
+
diff --git a/drafts/rosettacode/harshad-or-niven-series.wiki b/drafts/rosettacode/harshad-or-niven-series.wiki new file mode 100644 index 0000000..695619a --- /dev/null +++ b/drafts/rosettacode/harshad-or-niven-series.wiki @@ -0,0 +1,74 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text first "first 20 Harshad numbers:" +text above "first Harshad number above 1000: " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note n is a Harshad number when its digit sum divides it +fn harshad n ulong to bool +s = ulong 0 +v = n +while gt v 0 bound 20 +s = add s rem v 10 +v = div v 10 +end +ret eq rem n s 0 +end +note bound: every multiple of 10 up to 100 is Harshad, so neither search passes 1000 candidates +fn main to int +emit first +n = ulong 0 +k = ulong 0 +while lt k 20 bound 1000 +n = add n 1 +if harshad n +emit sp +emitu n +k = add k 1 +end +end +emit nl +n = ulong 1001 +while not harshad n bound 1000 +n = add n 1 +end +emit above +emitu n +emit nl +ret 0 +end + +{{out}} +
+first 20 Harshad numbers: 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42
+first Harshad number above 1000: 1002
+
diff --git a/drafts/rosettacode/heapsort.wiki b/drafts/rosettacode/heapsort.wiki new file mode 100644 index 0000000..bec20ef --- /dev/null +++ b/drafts/rosettacode/heapsort.wiki @@ -0,0 +1,135 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "12 -4 7 88 0 31 -4 56 23 9 -70 45 2" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note sift moves vals[s] down the max-heap vals[0..n-1]; r at least doubles each pass, so 16 passes suffice +fn sift s ulong n ulong to void +r = s +while lt add mul r 2 1 n bound 16 +c = add mul r 2 1 +if lt add c 1 n +if lt get vals c get vals add c 1 +c = add c 1 +end +end +if ge get vals r get vals c +ret +end +t = get vals r +put vals r get vals c +put vals c t +r = c +end +end +note hsort heapifies bottom-up, then swaps the maximum to the end n-1 times +fn hsort n ulong to void +s = shr n 1 +while gt s 0 bound 16 +s = sub s 1 +sift s n +end +e = n +while gt e 1 bound 16 +e = sub e 1 +t = get vals 0 +put vals 0 get vals e +put vals e t +sift 0 e +end +end +fn main to int +n = parse data +show n +hsort n +show n +ret 0 +end + +{{out}} +
+12 -4 7 88 0 31 -4 56 23 9 -70 45 2
+-70 -4 -4 0 2 7 9 12 23 31 45 56 88
+
diff --git a/drafts/rosettacode/hello-world-newline-omission.wiki b/drafts/rosettacode/hello-world-newline-omission.wiki new file mode 100644 index 0000000..a352502 --- /dev/null +++ b/drafts/rosettacode/hello-world-newline-omission.wiki @@ -0,0 +1,26 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text msg "Goodbye, World!" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +fn main to int +emit msg +ret 0 +end + +{{out}} +
+Goodbye, World!
+
diff --git a/drafts/rosettacode/hofstadter-figure-figure-sequences.wiki b/drafts/rosettacode/hofstadter-figure-figure-sequences.wiki new file mode 100644 index 0000000..33ac423 --- /dev/null +++ b/drafts/rosettacode/hofstadter-figure-figure-sequences.wiki @@ -0,0 +1,110 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text first "R(1) to R(10):" +text chk "first 40 R and first 960 S cover 1..1000 exactly once: " +text yes "yes\n" +text no "no\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note R(1) = 1, S(1) = 2, R(n) = R(n-1) + S(n-1), S(n) = the least integer above S(n-1) not in R +note r holds R(1..50) (R(50) = 1509 > 1000, so every R value up to 1000 is marked in ir); s holds S(1..960) +fn main to int +r = new ulong 51 0 +s = new ulong 961 0 +ir = new uchar 1600 0 +put r 1 1 +put s 1 2 +put ir 1 1 +for i ulong 49 +n = add i 2 +v = add get r sub n 1 get s sub n 1 +put r n v +put ir v 1 +c = add get s sub n 1 1 +while eq get ir c 1 bound 2 +c = add c 1 +end +put s n c +end +note S as the complement of R, now that R is known through 1509: the k-th unmarked integer is S(k) +k = ulong 0 +c = ulong 1 +while lt k 960 bound 1600 +if eq get ir c 0 +k = add k 1 +put s k c +end +c = add c 1 +end +emit first +for i ulong 10 +emit sp +emitu get r add i 1 +end +emit nl +cnt = new ulong 1001 0 +ok = ulong 1 +for i ulong 40 +v = get r add i 1 +if gt v 1000 +ok = ulong 0 +else +put cnt v add get cnt v 1 +end +end +for i ulong 960 +v = get s add i 1 +if gt v 1000 +ok = ulong 0 +else +put cnt v add get cnt v 1 +end +end +for i ulong 1000 +if ne get cnt add i 1 1 +ok = ulong 0 +end +end +emit chk +if eq ok 1 +emit yes +else +emit no +end +ret 0 +end + +{{out}} +
+R(1) to R(10): 1 3 7 12 18 26 35 45 56 69
+first 40 R and first 960 S cover 1..1000 exactly once: yes
+
diff --git a/drafts/rosettacode/hofstadter-q-sequence.wiki b/drafts/rosettacode/hofstadter-q-sequence.wiki new file mode 100644 index 0000000..cfb9510 --- /dev/null +++ b/drafts/rosettacode/hofstadter-q-sequence.wiki @@ -0,0 +1,75 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text first "first 10 terms:" +text th "the 1000th term: " +text fl "terms up to the 100000th that are less than the term before: " +note q n is Q(n) for n = 1..100000; Q(n) < n here, so every index n - Q(n-1) and n - Q(n-2) is in range +global q ulong 100001 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn main to int +put q 1 1 +put q 2 1 +for i ulong 99998 +n = add i 3 +a = get q sub n get q sub n 1 +b = get q sub n get q sub n 2 +put q n add a b +end +emit first +for i ulong 10 +emit sp +emitu get q add i 1 +end +emit nl +emit th +emitu get q 1000 +emit nl +c = ulong 0 +for i ulong 99999 +n = add i 2 +if lt get q n get q sub n 1 +c = add c 1 +end +end +emit fl +emitu c +emit nl +ret 0 +end + +{{out}} +
+first 10 terms: 1 1 2 3 3 4 5 5 6 6
+the 1000th term: 502
+terms up to the 100000th that are less than the term before: 49798
+
diff --git a/drafts/rosettacode/identity-matrix.wiki b/drafts/rosettacode/identity-matrix.wiki new file mode 100644 index 0000000..12db162 --- /dev/null +++ b/drafts/rosettacode/identity-matrix.wiki @@ -0,0 +1,74 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note entry (i, j) of the n x n identity is 1 on the diagonal i = j and 0 elsewhere +fn ident n ulong to void +for i ulong n +for j ulong n +if gt j 0 +emit sp +end +if eq i j +emitu 1 +else +emitu 0 +end +end +emit nl +end +end +fn main to int +ident 1 +emit nl +ident 3 +emit nl +ident 6 +ret 0 +end + +{{out}} +
+1
+
+1 0 0
+0 1 0
+0 0 1
+
+1 0 0 0 0 0
+0 1 0 0 0 0
+0 0 1 0 0 0
+0 0 0 1 0 0
+0 0 0 0 1 0
+0 0 0 0 0 1
+
diff --git a/drafts/rosettacode/isbn13-check-digit.wiki b/drafts/rosettacode/isbn13-check-digit.wiki new file mode 100644 index 0000000..a63d030 --- /dev/null +++ b/drafts/rosettacode/isbn13-check-digit.wiki @@ -0,0 +1,63 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text tgood ": good\n" +text tbad ": bad\n" +text i1 "978-0596528126" +text i2 "978-0596528120" +text i3 "978-1788399081" +text i4 "978-1788399083" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note digits weigh 1, 3, 1, 3, ... skipping hyphens; valid when there are 13 and the sum is 0 mod 10 +fn isbn a view uchar * to bool +s = ulong 0 +k = ulong 0 +for i ulong len a +c = get a i +if and ge c 48 le c 57 +d = ulong sub c 48 +if eq rem k 2 1 +d = mul d 3 +end +s = add s d +k = add k 1 +end +end +ret and eq k 13 eq rem s 10 0 +end +fn show a view uchar * to void +emit a +if isbn a +emit tgood +else +emit tbad +end +end +fn main to int +show i1 +show i2 +show i3 +show i4 +ret 0 +end + +{{out}} +
+978-0596528126: good
+978-0596528120: bad
+978-1788399081: good
+978-1788399083: bad
+
diff --git a/drafts/rosettacode/isqrt-integer-square-root-of-x.wiki b/drafts/rosettacode/isqrt-integer-square-root-of-x.wiki new file mode 100644 index 0000000..8ca5c74 --- /dev/null +++ b/drafts/rosettacode/isqrt-integer-square-root-of-x.wiki @@ -0,0 +1,93 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text small "isqrt of 0..65:" +text pre "isqrt(7^" +text eqs ") = " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note the task's quadratic residue method: q is the least power of 4 above x, then one result bit per step +note bound: inputs here are below 2^60, so q stays at most 2^60 (30 doublings of the exponent) and never wraps; +note 7^n is shown for odd n up to 21, the largest odd power of 7 below 2^64 (the task goes on to 7^73 with bignums) +fn isqrt x ulong to ulong +q = ulong 1 +while le q x bound 31 +q = shl q 2 +end +z = x +r = ulong 0 +while gt q 1 bound 31 +q = shr q 2 +s = add r q +r = shr r 1 +if ge z s +z = sub z s +r = add r q +end +end +ret r +end +fn main to int +emit small +for x ulong 66 +emit sp +emitu isqrt x +end +emit nl +p = ulong 7 +n = ulong 1 +while le n 21 bound 11 +emit pre +emitu n +emit eqs +emitu isqrt p +emit nl +p = mul p 49 +n = add n 2 +end +ret 0 +end + +{{out}} +
+isqrt of 0..65: 0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8
+isqrt(7^1) = 2
+isqrt(7^3) = 18
+isqrt(7^5) = 129
+isqrt(7^7) = 907
+isqrt(7^9) = 6352
+isqrt(7^11) = 44467
+isqrt(7^13) = 311269
+isqrt(7^15) = 2178889
+isqrt(7^17) = 15252229
+isqrt(7^19) = 106765608
+isqrt(7^21) = 747359260
+
diff --git a/drafts/rosettacode/jewels-and-stones.wiki b/drafts/rosettacode/jewels-and-stones.wiki new file mode 100644 index 0000000..9541e3d --- /dev/null +++ b/drafts/rosettacode/jewels-and-stones.wiki @@ -0,0 +1,69 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text s1 "aAAbbbb" +text j1 "aA" +text s2 "ZZ" +text j2 "z" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note count the stones (bytes of s) that are jewels (occur in j); case matters +fn jewels s view uchar * j view uchar * to ulong +n = ulong 0 +for i ulong len s +for k ulong len j +if eq get s i get j k +n = add n 1 +break +end +end +end +ret n +end +fn show s view uchar * j view uchar * to void +emit s +emit sp +emit j +emit sp +emitu jewels s j +emit nl +end +fn main to int +show s1 j1 +show s2 j2 +ret 0 +end + +{{out}} +
+aAAbbbb aA 3
+ZZ z 0
+
diff --git a/drafts/rosettacode/josephus-problem.wiki b/drafts/rosettacode/josephus-problem.wiki new file mode 100644 index 0000000..c509b58 --- /dev/null +++ b/drafts/rosettacode/josephus-problem.wiki @@ -0,0 +1,75 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tkill "executed in order:" +text tsurv "survivor: " +global alive uchar 41 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note 41 prisoners numbered 0..40 stand in a circle; counting from 0, every 3rd one still alive is killed +note until one is left; each kill walks at most 3 live places, so 3*41 steps per kill bound the walk +fn main to int +n = ulong 41 +k = ulong 3 +for i ulong n +put alive i 1 +end +p = sub n 1 +emit tkill +for x ulong sub n 1 +c = ulong 0 +while lt c k bound 123 +p = rem add p 1 n +if eq get alive p 1 +c = add c 1 +end +end +put alive p 0 +emit sp +emitu p +end +emit nl +emit tsurv +for i ulong n +if eq get alive i 1 +emitu i +end +end +emit nl +ret 0 +end + +{{out}} +
+executed in order: 2 5 8 11 14 17 20 23 26 29 32 35 38 0 4 9 13 18 22 27 31 36 40 6 12 19 25 33 39 7 16 28 37 10 24 1 21 3 34 15
+survivor: 30
+
diff --git a/drafts/rosettacode/kaprekar-numbers.wiki b/drafts/rosettacode/kaprekar-numbers.wiki new file mode 100644 index 0000000..8371e06 --- /dev/null +++ b/drafts/rosettacode/kaprekar-numbers.wiki @@ -0,0 +1,79 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text hdr "Kaprekar numbers below 10000:" +text cnt "count of Kaprekar numbers below 1000000: " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note n is Kaprekar when n * n splits as a * 10^k + b with b > 0 and a + b = n (1 counts by definition) +note bound: n < 10^6, so n * n < 10^12 and p walks at most 12 powers of ten +fn kap n ulong to bool +if eq n 1 +ret true +end +sq = mul n n +p = ulong 10 +while le p sq bound 20 +b = rem sq p +if gt b 0 +if eq add div sq p b n +ret true +end +end +p = mul p 10 +end +ret false +end +fn main to int +emit hdr +c = ulong 0 +for i ulong 999999 +n = add i 1 +if kap n +c = add c 1 +if lt n 10000 +emit sp +emitu n +end +end +end +emit nl +emit cnt +emitu c +emit nl +ret 0 +end + +{{out}} +
+Kaprekar numbers below 10000: 1 9 45 55 99 297 703 999 2223 2728 4879 4950 5050 5292 7272 7777 9999
+count of Kaprekar numbers below 1000000: 54
+
diff --git a/drafts/rosettacode/knapsack-problem-0-1.wiki b/drafts/rosettacode/knapsack-problem-0-1.wiki new file mode 100644 index 0000000..df1fa32 --- /dev/null +++ b/drafts/rosettacode/knapsack-problem-0-1.wiki @@ -0,0 +1,162 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text names "map|compass|water|sandwich|glucose|tin|banana|apple|cheese|beer|suntan cream|camera|T-shirt|trousers|umbrella|waterproof trousers|waterproof overclothes|note-case|sunglasses|towel|socks|book" +text data "9 150 13 35 153 200 50 160 15 60 68 45 27 60 39 40 23 30 52 10 11 70 32 30 24 15 48 10 73 40 42 70 43 75 22 80 7 20 18 12 4 50 30 10" +text tw "total weight " +text tv "total value " +global vals long 44 +global best long 401 +global keep uchar 9223 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note item writes the k-th name of the |-separated names text +fn item k ulong to void +f = ulong 0 +st = ulong 0 +for i ulong len names +if eq get names i 124 +if eq f k +emit part names st sub i st +ret +end +f = add f 1 +st = add i 1 +end +end +emit part names st sub len names st +end +note pack runs the 0-1 dynamic program over capacities 0..400: best[c] is the best value with weight <= c +note using the items seen so far; keep[i*401+c] records that item i was taken at capacity c +fn pack n ulong to void +for i ulong n +w = ulong get vals mul i 2 +v = get vals add mul i 2 1 +for x ulong 401 +c = sub 400 x +if ge c w +if gt add get best sub c w v get best c +put best c add get best sub c w v +put keep add mul i 401 c 1 +end +end +end +end +end +fn main to int +n = div parse data 2 +pack n +note walk back from capacity 400 over the items in reverse, taking each recorded item +c = ulong 400 +tot = ulong 0 +for x ulong n +i = sub sub n x 1 +if eq get keep add mul i 401 c 1 +put vals add mul i 2 1 -1 +w = ulong get vals mul i 2 +c = sub c w +tot = add tot w +end +end +for i ulong n +if eq get vals add mul i 2 1 -1 +item i +emit nl +end +end +emit tw +emitu tot +emit nl +emit tv +emiti get best 400 +emit nl +ret 0 +end + +{{out}} +
+map
+compass
+water
+sandwich
+glucose
+banana
+suntan cream
+waterproof trousers
+waterproof overclothes
+note-case
+sunglasses
+socks
+total weight 396
+total value 1030
+
diff --git a/drafts/rosettacode/knapsack-problem-unbounded.wiki b/drafts/rosettacode/knapsack-problem-unbounded.wiki new file mode 100644 index 0000000..fc20f8e --- /dev/null +++ b/drafts/rosettacode/knapsack-problem-unbounded.wiki @@ -0,0 +1,99 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text thead "panacea ichor gold value weight volume\n" +text ttenth "/10 " +text tthou "/1000\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note the weights are in tenths (0.3, 0.2, 2.0 -> 3, 2, 20; limit 25 -> 250) and the volumes in +note thousandths (0.025, 0.015, 0.002 -> 25, 15, 2; limit 0.25 -> 250), so all arithmetic is exact; +note values are 3000, 1800 and 2500; every count that fits is tried (at most 11 * 17 * 13 cases) +fn fits a ulong b ulong c ulong to bool +w = add add mul a 3 mul b 2 mul c 20 +v = add add mul a 25 mul b 15 mul c 2 +ret and le w 250 le v 250 +end +fn value a ulong b ulong c ulong to ulong +ret add add mul a 3000 mul b 1800 mul c 2500 +end +fn row a ulong b ulong c ulong to void +emitu a +emit sp +emitu b +emit sp +emitu c +emit sp +emitu value a b c +emit sp +emitu add add mul a 3 mul b 2 mul c 20 +emit ttenth +emitu add add mul a 25 mul b 15 mul c 2 +emit tthou +end +note pass 1 finds the best value, pass 2 prints every combination that reaches it +fn main to int +best = ulong 0 +for a ulong 11 +for b ulong 17 +for c ulong 13 +if fits a b c +if gt value a b c best +best = value a b c +end +end +end +end +end +emit thead +for a ulong 11 +for b ulong 17 +for c ulong 13 +if fits a b c +if eq value a b c best +row a b c +end +end +end +end +end +ret 0 +end + +{{out}} +
+panacea ichor gold value weight volume
+0 15 11 54500 250/10 247/1000
+3 10 11 54500 249/10 247/1000
+6 5 11 54500 248/10 247/1000
+9 0 11 54500 247/10 247/1000
+
diff --git a/drafts/rosettacode/knights-tour.wiki b/drafts/rosettacode/knights-tour.wiki new file mode 100644 index 0000000..f79f1ee --- /dev/null +++ b/drafts/rosettacode/knights-tour.wiki @@ -0,0 +1,149 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tstart "Warnsdorff tour from a1; each square shows its move number, rank 8 at the top\n" +global board ulong 64 +global mdx long 8 +global mdy long 8 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt w sub 20 i +i = sub 20 w +end +emit part d i sub 20 i +end +note target is the square a knight reaches from sq with move m, or 64 when it leaves the board +fn target sq ulong m ulong to ulong +f = add long rem sq 8 get mdx m +r = add long div sq 8 get mdy m +if or or lt f 0 gt f 7 or lt r 0 gt r 7 +ret 64 +end +ret ulong add mul r 8 f +end +note degree counts the unvisited squares reachable from sq +fn degree sq ulong to ulong +d = ulong 0 +for m ulong 8 +t = target sq m +if lt t 64 +if eq get board t 0 +d = add d 1 +end +end +end +ret d +end +note Warnsdorff's rule: always move to the unvisited square with the fewest onward moves; ties go to +note the first in the move order (2,1) (1,2) (-1,2) (-2,1) (-2,-1) (-1,-2) (1,-2) (2,-1) +fn tour to bool +sq = ulong 0 +put board 0 1 +for k ulong 63 +b = ulong 64 +bd = ulong 9 +for m ulong 8 +t = target sq m +if lt t 64 +if eq get board t 0 +d = degree t +if lt d bd +b = t +bd = d +end +end +end +end +if eq b 64 +ret false +end +put board b add k 2 +sq = b +end +ret true +end +fn main to int +put mdx 0 2 +put mdy 0 1 +put mdx 1 1 +put mdy 1 2 +put mdx 2 -1 +put mdy 2 2 +put mdx 3 -2 +put mdy 3 1 +put mdx 4 -2 +put mdy 4 -1 +put mdx 5 -1 +put mdy 5 -2 +put mdx 6 1 +put mdy 6 -2 +put mdx 7 2 +put mdy 7 -1 +if not tour +exit 1 +end +emit tstart +for x ulong 8 +r = sub 7 x +for f ulong 8 +emitw get board add mul r 8 f 3 +end +emit nl +end +ret 0 +end + +{{out}} +
+Warnsdorff tour from a1; each square shows its move number, rank 8 at the top
+ 16 31 12 51 26 29 10 63
+ 13 50 15 30 11 64 25 28
+ 32 17 52 47 54 27 62  9
+ 49 14 55 36 61 38 45 24
+ 18 33 48 53 46 59  8 39
+  3 56 35 60 37 42 23 44
+ 34 19  2  5 58 21 40  7
+  1  4 57 20 41  6 43 22
+
diff --git a/drafts/rosettacode/kronecker-product.wiki b/drafts/rosettacode/kronecker-product.wiki new file mode 100644 index 0000000..ac17fe2 --- /dev/null +++ b/drafts/rosettacode/kronecker-product.wiki @@ -0,0 +1,168 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tdata "1 2 3 4 0 5 6 7 0 1 0 1 1 1 0 1 0 1 1 1 1 1 0 0 1 1 1 1 1" +global vals long 32 +global ka long 16 +global kb long 16 +global dims ulong 4 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note emitwi writes a signed n right-aligned in w columns (w at most 20) +fn emitwi n long w ulong to void +d = new uchar 21 32 +i = ulong 21 +v = n +if lt n 0 +v = neg n +end +while true bound 20 +i = sub i 1 +put d i uchar add long 48 rem v 10 +v = div v 10 +if eq v 0 +break +end +end +if lt n 0 +i = sub i 1 +put d i 45 +end +if gt w sub 21 i +i = sub 21 w +end +emit part d i sub 21 i +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note the matrices come from tdata: 2x2 (a), 2x2 (b), then 3x3 (a), 3x4 (b); dims holds ra ca rb cb +note entry (i, j) of a (x) b is a[i / rb][j / cb] * b[i mod rb][j mod cb] +fn entry i ulong j ulong to long +rb = get dims 2 +cb = get dims 3 +x = get ka add mul div i rb get dims 1 div j cb +y = get kb add mul rem i rb cb rem j cb +ret mul x y +end +fn kron to void +rows = mul get dims 0 get dims 2 +cols = mul get dims 1 get dims 3 +for i ulong rows +for j ulong cols +emitwi entry i j 3 +end +emit nl +end +end +note load copies na entries of vals from offset o into ka, then nb entries into kb +fn load o ulong na ulong nb ulong to void +for i ulong na +put ka i get vals add o i +end +for i ulong nb +put kb i get vals add add o na i +end +end +fn main to int +n = parse tdata +put dims 0 2 +put dims 1 2 +put dims 2 2 +put dims 3 2 +load 0 4 4 +kron +emit nl +put dims 0 3 +put dims 1 3 +put dims 2 3 +put dims 3 4 +load 8 9 12 +kron +ret 0 +end + +{{out}} +
+  0  5  0 10
+  6  7 12 14
+  0 15  0 20
+ 18 21 24 28
+
+  0  0  0  0  1  1  1  1  0  0  0  0
+  0  0  0  0  1  0  0  1  0  0  0  0
+  0  0  0  0  1  1  1  1  0  0  0  0
+  1  1  1  1  1  1  1  1  1  1  1  1
+  1  0  0  1  1  0  0  1  1  0  0  1
+  1  1  1  1  1  1  1  1  1  1  1  1
+  0  0  0  0  1  1  1  1  0  0  0  0
+  0  0  0  0  1  0  0  1  0  0  0  0
+  0  0  0  0  1  1  1  1  0  0  0  0
+
diff --git a/drafts/rosettacode/langtons-ant.wiki b/drafts/rosettacode/langtons-ant.wiki new file mode 100644 index 0000000..5cfc945 --- /dev/null +++ b/drafts/rosettacode/langtons-ant.wiki @@ -0,0 +1,170 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tsteps "steps until the ant leaves the grid: " +text tbox "black cells, rows and columns " +text tto ".." +text tdot "." +text thash "#" +global grid uchar 10000 +global box ulong 4 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note the ant starts at (50, 50) of a white 100 x 100 grid facing up; on white it turns right, on black +note left, then flips the cell and steps forward; directions 0..3 are up, right, down, left +fn walk to ulong +r = long 50 +c = long 50 +d = ulong 0 +s = ulong 0 +note the ant is known to leave a grid this size after about 12000 steps (the highway starts near +note step 10000), so 999999 is a safe bound +while and and ge r 0 lt r 100 and ge c 0 lt c 100 bound 999999 +k = add mul ulong r 100 ulong c +if eq get grid k 0 +d = rem add d 1 4 +put grid k 1 +else +d = rem add d 3 4 +put grid k 0 +end +r = add r dy d +c = add c dx d +s = add s 1 +end +ret s +end +fn dy d ulong to long +if eq d 0 +ret -1 +end +if eq d 2 +ret 1 +end +ret 0 +end +fn dx d ulong to long +if eq d 1 +ret 1 +end +if eq d 3 +ret -1 +end +ret 0 +end +note bounds finds the smallest box holding every black cell: box = top, bottom, left, right +fn bounds to void +put box 0 99 +put box 1 0 +put box 2 99 +put box 3 0 +for r ulong 100 +for c ulong 100 +if eq get grid add mul r 100 c 1 +grow r c +end +end +end +end +fn grow r ulong c ulong to void +if lt r get box 0 +put box 0 r +end +if gt r get box 1 +put box 1 r +end +if lt c get box 2 +put box 2 c +end +if gt c get box 3 +put box 3 c +end +end +note row prints grid row r between the box's left and right columns +fn row r ulong to void +for x ulong add sub get box 3 get box 2 1 +c = add x get box 2 +if eq get grid add mul r 100 c 1 +emit thash +else +emit tdot +end +end +emit nl +end +fn main to int +emit tsteps +emitu walk +emit nl +bounds +emit tbox +emitu get box 0 +emit tto +emitu get box 1 +emit sp +emitu get box 2 +emit tto +emitu get box 3 +emit nl +for r ulong add sub get box 1 get box 0 1 +row add r get box 0 +end +ret 0 +end + +{{out}} +
+steps until the ant leaves the grid: 11669
+black cells, rows and columns 28..78 0..79
+..........................................##..############..##..................
+.........................................#..####..........#..##.................
+........................................###...##............##.#................
+........................................#.#..#.........#..#....#................
+....................................##..##.#.#.........###.......#..............
+.................................###.#..#...#.....#.....##.##..###..............
+..................................#.#..###..##.####.##...#.#..#.##..##..........
+..................................#.###.##..#.##..###.#.#.....###...###.........
+................................#.....#...#####.#.#..####..#...###.#.#.#........
+...............................###.##...#.####..##.##.######.#.###.#...#........
+...............................#.###.#.##.#.#.##.##.##.#...#####.###.##.........
+...................................#.#...#.##.###...#...#.#..####....#.##.......
+................................#..#.........##.##...#..##.....##.#.....##......
+...
+.###...#..##........................................................##......##..
+#..###..#.#..........................................................##.........
+.###.#..##......................................................................
+#.#.#.#.#.......................................................................
+.####.##........................................................................
+.#.##.#.........................................................................
+..####..........................................................................
+...##...........................................................................
+
diff --git a/drafts/rosettacode/least-common-multiple.wiki b/drafts/rosettacode/least-common-multiple.wiki new file mode 100644 index 0000000..206aa45 --- /dev/null +++ b/drafts/rosettacode/least-common-multiple.wiki @@ -0,0 +1,98 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text pre "lcm(" +text cm ", " +text eqs ") = " +text tup "lcm(1.." +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note Euclid: each two steps at least halve x, so 128 steps cover any 64-bit pair +fn gcd a ulong b ulong to ulong +x = a +y = b +while ne y 0 bound 128 +t = rem x y +x = y +y = t +end +ret x +end +note lcm(a, b) = a / gcd(a, b) * b, dividing first so the product never exceeds the result +fn lcm a ulong b ulong to ulong +if or eq a 0 eq b 0 +ret 0 +end +ret mul div a gcd a b b +end +fn tell a ulong b ulong to void +emit pre +emitu a +emit cm +emitu b +emit eqs +emitu lcm a b +emit nl +end +note lcm of 1..n by folding; lcm(1..40) = 5342931457063200 is below 2^64 +fn upto n ulong to void +r = ulong 1 +k = ulong 1 +while le k n bound 40 +r = lcm r k +k = add k 1 +end +emit tup +emitu n +emit eqs +emitu r +emit nl +end +fn main to int +tell 12 18 +tell 35 0 +tell 0 5 +tell 4294967296 6000000000 +upto 10 +upto 20 +upto 40 +ret 0 +end + +{{out}} +
+lcm(12, 18) = 36
+lcm(35, 0) = 0
+lcm(0, 5) = 0
+lcm(4294967296, 6000000000) = 25165824000000000
+lcm(1..10) = 2520
+lcm(1..20) = 232792560
+lcm(1..40) = 5342931457063200
+
diff --git a/drafts/rosettacode/letter-frequency.wiki b/drafts/rosettacode/letter-frequency.wiki new file mode 100644 index 0000000..07c2f93 --- /dev/null +++ b/drafts/rosettacode/letter-frequency.wiki @@ -0,0 +1,88 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text msg "The quick brown fox jumps over the lazy dog. Pack my box with five dozen liquor jugs! Sphinx of black quartz, judge my vow." +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note counts the letters a..z, upper and lower case together, and prints every letter with its count +fn main to int +f = new ulong 26 0 +for i ulong len msg +c = get msg i +if and ge c 65 le c 90 +c = add c 32 +end +if and ge c 97 le c 122 +put f sub c 97 add get f sub c 97 1 +end +end +for j ulong 26 +emitc add uchar j 97 +emit sp +emitu get f j +emit nl +end +ret 0 +end + +{{out}} +
+a 4
+b 3
+c 3
+d 3
+e 6
+f 3
+g 3
+h 4
+i 5
+j 3
+k 3
+l 3
+m 3
+n 3
+o 9
+...
+s 3
+t 4
+u 6
+v 3
+w 3
+x 3
+y 3
+z 3
+
diff --git a/drafts/rosettacode/levenshtein-distance.wiki b/drafts/rosettacode/levenshtein-distance.wiki new file mode 100644 index 0000000..386cd5d --- /dev/null +++ b/drafts/rosettacode/levenshtein-distance.wiki @@ -0,0 +1,90 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text a1 "kitten" +text b1 "sitting" +text a2 "rosettacode" +text b2 "raisethysword" +text a3 "saturday" +text b3 "sunday" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note two-row dynamic programme; strings up to 63 bytes (longer traps 3) +fn lev a view uchar * b view uchar * to ulong +p = new ulong 64 0 +q = new ulong 64 0 +for j ulong add len b 1 +put p j j +end +for i ulong len a +put q 0 add i 1 +for j ulong len b +c = add get p j 1 +if eq get a i get b j +c = get p j +end +x = add get p add j 1 1 +if lt x c +c = x +end +x = add get q j 1 +if lt x c +c = x +end +put q add j 1 c +end +for j ulong add len b 1 +put p j get q j +end +end +ret get p len b +end +fn show a view uchar * b view uchar * to void +emit a +emit sp +emit b +emit sp +emitu lev a b +emit nl +end +fn main to int +show a1 b1 +show a2 b2 +show a3 b3 +ret 0 +end + +{{out}} +
+kitten sitting 3
+rosettacode raisethysword 8
+saturday sunday 3
+
diff --git a/drafts/rosettacode/longest-common-prefix.wiki b/drafts/rosettacode/longest-common-prefix.wiki new file mode 100644 index 0000000..abefd02 --- /dev/null +++ b/drafts/rosettacode/longest-common-prefix.wiki @@ -0,0 +1,100 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text tarr " -> \"" +text tq "\"\n" +text lb "[" +text rb "]" +text l0 "interspecies|interstellar|interstate" +text l1 "throne|throne" +text l2 "throne|dungeon" +text l3 "throne||throne" +text l4 "cheese" +text l6 "prefix|suffix" +text l7 "foo|foobar" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note a list is its elements separated by '|'; an empty text is the empty list. +note split records each element as (start, length) and returns how many +fn split a view uchar * st edit ulong 16 ln edit ulong 16 to ulong +k = ulong 0 +s = ulong 0 +for i ulong add len a 1 +e = eq i len a +if not e +e = eq get a i 124 +end +if e +put st k s +put ln k sub i s +k = add k 1 +s = add i 1 +end +end +ret k +end +note common walks the first element and stops where any other element ends or differs +fn common a view uchar * st view ulong 16 ln view ulong 16 k ulong to ulong +p = ulong 0 +while lt p get ln 0 bound 4096 +for w ulong k +if eq p get ln w +ret p +end +c = get a add get st 0 p +if ne get a add get st w p c +ret p +end +end +p = add p 1 +end +ret p +end +fn lcp a view uchar * to void +st = new ulong 16 0 +ln = new ulong 16 0 +emit lb +emit a +emit rb +emit tarr +if gt len a 0 +k = split a st ln +emit part a get st 0 common a st ln k +end +emit tq +end +fn main to int +lcp l0 +lcp l1 +lcp l2 +lcp l3 +lcp l4 +lcp part l0 0 0 +lcp l6 +lcp l7 +ret 0 +end + +{{out}} +
+[interspecies|interstellar|interstate] -> "inters"
+[throne|throne] -> "throne"
+[throne|dungeon] -> ""
+[throne||throne] -> ""
+[cheese] -> "cheese"
+[] -> ""
+[prefix|suffix] -> ""
+[foo|foobar] -> "foo"
+
diff --git a/drafts/rosettacode/longest-common-subsequence.wiki b/drafts/rosettacode/longest-common-subsequence.wiki new file mode 100644 index 0000000..438dae5 --- /dev/null +++ b/drafts/rosettacode/longest-common-subsequence.wiki @@ -0,0 +1,88 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text a1 "thisisatest" +text b1 "testing123testing" +text a2 "1234" +text b2 "1224533324" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note fill writes the (len a + 1) x (len b + 1) length table into t, row-major with width len b + 1; +note both lengths up to 63 +fn fill a view uchar * b view uchar * t edit ulong 4096 to void +w = add len b 1 +for i ulong len a +for j ulong len b +up = add mul i w add j 1 +x = add get t sub up 1 1 +if ne get a i get b j +x = get t up +l = get t add up sub w 1 +if lt x l +x = l +end +end +put t add up w x +end +end +end +note goup: the cell above is at least the cell to the left +fn goup t view ulong 4096 i ulong j ulong w ulong to bool +u = get t add mul sub i 1 w j +ret ge u get t add mul i w sub j 1 +end +note walk goes back from the corner of the table: a match is taken, otherwise the larger neighbour, +note the upper one on a tie. The subsequence is written into o back to front. +fn walk a view uchar * b view uchar * t view ulong 4096 o edit uchar 64 to void +w = add len b 1 +i = len a +j = len b +k = get t add mul i w j +while and gt i 0 gt j 0 bound 200 +if eq get a sub i 1 get b sub j 1 +k = sub k 1 +put o k get a sub i 1 +i = sub i 1 +j = sub j 1 +else +if goup t i j w +i = sub i 1 +else +j = sub j 1 +end +end +end +end +fn lcs a view uchar * b view uchar * to void +t = new ulong 4096 0 +fill a b t +o = new uchar 64 0 +n = get t add mul len a add len b 1 len b +walk a b t o +emit part o 0 n +emit nl +end +fn main to int +lcs a1 b1 +lcs a2 b2 +ret 0 +end + +{{out}} +
+tsitest
+1234
+
diff --git a/drafts/rosettacode/longest-increasing-subsequence.wiki b/drafts/rosettacode/longest-increasing-subsequence.wiki new file mode 100644 index 0000000..b6fb342 --- /dev/null +++ b/drafts/rosettacode/longest-increasing-subsequence.wiki @@ -0,0 +1,160 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text d1 "3 2 6 4 5 1" +text d2 "0 8 4 12 2 10 6 14 1 9 5 13 3 11 7 15" +global vals long 16 +global ln ulong 16 +global nx ulong 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note lis prints the lexicographically smallest of the longest strictly increasing subsequences: +note ln[i] is the longest increasing run starting at i (computed right to left), nx[i] the smallest +note next value that keeps it longest; the walk then starts at the smallest value with the maximum ln +note grow considers continuing the run at i with element k > i +fn grow i ulong k ulong to void +if gt get vals k get vals i +c = add get ln k 1 +if gt c get ln i +put ln i c +put nx i k +else +if eq c get ln i +if lt get vals k get vals get nx i +put nx i k +end +end +end +end +end +note better says whether a run starting at i beats the best so far (length m starting at s) +fn better i ulong s ulong m ulong to bool +if gt get ln i m +ret true +end +ret and eq get ln i m lt get vals i get vals s +end +fn lis n ulong to void +for x ulong n +i = sub sub n x 1 +put ln i 1 +put nx i n +for j ulong sub sub n i 1 +grow i add add i j 1 +end +end +m = ulong 0 +s = ulong 0 +for i ulong n +if better i s m +m = get ln i +s = i +end +end +i = s +for x ulong m +if gt x 0 +emit sp +end +emiti get vals i +i = get nx i +end +emit nl +end +fn main to int +n = parse d1 +show n +lis n +n = parse d2 +show n +lis n +ret 0 +end + +{{out}} +
+3 2 6 4 5 1
+2 4 5
+0 8 4 12 2 10 6 14 1 9 5 13 3 11 7 15
+0 2 6 9 11 15
+
diff --git a/drafts/rosettacode/look-and-say-sequence.wiki b/drafts/rosettacode/look-and-say-sequence.wiki new file mode 100644 index 0000000..604bc84 --- /dev/null +++ b/drafts/rosettacode/look-and-say-sequence.wiki @@ -0,0 +1,91 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitc writes the single byte c +fn emitc c uchar to void +b = new uchar 1 c +emit b +end +note each term reads the previous one as runs of equal digits, writing run length then digit +note bound: the 15th term has 102 digits; 256-byte buffers hold every term here, and no run is longer than 3 +fn main to int +a = new uchar 256 0 +b = new uchar 256 0 +put a 0 1 +n = ulong 1 +for t ulong 15 +for i ulong n +emitc add get a i uchar 48 +end +emit nl +m = ulong 0 +i = ulong 0 +while lt i n bound 256 +d = get a i +j = i +while lt j n bound 256 +if ne get a j d +break +end +j = add j 1 +end +put b m uchar sub j i +put b add m 1 d +m = add m 2 +i = j +end +for k ulong m +put a k get b k +end +n = m +end +ret 0 +end + +{{out}} +
+1
+11
+21
+1211
+111221
+312211
+13112221
+1113213211
+31131211131221
+13211311123113112211
+11131221133112132113212221
+3113112221232112111312211312113211
+1321132132111213122112311311222113111221131221
+11131221131211131231121113112221121321132132211331222113112211
+311311222113111231131112132112311321322112111312211312111322212311322113212221
+
diff --git a/drafts/rosettacode/loops-break.wiki b/drafts/rosettacode/loops-break.wiki new file mode 100644 index 0000000..d630e4a --- /dev/null +++ b/drafts/rosettacode/loops-break.wiki @@ -0,0 +1,79 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a fixed linear congruential generator stands in for a random source (Rosetta asks for +note random numbers 0..19): x' = (1103515245 x + 12345) mod 2^31, r = (x' / 65536) mod 20. +note Print r; stop after printing 10; otherwise print the next value too +fn main to int +x = ulong 42 +while true bound 1000 +x = and add mul x 1103515245 12345 2147483647 +r = rem div x 65536 20 +emitu r +emit nl +if eq r 10 +break +end +x = and add mul x 1103515245 12345 2147483647 +emitu rem div x 65536 20 +emit nl +end +ret 0 +end + +{{out}} +
+1
+13
+9
+1
+16
+13
+17
+0
+5
+5
+12
+14
+14
+8
+13
+...
+0
+5
+15
+14
+6
+15
+14
+10
+
diff --git a/drafts/rosettacode/loops-continue.wiki b/drafts/rosettacode/loops-continue.wiki new file mode 100644 index 0000000..31e20b7 --- /dev/null +++ b/drafts/rosettacode/loops-continue.wiki @@ -0,0 +1,52 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sep ", " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note 1 to 10; after a multiple of 5 a newline and continue, otherwise a separator +fn main to int +for k ulong 10 +i = add k 1 +emitu i +if eq rem i 5 0 +emit nl +continue +end +emit sep +end +ret 0 +end + +{{out}} +
+1, 2, 3, 4, 5
+6, 7, 8, 9, 10
+
diff --git a/drafts/rosettacode/loops-do-while.wiki b/drafts/rosettacode/loops-do-while.wiki new file mode 100644 index 0000000..0c9a10d --- /dev/null +++ b/drafts/rosettacode/loops-do-while.wiki @@ -0,0 +1,55 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note start at 0; add 1 and print, then test: repeat while the value is not a multiple of 6 +fn main to int +v = ulong 0 +while true bound 100 +v = add v 1 +emitu v +emit nl +if eq rem v 6 0 +break +end +end +ret 0 +end + +{{out}} +
+1
+2
+3
+4
+5
+6
+
diff --git a/drafts/rosettacode/loops-downward-for.wiki b/drafts/rosettacode/loops-downward-for.wiki new file mode 100644 index 0000000..644e6ad --- /dev/null +++ b/drafts/rosettacode/loops-downward-for.wiki @@ -0,0 +1,55 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note count from 10 down to 0 +fn main to int +for i ulong 11 +emitu sub 10 i +emit nl +end +ret 0 +end + +{{out}} +
+10
+9
+8
+7
+6
+5
+4
+3
+2
+1
+0
+
diff --git a/drafts/rosettacode/loops-for-with-a-specified-step.wiki b/drafts/rosettacode/loops-for-with-a-specified-step.wiki new file mode 100644 index 0000000..ed065b6 --- /dev/null +++ b/drafts/rosettacode/loops-for-with-a-specified-step.wiki @@ -0,0 +1,50 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sep ", " +text tail "who do we appreciate?\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note from 2 to 8 in steps of 2 +fn main to int +i = ulong 2 +while le i 8 bound 10 +emitu i +emit sep +i = add i 2 +end +emit tail +ret 0 +end + +{{out}} +
+2, 4, 6, 8, who do we appreciate?
+
diff --git a/drafts/rosettacode/loops-for.wiki b/drafts/rosettacode/loops-for.wiki new file mode 100644 index 0000000..56c9181 --- /dev/null +++ b/drafts/rosettacode/loops-for.wiki @@ -0,0 +1,37 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text star "*" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note row i has i + 1 stars, five rows +fn main to int +for i ulong 5 +for j ulong add i 1 +emit star +end +emit nl +end +ret 0 +end + +{{out}} +
+*
+**
+***
+****
+*****
+
diff --git a/drafts/rosettacode/loops-foreach.wiki b/drafts/rosettacode/loops-foreach.wiki new file mode 100644 index 0000000..34c4e33 --- /dev/null +++ b/drafts/rosettacode/loops-foreach.wiki @@ -0,0 +1,39 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +note the collection: names separated by newlines +text fruit "apple\nbanana\ncherry\ndate\nelderberry\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note foreach visits every newline-terminated element of the collection in order +fn main to int +s = ulong 0 +for i ulong len fruit +if eq get fruit i 10 +emit part fruit s add sub i s 1 +s = add i 1 +end +end +ret 0 +end + +{{out}} +
+apple
+banana
+cherry
+date
+elderberry
+
diff --git a/drafts/rosettacode/loops-n-plus-one-half.wiki b/drafts/rosettacode/loops-n-plus-one-half.wiki new file mode 100644 index 0000000..d085bbd --- /dev/null +++ b/drafts/rosettacode/loops-n-plus-one-half.wiki @@ -0,0 +1,52 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sep ", " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note the loop writes the number, leaves at 10, else the separator: the last pass is half a pass +fn main to int +i = ulong 1 +while true bound 100 +emitu i +if eq i 10 +break +end +emit sep +i = add i 1 +end +emit nl +ret 0 +end + +{{out}} +
+1, 2, 3, 4, 5, 6, 7, 8, 9, 10
+
diff --git a/drafts/rosettacode/loops-while.wiki b/drafts/rosettacode/loops-while.wiki new file mode 100644 index 0000000..1f6413e --- /dev/null +++ b/drafts/rosettacode/loops-while.wiki @@ -0,0 +1,57 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note print n and halve it (integer division) while it is above zero, from 1024 +fn main to int +n = ulong 1024 +while gt n 0 bound 64 +emitu n +emit nl +n = div n 2 +end +ret 0 +end + +{{out}} +
+1024
+512
+256
+128
+64
+32
+16
+8
+4
+2
+1
+
diff --git a/drafts/rosettacode/lucas-lehmer-test.wiki b/drafts/rosettacode/lucas-lehmer-test.wiki new file mode 100644 index 0000000..57bfe01 --- /dev/null +++ b/drafts/rosettacode/lucas-lehmer-test.wiki @@ -0,0 +1,119 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text em "M" +text hdr "Mersenne primes 2^p - 1 for p <= 63, by the Lucas-Lehmer test:" +note w 0 and w 1 hold the high and low 64 bits of the last 128-bit product +global w ulong 2 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note mul128 forms a * b exactly from four 32 x 32 -> 64-bit partial products +fn mul128 a ulong b ulong to void +al = and a 0xffffffff +ah = shr a 32 +bl = and b 0xffffffff +bh = shr b 32 +ll = mul al bl +lh = mul al bh +hl = mul ah bl +hi = add mul ah bh shr lh 32 +hi = add hi shr hl 32 +mid = add shr ll 32 and lh 0xffffffff +mid = add mid and hl 0xffffffff +put w 1 add and ll 0xffffffff shl mid 32 +put w 0 add hi shr mid 32 +end +note sqm is s * s mod m for m = 2^p - 1, 2 < p <= 63,, s < m: the product x = q * 2^p + r folds to q + r +note x < 2^(2p), so q < 2^p and q + r < 2^64; a second fold and one subtraction finish it +fn sqm s ulong p ulong m ulong to ulong +mul128 s s +q = or shl get w 0 sub 64 p shr get w 1 p +t = add q and get w 1 m +t = add and t m shr t p +if ge t m +t = sub t m +end +ret t +end +note p is prime by trial division (p <= 63) +fn smallp p ulong to bool +if lt p 2 +ret false +end +d = ulong 2 +while le mul d d p bound 8 +if eq rem p d 0 +ret false +end +d = add d 1 +end +ret true +end +note Lucas-Lehmer for odd prime p: s = 4, then p - 2 times s = s^2 - 2 mod M; M is prime iff s ends at 0 +fn lltest p ulong to bool +m = sub shl ulong 1 p 1 +s = ulong 4 +for i ulong sub p 2 +s = sqm s p m +if ge s 2 +s = sub s 2 +else +s = sub add s m 2 +end +end +ret eq s 0 +end +fn main to int +emit hdr +emit nl +note M2 = 3 is prime by definition; the test as stated covers odd prime p +emit em +emitu 2 +p = ulong 3 +while le p 63 bound 61 +if smallp p +if lltest p +emit sp +emit em +emitu p +end +end +p = add p 2 +end +emit nl +ret 0 +end + +{{out}} +
+Mersenne primes 2^p - 1 for p <= 63, by the Lucas-Lehmer test:
+M2 M3 M5 M7 M13 M17 M19 M31 M61
+
diff --git a/drafts/rosettacode/ludic-numbers.wiki b/drafts/rosettacode/ludic-numbers.wiki new file mode 100644 index 0000000..9382ea8 --- /dev/null +++ b/drafts/rosettacode/ludic-numbers.wiki @@ -0,0 +1,116 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text first "first 25 ludic numbers:" +text upto "ludic numbers <= 1000: " +text tfrom "2000th to 2005th ludic numbers:" +text trip "ludic triplets (x, x+2, x+6) below 250:" +text lp " (" +text cm ", " +text rp ")" +note the sieve runs on 2..21999; the 2005th ludic number, 21511, is below that limit, and every ludic number +note found is final because removing every k-th survivor never touches the survivors in front of it +global ls ulong 22000 +global lu ulong 22000 +global isl uchar 22000 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note round: the first survivor k is ludic; it and every k-th survivor after it (positions 0, k, 2k, ...) go +note bound: each round removes at least one survivor, so at most 21998 rounds over at most 21998 survivors +fn main to int +n = ulong 0 +for i ulong 21998 +put ls i add i 2 +n = add n 1 +end +put lu 0 1 +put isl 1 1 +c = ulong 1 +while gt n 0 bound 22000 +k = get ls 0 +put lu c k +put isl k 1 +c = add c 1 +m = ulong 0 +for i ulong n +if ne rem i k 0 +put ls m get ls i +m = add m 1 +end +end +n = m +end +emit first +for i ulong 25 +emit sp +emitu get lu i +end +emit nl +emit upto +m = ulong 0 +for i ulong c +if le get lu i 1000 +m = add m 1 +end +end +emitu m +emit nl +emit tfrom +for i ulong 6 +emit sp +emitu get lu add 1999 i +end +emit nl +emit trip +for x ulong 244 +if eq get isl x 1 +if and eq get isl add x 2 1 eq get isl add x 6 1 +emit lp +emitu x +emit cm +emitu add x 2 +emit cm +emitu add x 6 +emit rp +end +end +end +emit nl +ret 0 +end + +{{out}} +
+first 25 ludic numbers: 1 2 3 5 7 11 13 17 23 25 29 37 41 43 47 53 61 67 71 77 83 89 91 97 107
+ludic numbers <= 1000: 142
+2000th to 2005th ludic numbers: 21475 21481 21487 21493 21503 21511
+ludic triplets (x, x+2, x+6) below 250: (1, 3, 7) (5, 7, 11) (11, 13, 17) (23, 25, 29) (41, 43, 47) (173, 175, 179) (221, 223, 227) (233, 235, 239)
+
diff --git a/drafts/rosettacode/luhn-test-of-credit-card-numbers.wiki b/drafts/rosettacode/luhn-test-of-credit-card-numbers.wiki new file mode 100644 index 0000000..47014d1 --- /dev/null +++ b/drafts/rosettacode/luhn-test-of-credit-card-numbers.wiki @@ -0,0 +1,62 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text tv " valid\n" +text ti " invalid\n" +text n1 "49927398716" +text n2 "49927398717" +text n3 "1234567812345678" +text n4 "1234567812345670" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note from the right, odd places add their digit, even places add the digit sum of twice the digit +fn luhn a view uchar * to bool +s = ulong 0 +n = len a +for i ulong n +d = ulong sub get a sub sub n 1 i 48 +if eq rem i 2 1 +d = mul d 2 +if gt d 9 +d = sub d 9 +end +end +s = add s d +end +ret eq rem s 10 0 +end +fn show a view uchar * to void +emit a +if luhn a +emit tv +else +emit ti +end +end +fn main to int +show n1 +show n2 +show n3 +show n4 +ret 0 +end + +{{out}} +
+49927398716 valid
+49927398717 invalid
+1234567812345678 invalid
+1234567812345670 valid
+
diff --git a/drafts/rosettacode/magic-squares-of-odd-order.wiki b/drafts/rosettacode/magic-squares-of-odd-order.wiki new file mode 100644 index 0000000..af9aa71 --- /dev/null +++ b/drafts/rosettacode/magic-squares-of-odd-order.wiki @@ -0,0 +1,122 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tconst "magic constant: " +global grid ulong 81 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt w sub 20 i +i = sub 20 w +end +emit part d i sub 20 i +end +note the Siamese method: 1 goes in the middle of the top row; each next number goes one up and one +note right (wrapping round), or straight down when that cell is taken, which happens every n numbers +fn siamese n ulong to void +for i ulong mul n n +put grid i 0 +end +r = ulong 0 +c = div n 2 +for k ulong mul n n +put grid add mul r n c add k 1 +ur = rem sub add r n 1 n +uc = rem add c 1 n +if eq get grid add mul ur n uc 0 +r = ur +c = uc +else +r = rem add r 1 n +end +end +end +fn show n ulong to void +for i ulong n +for j ulong n +emitw get grid add mul i n j 4 +end +emit nl +end +emit tconst +emitu div mul n add mul n n 1 2 +emit nl +end +fn main to int +siamese 3 +show 3 +emit nl +siamese 5 +show 5 +emit nl +siamese 9 +show 9 +ret 0 +end + +{{out}} +
+   8   1   6
+   3   5   7
+   4   9   2
+magic constant: 15
+
+  17  24   1   8  15
+  23   5   7  14  16
+   4   6  13  20  22
+  10  12  19  21   3
+  11  18  25   2   9
+magic constant: 65
+
+  47  58  69  80   1  12  23  34  45
+  57  68  79   9  11  22  33  44  46
+  67  78   8  10  21  32  43  54  56
+  77   7  18  20  31  42  53  55  66
+   6  17  19  30  41  52  63  65  76
+  16  27  29  40  51  62  64  75   5
+  26  28  39  50  61  72  74   4  15
+  36  38  49  60  71  73   3  14  25
+  37  48  59  70  81   2  13  24  35
+magic constant: 369
+
diff --git a/drafts/rosettacode/matrix-multiplication.wiki b/drafts/rosettacode/matrix-multiplication.wiki new file mode 100644 index 0000000..bfc0849 --- /dev/null +++ b/drafts/rosettacode/matrix-multiplication.wiki @@ -0,0 +1,139 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text ttimes "times\n" +text tequals "equals\n" +global mat long 27 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitwi writes a signed n right-aligned in w columns (w at most 20) +fn emitwi n long w ulong to void +d = new uchar 21 32 +i = ulong 21 +v = n +if lt n 0 +v = neg n +end +while true bound 20 +i = sub i 1 +put d i uchar add long 48 rem v 10 +v = div v 10 +if eq v 0 +break +end +end +if lt n 0 +i = sub i 1 +put d i 45 +end +if gt w sub 21 i +i = sub 21 w +end +emit part d i sub 21 i +end +note mat holds the three matrices row by row: a at offset 0, b at 9 and their product at 18 +note showm writes the r x c matrix at offset o of mat, each entry right-aligned in w columns +fn showm o ulong r ulong c ulong w ulong to void +for i ulong r +for j ulong c +emitwi get mat add o add mul i c j w +end +emit nl +end +end +note dot is row i of the m-column matrix a times column j of the p-column matrix b +fn dot i ulong j ulong m ulong p ulong to long +s = long 0 +for k ulong m +x = get mat add mul i m k +y = get mat add 9 add mul k p j +s = add s mul x y +end +ret s +end +note matmul computes the n x p product of the n x m matrix a and the m x p matrix b +fn matmul n ulong m ulong p ulong to void +for i ulong n +for j ulong p +put mat add 18 add mul i p j dot i j m p +end +end +end +fn main to int +for i ulong 6 +put mat i long add i 1 +put mat add 9 i long add i 7 +end +showm 0 2 3 5 +emit ttimes +showm 9 3 2 5 +emit tequals +matmul 2 3 2 +showm 18 2 2 5 +emit nl +for i ulong 9 +put mat i sub long mul i 3 10 +put mat add 9 i sub 4 long rem mul i 5 7 +end +showm 0 3 3 5 +emit ttimes +showm 9 3 3 5 +emit tequals +matmul 3 3 3 +showm 18 3 3 5 +ret 0 +end + +{{out}} +
+    1    2    3
+    4    5    6
+times
+    7    8
+    9   10
+   11   12
+equals
+   58   64
+  139  154
+
+  -10   -7   -4
+   -1    2    5
+    8   11   14
+times
+    4   -1    1
+    3   -2    0
+    2    4   -1
+equals
+  -69    8   -6
+   12   17   -6
+   93   26   -6
+
diff --git a/drafts/rosettacode/matrix-transposition.wiki b/drafts/rosettacode/matrix-transposition.wiki new file mode 100644 index 0000000..abedb72 --- /dev/null +++ b/drafts/rosettacode/matrix-transposition.wiki @@ -0,0 +1,106 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tt "transposed:\n" +global ma long 15 +global mt long 15 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitwi writes a signed n right-aligned in w columns (w at most 20) +fn emitwi n long w ulong to void +d = new uchar 21 32 +i = ulong 21 +v = n +if lt n 0 +v = neg n +end +while true bound 20 +i = sub i 1 +put d i uchar add long 48 rem v 10 +v = div v 10 +if eq v 0 +break +end +end +if lt n 0 +i = sub i 1 +put d i 45 +end +if gt w sub 21 i +i = sub 21 w +end +emit part d i sub 21 i +end +note ma is a 3 x 5 matrix stored row by row; its transpose mt is 5 x 3, mt[j][i] = ma[i][j] +fn showt r ulong c ulong to void +for i ulong r +for j ulong c +emitwi get mt add mul i c j 4 +end +emit nl +end +end +fn showa r ulong c ulong to void +for i ulong r +for j ulong c +emitwi get ma add mul i c j 4 +end +emit nl +end +end +fn main to int +for i ulong 15 +put ma i sub long mul i i 50 +end +showa 3 5 +for i ulong 3 +for j ulong 5 +put mt add mul j 3 i get ma add mul i 5 j +end +end +emit tt +showt 5 3 +ret 0 +end + +{{out}} +
+ -50 -49 -46 -41 -34
+ -25 -14  -1  14  31
+  50  71  94 119 146
+transposed:
+ -50 -25  50
+ -49 -14  71
+ -46  -1  94
+ -41  14 119
+ -34  31 146
+
diff --git a/drafts/rosettacode/matrix-with-two-diagonals.wiki b/drafts/rosettacode/matrix-with-two-diagonals.wiki new file mode 100644 index 0000000..32e8ac2 --- /dev/null +++ b/drafts/rosettacode/matrix-with-two-diagonals.wiki @@ -0,0 +1,74 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note entry (i, j) is 1 on the main diagonal i = j and on the anti-diagonal i + j = n - 1, else 0 +fn twodiag n ulong to void +for i ulong n +for j ulong n +if gt j 0 +emit sp +end +if or eq i j eq add i j sub n 1 +emitu 1 +else +emitu 0 +end +end +emit nl +end +end +fn main to int +twodiag 6 +emit nl +twodiag 7 +ret 0 +end + +{{out}} +
+1 0 0 0 0 1
+0 1 0 0 1 0
+0 0 1 1 0 0
+0 0 1 1 0 0
+0 1 0 0 1 0
+1 0 0 0 0 1
+
+1 0 0 0 0 0 1
+0 1 0 0 0 1 0
+0 0 1 0 1 0 0
+0 0 0 1 0 0 0
+0 0 1 0 1 0 0
+0 1 0 0 0 1 0
+1 0 0 0 0 0 1
+
diff --git a/drafts/rosettacode/maximum-triangle-path-sum.wiki b/drafts/rosettacode/maximum-triangle-path-sum.wiki new file mode 100644 index 0000000..d66d78f --- /dev/null +++ b/drafts/rosettacode/maximum-triangle-path-sum.wiki @@ -0,0 +1,119 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tri "55 94 48 95 30 96 77 71 26 67 97 13 76 38 45 07 36 79 16 37 68 48 07 09 18 70 26 06 18 72 79 46 59 79 29 90 20 76 87 11 32 07 07 49 18 27 83 58 35 71 11 25 57 29 85 14 64 36 96 27 11 58 56 92 18 55 02 90 03 60 48 49 41 46 33 36 47 23 92 50 48 02 36 59 42 79 72 20 82 77 42 56 78 38 80 39 75 02 71 66 66 01 03 55 72 44 25 67 84 71 67 11 61 40 57 58 89 40 56 36 85 32 25 85 57 48 84 35 47 62 17 01 01 99 89 52 06 71 28 75 94 48 37 10 23 51 06 48 53 18 74 98 15 27 02 92 23 08 71 76 84 15 52 92 63 81 10 44 10 69 93" +text tsmall "55 94 48 95 30 96 77 71 26 67" +text tmax "maximum path sum: " +global vals long 171 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note fold adds to vals[p] the larger of vals[q] and vals[q+1], the two entries below it +fn fold p ulong q ulong to void +lft = get vals q +rgt = get vals add q 1 +if gt lft rgt +put vals p add get vals p lft +else +put vals p add get vals p rgt +end +end +note rows are stored one after another, so row r starts at r*(r+1)/2; the sum folds from the bottom row +note up, each entry adding the larger of the two entries below it, until vals[0] holds the answer +fn best rows ulong to long +for x ulong sub rows 1 +r = sub sub rows x 2 +b = div mul r add r 1 2 +nb = add b add r 1 +for c ulong add r 1 +fold add b c add nb c +end +end +ret get vals 0 +end +fn main to int +n = parse tsmall +emit tmax +emiti best 4 +emit nl +n = parse tri +emit tmax +emiti best 18 +emit nl +ret 0 +end + +{{out}} +
+maximum path sum: 321
+maximum path sum: 1320
+
diff --git a/drafts/rosettacode/maze-generation.wiki b/drafts/rosettacode/maze-generation.wiki new file mode 100644 index 0000000..8633df3 --- /dev/null +++ b/drafts/rosettacode/maze-generation.wiki @@ -0,0 +1,181 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tcorner "+" +text thwall "---+" +text thopen " +" +text tvwall "|" +text tcell " " +text tpath " * " +text tspace " " +global seed ulong 1 +global ewall uchar 60 +global swall uchar 60 +global seen uchar 60 +global stk ulong 60 +global cand ulong 4 +global mark uchar 60 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note rnd is Knuth's MMIX linear congruential generator, x = x*6364136223846793005 + 1442695040888963407 +note mod 2^64 (ulong arithmetic wraps), starting from seed 0; it returns the top 31 bits, x >> 33 +fn rnd to ulong +x = add mul get seed 0 6364136223846793005 1442695040888963407 +put seed 0 x +ret shr x 33 +end +note the maze has 10 columns and 6 rows of cells, cell c = row*10 + column; ewall[c] and swall[c] are the +note walls on its east and south sides, all standing at the start +note neighbour says which cell lies in direction d (0 north, 1 east, 2 south, 3 west), 60 if none +fn neighbour c ulong d ulong to ulong +r = div c 10 +k = rem c 10 +if and eq d 0 gt r 0 +ret sub c 10 +end +if and eq d 1 lt k 9 +ret add c 1 +end +if and eq d 2 lt r 5 +ret add c 10 +end +if and eq d 3 gt k 0 +ret sub c 1 +end +ret 60 +end +note knock removes the wall between neighbouring cells a and b +fn knock a ulong b ulong to void +if eq b add a 1 +put ewall a 0 +end +if eq a add b 1 +put ewall b 0 +end +if eq b add a 10 +put swall a 0 +end +if eq a add b 10 +put swall b 0 +end +end +note recursive backtracker with an explicit stack: from the top cell, collect the unvisited neighbours in +note the order north, east, south, west, pick one with rnd mod their count, knock through and push it; +note with none left, pop; every pass pushes a new cell or pops, so 2*60 passes suffice +fn generate to void +for c ulong 60 +put ewall c 1 +put swall c 1 +put seen c 0 +end +put seen 0 1 +put stk 0 0 +sp1 = ulong 1 +while gt sp1 0 bound 120 +c = get stk sub sp1 1 +k = ulong 0 +for d ulong 4 +b = neighbour c d +if lt b 60 +if eq get seen b 0 +put cand k b +k = add k 1 +end +end +end +if eq k 0 +sp1 = sub sp1 1 +else +b = get cand rem rnd k +knock c b +put seen b 1 +put stk sp1 b +sp1 = add sp1 1 +end +end +end +note draw prints the maze; cells with mark[c] = 1 show a * +fn draw to void +emit tcorner +for k ulong 10 +emit thwall +end +emit nl +for r ulong 6 +emit tvwall +for k ulong 10 +c = add mul r 10 k +if eq get mark c 1 +emit tpath +else +emit tcell +end +if eq get ewall c 1 +emit tvwall +else +emit tspace +end +end +emit nl +emit tcorner +for k ulong 10 +if eq get swall add mul r 10 k 1 +emit thwall +else +emit thopen +end +end +emit nl +end +end +fn main to int +generate +draw +ret 0 +end + +{{out}} +
++---+---+---+---+---+---+---+---+---+---+
+|   |                   |               |
++   +---+---+---+---+   +   +   +---+---+
+|               |       |   |           |
++---+---+---+   +   +---+   +---+---+   +
+|       |       |               |       |
++   +   +   +---+---+---+---+---+   +   +
+|   |       |               |       |   |
++   +---+---+   +---+---+   +   +---+---+
+|       |       |       |   |           |
++---+   +   +---+   +---+   +---+---+   +
+|           |                           |
++---+---+---+---+---+---+---+---+---+---+
+
diff --git a/drafts/rosettacode/maze-solving.wiki b/drafts/rosettacode/maze-solving.wiki new file mode 100644 index 0000000..ed35571 --- /dev/null +++ b/drafts/rosettacode/maze-solving.wiki @@ -0,0 +1,245 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tcorner "+" +text thwall "---+" +text thopen " +" +text tvwall "|" +text tcell " " +text tpath " * " +text tspace " " +global seed ulong 1 +global ewall uchar 60 +global swall uchar 60 +global seen uchar 60 +global stk ulong 60 +global cand ulong 4 +global mark uchar 60 +text tlen "cells on the path from top left to bottom right: " +global dist ulong 60 +global queue ulong 60 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note rnd is Knuth's MMIX linear congruential generator, x = x*6364136223846793005 + 1442695040888963407 +note mod 2^64 (ulong arithmetic wraps), starting from seed 0; it returns the top 31 bits, x >> 33 +fn rnd to ulong +x = add mul get seed 0 6364136223846793005 1442695040888963407 +put seed 0 x +ret shr x 33 +end +note the maze has 10 columns and 6 rows of cells, cell c = row*10 + column; ewall[c] and swall[c] are the +note walls on its east and south sides, all standing at the start +note neighbour says which cell lies in direction d (0 north, 1 east, 2 south, 3 west), 60 if none +fn neighbour c ulong d ulong to ulong +r = div c 10 +k = rem c 10 +if and eq d 0 gt r 0 +ret sub c 10 +end +if and eq d 1 lt k 9 +ret add c 1 +end +if and eq d 2 lt r 5 +ret add c 10 +end +if and eq d 3 gt k 0 +ret sub c 1 +end +ret 60 +end +note knock removes the wall between neighbouring cells a and b +fn knock a ulong b ulong to void +if eq b add a 1 +put ewall a 0 +end +if eq a add b 1 +put ewall b 0 +end +if eq b add a 10 +put swall a 0 +end +if eq a add b 10 +put swall b 0 +end +end +note recursive backtracker with an explicit stack: from the top cell, collect the unvisited neighbours in +note the order north, east, south, west, pick one with rnd mod their count, knock through and push it; +note with none left, pop; every pass pushes a new cell or pops, so 2*60 passes suffice +fn generate to void +for c ulong 60 +put ewall c 1 +put swall c 1 +put seen c 0 +end +put seen 0 1 +put stk 0 0 +sp1 = ulong 1 +while gt sp1 0 bound 120 +c = get stk sub sp1 1 +k = ulong 0 +for d ulong 4 +b = neighbour c d +if lt b 60 +if eq get seen b 0 +put cand k b +k = add k 1 +end +end +end +if eq k 0 +sp1 = sub sp1 1 +else +b = get cand rem rnd k +knock c b +put seen b 1 +put stk sp1 b +sp1 = add sp1 1 +end +end +end +note draw prints the maze; cells with mark[c] = 1 show a * +fn draw to void +emit tcorner +for k ulong 10 +emit thwall +end +emit nl +for r ulong 6 +emit tvwall +for k ulong 10 +c = add mul r 10 k +if eq get mark c 1 +emit tpath +else +emit tcell +end +if eq get ewall c 1 +emit tvwall +else +emit tspace +end +end +emit nl +emit tcorner +for k ulong 10 +if eq get swall add mul r 10 k 1 +emit thwall +else +emit thopen +end +end +emit nl +end +end +note opento says whether the wall between neighbouring cells a and b is open +fn opento a ulong b ulong to bool +if eq b add a 1 +ret eq get ewall a 0 +end +if eq a add b 1 +ret eq get ewall b 0 +end +if eq b add a 10 +ret eq get swall a 0 +end +ret eq get swall b 0 +end +note bfs labels every cell with its distance from the top-left cell (60 = not reached yet) +fn bfs to void +for c ulong 60 +put dist c 60 +end +put dist 0 0 +put queue 0 0 +h = ulong 0 +t = ulong 1 +while lt h t bound 60 +c = get queue h +h = add h 1 +for d ulong 4 +b = neighbour c d +if lt b 60 +if and eq get dist b 60 opento c b +put dist b add get dist c 1 +put queue t b +t = add t 1 +end +end +end +end +end +note walk back from the bottom-right cell, each step to the neighbour one closer to the start +fn markpath to ulong +c = ulong 59 +put mark c 1 +while ne c 0 bound 60 +for d ulong 4 +b = neighbour c d +if lt b 60 +if and opento c b eq add get dist b 1 get dist c +c = b +break +end +end +end +put mark c 1 +end +ret add get dist 59 1 +end +fn main to int +generate +bfs +n = markpath +draw +emit tlen +emitu n +emit nl +ret 0 +end + +{{out}} +
++---+---+---+---+---+---+---+---+---+---+
+| * |                   |               |
++   +---+---+---+---+   +   +   +---+---+
+| *   *   *   * |       |   |           |
++---+---+---+   +   +---+   +---+---+   +
+| *   * | *   * |               |       |
++   +   +   +---+---+---+---+---+   +   +
+| * | *   * | *   *   *   * |       |   |
++   +---+---+   +---+---+   +   +---+---+
+| *   * | *   * |       | * |           |
++---+   +   +---+   +---+   +---+---+   +
+|     *   * |             *   *   *   * |
++---+---+---+---+---+---+---+---+---+---+
+cells on the path from top left to bottom right: 27
+
diff --git a/drafts/rosettacode/md5.wiki b/drafts/rosettacode/md5.wiki new file mode 100644 index 0000000..11a146a --- /dev/null +++ b/drafts/rosettacode/md5.wiki @@ -0,0 +1,169 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text hexd "0123456789abcdef" +note K[i] = floor(abs(sin(i + 1)) * 2^32) as 64 eight-digit hex words (RFC 1321, section 3.4) +text khex "d76aa478e8c7b756242070dbc1bdceeef57c0faf4787c62aa8304613fd469501698098d88b44f7afffff5bb1895cd7be6b901122fd987193a679438e49b40821f61e2562c040b340265e5a51e9b6c7aad62f105d02441453d8a1e681e7d3fbc821e1cde6c33707d6f4d50d87455a14eda9e3e905fcefa3f8676f02d98d2a4c8afffa39428771f6816d9d6122fde5380ca4beea444bdecfa9f6bb4b60bebfbc70289b7ec6eaa127fad4ef308504881d05d9d4d039e6db99e51fa27cf8c4ac5665f4292244432aff97ab9423a7fc93a039655b59c38f0ccc92ffeff47d85845dd16fa87e4ffe2ce6e0a30143144e0811a1f7537e82bd3af2352ad7d2bbeb86d391" +note the rotate amounts of the four rounds, two decimal digits each +text rots "07121722050914200411162306101521" +text m0 "The quick brown fox jumps over the lazy dog" +text m1 "The quick brown fox jumped over the lazy dog's back" +text m2 "Rosetta Code" +text m3 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +global kt uint 64 +global hs uint 4 +global mw uint 16 +global buf uchar 128 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note hexv gives the value of one lower-case hex digit +fn hexv c uchar to uint +if le c 57 +ret uint sub c 48 +end +ret uint sub c 87 +end +note emith writes the byte v as two lower-case hex digits +fn emith v uchar to void +emitc get hexd shr v 4 +emitc get hexd and v 15 +end +fn initk to void +for i ulong 64 +v = uint 0 +for j ulong 8 +v = or shl v 4 hexv get khex add mul i 8 j +end +put kt i v +end +end +note rotamt is the rotate amount of step i: round i / 16, place i mod 4 +fn rotamt i ulong to uint +x = mul add mul div i 16 4 rem i 4 2 +h = sub get rots x 48 +l = sub get rots add x 1 48 +ret uint add mul h 10 l +end +note rnd is step i of 64 on the state (a, b, c, d) in hs, with the block's words in mw +fn rnd i ulong to void +a = get hs 0 +b = get hs 1 +c = get hs 2 +d = get hs 3 +f = uint 0 +g = ulong 0 +if lt i 16 +f = or and b c and not b d +g = i +else +if lt i 32 +f = or and d b and not d c +g = rem add mul i 5 1 16 +else +if lt i 48 +f = xor xor b c d +g = rem add mul i 3 5 16 +else +f = xor c or b not d +g = rem mul i 7 16 +end +end +end +f = add add add f a get kt i get mw g +s = rotamt i +put hs 0 d +put hs 3 c +put hs 2 b +put hs 1 add b rotl f s +end +note block loads the 64 bytes of buf at p as 16 little-endian words and runs the 64 steps +fn block p ulong to void +for w ulong 16 +v = uint 0 +for j ulong 4 +v = or shl v 8 uint get buf add add p mul w 4 sub 3 j +end +put mw w v +end +sv = new uint 4 0 +for k ulong 4 +put sv k get hs k +end +for i ulong 64 +rnd i +end +for k ulong 4 +put hs k add get hs k get sv k +end +end +note md5 pads a (up to 119 bytes) with 0x80, zeros and the bit length (little-endian) to one or two +note 64-byte blocks, hashes them and prints the digest in hex +fn md5 a view uchar * to void +put hs 0 uint 1732584193 +put hs 1 uint 4023233417 +put hs 2 uint 2562383102 +put hs 3 uint 271733878 +n = len a +for i ulong 128 +put buf i 0 +end +for i ulong n +put buf i get a i +end +put buf n 128 +t = ulong 64 +if ge n 56 +t = 128 +end +bits = mul n 8 +for j ulong 8 +put buf add sub t 8 j uchar and shr bits mul j 8 255 +end +for b ulong div t 64 +block mul b 64 +end +for k ulong 4 +v = get hs k +for j ulong 4 +emith uchar and shr v mul uint j 8 255 +end +end +emit nl +end +fn main to int +initk +note the empty message, as a slice of length 0 +md5 part m0 0 0 +md5 m0 +md5 m1 +md5 m2 +md5 m3 +ret 0 +end + +{{out}} +
+d41d8cd98f00b204e9800998ecf8427e
+9e107d9d372bb6826bd81d3542a419d6
+e38ca1d920c4b8b8d3946b2c72f01680
+cca1bf66b09554e10f837838c3d3efb1
+0f5c6c4e740bfcc08c3c26ccb2673d46
+
diff --git a/drafts/rosettacode/merge-sort.wiki b/drafts/rosettacode/merge-sort.wiki new file mode 100644 index 0000000..70a6c78 --- /dev/null +++ b/drafts/rosettacode/merge-sort.wiki @@ -0,0 +1,137 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "38 27 43 3 9 82 10 -5 61 3 0 -19 77 14 50 8" +global vals long 16 +global tmp long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note msort sorts vals[lo..hi-1]: sort both halves, merge them into tmp, copy back; the merge writes +note one element per pass, so hi-lo <= 16 passes; the halves are strictly smaller, so recursion ends +fn msort lo ulong hi ulong to void +if le sub hi lo 1 +ret +end +m = add lo shr sub hi lo 1 +msort lo m +msort m hi +i = lo +j = m +k = lo +while lt k hi bound 16 +left = false +if ge j hi +left = true +else +if lt i m +if le get vals i get vals j +left = true +end +end +end +if left +put tmp k get vals i +i = add i 1 +else +put tmp k get vals j +j = add j 1 +end +k = add k 1 +end +for x ulong sub hi lo +put vals add lo x get tmp add lo x +end +end +fn main to int +n = parse data +show n +msort 0 n +show n +ret 0 +end + +{{out}} +
+38 27 43 3 9 82 10 -5 61 3 0 -19 77 14 50 8
+-19 -5 0 3 3 8 9 10 14 27 38 43 50 61 77 82
+
diff --git a/drafts/rosettacode/mersenne-primes.wiki b/drafts/rosettacode/mersenne-primes.wiki new file mode 100644 index 0000000..fda5bf3 --- /dev/null +++ b/drafts/rosettacode/mersenne-primes.wiki @@ -0,0 +1,150 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text two "2^" +text mid " - 1 = " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note addm is (a + b) mod m for a, b < m; it never forms the wrapped sum a + b +fn addm a ulong b ulong m ulong to ulong +if ge a sub m b +ret sub a sub m b +end +ret add a b +end +note mulm is a * b mod m for any 64-bit m > 0: double and add over the 64 bits of b, high bit first +fn mulm a ulong b ulong m ulong to ulong +x = rem a m +r = ulong 0 +for i ulong 64 +r = addm r r m +if ne and shr b sub 63 i 1 0 +r = addm r x m +end +end +ret r +end +note powm is b ^ e mod m; e has at most 64 bits, so 64 squarings +fn powm b ulong e ulong m ulong to ulong +r = rem ulong 1 m +x = rem b m +v = e +while gt v 0 bound 64 +if eq and v 1 1 +r = mulm r x m +end +x = mulm x x m +v = shr v 1 +end +ret r +end +note sprp: is odd n > 2 a strong probable prime to base a? n - 1 = d * 2^s with d odd, s at most 63 +fn sprp n ulong a ulong to bool +d = sub n 1 +s = ulong 0 +while eq and d 1 0 bound 64 +d = shr d 1 +s = add s 1 +end +x = powm a d n +if or eq x 1 eq x sub n 1 +ret true +end +for i ulong sub s 1 +x = mulm x x n +if eq x sub n 1 +ret true +end +end +ret false +end +note the first 12 primes as bases decide every n < 2^64 (3.18 * 10^23 is the first strong pseudoprime to all of them) +fn isprime n ulong to bool +bs = new ulong 12 0 +put bs 0 2 +put bs 1 3 +put bs 2 5 +put bs 3 7 +put bs 4 11 +put bs 5 13 +put bs 6 17 +put bs 7 19 +put bs 8 23 +put bs 9 29 +put bs 10 31 +put bs 11 37 +if lt n 2 +ret false +end +for i ulong 12 +a = get bs i +if eq n a +ret true +end +if eq rem n a 0 +ret false +end +end +for i ulong 12 +if not sprp n get bs i +ret false +end +end +ret true +end +note every Mersenne number 2^p - 1 that fits a ulong (p <= 63), tested by deterministic Miller-Rabin +fn main to int +p = ulong 2 +while le p 63 bound 62 +m = sub shl ulong 1 p 1 +if isprime m +emit two +emitu p +emit mid +emitu m +emit nl +end +p = add p 1 +end +ret 0 +end + +{{out}} +
+2^2 - 1 = 3
+2^3 - 1 = 7
+2^5 - 1 = 31
+2^7 - 1 = 127
+2^13 - 1 = 8191
+2^17 - 1 = 131071
+2^19 - 1 = 524287
+2^31 - 1 = 2147483647
+2^61 - 1 = 2305843009213693951
+
diff --git a/drafts/rosettacode/miller-rabin-primality-test.wiki b/drafts/rosettacode/miller-rabin-primality-test.wiki new file mode 100644 index 0000000..f90f653 --- /dev/null +++ b/drafts/rosettacode/miller-rabin-primality-test.wiki @@ -0,0 +1,171 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text hdr "primes below 100:" +text yes ": prime\n" +text no ": composite\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note addm is (a + b) mod m for a, b < m; it never forms the wrapped sum a + b +fn addm a ulong b ulong m ulong to ulong +if ge a sub m b +ret sub a sub m b +end +ret add a b +end +note mulm is a * b mod m for any 64-bit m > 0: double and add over the 64 bits of b, high bit first +fn mulm a ulong b ulong m ulong to ulong +x = rem a m +r = ulong 0 +for i ulong 64 +r = addm r r m +if ne and shr b sub 63 i 1 0 +r = addm r x m +end +end +ret r +end +note powm is b ^ e mod m; e has at most 64 bits, so 64 squarings +fn powm b ulong e ulong m ulong to ulong +r = rem ulong 1 m +x = rem b m +v = e +while gt v 0 bound 64 +if eq and v 1 1 +r = mulm r x m +end +x = mulm x x m +v = shr v 1 +end +ret r +end +note sprp: is odd n > 2 a strong probable prime to base a? n - 1 = d * 2^s with d odd, s at most 63 +fn sprp n ulong a ulong to bool +d = sub n 1 +s = ulong 0 +while eq and d 1 0 bound 64 +d = shr d 1 +s = add s 1 +end +x = powm a d n +if or eq x 1 eq x sub n 1 +ret true +end +for i ulong sub s 1 +x = mulm x x n +if eq x sub n 1 +ret true +end +end +ret false +end +note the first 12 primes as bases decide every n < 2^64 (3.18 * 10^23 is the first strong pseudoprime to all of them) +fn isprime n ulong to bool +bs = new ulong 12 0 +put bs 0 2 +put bs 1 3 +put bs 2 5 +put bs 3 7 +put bs 4 11 +put bs 5 13 +put bs 6 17 +put bs 7 19 +put bs 8 23 +put bs 9 29 +put bs 10 31 +put bs 11 37 +if lt n 2 +ret false +end +for i ulong 12 +a = get bs i +if eq n a +ret true +end +if eq rem n a 0 +ret false +end +end +for i ulong 12 +if not sprp n get bs i +ret false +end +end +ret true +end +fn tell n ulong to void +emitu n +if isprime n +emit yes +else +emit no +end +end +fn main to int +emit hdr +for i ulong 100 +if isprime i +emit sp +emitu i +end +end +emit nl +tell 561 +tell 1105 +tell 1729 +tell 2047 +tell 3215031751 +tell 2147483647 +tell 3825123056546413051 +tell 4611686018427387903 +tell 2305843009213693951 +tell 9223372036854775783 +tell 18446744073709551557 +tell 18446744073709551615 +ret 0 +end + +{{out}} +
+primes below 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
+561: composite
+1105: composite
+1729: composite
+2047: composite
+3215031751: composite
+2147483647: prime
+3825123056546413051: composite
+4611686018427387903: composite
+2305843009213693951: prime
+9223372036854775783: prime
+18446744073709551557: prime
+18446744073709551615: composite
+
diff --git a/drafts/rosettacode/mobius-function.wiki b/drafts/rosettacode/mobius-function.wiki new file mode 100644 index 0000000..9abd1d2 --- /dev/null +++ b/drafts/rosettacode/mobius-function.wiki @@ -0,0 +1,82 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text hdr "first 199 terms of the Moebius sequence (the first cell, n = 0, is blank):\n" +text blank " " +text tneg " -1" +text tzer " 0" +text tpos " 1" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note mu(n) by trial division: 0 if some prime divides n twice, else (-1)^(number of prime factors) +note bound: n < 200, so d walks fewer than 15 values with d * d <= n +fn mu n ulong to long +v = n +s = long 1 +d = ulong 2 +while le mul d d v bound 200 +if eq rem v d 0 +v = div v d +if eq rem v d 0 +ret 0 +end +s = sub long 0 s +end +d = add d 1 +end +if gt v 1 +s = sub long 0 s +end +ret s +end +fn main to int +emit hdr +for r ulong 10 +for c ulong 20 +n = add mul r 20 c +if eq n 0 +emit blank +else +m = mu n +if lt m 0 +emit tneg +end +if eq m 0 +emit tzer +end +if gt m 0 +emit tpos +end +end +end +emit nl +end +ret 0 +end + +{{out}} +
+first 199 terms of the Moebius sequence (the first cell, n = 0, is blank):
+     1 -1 -1  0 -1  1 -1  0  0  1 -1  0 -1  1  1  0 -1  0 -1
+  0  1  1 -1  0  0  1  0  0 -1 -1 -1  0  1  1  1  0 -1  1  1
+  0 -1 -1 -1  0  0  1 -1  0  0  0  1  0 -1  0  1  0  1  1 -1
+  0 -1  1  0  0  1 -1 -1  0  1 -1 -1  0 -1  1  0  0  1 -1 -1
+  0  0  1 -1  0  1  1  1  0 -1  0  1  0  1  1  1  0 -1  0  0
+  0 -1 -1 -1  0 -1  1 -1  0 -1 -1  1  0 -1 -1  1  0  0  1  1
+  0  0  1  1  0  0  0 -1  0  1 -1 -1  0  1  1  0  0 -1 -1 -1
+  0  1  1  1  0  1  1  0  0 -1  0 -1  0  0 -1  1  0 -1  1  1
+  0  1  0 -1  0 -1  1 -1  0  0 -1  0  0 -1 -1  0  0  1  1 -1
+  0 -1 -1  1  0  1 -1  1  0  0 -1 -1  0 -1  1 -1  0 -1  0 -1
+
diff --git a/drafts/rosettacode/modular-exponentiation.wiki b/drafts/rosettacode/modular-exponentiation.wiki new file mode 100644 index 0000000..053b702 --- /dev/null +++ b/drafts/rosettacode/modular-exponentiation.wiki @@ -0,0 +1,130 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text ta "2988348162058574136915891421498819466320163312926952423791023078876139" +text tb "2351399303373464486466122544523690094744975233415544072992656881240319" +text hdr "a^b mod 10^40 = " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a residue mod 10^40 is 5 limbs base 10^8, least significant first +note limbs reads the last 40 decimal digits of the 70-digit text s +fn limbs s view uchar 70 z edit ulong 5 to void +for k ulong 5 +v = ulong 0 +o = sub 70 mul 8 add k 1 +for t ulong 8 +v = add mul v 10 sub ulong get s add o t 48 +end +put z k v +end +end +note mulm: z = x * y mod 10^40, keeping only limbs 0..4; each column sums at most 5 products below 10^16 +note plus a carry below 10^9, so no column passes 2^64 +fn mulm x view ulong 5 y view ulong 5 z edit ulong 5 to void +c = ulong 0 +for k ulong 5 +s = c +for i ulong add k 1 +s = add s mul get x i get y sub k i +end +put z k rem s 100000000 +c = div s 100000000 +end +end +fn cpy x view ulong 5 z edit ulong 5 to void +for k ulong 5 +put z k get x k +end +end +note bits turns the 70-digit decimal text e into binary, least significant bit first, by repeated halving +note bound: e < 10^70 < 2^233, so at most 233 halvings; returns the bit count +fn bits e view uchar 70 bt edit uchar 256 to ulong +dg = new uchar 70 0 +for i ulong 70 +put dg i sub get e i uchar 48 +end +n = ulong 0 +while true bound 256 +z = true +for i ulong 70 +if ne get dg i 0 +z = false +end +end +if z +ret n +end +put bt n and get dg 69 uchar 1 +r = ulong 0 +for i ulong 70 +v = add mul r 10 ulong get dg i +put dg i uchar div v 2 +r = rem v 2 +end +n = add n 1 +end +trap 4 +end +fn main to int +a = new ulong 5 0 +r = new ulong 5 0 +t = new ulong 5 0 +u = new ulong 5 0 +bt = new uchar 256 0 +limbs ta a +put r 0 1 +n = bits tb bt +for i ulong n +if ne get bt i 0 +mulm r a t +cpy t r +end +cpy a u +mulm a u t +cpy t a +end +emit hdr +emitu get r 4 +for k ulong 4 +v = get r sub 3 k +d = ulong 10000000 +while gt d 0 bound 8 +emitu rem div v d 10 +d = div d 10 +end +end +emit nl +ret 0 +end + +{{out}} +
+a^b mod 10^40 = 1527229998585248450016808958343740453059
+
diff --git a/drafts/rosettacode/modular-inverse.wiki b/drafts/rosettacode/modular-inverse.wiki new file mode 100644 index 0000000..38eb332 --- /dev/null +++ b/drafts/rosettacode/modular-inverse.wiki @@ -0,0 +1,105 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text inv "^-1 mod " +text eqs " = " +text none " does not exist\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n in decimal; the minimum long is turned positive as (-(n+1))+1 in ulong +fn emiti n long to void +m = new uchar 1 45 +if lt n 0 +emit m +emitu add ulong neg add n 1 1 +else +emitu ulong n +end +end +note minv: extended Euclid on (m, a mod m) for m > 0; returns -1 when gcd(a, m) > 1 +note bound: Euclid on values below 2^31 takes fewer than 50 steps; q * nt and q * nr stay below m in size +fn minv a long m long to long +x = rem a m +if lt x 0 +x = add x m +end +t = long 0 +nt = long 1 +r = m +nr = x +while ne nr 0 bound 100 +q = div r nr +k = sub t mul q nt +t = nt +nt = k +k = sub r mul q nr +r = nr +nr = k +end +if gt r 1 +ret -1 +end +if lt t 0 +t = add t m +end +ret t +end +fn tell a long m long to void +emiti a +emit inv +emiti m +v = minv a m +if lt v 0 +emit none +else +emit eqs +emiti v +emit nl +end +end +fn main to int +tell 42 2017 +tell 40 1 +tell 52 217 +tell -486 217 +tell 40 2018 +tell 1234567 1000000007 +ret 0 +end + +{{out}} +
+42^-1 mod 2017 = 1969
+40^-1 mod 1 = 0
+52^-1 mod 217 = 96
+-486^-1 mod 217 = 121
+40^-1 mod 2018 does not exist
+1234567^-1 mod 1000000007 = 989145189
+
diff --git a/drafts/rosettacode/narcissistic-decimal-number.wiki b/drafts/rosettacode/narcissistic-decimal-number.wiki new file mode 100644 index 0000000..97e707f --- /dev/null +++ b/drafts/rosettacode/narcissistic-decimal-number.wiki @@ -0,0 +1,80 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text hdr "first 25 narcissistic decimal numbers:" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note n with k digits is narcissistic when the sum of its digits each raised to k equals n +note pw holds d^k at 8 * d + k for digits d and lengths k up to 7 +fn narc n ulong pw view ulong 80 to bool +k = ulong 1 +v = div n 10 +while gt v 0 bound 8 +k = add k 1 +v = div v 10 +end +s = ulong 0 +v = n +for i ulong k +s = add s get pw add mul rem v 10 8 k +v = div v 10 +end +ret eq s n +end +note bound: the 25th such number, 9926315, has 7 digits, so 10^7 candidates suffice +fn main to int +pw = new ulong 80 0 +for d ulong 10 +x = ulong 1 +for k ulong 8 +put pw add mul d 8 k x +x = mul x d +end +end +emit hdr +c = ulong 0 +for n ulong 10000000 +if lt c 25 +if narc n pw +emit sp +emitu n +c = add c 1 +end +end +end +emit nl +ret 0 +end + +{{out}} +
+first 25 narcissistic decimal numbers: 0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315
+
diff --git a/drafts/rosettacode/number-names.wiki b/drafts/rosettacode/number-names.wiki new file mode 100644 index 0000000..0c2db45 --- /dev/null +++ b/drafts/rosettacode/number-names.wiki @@ -0,0 +1,147 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text hy "-" +text colon ": " +text ones "zero\none\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten\neleven\ntwelve\nthirteen\nfourteen\nfifteen\nsixteen\nseventeen\neighteen\nnineteen\n" +text tens "twenty\nthirty\nforty\nfifty\nsixty\nseventy\neighty\nninety\n" +text scales "thousand\nmillion\nbillion\ntrillion\nquadrillion\nquintillion\n" +text hund " hundred" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note word writes element k of a newline-terminated list +fn word t view uchar * k ulong to void +s = ulong 0 +m = ulong 0 +for i ulong len t +if eq get t i 10 +if eq m k +emit part t s sub i s +ret +end +m = add m 1 +s = add i 1 +end +end +trap 9 +end +note three writes 1..999 in words: hundreds, then the rest (under 20 by name, else tens-ones) +fn three g ulong to void +h = div g 100 +r = rem g 100 +if gt h 0 +word ones h +emit hund +if gt r 0 +emit sp +end +end +if and gt r 0 lt r 20 +word ones r +end +if ge r 20 +word tens sub div r 10 2 +if gt rem r 10 0 +emit hy +word ones rem r 10 +end +end +end +note spell splits n into groups of three digits and writes each non-zero one with its scale +fn spell n ulong to void +if eq n 0 +word ones 0 +ret +end +g = new ulong 7 0 +v = n +for i ulong 7 +put g i rem v 1000 +v = div v 1000 +end +first = true +for k ulong 7 +i = sub 6 k +x = get g i +if gt x 0 +if not first +emit sp +end +three x +if gt i 0 +emit sp +word scales sub i 1 +end +first = false +end +end +end +fn show n ulong to void +emitu n +emit colon +spell n +emit nl +end +fn main to int +show 0 +show 7 +show 13 +show 21 +show 99 +show 100 +show 101 +show 999 +show 1000 +show 1001 +show 12345 +show 1000000 +show 1234567890 +show 18446744073709551615 +ret 0 +end + +{{out}} +
+0: zero
+7: seven
+13: thirteen
+21: twenty-one
+99: ninety-nine
+100: one hundred
+101: one hundred one
+999: nine hundred ninety-nine
+1000: one thousand
+1001: one thousand one
+12345: twelve thousand three hundred forty-five
+1000000: one million
+1234567890: one billion two hundred thirty-four million five hundred sixty-seven thousand eight hundred ninety
+18446744073709551615: eighteen quintillion four hundred forty-six quadrillion seven hundred forty-four trillion seventy-three billion seven hundred nine million five hundred fifty-one thousand six hundred fifteen
+
diff --git a/drafts/rosettacode/one-dimensional-cellular-automata.wiki b/drafts/rosettacode/one-dimensional-cellular-automata.wiki new file mode 100644 index 0000000..c7e952c --- /dev/null +++ b/drafts/rosettacode/one-dimensional-cellular-automata.wiki @@ -0,0 +1,111 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tstart "_###_##_#_#_#_#__#__" +text tgen "generation " +text tcolon ": " +global cur uchar 20 +global nxt uchar 20 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitb writes the single byte c +fn emitb c ulong to void +d = new uchar 1 0 +put d 0 uchar c +emit d +end +note a cell is alive next generation when exactly two of itself and its two neighbours are alive +note (cells beyond the ends are dead); the run stops when a generation repeats the one before +fn cell i ulong to ulong +if eq get cur i 35 +ret 1 +end +ret 0 +end +fn step to bool +changed = false +for i ulong 20 +n = cell i +if gt i 0 +n = add n cell sub i 1 +end +if lt i 19 +n = add n cell add i 1 +end +v = uchar 95 +if eq n 2 +v = 35 +end +if ne v get cur i +changed = true +end +put nxt i v +end +for i ulong 20 +put cur i get nxt i +end +ret changed +end +note 20 cells can change at most 2^20 ways, but this start settles after 9 generations; 100 is ample +fn main to int +for i ulong 20 +put cur i get tstart i +end +g = ulong 0 +while true bound 100 +emit tgen +emitu g +emit tcolon +for i ulong 20 +emitb ulong get cur i +end +emit nl +if not step +break +end +g = add g 1 +end +ret 0 +end + +{{out}} +
+generation 0: _###_##_#_#_#_#__#__
+generation 1: _#_#####_#_#_#______
+generation 2: __##___##_#_#_______
+generation 3: __##___###_#________
+generation 4: __##___#_##_________
+generation 5: __##____###_________
+generation 6: __##____#_#_________
+generation 7: __##_____#__________
+generation 8: __##________________
+
diff --git a/drafts/rosettacode/ordered-partitions.wiki b/drafts/rosettacode/ordered-partitions.wiki new file mode 100644 index 0000000..0a7a945 --- /dev/null +++ b/drafts/rosettacode/ordered-partitions.wiki @@ -0,0 +1,141 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tlp "(" +text trp ")" +text tlb "{" +text trb "}" +text tcomma ", " +text tpart "partitions" +global size ulong 4 +global owner ulong 8 +global nb ulong 1 +global nel ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note owner[i] is the block holding element i+1 (nb = unassigned); blocks are filled in order, each with +note its elements chosen as a lexicographic combination of what is left, so the output is in order +fn show to void +emit tlp +for b ulong get nb 0 +if gt b 0 +emit tcomma +end +emit tlb +first = true +for i ulong get nel 0 +if eq get owner i b +if not first +emit tcomma +end +emitu add i 1 +first = false +end +end +emit trb +end +emit trp +emit nl +end +note fill starts block b, or prints the partition once every block is full +fn fill b ulong to void +if eq b get nb 0 +show +ret +end +choose b get size b 0 +end +note choose gives block b `need` more elements, each larger than the last one chosen (at index >= from) +fn choose b ulong need ulong from ulong to void +if eq need 0 +fill add b 1 +ret +end +n = get nel 0 +for x ulong sub n from +i = add from x +if eq get owner i get nb 0 +put owner i b +choose b sub need 1 add i 1 +put owner i get nb 0 +end +end +end +fn run s0 ulong s1 ulong s2 ulong to void +put nb 0 3 +put size 0 s0 +put size 1 s1 +put size 2 s2 +put nel 0 add add s0 s1 s2 +for i ulong get nel 0 +put owner i 3 +end +emit tpart +emit tlp +emitu s0 +emit tcomma +emitu s1 +emit tcomma +emitu s2 +emit trp +emit nl +fill 0 +end +fn main to int +run 2 0 2 +run 1 2 1 +ret 0 +end + +{{out}} +
+partitions(2, 0, 2)
+({1, 2}, {}, {3, 4})
+({1, 3}, {}, {2, 4})
+({1, 4}, {}, {2, 3})
+({2, 3}, {}, {1, 4})
+({2, 4}, {}, {1, 3})
+({3, 4}, {}, {1, 2})
+partitions(1, 2, 1)
+({1}, {2, 3}, {4})
+({1}, {2, 4}, {3})
+({1}, {3, 4}, {2})
+({2}, {1, 3}, {4})
+({2}, {1, 4}, {3})
+({2}, {3, 4}, {1})
+({3}, {1, 2}, {4})
+({3}, {1, 4}, {2})
+({3}, {2, 4}, {1})
+({4}, {1, 2}, {3})
+({4}, {1, 3}, {2})
+({4}, {2, 3}, {1})
+
diff --git a/drafts/rosettacode/ordered-words.wiki b/drafts/rosettacode/ordered-words.wiki new file mode 100644 index 0000000..62b2ae5 --- /dev/null +++ b/drafts/rosettacode/ordered-words.wiki @@ -0,0 +1,64 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +note the word list (Rosetta Code reads unixdict.txt; this is a fixed sample of it), newline-terminated +text dict "almost\nabbey\naccent\nbegin\nbiopsy\nchimp\nchintz\ndeist\neffort\nfirst\nghost\nglory\nfloor\nknotty\nbillowy\nbijoux\nabhors\naegilops\nthis\nthat\nzebra\nbellow\nadder\ncello\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note ordered: every letter is at least the one before it +fn ordered a view uchar * to bool +for i ulong len a +if gt i 0 +if lt get a i get a sub i 1 +ret false +end +end +end +ret true +end +note two passes over the list: the longest ordered length, then every ordered word of that length +fn longest to ulong +best = ulong 0 +s = ulong 0 +for i ulong len dict +if eq get dict i 10 +n = sub i s +if and ordered part dict s n gt n best +best = n +end +s = add i 1 +end +end +ret best +end +fn main to int +best = longest +s = ulong 0 +for i ulong len dict +if eq get dict i 10 +n = sub i s +if and ordered part dict s n eq n best +emit part dict s add n 1 +end +s = add i 1 +end +end +ret 0 +end + +{{out}} +
+aegilops
+
diff --git a/drafts/rosettacode/palindrome-detection.wiki b/drafts/rosettacode/palindrome-detection.wiki new file mode 100644 index 0000000..9b68628 --- /dev/null +++ b/drafts/rosettacode/palindrome-detection.wiki @@ -0,0 +1,86 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text tq "\"" +text tex "\" exact " +text tin " inexact " +text tyes "yes" +text tno "no" +text p1 "rotor" +text p2 "level" +text p3 "A man, a plan, a canal: Panama!" +text p4 "In girum imus nocte et consumimur igni" +text p5 "hello" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +fn emitb b bool to void +if b +emit tyes +else +emit tno +end +end +note exact compares bytes from both ends +fn exact a view uchar * to bool +n = len a +for i ulong div n 2 +if ne get a i get a sub sub n 1 i +ret false +end +end +ret true +end +note inexact keeps letters only, lower-cased, then compares (strings up to 256 bytes) +fn inexact a view uchar * to bool +b = new uchar 256 0 +n = ulong 0 +for i ulong len a +c = get a i +if and ge c 65 le c 90 +c = add c 32 +end +if and ge c 97 le c 122 +put b n c +n = add n 1 +end +end +ret exact part b 0 n +end +fn show a view uchar * to void +emit tq +emit a +emit tex +emitb exact a +emit tin +emitb inexact a +emit nl +end +fn main to int +show p1 +show p2 +show p3 +show p4 +show p5 +ret 0 +end + +{{out}} +
+"rotor" exact yes inexact yes
+"level" exact yes inexact yes
+"A man, a plan, a canal: Panama!" exact no inexact yes
+"In girum imus nocte et consumimur igni" exact no inexact yes
+"hello" exact no inexact no
+
diff --git a/drafts/rosettacode/pancake-sort.wiki b/drafts/rosettacode/pancake-sort.wiki new file mode 100644 index 0000000..fd85123 --- /dev/null +++ b/drafts/rosettacode/pancake-sort.wiki @@ -0,0 +1,136 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "23 -7 61 4 0 38 15 -7 90 2 11" +text tflips "flips: " +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note flip reverses vals[0..k-1]; k/2 swaps +fn flip k ulong to void +for i ulong shr k 1 +t = get vals i +put vals i get vals sub sub k i 1 +put vals sub sub k i 1 t +end +end +note pksort brings the largest of vals[0..s-1] to the front, then flips it to position s-1, for s = n .. 2; +note it returns the number of flips made, at most 2*(n-1) +fn pksort n ulong to ulong +f = ulong 0 +for x ulong sub n 1 +s = sub n x +m = ulong 0 +for i ulong s +if gt get vals i get vals m +m = i +end +end +if ne m sub s 1 +if ne m 0 +flip add m 1 +f = add f 1 +end +flip s +f = add f 1 +end +end +ret f +end +fn main to int +n = parse data +show n +f = pksort n +show n +emit tflips +emitu f +emit nl +ret 0 +end + +{{out}} +
+23 -7 61 4 0 38 15 -7 90 2 11
+-7 -7 0 2 4 11 15 23 38 61 90
+flips: 16
+
diff --git a/drafts/rosettacode/pangram-checker.wiki b/drafts/rosettacode/pangram-checker.wiki new file mode 100644 index 0000000..75bf3cf --- /dev/null +++ b/drafts/rosettacode/pangram-checker.wiki @@ -0,0 +1,63 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text tq "\"" +text tis "\" is a pangram\n" +text tnot "\" is not a pangram\n" +text p1 "The quick brown fox jumps over the lazy dog" +text p2 "The quick brown fox jumped over the lazy dog" +text p3 "Pack my box with five dozen liquor jugs." +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +fn pangram a view uchar * to bool +seen = new uchar 26 0 +for i ulong len a +c = get a i +if and ge c 65 le c 90 +c = add c 32 +end +if and ge c 97 le c 122 +put seen sub c 97 1 +end +end +for j ulong 26 +if eq get seen j 0 +ret false +end +end +ret true +end +fn show a view uchar * to void +emit tq +emit a +if pangram a +emit tis +else +emit tnot +end +end +fn main to int +show p1 +show p2 +show p3 +ret 0 +end + +{{out}} +
+"The quick brown fox jumps over the lazy dog" is a pangram
+"The quick brown fox jumped over the lazy dog" is not a pangram
+"Pack my box with five dozen liquor jugs." is a pangram
+
diff --git a/drafts/rosettacode/pascals-triangle.wiki b/drafts/rosettacode/pascals-triangle.wiki new file mode 100644 index 0000000..8ee09d4 --- /dev/null +++ b/drafts/rosettacode/pascals-triangle.wiki @@ -0,0 +1,75 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +global row ulong 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note each row comes from the one above in place, right to left: row[j] += row[j-1]; each row is +note indented so the triangle is centred, two columns per level +fn pascal n ulong to void +put row 0 1 +for r ulong n +for x ulong mul 2 sub sub n r 1 +emit sp +end +for j ulong add r 1 +if gt j 0 +emit sp +end +emitu get row j +end +emit nl +put row add r 1 1 +for x ulong r +j = sub r x +put row j add get row j get row sub j 1 +end +end +end +fn main to int +pascal 10 +ret 0 +end + +{{out}} +
+                  1
+                1 1
+              1 2 1
+            1 3 3 1
+          1 4 6 4 1
+        1 5 10 10 5 1
+      1 6 15 20 15 6 1
+    1 7 21 35 35 21 7 1
+  1 8 28 56 70 56 28 8 1
+1 9 36 84 126 126 84 36 9 1
+
diff --git a/drafts/rosettacode/patience-sort.wiki b/drafts/rosettacode/patience-sort.wiki new file mode 100644 index 0000000..c6915a6 --- /dev/null +++ b/drafts/rosettacode/patience-sort.wiki @@ -0,0 +1,146 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "4 65 2 -31 0 99 83 782 1 4 -31 18" +global vals long 16 +global pile long 256 +global plen ulong 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note piles: pile p holds its cards in pile[p*16 .. p*16+plen[p]-1], the top card last; tops decrease +note bottom to top, so a new card goes on the leftmost pile whose top is >= it, else on a new pile +fn deal n ulong to ulong +np = ulong 0 +for i ulong n +v = get vals i +p = ulong 0 +while lt p np bound 16 +if ge top p v +break +end +p = add p 1 +end +if eq p np +np = add np 1 +end +put pile add mul p 16 get plen p v +put plen p add get plen p 1 +end +ret np +end +note top is the top card of pile p, which must not be empty +fn top p ulong to long +ret get pile add mul p 16 sub get plen p 1 +end +note merge takes the smallest top card of all piles n times; at most 16 piles to scan each time +fn merge n ulong np ulong to void +for k ulong n +b = np +for p ulong np +if gt get plen p 0 +if eq b np +b = p +else +if lt top p top b +b = p +end +end +end +end +put plen b sub get plen b 1 +put vals k get pile add mul b 16 get plen b +end +end +fn main to int +n = parse data +show n +merge n deal n +show n +ret 0 +end + +{{out}} +
+4 65 2 -31 0 99 83 782 1 4 -31 18
+-31 -31 0 1 2 4 4 18 65 83 99 782
+
diff --git a/drafts/rosettacode/perfect-totient-numbers.wiki b/drafts/rosettacode/perfect-totient-numbers.wiki new file mode 100644 index 0000000..423c9f0 --- /dev/null +++ b/drafts/rosettacode/perfect-totient-numbers.wiki @@ -0,0 +1,82 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text first "first 20 perfect totient numbers:" +note ph n is Euler's totient for n below 6000 (the 20th perfect totient number is 5571) +global ph ulong 6000 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn sieve to void +for i ulong 6000 +put ph i i +end +p = ulong 2 +while lt p 6000 bound 6000 +if eq get ph p p +j = p +while lt j 6000 bound 6000 +put ph j sub get ph j div get ph j p +j = add j p +end +end +p = add p 1 +end +end +note n is perfect totient when phi(n) + phi(phi(n)) + ... down to 1 adds up to n; each step at least halves +note an even value, so 64 steps are plenty +fn main to int +sieve +emit first +c = ulong 0 +n = ulong 2 +while lt c 20 bound 5999 +s = ulong 0 +v = get ph n +while gt v 1 bound 64 +s = add s v +v = get ph v +end +s = add s 1 +if eq s n +emit sp +emitu n +c = add c 1 +end +n = add n 1 +end +emit nl +ret 0 +end + +{{out}} +
+first 20 perfect totient numbers: 3 9 15 27 39 81 111 183 243 255 327 363 471 729 2187 2199 3063 4359 4375 5571
+
diff --git a/drafts/rosettacode/permutation-sort.wiki b/drafts/rosettacode/permutation-sort.wiki new file mode 100644 index 0000000..6889aad --- /dev/null +++ b/drafts/rosettacode/permutation-sort.wiki @@ -0,0 +1,165 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "8 -2 15 3 0 6" +text ttried "permutations tried: " +global vals long 8 +global perm ulong 8 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note sorted says whether vals taken in the order perm[0..n-1] is non-decreasing +fn sorted n ulong to bool +for i ulong sub n 1 +if gt get vals get perm i get vals get perm add i 1 +ret false +end +end +ret true +end +note nextp steps perm to the next permutation in lexicographic order; false after the last one +fn nextp n ulong to bool +i = sub n 1 +while gt i 0 bound 8 +if lt get perm sub i 1 get perm i +break +end +i = sub i 1 +end +if eq i 0 +ret false +end +j = sub n 1 +while le get perm j get perm sub i 1 bound 8 +j = sub j 1 +end +t = get perm sub i 1 +put perm sub i 1 get perm j +put perm j t +k = sub n 1 +while lt i k bound 8 +t2 = get perm i +put perm i get perm k +put perm k t2 +i = add i 1 +k = sub k 1 +end +ret true +end +note pmsort tries the index permutations in lexicographic order (at most 6! = 720) until one is sorted +fn pmsort n ulong to ulong +for i ulong n +put perm i i +end +c = ulong 1 +while not sorted n bound 720 +if not nextp n +trap 9 +end +c = add c 1 +end +t = new long 8 0 +for i ulong n +put t i get vals get perm i +end +for i ulong n +put vals i get t i +end +ret c +end +fn main to int +n = parse data +show n +c = pmsort n +show n +emit ttried +emitu c +emit nl +ret 0 +end + +{{out}} +
+8 -2 15 3 0 6
+-2 0 3 6 8 15
+permutations tried: 209
+
diff --git a/drafts/rosettacode/permutations-by-swapping.wiki b/drafts/rosettacode/permutations-by-swapping.wiki new file mode 100644 index 0000000..b059876 --- /dev/null +++ b/drafts/rosettacode/permutations-by-swapping.wiki @@ -0,0 +1,141 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tperm "Perm: [" +text tsign "] Sign: " +text tcomma ", " +global perm ulong 8 +global dir long 8 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note Steinhaus-Johnson-Trotter with Even's speedup: every element has a direction (all start left); +note the largest mobile element (one whose neighbour in its direction is smaller) swaps that way, and +note every larger element turns round; each step is one swap, so the sign alternates +fn mobile n ulong to ulong +b = n +for i ulong n +j = add long i get dir i +if and ge j 0 lt j long n +if gt get perm i get perm ulong j +if or eq b n gt get perm i get perm b +b = i +end +end +end +end +ret b +end +fn show n ulong s long to void +emit tperm +for i ulong n +if gt i 0 +emit tcomma +end +emitu get perm i +end +emit tsign +emiti s +emit nl +end +fn sjt n ulong to void +for i ulong n +put perm i add i 1 +put dir i -1 +end +s = long 1 +show n s +note each pass makes one new permutation and n! of them end the run: 720 = 6! covers n up to 6 +while true bound 720 +m = mobile n +if eq m n +break +end +v = get perm m +j = ulong add long m get dir m +put perm m get perm j +put perm j v +t = get dir m +put dir m get dir j +put dir j t +for i ulong n +if gt get perm i v +put dir i neg get dir i +end +end +s = neg s +show n s +end +end +note n = 3 and n = 4: 3! + 4! = 30 lines; 6! = 720 bounds the loop for any n up to 6 +fn main to int +sjt 3 +emit nl +sjt 4 +ret 0 +end + +{{out}} +
+Perm: [1, 2, 3] Sign: 1
+Perm: [1, 3, 2] Sign: -1
+Perm: [3, 1, 2] Sign: 1
+Perm: [3, 2, 1] Sign: -1
+Perm: [2, 3, 1] Sign: 1
+Perm: [2, 1, 3] Sign: -1
+
+Perm: [1, 2, 3, 4] Sign: 1
+Perm: [1, 2, 4, 3] Sign: -1
+Perm: [1, 4, 2, 3] Sign: 1
+Perm: [4, 1, 2, 3] Sign: -1
+Perm: [4, 1, 3, 2] Sign: 1
+Perm: [1, 4, 3, 2] Sign: -1
+Perm: [1, 3, 4, 2] Sign: 1
+Perm: [1, 3, 2, 4] Sign: -1
+...
+Perm: [2, 3, 1, 4] Sign: 1
+Perm: [2, 3, 4, 1] Sign: -1
+Perm: [2, 4, 3, 1] Sign: 1
+Perm: [4, 2, 3, 1] Sign: -1
+Perm: [4, 2, 1, 3] Sign: 1
+Perm: [2, 4, 1, 3] Sign: -1
+Perm: [2, 1, 4, 3] Sign: 1
+Perm: [2, 1, 3, 4] Sign: -1
+
diff --git a/drafts/rosettacode/permutations-derangements.wiki b/drafts/rosettacode/permutations-derangements.wiki new file mode 100644 index 0000000..0b8b6a6 --- /dev/null +++ b/drafts/rosettacode/permutations-derangements.wiki @@ -0,0 +1,181 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tder "derangements of 0 1 2 3:\n" +text thead "n counted !n\n" +text tbig "!20 = " +global perm ulong 10 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt w sub 20 i +i = sub 20 w +end +emit part d i sub 20 i +end +note nextp: the next permutation in lexicographic order, false after the last +fn nextp n ulong to bool +if lt n 2 +ret false +end +i = sub n 1 +while gt i 0 bound 10 +if lt get perm sub i 1 get perm i +break +end +i = sub i 1 +end +if eq i 0 +ret false +end +j = sub n 1 +while le get perm j get perm sub i 1 bound 10 +j = sub j 1 +end +t = get perm sub i 1 +put perm sub i 1 get perm j +put perm j t +k = sub n 1 +while lt i k bound 10 +t2 = get perm i +put perm i get perm k +put perm k t2 +i = add i 1 +k = sub k 1 +end +ret true +end +fn deranged n ulong to bool +for i ulong n +if eq get perm i i +ret false +end +end +ret true +end +note count walks all n! permutations (at most 9! = 362880) and counts those that fix no point; +note with show set it also prints them +fn count n ulong show bool to ulong +for i ulong n +put perm i i +end +c = ulong 0 +while true bound 362880 +if deranged n +c = add c 1 +if show +for i ulong n +if gt i 0 +emit sp +end +emitu get perm i +end +emit nl +end +end +if not nextp n +break +end +end +ret c +end +note subfactorial by the recurrence !0 = 1, !1 = 0, !n = (n-1)(!(n-1) + !(n-2)); !20 fits a ulong +fn subfact n ulong to ulong +a = ulong 1 +b = ulong 0 +if eq n 0 +ret a +end +for x ulong sub n 1 +m = add x 1 +c = mul m add a b +a = b +b = c +end +ret b +end +fn main to int +emit tder +z = count 4 true +emit thead +for n ulong 10 +emitu n +emit sp +emitw count n false 8 +emit sp +emit sp +emitu subfact n +emit nl +end +emit tbig +emitu subfact 20 +emit nl +ret 0 +end + +{{out}} +
+derangements of 0 1 2 3:
+1 0 3 2
+1 2 3 0
+1 3 0 2
+2 0 3 1
+2 3 0 1
+2 3 1 0
+3 0 1 2
+3 2 0 1
+3 2 1 0
+n  counted  !n
+0        1  1
+1        0  0
+2        1  1
+3        2  2
+4        9  9
+5       44  44
+6      265  265
+7     1854  1854
+8    14833  14833
+9   133496  133496
+!20 = 895014631192902121
+
diff --git a/drafts/rosettacode/permutations.wiki b/drafts/rosettacode/permutations.wiki new file mode 100644 index 0000000..31b09ad --- /dev/null +++ b/drafts/rosettacode/permutations.wiki @@ -0,0 +1,120 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tcount "permutations: " +global perm ulong 8 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note nextp steps perm[0..n-1] to the next permutation in lexicographic order; false after the last: +note find the last ascent i-1 < i, swap perm[i-1] with the last larger element, reverse the tail +fn nextp n ulong to bool +i = sub n 1 +while gt i 0 bound 8 +if lt get perm sub i 1 get perm i +break +end +i = sub i 1 +end +if eq i 0 +ret false +end +j = sub n 1 +while le get perm j get perm sub i 1 bound 8 +j = sub j 1 +end +t = get perm sub i 1 +put perm sub i 1 get perm j +put perm j t +k = sub n 1 +while lt i k bound 8 +t2 = get perm i +put perm i get perm k +put perm k t2 +i = add i 1 +k = sub k 1 +end +ret true +end +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emitu get perm i +end +emit nl +end +note all 4! orders of 1 2 3 4; 24 permutations bound the loop +fn main to int +n = ulong 4 +for i ulong n +put perm i add i 1 +end +c = ulong 1 +show n +while nextp n bound 24 +show n +c = add c 1 +end +emit tcount +emitu c +emit nl +ret 0 +end + +{{out}} +
+1 2 3 4
+1 2 4 3
+1 3 2 4
+1 3 4 2
+1 4 2 3
+1 4 3 2
+2 1 3 4
+2 1 4 3
+2 3 1 4
+2 3 4 1
+2 4 1 3
+2 4 3 1
+3 1 2 4
+3 1 4 2
+3 2 1 4
+...
+3 4 2 1
+4 1 2 3
+4 1 3 2
+4 2 1 3
+4 2 3 1
+4 3 1 2
+4 3 2 1
+permutations: 24
+
diff --git a/drafts/rosettacode/pernicious-numbers.wiki b/drafts/rosettacode/pernicious-numbers.wiki new file mode 100644 index 0000000..216e978 --- /dev/null +++ b/drafts/rosettacode/pernicious-numbers.wiki @@ -0,0 +1,88 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text first "first 25 pernicious numbers:" +text range "pernicious numbers from 888888877 to 888888888:" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn popc n ulong to ulong +c = ulong 0 +v = n +while gt v 0 bound 64 +c = add c and v 1 +v = shr v 1 +end +ret c +end +note a pernicious number has a prime population count; counts here are at most 30, so trial division is enough +fn isprime n ulong to bool +if lt n 2 +ret false +end +d = ulong 2 +while le mul d d n bound 8 +if eq rem n d 0 +ret false +end +d = add d 1 +end +ret true +end +fn main to int +emit first +c = ulong 0 +n = ulong 1 +while lt c 25 bound 100 +if isprime popc n +emit sp +emitu n +c = add c 1 +end +n = add n 1 +end +emit nl +emit range +for i ulong 12 +n = add 888888877 i +if isprime popc n +emit sp +emitu n +end +end +emit nl +ret 0 +end + +{{out}} +
+first 25 pernicious numbers: 3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36
+pernicious numbers from 888888877 to 888888888: 888888877 888888878 888888880 888888883 888888885 888888886
+
diff --git a/drafts/rosettacode/phrase-reversals.wiki b/drafts/rosettacode/phrase-reversals.wiki new file mode 100644 index 0000000..4754288 --- /dev/null +++ b/drafts/rosettacode/phrase-reversals.wiki @@ -0,0 +1,81 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text msg "rosetta code phrase reversal" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note words splits a into (start, length) pairs at single spaces and returns how many +fn words a view uchar * st edit ulong 16 ln edit ulong 16 to ulong +k = ulong 0 +s = ulong 0 +for i ulong add len a 1 +e = eq i len a +if not e +e = eq get a i 32 +end +if e +put st k s +put ln k sub i s +k = add k 1 +s = add i 1 +end +end +ret k +end +fn revb a view uchar * to void +for i ulong len a +emitc get a sub sub len a 1 i +end +end +fn main to int +st = new ulong 16 0 +ln = new ulong 16 0 +k = words msg st ln +note 1: the whole string reversed +revb msg +emit nl +note 2: each word reversed, order kept +for w ulong k +if gt w 0 +emit sp +end +revb part msg get st w get ln w +end +emit nl +note 3: the word order reversed, words kept +for w ulong k +if gt w 0 +emit sp +end +x = sub sub k 1 w +emit part msg get st x get ln x +end +emit nl +ret 0 +end + +{{out}} +
+lasrever esarhp edoc attesor
+attesor edoc esarhp lasrever
+reversal phrase code rosetta
+
diff --git a/drafts/rosettacode/population-count.wiki b/drafts/rosettacode/population-count.wiki new file mode 100644 index 0000000..f38a1f4 --- /dev/null +++ b/drafts/rosettacode/population-count.wiki @@ -0,0 +1,82 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text pw "popcount of 3^0 .. 3^29:" +text evil "first 30 evil numbers: " +text odious "first 30 odious numbers:" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note Kernighan: n & (n - 1) clears the lowest set bit +fn popc n ulong to ulong +c = ulong 0 +v = n +while gt v 0 bound 64 +v = and v sub v 1 +c = add c 1 +end +ret c +end +note evil numbers have an even population count, odious ones an odd count +fn list want ulong to void +c = ulong 0 +n = ulong 0 +while lt c 30 bound 100 +if eq rem popc n 2 want +emit sp +emitu n +c = add c 1 +end +n = add n 1 +end +emit nl +end +fn main to int +emit pw +p = ulong 1 +for i ulong 30 +emit sp +emitu popc p +p = mul p 3 +end +emit nl +emit evil +list 0 +emit odious +list 1 +ret 0 +end + +{{out}} +
+popcount of 3^0 .. 3^29: 1 2 2 4 3 6 6 5 6 8 9 13 10 11 14 15 11 14 14 17 17 20 19 22 16 18 24 30 25 25
+first 30 evil numbers:   0 3 5 6 9 10 12 15 17 18 20 23 24 27 29 30 33 34 36 39 40 43 45 46 48 51 53 54 57 58
+first 30 odious numbers: 1 2 4 7 8 11 13 14 16 19 21 22 25 26 28 31 32 35 37 38 41 42 44 47 49 50 52 55 56 59
+
diff --git a/drafts/rosettacode/power-set.wiki b/drafts/rosettacode/power-set.wiki new file mode 100644 index 0000000..6cb93c4 --- /dev/null +++ b/drafts/rosettacode/power-set.wiki @@ -0,0 +1,85 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tlb "{" +text trb "}" +text tcomma ", " +text tcount "subsets: " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note subset m of {1, 2, 3, 4} holds element i+1 when bit i of m is set; the 16 subsets are listed in +note the order of m, which is binary counting +fn subset m ulong to void +emit tlb +first = true +for i ulong 4 +if ne and shr m i 1 0 +if not first +emit tcomma +end +emitu add i 1 +first = false +end +end +emit trb +emit nl +end +fn main to int +for m ulong 16 +subset m +end +emit tcount +emitu 16 +emit nl +ret 0 +end + +{{out}} +
+{}
+{1}
+{2}
+{1, 2}
+{3}
+{1, 3}
+{2, 3}
+{1, 2, 3}
+{4}
+{1, 4}
+{2, 4}
+{1, 2, 4}
+{3, 4}
+{1, 3, 4}
+{2, 3, 4}
+{1, 2, 3, 4}
+subsets: 16
+
diff --git a/drafts/rosettacode/primality-by-trial-division.wiki b/drafts/rosettacode/primality-by-trial-division.wiki new file mode 100644 index 0000000..c839ea1 --- /dev/null +++ b/drafts/rosettacode/primality-by-trial-division.wiki @@ -0,0 +1,96 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text hdr "primes below 100:" +text yes " is prime\n" +text no " is not prime\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note trial division by 2, then by odd d while d * d <= n +note bound: the largest input is below 10^12 + 40, so d stays below 10^6 + 2 (at most 500001 odd values) and mul d d does not wrap +fn prime n ulong to bool +if lt n 2 +ret false +end +if eq rem n 2 0 +ret eq n 2 +end +d = ulong 3 +while le mul d d n bound 999999 +if eq rem n d 0 +ret false +end +d = add d 2 +end +ret true +end +fn tell n ulong to void +emitu n +if prime n +emit yes +else +emit no +end +end +fn main to int +emit hdr +for i ulong 100 +if prime i +emit sp +emitu i +end +end +emit nl +tell 1 +tell 2 +tell 9 +tell 561 +tell 7919 +tell 2147483647 +tell 999999999989 +tell 999999999999 +tell 1000000000039 +ret 0 +end + +{{out}} +
+primes below 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
+1 is not prime
+2 is prime
+9 is not prime
+561 is not prime
+7919 is prime
+2147483647 is prime
+999999999989 is prime
+999999999999 is not prime
+1000000000039 is prime
+
diff --git a/drafts/rosettacode/priority-queue.wiki b/drafts/rosettacode/priority-queue.wiki new file mode 100644 index 0000000..21bfbf2 --- /dev/null +++ b/drafts/rosettacode/priority-queue.wiki @@ -0,0 +1,139 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tins "insert " +text tpop "pop " +text tdata "3 Clear drains|4 Feed cat|5 Make tea|1 Solve RC tasks|2 Tax return" +global heap ulong 16 +global pri ulong 16 +global hn ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a binary min-heap of task numbers keyed by priority: heap[0] has the smallest pri; the five tasks +note are the |-separated entries of tdata, each a priority digit and a name +fn entry k ulong to void +f = ulong 0 +st = ulong 0 +for i ulong len tdata +if eq get tdata i 124 +if eq f k +emit part tdata st sub i st +ret +end +f = add f 1 +st = add i 1 +end +end +emit part tdata st sub len tdata st +end +fn swap i ulong j ulong to void +t = get heap i +put heap i get heap j +put heap j t +end +fn less i ulong j ulong to bool +ret lt get pri get heap i get pri get heap j +end +note insert sifts the new task up; the index halves each step, so 16 steps bound it +fn insert k ulong p ulong to void +put pri k p +n = get hn 0 +put heap n k +put hn 0 add n 1 +i = n +while gt i 0 bound 16 +u = shr sub i 1 1 +if not less i u +break +end +swap i u +i = u +end +emit tins +entry k +emit nl +end +note popmin moves the last task to the root and sifts it down; the index doubles each step +fn popmin to void +n = sub get hn 0 1 +k = get heap 0 +put heap 0 get heap n +put hn 0 n +i = ulong 0 +while lt add mul i 2 1 n bound 16 +c = add mul i 2 1 +if lt add c 1 n +if less add c 1 c +c = add c 1 +end +end +if not less c i +break +end +swap i c +i = c +end +emit tpop +entry k +emit nl +end +fn main to int +st = ulong 0 +k = ulong 0 +for i ulong len tdata +if eq i st +insert k ulong sub get tdata i 48 +k = add k 1 +end +if eq get tdata i 124 +st = add i 1 +end +end +for x ulong k +popmin +end +ret 0 +end + +{{out}} +
+insert 3 Clear drains
+insert 4 Feed cat
+insert 5 Make tea
+insert 1 Solve RC tasks
+insert 2 Tax return
+pop 1 Solve RC tasks
+pop 2 Tax return
+pop 3 Clear drains
+pop 4 Feed cat
+pop 5 Make tea
+
diff --git a/drafts/rosettacode/proper-divisors.wiki b/drafts/rosettacode/proper-divisors.wiki new file mode 100644 index 0000000..4da5abe --- /dev/null +++ b/drafts/rosettacode/proper-divisors.wiki @@ -0,0 +1,93 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text col ":" +text most "the number in 1..20000 with the most proper divisors: " +text has " has " +text pd " proper divisors\n" +note nd n counts the proper divisors of n, for n up to 20000 +global nd ulong 20001 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn main to int +note proper divisors of 1..10 by trial over 1..n-1 +for i ulong 10 +n = add i 1 +emitu n +emit col +for j ulong i +d = add j 1 +if eq rem n d 0 +emit sp +emitu d +end +end +emit nl +end +note each d counts once for each multiple 2d, 3d, ... up to 20000; fewer than 20000 multiples per d +d = ulong 1 +while le d 10000 bound 10000 +j = mul d 2 +while le j 20000 bound 20000 +put nd j add get nd j 1 +j = add j d +end +d = add d 1 +end +b = ulong 1 +for i ulong 20000 +n = add i 1 +if gt get nd n get nd b +b = n +end +end +emit most +emitu b +emit has +emitu get nd b +emit pd +ret 0 +end + +{{out}} +
+1:
+2: 1
+3: 1
+4: 1 2
+5: 1
+6: 1 2 3
+7: 1
+8: 1 2 4
+9: 1 3
+10: 1 2 5
+the number in 1..20000 with the most proper divisors: 15120 has 79 proper divisors
+
diff --git a/drafts/rosettacode/pythagorean-triples.wiki b/drafts/rosettacode/pythagorean-triples.wiki new file mode 100644 index 0000000..988a06b --- /dev/null +++ b/drafts/rosettacode/pythagorean-triples.wiki @@ -0,0 +1,97 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text pre "perimeter <= " +text col ": " +text tr " triples, " +text pr " primitive\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn gcd a ulong b ulong to ulong +x = a +y = b +while ne y 0 bound 128 +t = rem x y +x = y +y = t +end +ret x +end +note Euclid: m > n > 0 coprime of opposite parity give each primitive triple once, with perimeter 2m(m+n); +note a primitive perimeter p counts for limit L once as primitive and L / p times among all triples +note bound: 2m(m+1) <= 10^7 needs m <= 2235 +fn main to int +pc = new ulong 7 0 +tc = new ulong 7 0 +m = ulong 2 +while le mul mul 2 m add m 1 10000000 bound 2236 +n = add rem m 2 1 +while lt n m bound 2236 +p = mul mul 2 m add m n +if gt p 10000000 +break +end +if eq gcd m n 1 +l = ulong 100 +for k ulong 6 +if le p l +put pc k add get pc k 1 +put tc k add get tc k div l p +end +l = mul l 10 +end +end +n = add n 2 +end +m = add m 1 +end +l = ulong 100 +for k ulong 6 +emit pre +emitu l +emit col +emitu get tc k +emit tr +emitu get pc k +emit pr +l = mul l 10 +end +ret 0 +end + +{{out}} +
+perimeter <= 100: 17 triples, 7 primitive
+perimeter <= 1000: 325 triples, 70 primitive
+perimeter <= 10000: 4858 triples, 703 primitive
+perimeter <= 100000: 64741 triples, 7026 primitive
+perimeter <= 1000000: 808950 triples, 70229 primitive
+perimeter <= 10000000: 9706567 triples, 702309 primitive
+
diff --git a/drafts/rosettacode/queue-definition.wiki b/drafts/rosettacode/queue-definition.wiki new file mode 100644 index 0000000..9b45e4d --- /dev/null +++ b/drafts/rosettacode/queue-definition.wiki @@ -0,0 +1,137 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tpush "push " +text tpop "pop " +text tempty "empty: " +text tyes "yes" +text tno "no" +text terr "pop from an empty queue: error" +global ring long 8 +global head ulong 1 +global cnt ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note a FIFO queue in a ring of 8 slots: cnt elements starting at ring[head]; push writes behind the +note last one, pop takes ring[head]; a full ring stops the program (exit 2) +fn push v long to void +c = get cnt 0 +if eq c 8 +exit 2 +end +put ring rem add get head 0 c 8 v +put cnt 0 add c 1 +emit tpush +emiti v +emit nl +end +note pop returns false, and prints the error, when the queue is empty +fn pop to bool +c = get cnt 0 +if eq c 0 +emit terr +emit nl +ret false +end +h = get head 0 +emit tpop +emiti get ring h +emit nl +put head 0 rem add h 1 8 +put cnt 0 sub c 1 +ret true +end +fn empty to void +emit tempty +if eq get cnt 0 0 +emit tyes +else +emit tno +end +emit nl +end +note the ring wraps: 6 pushes, 4 pops and 5 more pushes cross slot 7 back to slot 0 +fn main to int +empty +for i ulong 6 +push mul long add i 1 11 +end +for i ulong 4 +ok = pop +end +for i ulong 5 +push mul long add i 7 -11 +end +empty +note 7 elements remain, so the 8th pop finds the queue empty and ends the loop +while pop bound 9 +end +empty +ret 0 +end + +{{out}} +
+empty: yes
+push 11
+push 22
+push 33
+push 44
+push 55
+push 66
+pop 11
+pop 22
+pop 33
+pop 44
+push -77
+push -88
+push -99
+push -110
+...
+pop 66
+pop -77
+pop -88
+pop -99
+pop -110
+pop -121
+pop from an empty queue: error
+empty: yes
+
diff --git a/drafts/rosettacode/queue-usage.wiki b/drafts/rosettacode/queue-usage.wiki new file mode 100644 index 0000000..407e390 --- /dev/null +++ b/drafts/rosettacode/queue-usage.wiki @@ -0,0 +1,121 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tpush "push " +text tpop "pop " +text tempty "empty: " +text tyes "yes" +text tno "no" +text terr "pop from an empty queue: error" +global ring long 8 +global head ulong 1 +global cnt ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note a FIFO queue in a ring of 8 slots: cnt elements starting at ring[head]; push writes behind the +note last one, pop takes ring[head]; a full ring stops the program (exit 2) +fn push v long to void +c = get cnt 0 +if eq c 8 +exit 2 +end +put ring rem add get head 0 c 8 v +put cnt 0 add c 1 +emit tpush +emiti v +emit nl +end +note pop returns false, and prints the error, when the queue is empty +fn pop to bool +c = get cnt 0 +if eq c 0 +emit terr +emit nl +ret false +end +h = get head 0 +emit tpop +emiti get ring h +emit nl +put head 0 rem add h 1 8 +put cnt 0 sub c 1 +ret true +end +fn empty to void +emit tempty +if eq get cnt 0 0 +emit tyes +else +emit tno +end +emit nl +end +fn main to int +empty +push 1 +push 2 +push 3 +ok = pop +empty +push 4 +ok = pop +ok = pop +ok = pop +empty +ok = pop +ret 0 +end + +{{out}} +
+empty: yes
+push 1
+push 2
+push 3
+pop 1
+empty: no
+push 4
+pop 2
+pop 3
+pop 4
+empty: yes
+pop from an empty queue: error
+
diff --git a/drafts/rosettacode/quicksort.wiki b/drafts/rosettacode/quicksort.wiki new file mode 100644 index 0000000..f9d33fa --- /dev/null +++ b/drafts/rosettacode/quicksort.wiki @@ -0,0 +1,123 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "65 -12 0 33 -7 99 18 18 -40 5 71 2 -1 27" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note qsort sorts vals[lo..hi-1] with a Lomuto partition around the last element; each call recurses +note on strictly smaller ranges, so the depth is at most hi-lo and the scan at most 16 passes +fn qsort lo ulong hi ulong to void +if le sub hi lo 1 +ret +end +p = get vals sub hi 1 +i = lo +j = lo +while lt j sub hi 1 bound 16 +if lt get vals j p +t = get vals i +put vals i get vals j +put vals j t +i = add i 1 +end +j = add j 1 +end +put vals sub hi 1 get vals i +put vals i p +qsort lo i +qsort add i 1 hi +end +fn main to int +n = parse data +show n +qsort 0 n +show n +ret 0 +end + +{{out}} +
+65 -12 0 33 -7 99 18 18 -40 5 71 2 -1 27
+-40 -12 -7 -1 0 2 5 18 18 27 33 65 71 99
+
diff --git a/drafts/rosettacode/radix-sort.wiki b/drafts/rosettacode/radix-sort.wiki new file mode 100644 index 0000000..bab8bf1 --- /dev/null +++ b/drafts/rosettacode/radix-sort.wiki @@ -0,0 +1,152 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "170 45 75 -90 -802 24 2 66 0 1999 -1 333" +global vals long 16 +global key ulong 16 +global tk ulong 16 +global bk ulong 10 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note rxsort shifts every value by the minimum so the keys are unsigned, then sorts the keys with a +note stable least-significant-digit-first counting pass per decimal digit; 20 passes cover any ulong +fn rxsort n ulong to void +lo = get vals 0 +for i ulong n +if lt get vals i lo +lo = get vals i +end +end +mx = ulong 0 +for i ulong n +put key i ulong sub get vals i lo +if gt get key i mx +mx = get key i +end +end +e = ulong 1 +for p ulong 20 +for b ulong 10 +put bk b 0 +end +for i ulong n +dg = rem div get key i e 10 +put bk dg add get bk dg 1 +end +s = ulong 0 +for b ulong 10 +c = get bk b +put bk b s +s = add s c +end +for i ulong n +dg = rem div get key i e 10 +put tk get bk dg get key i +put bk dg add get bk dg 1 +end +for i ulong n +put key i get tk i +end +note stop when no key has a digit at 10*e; e > mx/10 means 10*e > mx, and 10*e is never formed past that +if gt e div mx 10 +break +end +e = mul e 10 +end +for i ulong n +put vals i add lo long get key i +end +end +fn main to int +n = parse data +show n +rxsort n +show n +ret 0 +end + +{{out}} +
+170 45 75 -90 -802 24 2 66 0 1999 -1 333
+-802 -90 -1 0 2 24 45 66 75 170 333 1999
+
diff --git a/drafts/rosettacode/recamans-sequence.wiki b/drafts/rosettacode/recamans-sequence.wiki new file mode 100644 index 0000000..cb327a1 --- /dev/null +++ b/drafts/rosettacode/recamans-sequence.wiki @@ -0,0 +1,92 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text first "first 15 terms:" +text dup "first term already seen: a(" +text eqs ") = " +text cov "terms a(0)..a(n) first contain every integer 0..1000 at n = " +note sn v is 1 once v has appeared; the oracle's run shows no term before 0..1000 is covered exceeds 1942300 +global sn uchar 1942301 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a(0) = 0; a(n) = a(n-1) - n if that is positive and new, else a(n-1) + n +note bound: the cover is complete at n = 328002, below the 999999 loop bound +fn main to int +emit first +emit sp +emitu 0 +put sn 0 1 +a = ulong 0 +n = ulong 0 +d = ulong 0 +c = ulong 1 +while lt c 1001 bound 999999 +n = add n 1 +nw = add a n +if gt a n +if eq get sn sub a n 0 +nw = sub a n +end +end +a = nw +if eq get sn a 0 +if le a 1000 +c = add c 1 +end +else +if eq d 0 +d = n +emit nl +emit dup +emitu n +emit eqs +emitu a +emit nl +end +end +put sn a 1 +if lt n 15 +emit sp +emitu a +end +end +emit cov +emitu n +emit nl +ret 0 +end + +{{out}} +
+first 15 terms: 0 1 3 6 2 7 13 20 12 21 11 22 10 23 9
+first term already seen: a(24) = 42
+terms a(0)..a(n) first contain every integer 0..1000 at n = 328002
+
diff --git a/drafts/rosettacode/remove-duplicate-elements.wiki b/drafts/rosettacode/remove-duplicate-elements.wiki new file mode 100644 index 0000000..88992ab --- /dev/null +++ b/drafts/rosettacode/remove-duplicate-elements.wiki @@ -0,0 +1,120 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "4 9 4 1 -3 9 0 1 7 4 -3 12 0 9 5" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note dedup keeps the first copy of each value, in order: an element is kept when no earlier kept element +note equals it; O(n^2) with n at most 16, and it returns the new length +fn dedup n ulong to ulong +k = ulong 0 +for i ulong n +v = get vals i +seen = false +for j ulong k +if eq get vals j v +seen = true +break +end +end +if not seen +put vals k v +k = add k 1 +end +end +ret k +end +fn main to int +n = parse data +show n +k = dedup n +show k +ret 0 +end + +{{out}} +
+4 9 4 1 -3 9 0 1 7 4 -3 12 0 9 5
+4 9 1 -3 0 7 12 5
+
diff --git a/drafts/rosettacode/remove-vowels-from-a-string.wiki b/drafts/rosettacode/remove-vowels-from-a-string.wiki new file mode 100644 index 0000000..da8945b --- /dev/null +++ b/drafts/rosettacode/remove-vowels-from-a-string.wiki @@ -0,0 +1,51 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text msg "Rosetta Code is a programming chrestomathy site, AEIOU and aeiou alike." +text vow "aeiouAEIOU" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +fn isvowel c uchar to bool +for i ulong len vow +if eq c get vow i +ret true +end +end +ret false +end +fn main to int +emit msg +emit nl +for i ulong len msg +c = get msg i +if not isvowel c +emitc c +end +end +emit nl +ret 0 +end + +{{out}} +
+Rosetta Code is a programming chrestomathy site, AEIOU and aeiou alike.
+Rstt Cd s  prgrmmng chrstmthy st,  nd  lk.
+
diff --git a/drafts/rosettacode/repeat-a-string.wiki b/drafts/rosettacode/repeat-a-string.wiki new file mode 100644 index 0000000..754d787 --- /dev/null +++ b/drafts/rosettacode/repeat-a-string.wiki @@ -0,0 +1,43 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text ha "ha" +text star "*" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note repeat writes a, k times, into one buffer (up to 256 bytes, more traps 3) and prints it +fn repeat a view uchar * k ulong to void +b = new uchar 256 0 +p = ulong 0 +for j ulong k +for i ulong len a +put b p get a i +p = add p 1 +end +end +emit part b 0 p +emit nl +end +fn main to int +repeat ha 5 +repeat star 5 +ret 0 +end + +{{out}} +
+hahahahaha
+*****
+
diff --git a/drafts/rosettacode/reverse-words-in-a-string.wiki b/drafts/rosettacode/reverse-words-in-a-string.wiki new file mode 100644 index 0000000..cfe8142 --- /dev/null +++ b/drafts/rosettacode/reverse-words-in-a-string.wiki @@ -0,0 +1,75 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text poem "---------- Ice and Fire ------------\n\nfire, in end will world the say Some\nice. in say Some\ndesire of tasted I've what From\nfire. favor who those with hold I\n\n... elided paragraph last ...\n\nFrost Robert -----------------------\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note revline prints the space-separated words of a in reverse order, one space between them +fn revline a view uchar * to void +st = new ulong 64 0 +ln = new ulong 64 0 +k = ulong 0 +i = ulong 0 +while lt i len a bound 4096 +if eq get a i 32 +i = add i 1 +else +j = i +while lt j len a bound 4096 +if eq get a j 32 +break +end +j = add j 1 +end +put st k i +put ln k sub j i +k = add k 1 +i = j +end +end +for w ulong k +if gt w 0 +emit sp +end +x = sub sub k 1 w +emit part a get st x get ln x +end +emit nl +end +fn main to int +s = ulong 0 +for i ulong len poem +if eq get poem i 10 +revline part poem s sub i s +s = add i 1 +end +end +ret 0 +end + +{{out}} +
+------------ Fire and Ice ----------
+
+Some say the world will end in fire,
+Some say in ice.
+From what I've tasted of desire
+I hold with those who favor fire.
+
+... last paragraph elided ...
+
+----------------------- Robert Frost
+
diff --git a/drafts/rosettacode/run-length-encoding.wiki b/drafts/rosettacode/run-length-encoding.wiki new file mode 100644 index 0000000..719b20b --- /dev/null +++ b/drafts/rosettacode/run-length-encoding.wiki @@ -0,0 +1,93 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text msg "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note putnum writes n in decimal into r at p and returns the offset after it +fn putnum r edit uchar * p ulong n ulong to ulong +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +q = p +for j ulong sub 20 i +put r q get d add i j +q = add q 1 +end +ret q +end +note encode writes count then byte for every run of equal bytes; returns the length +fn encode a view uchar * r edit uchar * to ulong +p = ulong 0 +i = ulong 0 +while lt i len a bound 4096 +c = get a i +j = i +while lt j len a bound 4096 +if ne get a j c +break +end +j = add j 1 +end +p = putnum r p sub j i +put r p c +p = add p 1 +i = j +end +ret p +end +note decode reads a decimal count then one byte, and repeats the byte; returns the length +fn decode a view uchar * r edit uchar * to ulong +p = ulong 0 +v = ulong 0 +for i ulong len a +c = get a i +if and ge c 48 le c 57 +v = add mul v 10 ulong sub c 48 +else +for k ulong v +put r p c +p = add p 1 +end +v = 0 +end +end +ret p +end +fn main to int +e = new uchar 256 0 +d = new uchar 256 0 +n = encode msg e +emit part e 0 n +emit nl +m = decode part e 0 n d +emit part d 0 m +emit nl +ret 0 +end + +{{out}} +
+12W1B12W3B24W1B14W
+WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
+
diff --git a/drafts/rosettacode/selection-sort.wiki b/drafts/rosettacode/selection-sort.wiki new file mode 100644 index 0000000..5c96e38 --- /dev/null +++ b/drafts/rosettacode/selection-sort.wiki @@ -0,0 +1,115 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "29 -3 72 15 0 -88 41 41 6 100 -1" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note ssort puts the smallest remaining element at position i, for i = 0 .. n-2 +fn ssort n ulong to void +for i ulong sub n 1 +m = i +for j ulong sub sub n i 1 +k = add add i j 1 +if lt get vals k get vals m +m = k +end +end +t = get vals i +put vals i get vals m +put vals m t +end +end +fn main to int +n = parse data +show n +ssort n +show n +ret 0 +end + +{{out}} +
+29 -3 72 15 0 -88 41 41 6 100 -1
+-88 -3 -1 0 6 15 29 41 41 72 100
+
diff --git a/drafts/rosettacode/semiprime.wiki b/drafts/rosettacode/semiprime.wiki new file mode 100644 index 0000000..e38f697 --- /dev/null +++ b/drafts/rosettacode/semiprime.wiki @@ -0,0 +1,93 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text lst "semiprimes below 100:" +text cnt "semiprimes below 10000: " +text tst "1679 = 23 * 73 is semiprime: " +text yes "yes\n" +text no "no\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note semiprime: the least factor d of n leaves a prime cofactor n / d +fn semi n ulong to bool +if lt n 4 +ret false +end +d = ulong 2 +while le mul d d n bound 100 +if eq rem n d 0 +m = div n d +e = ulong 2 +while le mul e e m bound 100 +if eq rem m e 0 +ret false +end +e = add e 1 +end +ret true +end +d = add d 1 +end +ret false +end +fn main to int +emit lst +for n ulong 100 +if semi n +emit sp +emitu n +end +end +emit nl +c = ulong 0 +for n ulong 10000 +if semi n +c = add c 1 +end +end +emit cnt +emitu c +emit nl +emit tst +if semi 1679 +emit yes +else +emit no +end +ret 0 +end + +{{out}} +
+semiprimes below 100: 4 6 9 10 14 15 21 22 25 26 33 34 35 38 39 46 49 51 55 57 58 62 65 69 74 77 82 85 86 87 91 93 94 95
+semiprimes below 10000: 2625
+1679 = 23 * 73 is semiprime: yes
+
diff --git a/drafts/rosettacode/sha-1.wiki b/drafts/rosettacode/sha-1.wiki new file mode 100644 index 0000000..4b2c85a --- /dev/null +++ b/drafts/rosettacode/sha-1.wiki @@ -0,0 +1,153 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text hexd "0123456789abcdef" +text m0 "abc" +text m1 "Rosetta Code" +text m2 "The quick brown fox jumps over the lazy dog" +text m3 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +global hs uint 5 +global ww uint 80 +global buf uchar 128 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note emith writes the byte v as two lower-case hex digits +fn emith v uchar to void +emitc get hexd shr v 4 +emitc get hexd and v 15 +end +note rnd is step i of 80 on the state (a, b, c, d, e) in hs, with the expanded schedule in ww +fn rnd i ulong to void +a = get hs 0 +b = get hs 1 +c = get hs 2 +d = get hs 3 +f = uint 0 +k = uint 0 +if lt i 20 +f = or and b c and not b d +k = 1518500249 +else +if lt i 40 +f = xor xor b c d +k = 1859775393 +else +if lt i 60 +f = or or and b c and b d and c d +k = 2400959708 +else +f = xor xor b c d +k = 3395469782 +end +end +end +t = add add add add rotl a uint 5 f get hs 4 k get ww i +put hs 4 d +put hs 3 c +put hs 2 rotl b uint 30 +put hs 1 a +put hs 0 t +end +note block loads the 64 bytes of buf at p as 16 big-endian words, extends them to 80 and runs the steps +fn block p ulong to void +for w ulong 16 +v = uint 0 +for j ulong 4 +v = or shl v 8 uint get buf add add p mul w 4 j +end +put ww w v +end +for w ulong 64 +x = add w 16 +v = xor get ww sub x 3 get ww sub x 8 +v = xor v get ww sub x 14 +v = xor v get ww w +put ww x rotl v uint 1 +end +sv = new uint 5 0 +for k ulong 5 +put sv k get hs k +end +for i ulong 80 +rnd i +end +for k ulong 5 +put hs k add get hs k get sv k +end +end +note digest prints the five state words big-endian in hex +fn digest to void +for k ulong 5 +v = get hs k +for j ulong 4 +s = mul uint sub 3 j 8 +emith uchar and shr v s 255 +end +end +emit nl +end +note sha1 pads a (up to 119 bytes) with 0x80, zeros and the bit length (big-endian) to one or two +note 64-byte blocks, hashes them and prints the digest in hex +fn sha1 a view uchar * to void +put hs 0 uint 1732584193 +put hs 1 uint 4023233417 +put hs 2 uint 2562383102 +put hs 3 uint 271733878 +put hs 4 uint 3285377520 +n = len a +for i ulong 128 +put buf i 0 +end +for i ulong n +put buf i get a i +end +put buf n 128 +t = ulong 64 +if ge n 56 +t = 128 +end +bits = mul n 8 +for j ulong 8 +put buf sub sub t 1 j uchar and shr bits mul j 8 255 +end +for b ulong div t 64 +block mul b 64 +end +digest +end +fn main to int +note the empty message, as a slice of length 0 +sha1 part m0 0 0 +sha1 m0 +sha1 m1 +sha1 m2 +sha1 m3 +ret 0 +end + +{{out}} +
+da39a3ee5e6b4b0d3255bfef95601890afd80709
+a9993e364706816aba3e25717850c26c9cd0d89d
+48c98f7e5a6e736d790ab740dfc3f51a61abe2b5
+2fd4e1c67a2d28fced849ee1bb76e7391b93eb12
+ed6c69d9e8b4373af86303dfaa3528dfbc129902
+
diff --git a/drafts/rosettacode/shell-sort.wiki b/drafts/rosettacode/shell-sort.wiki new file mode 100644 index 0000000..fe226fc --- /dev/null +++ b/drafts/rosettacode/shell-sort.wiki @@ -0,0 +1,121 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "54 26 93 17 77 31 44 55 20 -6 0 13 -21 93 8" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note shsort uses the gaps n/2, n/4, .. 1; each gap is a gapped insertion sort, whose inner walk moves +note down by gap at least once per pass, so 16 passes bound it; the gap halves, so 5 gaps suffice for 16 +fn shsort n ulong to void +g = shr n 1 +while gt g 0 bound 5 +for x ulong sub n g +i = add x g +t = get vals i +j = i +while ge j g bound 16 +if le get vals sub j g t +break +end +put vals j get vals sub j g +j = sub j g +end +put vals j t +end +g = shr g 1 +end +end +fn main to int +n = parse data +show n +shsort n +show n +ret 0 +end + +{{out}} +
+54 26 93 17 77 31 44 55 20 -6 0 13 -21 93 8
+-21 -6 0 8 13 17 20 26 31 44 54 55 77 93 93
+
diff --git a/drafts/rosettacode/smith-numbers.wiki b/drafts/rosettacode/smith-numbers.wiki new file mode 100644 index 0000000..5653c2e --- /dev/null +++ b/drafts/rosettacode/smith-numbers.wiki @@ -0,0 +1,104 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text cnt "Smith numbers below 10000: " +text first "first 20:" +text last "last 10:" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn dsum n ulong to ulong +s = ulong 0 +v = n +while gt v 0 bound 20 +s = add s rem v 10 +v = div v 10 +end +ret s +end +note a Smith number is composite and its digit sum equals the digit sums of its prime factors, with +note multiplicity; returns 0 for primes and 1 +fn smith n ulong to bool +v = n +d = ulong 2 +f = ulong 0 +k = ulong 0 +while le mul d d v bound 200 +if eq rem v d 0 +f = add f dsum d +k = add k 1 +v = div v d +else +d = add d 1 +end +end +if gt v 1 +f = add f dsum v +k = add k 1 +end +if lt k 2 +ret false +end +ret eq f dsum n +end +fn main to int +sm = new ulong 400 0 +c = ulong 0 +for i ulong 9998 +n = add i 2 +if smith n +put sm c n +c = add c 1 +end +end +emit cnt +emitu c +emit nl +emit first +for i ulong 20 +emit sp +emitu get sm i +end +emit nl +emit last +for i ulong 10 +emit sp +emitu get sm add sub c 10 i +end +emit nl +ret 0 +end + +{{out}} +
+Smith numbers below 10000: 376
+first 20: 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 391 438 454 483
+last 10: 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985
+
diff --git a/drafts/rosettacode/sort-an-integer-array.wiki b/drafts/rosettacode/sort-an-integer-array.wiki new file mode 100644 index 0000000..fd93b73 --- /dev/null +++ b/drafts/rosettacode/sort-an-integer-array.wiki @@ -0,0 +1,123 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "2 4 3 1 2 -10 37 0 15 -10 8 100 5" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note Omega has no library sort, so this is a binary insertion sort: binary search finds where vals[i] +note goes among the sorted vals[0..i-1] (after equal values, so it is stable), then the tail shifts up; +note the search range halves each pass, so 16 elements need at most 5 passes (bound 8) +fn bisort n ulong to void +for i ulong n +v = get vals i +lo = ulong 0 +hi = i +while lt lo hi bound 8 +m = add lo shr sub hi lo 1 +if le get vals m v +lo = add m 1 +else +hi = m +end +end +for x ulong sub i lo +k = sub i x +put vals k get vals sub k 1 +end +put vals lo v +end +end +fn main to int +n = parse data +show n +bisort n +show n +ret 0 +end + +{{out}} +
+2 4 3 1 2 -10 37 0 15 -10 8 100 5
+-10 -10 0 1 2 2 3 4 5 8 15 37 100
+
diff --git a/drafts/rosettacode/sort-disjoint-sublist.wiki b/drafts/rosettacode/sort-disjoint-sublist.wiki new file mode 100644 index 0000000..fd3f5b5 --- /dev/null +++ b/drafts/rosettacode/sort-disjoint-sublist.wiki @@ -0,0 +1,143 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "7 6 5 4 3 2 1 0" +text tidx "indices: " +global vals long 8 +global idx ulong 3 +global sub3 long 3 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note sorts the three indices, gathers their values, sorts those, and writes them back in index order; +note both sorts are three compare-exchanges, enough for three elements +fn cx3u to void +for p ulong 3 +i = rem p 2 +if gt get idx i get idx add i 1 +t = get idx i +put idx i get idx add i 1 +put idx add i 1 t +end +end +end +fn cx3l to void +for p ulong 3 +i = rem p 2 +if gt get sub3 i get sub3 add i 1 +t = get sub3 i +put sub3 i get sub3 add i 1 +put sub3 add i 1 t +end +end +end +fn main to int +n = parse data +put idx 0 6 +put idx 1 1 +put idx 2 7 +show n +emit tidx +emitu get idx 0 +emit sp +emitu get idx 1 +emit sp +emitu get idx 2 +emit nl +cx3u +for i ulong 3 +put sub3 i get vals get idx i +end +cx3l +for i ulong 3 +put vals get idx i get sub3 i +end +show n +ret 0 +end + +{{out}} +
+7 6 5 4 3 2 1 0
+indices: 6 1 7
+7 0 5 4 3 2 1 6
+
diff --git a/drafts/rosettacode/sort-three-variables.wiki b/drafts/rosettacode/sort-three-variables.wiki new file mode 100644 index 0000000..6ee1c11 --- /dev/null +++ b/drafts/rosettacode/sort-three-variables.wiki @@ -0,0 +1,190 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tx "x = " +text ty "y = " +text tz "z = " +text s0 "lions, tigers, and" +text s1 "bears, oh my!" +text s2 "(from the \"Wizard of OZ\")" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note sort3 orders three longs with three compare-exchanges and prints them as x, y, z +fn sort3 x long y long z long to void +if gt x y +t = x +x = y +y = t +end +if gt y z +t = y +y = z +z = t +end +if gt x y +t = x +x = y +y = t +end +emit tx +emiti x +emit nl +emit ty +emiti y +emit nl +emit tz +emiti z +emit nl +end +note str writes text number k (0, 1 or 2); a text cannot be chosen by index, so this is an if chain +fn str k ulong to void +if eq k 0 +emit s0 +end +if eq k 1 +emit s1 +end +if eq k 2 +emit s2 +end +end +note cmpb compares two byte strings: negative, zero or positive, like C's strcmp on unsigned bytes +fn cmpb a view uchar * b view uchar * to long +for i ulong len a +if ge i len b +ret 1 +end +if ne get a i get b i +ret sub long get a i long get b i +end +end +if lt len a len b +ret -1 +end +ret 0 +end +note gtk says whether text j sorts after text k; the three pairs are spelled out for the same reason +fn gtk j ulong k ulong to bool +c = long 0 +if and eq j 0 eq k 1 +c = cmpb s0 s1 +end +if and eq j 1 eq k 0 +c = cmpb s1 s0 +end +if and eq j 0 eq k 2 +c = cmpb s0 s2 +end +if and eq j 2 eq k 0 +c = cmpb s2 s0 +end +if and eq j 1 eq k 2 +c = cmpb s1 s2 +end +if and eq j 2 eq k 1 +c = cmpb s2 s1 +end +ret gt c 0 +end +fn showk x ulong y ulong z ulong to void +emit tx +str x +emit nl +emit ty +str y +emit nl +emit tz +str z +emit nl +end +note sorts the strings the same way, by swapping which text each variable names +fn sorts to void +x = ulong 0 +y = ulong 1 +z = ulong 2 +showk x y z +if gtk x y +t = x +x = y +y = t +end +if gtk y z +t = y +y = z +z = t +end +if gtk x y +t = x +x = y +y = t +end +showk x y z +end +fn main to int +emit tx +emiti 77444 +emit nl +emit ty +emiti -12 +emit nl +emit tz +emiti 0 +emit nl +sort3 77444 -12 0 +sorts +ret 0 +end + +{{out}} +
+x = 77444
+y = -12
+z = 0
+x = -12
+y = 0
+z = 77444
+x = lions, tigers, and
+y = bears, oh my!
+z = (from the "Wizard of OZ")
+x = (from the "Wizard of OZ")
+y = bears, oh my!
+z = lions, tigers, and
+
diff --git a/drafts/rosettacode/soundex.wiki b/drafts/rosettacode/soundex.wiki new file mode 100644 index 0000000..28d8b8c --- /dev/null +++ b/drafts/rosettacode/soundex.wiki @@ -0,0 +1,97 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +note code of each letter a..z: 0 vowel (and y), 1..6 consonant groups, 7 h and w (ignored) +text codes "01230127022455012623017202" +text n0 "Robert" +text n1 "Rupert" +text n2 "Rubin" +text n3 "Ashcraft" +text n4 "Ashcroft" +text n5 "Tymczak" +text n6 "Pfister" +text n7 "Honeyman" +text n8 "Lee" +text n9 "Gutierrez" +text n10 "Jackson" +text n11 "VanDeusen" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note American Soundex: keep the first letter, code the rest, drop repeats of the same code (h and w +note do not separate them, vowels do), pad with zeros to four characters +fn soundex a view uchar * to void +o = new uchar 4 48 +c = get a 0 +if ge c 97 +c = sub c 32 +end +put o 0 c +prev = get codes sub c 65 +k = ulong 1 +for i ulong sub len a 1 +x = get a add i 1 +if ge x 97 +x = sub x 32 +end +d = get codes sub x 65 +if eq d 48 +prev = d +else +if and ne d 55 ne d prev +if lt k 4 +put o k d +k = add k 1 +end +prev = d +end +end +end +emit a +emit sp +emit o +emit nl +end +fn main to int +soundex n0 +soundex n1 +soundex n2 +soundex n3 +soundex n4 +soundex n5 +soundex n6 +soundex n7 +soundex n8 +soundex n9 +soundex n10 +soundex n11 +ret 0 +end + +{{out}} +
+Robert R163
+Rupert R163
+Rubin R150
+Ashcraft A261
+Ashcroft A261
+Tymczak T522
+Pfister P236
+Honeyman H555
+Lee L000
+Gutierrez G362
+Jackson J250
+VanDeusen V532
+
diff --git a/drafts/rosettacode/spiral-matrix.wiki b/drafts/rosettacode/spiral-matrix.wiki new file mode 100644 index 0000000..835b58a --- /dev/null +++ b/drafts/rosettacode/spiral-matrix.wiki @@ -0,0 +1,119 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +global grid ulong 64 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt w sub 20 i +i = sub 20 w +end +emit part d i sub 20 i +end +note the walk starts at the top left going right and turns clockwise whenever the next cell is off the +note grid or already numbered; grid holds number+1 so that 0 means empty +fn spiral n ulong to void +for i ulong mul n n +put grid i 0 +end +r = long 0 +c = long 0 +dr = long 0 +dc = long 1 +for k ulong mul n n +put grid add mul ulong r n ulong c add k 1 +nr = add r dr +nc = add c dc +if blocked nr nc n +t = dr +dr = dc +dc = neg t +nr = add r dr +nc = add c dc +end +r = nr +c = nc +end +end +note blocked says whether (r, c) is outside the n x n grid or already numbered +fn blocked r long c long n ulong to bool +if or or lt r 0 ge r long n or lt c 0 ge c long n +ret true +end +ret ne get grid add mul ulong r n ulong c 0 +end +fn show n ulong to void +for i ulong n +for j ulong n +emitw sub get grid add mul i n j 1 3 +end +emit nl +end +end +fn main to int +spiral 5 +show 5 +emit nl +spiral 8 +show 8 +ret 0 +end + +{{out}} +
+  0  1  2  3  4
+ 15 16 17 18  5
+ 14 23 24 19  6
+ 13 22 21 20  7
+ 12 11 10  9  8
+
+  0  1  2  3  4  5  6  7
+ 27 28 29 30 31 32 33  8
+ 26 47 48 49 50 51 34  9
+ 25 46 59 60 61 52 35 10
+ 24 45 58 63 62 53 36 11
+ 23 44 57 56 55 54 37 12
+ 22 43 42 41 40 39 38 13
+ 21 20 19 18 17 16 15 14
+
diff --git a/drafts/rosettacode/square-free-integers.wiki b/drafts/rosettacode/square-free-integers.wiki new file mode 100644 index 0000000..8037a5e --- /dev/null +++ b/drafts/rosettacode/square-free-integers.wiki @@ -0,0 +1,118 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text lo "square-free integers from 1 to 145:" +text hi "square-free integers from 1000000000000 to 1000000000145:" +text upto "square-free integers up to " +text col ": " +note cp marks composites below 10^6 (a prime sieve); sq n becomes 1 when some p^2 divides n, n <= 10^6; +note wn marks the window 10^12 .. 10^12 + 145 the same way. Any square factor of a number up to +note 10^12 + 145 has a prime p <= 10^6 + 1 (p^2 > 10^12 + 145 otherwise), and 10^6 + 1 = 101 * 9901 is composite +global cp uchar 1000001 +global sq uchar 1000001 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn main to int +wn = new uchar 146 0 +base = ulong 1000000000000 +p = ulong 2 +while le p 1000000 bound 999999 +if eq get cp p 0 +if le mul p p 1000000 +j = mul p p +while le j 1000000 bound 999999 +put cp j 1 +j = add j p +end +end +q = mul p p +j = q +while le j 1000000 bound 999999 +put sq j 1 +j = add j q +end +r = rem base q +j = ulong 0 +if gt r 0 +j = sub q r +end +while le j 145 bound 146 +put wn j 1 +j = add j q +end +end +p = add p 1 +end +emit lo +for i ulong 145 +if eq get sq add i 1 0 +emit sp +emitu add i 1 +end +end +emit nl +emit hi +for i ulong 146 +if eq get wn i 0 +emit sp +emitu add base i +end +end +emit nl +c = ulong 0 +lim = ulong 100 +for i ulong 1000000 +n = add i 1 +if eq get sq n 0 +c = add c 1 +end +if eq n lim +emit upto +emitu lim +emit col +emitu c +emit nl +lim = mul lim 10 +end +end +ret 0 +end + +{{out}} +
+square-free integers from 1 to 145: 1 2 3 5 6 7 10 11 13 14 15 17 19 21 22 23 26 29 30 31 33 34 35 37 38 39 41 42 43 46 47 51 53 55 57 58 59 61 62 65 66 67 69 70 71 73 74 77 78 79 82 83 85 86 87 89 91 93 94 95 97 101 102 103 105 106 107 109 110 111 113 114 115 118 119 122 123 127 129 130 131 133 134 137 138 139 141 142 143 145
+square-free integers from 1000000000000 to 1000000000145: 1000000000001 1000000000002 1000000000003 1000000000005 1000000000006 1000000000007 1000000000009 1000000000011 1000000000013 1000000000014 1000000000015 1000000000018 1000000000019 1000000000021 1000000000022 1000000000023 1000000000027 1000000000029 1000000000030 1000000000031 1000000000033 1000000000037 1000000000038 1000000000039 1000000000041 1000000000042 1000000000043 1000000000045 1000000000046 1000000000047 1000000000049 1000000000051 1000000000054 1000000000055 1000000000057 1000000000058 1000000000059 1000000000061 1000000000063 1000000000065 1000000000066 1000000000067 1000000000069 1000000000070 1000000000073 1000000000074 1000000000077 1000000000078 1000000000079 1000000000081 1000000000082 1000000000085 1000000000086 1000000000087 1000000000090 1000000000091 1000000000093 1000000000094 1000000000095 1000000000097 1000000000099 1000000000101 1000000000102 1000000000103 1000000000105 1000000000106 1000000000109 1000000000111 1000000000113 1000000000114 1000000000115 1000000000117 1000000000118 1000000000119 1000000000121 1000000000122 1000000000123 1000000000126 1000000000127 1000000000129 1000000000130 1000000000133 1000000000135 1000000000137 1000000000138 1000000000139 1000000000141 1000000000142 1000000000145
+square-free integers up to 100: 61
+square-free integers up to 1000: 608
+square-free integers up to 10000: 6083
+square-free integers up to 100000: 60794
+square-free integers up to 1000000: 607926
+
diff --git a/drafts/rosettacode/stack.wiki b/drafts/rosettacode/stack.wiki new file mode 100644 index 0000000..b7bbf78 --- /dev/null +++ b/drafts/rosettacode/stack.wiki @@ -0,0 +1,126 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tpush "push " +text tpop "pop " +text tpeek "peek " +text tempty "empty: " +text tsize "size " +text tyes "yes" +text tno "no" +global stk long 16 +global top ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note a fixed-capacity stack of 16 longs: stk[0..top-1], the top element last; push on a full stack and +note pop on an empty one stop the program (exit 2), since there is no heap to grow into +fn push v long to void +t = get top 0 +if eq t 16 +exit 2 +end +put stk t v +put top 0 add t 1 +emit tpush +emiti v +emit nl +end +fn pop to long +t = get top 0 +if eq t 0 +exit 2 +end +put top 0 sub t 1 +v = get stk sub t 1 +emit tpop +emiti v +emit nl +ret v +end +fn peek to long +v = get stk sub get top 0 1 +emit tpeek +emiti v +emit nl +ret v +end +fn empty to bool +e = eq get top 0 0 +emit tempty +if e +emit tyes +else +emit tno +end +emit nl +ret e +end +fn main to int +e = empty +push 10 +push -20 +push 30 +v = peek +emit tsize +emitu get top 0 +emit nl +v = pop +v = pop +e = empty +v = pop +e = empty +ret 0 +end + +{{out}} +
+empty: yes
+push 10
+push -20
+push 30
+peek 30
+size 3
+pop 30
+pop -20
+empty: no
+pop 10
+empty: yes
+
diff --git a/drafts/rosettacode/stern-brocot-sequence.wiki b/drafts/rosettacode/stern-brocot-sequence.wiki new file mode 100644 index 0000000..3b1f110 --- /dev/null +++ b/drafts/rosettacode/stern-brocot-sequence.wiki @@ -0,0 +1,124 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tfirst "first 15: " +text tat " first appears at " +text tgcd "gcd of each consecutive pair of the first 1000 is 1: " +text tyes "yes" +text tno "no" +global sb ulong 1200 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note start 1, 1; for each k from 1 on, append sb[k] + sb[k-1] and then sb[k], until 1200 members +fn build to void +put sb 0 1 +put sb 1 1 +n = ulong 2 +k = ulong 1 +note each pass adds two members, so 600 passes fill 1200 +while lt n 1200 bound 600 +put sb n add get sb k get sb sub k 1 +put sb add n 1 get sb k +n = add n 2 +k = add k 1 +end +end +note find gives the 1-based position of v's first appearance, 0 if not within the 1200 +fn find v ulong to ulong +for i ulong 1200 +if eq get sb i v +ret add i 1 +end +end +ret 0 +end +fn gcd a ulong b ulong to ulong +note Euclid's remainders shrink at least as fast as Fibonacci numbers, so 100 steps cover any ulong +while ne b 0 bound 100 +t = rem a b +a = b +b = t +end +ret a +end +fn show v ulong to void +emitu v +emit tat +emitu find v +emit nl +end +fn main to int +build +emit tfirst +for i ulong 15 +if gt i 0 +emit sp +end +emitu get sb i +end +emit nl +for i ulong 10 +show add i 1 +end +show 100 +ok = true +for i ulong 999 +if ne gcd get sb i get sb add i 1 1 +ok = false +end +end +emit tgcd +if ok +emit tyes +else +emit tno +end +emit nl +ret 0 +end + +{{out}} +
+first 15: 1 1 2 1 3 2 3 1 4 3 5 2 5 3 4
+1 first appears at 1
+2 first appears at 3
+3 first appears at 5
+4 first appears at 9
+5 first appears at 11
+6 first appears at 33
+7 first appears at 19
+8 first appears at 21
+9 first appears at 35
+10 first appears at 39
+100 first appears at 1179
+gcd of each consecutive pair of the first 1000 is 1: yes
+
diff --git a/drafts/rosettacode/stooge-sort.wiki b/drafts/rosettacode/stooge-sort.wiki new file mode 100644 index 0000000..3060de5 --- /dev/null +++ b/drafts/rosettacode/stooge-sort.wiki @@ -0,0 +1,115 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "1 4 5 3 -6 3 7 10 -2 -5 7 5 9 -3 7" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note stsort orders vals[i] and vals[j], then for 3 or more elements sorts the first two thirds, the last +note two thirds and the first two thirds again; each call works on a range of at most 2/3 the size, so it ends +fn stsort i ulong j ulong to void +if gt get vals i get vals j +t = get vals i +put vals i get vals j +put vals j t +end +if ge sub j i 2 +k = div add sub j i 1 3 +stsort i sub j k +stsort add i k j +stsort i sub j k +end +end +fn main to int +n = parse data +show n +stsort 0 sub n 1 +show n +ret 0 +end + +{{out}} +
+1 4 5 3 -6 3 7 10 -2 -5 7 5 9 -3 7
+-6 -5 -3 -2 1 3 3 4 5 5 7 7 7 9 10
+
diff --git a/drafts/rosettacode/strand-sort.wiki b/drafts/rosettacode/strand-sort.wiki new file mode 100644 index 0000000..3fa3d9f --- /dev/null +++ b/drafts/rosettacode/strand-sort.wiki @@ -0,0 +1,175 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "5 1 4 2 0 9 6 3 8 7 -2 11" +global vals long 16 +global rest long 16 +global strand long 16 +global res long 16 +global mrg long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note pull takes an increasing strand out of rest[0..m-1] into strand, keeps the others in order at the +note front of rest, and returns the strand length +fn pull m ulong to ulong +s = ulong 0 +k = ulong 0 +for i ulong m +v = get rest i +take = false +if eq s 0 +take = true +else +if ge v get strand sub s 1 +take = true +end +end +if take +put strand s v +s = add s 1 +else +put rest k v +k = add k 1 +end +end +ret s +end +note mergein merges strand[0..s-1] into res[0..r-1] through mrg and returns the new length +fn mergein r ulong s ulong to ulong +i = ulong 0 +j = ulong 0 +k = ulong 0 +while lt k add r s bound 32 +left = false +if ge j s +left = true +else +if lt i r +if le get res i get strand j +left = true +end +end +end +if left +put mrg k get res i +i = add i 1 +else +put mrg k get strand j +j = add j 1 +end +k = add k 1 +end +for x ulong k +put res x get mrg x +end +ret k +end +note stsort repeats pull and merge; each strand removes at least one element, so n rounds suffice +fn stsort n ulong to void +for i ulong n +put rest i get vals i +end +m = n +r = ulong 0 +while gt m 0 bound 16 +s = pull m +m = sub m s +r = mergein r s +end +for i ulong n +put vals i get res i +end +end +fn main to int +n = parse data +show n +stsort n +show n +ret 0 +end + +{{out}} +
+5 1 4 2 0 9 6 3 8 7 -2 11
+-2 0 1 2 3 4 5 6 7 8 9 11
+
diff --git a/drafts/rosettacode/string-append.wiki b/drafts/rosettacode/string-append.wiki new file mode 100644 index 0000000..4ea60a5 --- /dev/null +++ b/drafts/rosettacode/string-append.wiki @@ -0,0 +1,40 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text s0 "Hello," +text more " World!" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note copyto copies s into d at offset off and returns the offset after it (past the end traps 3) +fn copyto d edit uchar * off ulong s view uchar * to ulong +for i ulong len s +put d add off i get s i +end +ret add off len s +end +fn main to int +note the string lives in a 64-byte buffer b with length n; appending copies after byte n +b = new uchar 64 0 +n = copyto b 0 s0 +n = copyto b n more +emit part b 0 n +emit nl +ret 0 +end + +{{out}} +
+Hello, World!
+
diff --git a/drafts/rosettacode/string-case.wiki b/drafts/rosettacode/string-case.wiki new file mode 100644 index 0000000..9b04465 --- /dev/null +++ b/drafts/rosettacode/string-case.wiki @@ -0,0 +1,51 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text s "alphaBETA" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note upper, lower and swap change ASCII letters only; strings up to 256 bytes (longer traps 3) +fn conv a view uchar * mode ulong to void +r = new uchar 256 0 +for i ulong len a +c = get a i +up = and ge c 65 le c 90 +lo = and ge c 97 le c 122 +if and lo ne mode 1 +c = sub c 32 +else +if and up ne mode 0 +c = add c 32 +end +end +put r i c +end +emit part r 0 len a +emit nl +end +fn main to int +note mode 0 upper, 1 lower, 2 swap case +conv s 0 +conv s 1 +conv s 2 +ret 0 +end + +{{out}} +
+ALPHABETA
+alphabeta
+ALPHAbeta
+
diff --git a/drafts/rosettacode/string-comparison.wiki b/drafts/rosettacode/string-comparison.wiki new file mode 100644 index 0000000..3188484 --- /dev/null +++ b/drafts/rosettacode/string-comparison.wiki @@ -0,0 +1,97 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text a1 "alpha" +text b1 "beta" +text a2 "Hello" +text b2 "hello" +text a3 "abc" +text b3 "abcd" +text teq " eq=" +text tlt " lt=" +text tgt " gt=" +text tci " equal-ignoring-case=" +text tyes "yes" +text tno "no" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +fn emitb b bool to void +if b +emit tyes +else +emit tno +end +end +note cmp is byte order: -1, 0 or 1; a proper prefix sorts first; ci folds ASCII case first +fn cmp a view uchar * b view uchar * ci bool to int +n = len a +if lt len b n +n = len b +end +for i ulong n +x = get a i +y = get b i +if and ci and ge x 65 le x 90 +x = add x 32 +end +if and ci and ge y 65 le y 90 +y = add y 32 +end +if lt x y +ret -1 +end +if gt x y +ret 1 +end +end +if lt len a len b +ret -1 +end +if gt len a len b +ret 1 +end +ret 0 +end +fn show a view uchar * b view uchar * to void +c = cmp a b false +emit a +emit sp +emit b +emit teq +emitb eq c 0 +emit tlt +emitb lt c 0 +emit tgt +emitb gt c 0 +emit tci +emitb eq cmp a b true 0 +emit nl +end +fn main to int +show a1 b1 +show a2 b2 +show a3 b3 +show b3 a3 +ret 0 +end + +{{out}} +
+alpha beta eq=no lt=yes gt=no equal-ignoring-case=no
+Hello hello eq=no lt=yes gt=no equal-ignoring-case=yes
+abc abcd eq=no lt=yes gt=no equal-ignoring-case=no
+abcd abc eq=no lt=no gt=yes equal-ignoring-case=no
+
diff --git a/drafts/rosettacode/string-concatenation.wiki b/drafts/rosettacode/string-concatenation.wiki new file mode 100644 index 0000000..e070e64 --- /dev/null +++ b/drafts/rosettacode/string-concatenation.wiki @@ -0,0 +1,42 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text str "hello" +text lit " literal" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note copyto copies s into d at offset off and returns the offset after it (past the end traps 3) +fn copyto d edit uchar * off ulong s view uchar * to ulong +for i ulong len s +put d add off i get s i +end +ret add off len s +end +fn main to int +emit str +emit nl +b = new uchar 64 0 +p = copyto b 0 str +p = copyto b p lit +emit part b 0 p +emit nl +ret 0 +end + +{{out}} +
+hello
+hello literal
+
diff --git a/drafts/rosettacode/string-length.wiki b/drafts/rosettacode/string-length.wiki new file mode 100644 index 0000000..cb7b645 --- /dev/null +++ b/drafts/rosettacode/string-length.wiki @@ -0,0 +1,68 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text tb " bytes " +text tc " characters\n" +text s1 "møøse" +text s2 "𝔘𝔫𝔦𝔠𝔬𝔡𝔢" +text s3 "J̲o̲s̲é̲" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a UTF-8 code point starts at every byte that is not a continuation byte 10xxxxxx +fn show a view uchar * to void +n = ulong 0 +for i ulong len a +if ne and get a i 192 128 +n = add n 1 +end +end +emit a +emit nl +emitu len a +emit tb +emitu n +emit tc +end +fn main to int +show s1 +show s2 +show s3 +ret 0 +end + +{{out}} +
+møøse
+7 bytes 5 characters
+𝔘𝔫𝔦𝔠𝔬𝔡𝔢
+28 bytes 7 characters
+J̲o̲s̲é̲
+13 bytes 8 characters
+
diff --git a/drafts/rosettacode/string-matching.wiki b/drafts/rosettacode/string-matching.wiki new file mode 100644 index 0000000..829af4e --- /dev/null +++ b/drafts/rosettacode/string-matching.wiki @@ -0,0 +1,83 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text hay "tacoloco" +text nd "co" +text tst "starts with: " +text tct "found at:" +text ten "ends with: " +text tyes "yes" +text tno "no" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn emitb b bool to void +if b +emit tyes +else +emit tno +end +end +note at tells whether t occurs in s at offset i (false when it would run past the end) +fn at2 s view uchar * t view uchar * i ulong to bool +if gt add i len t len s +ret false +end +for j ulong len t +if ne get s add i j get t j +ret false +end +end +ret true +end +fn main to int +emit tst +emitb at2 hay nd 0 +emit nl +emit tct +for i ulong len hay +if at2 hay nd i +emit sp +emitu i +end +end +emit nl +emit ten +emitb at2 hay nd sub len hay len nd +emit nl +ret 0 +end + +{{out}} +
+starts with: no
+found at: 2 6
+ends with: yes
+
diff --git a/drafts/rosettacode/string-prepend.wiki b/drafts/rosettacode/string-prepend.wiki new file mode 100644 index 0000000..61e5bd7 --- /dev/null +++ b/drafts/rosettacode/string-prepend.wiki @@ -0,0 +1,49 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text s0 "World" +text pre "Hello, " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note copyto copies s into d at offset off and returns the offset after it (past the end traps 3) +fn copyto d edit uchar * off ulong s view uchar * to ulong +for i ulong len s +put d add off i get s i +end +ret add off len s +end +note prepend moves the n bytes of b right by len p (last byte first), then copies p in front +fn prepend b edit uchar * n ulong p view uchar * to ulong +k = len p +for i ulong n +j = sub sub n 1 i +put b add j k get b j +end +r = copyto b 0 p +ret add n k +end +fn main to int +b = new uchar 64 0 +n = copyto b 0 s0 +n = prepend b n pre +emit part b 0 n +emit nl +ret 0 +end + +{{out}} +
+Hello, World
+
diff --git a/drafts/rosettacode/strip-a-set-of-characters-from-a-string.wiki b/drafts/rosettacode/strip-a-set-of-characters-from-a-string.wiki new file mode 100644 index 0000000..1882257 --- /dev/null +++ b/drafts/rosettacode/strip-a-set-of-characters-from-a-string.wiki @@ -0,0 +1,50 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text msg "She was a soul stripper. She took my heart!" +text set1 "aei" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note stripchars writes the bytes of a that are not in set s +fn strip a view uchar * s view uchar * to void +for i ulong len a +c = get a i +k = true +for j ulong len s +if eq c get s j +k = false +end +end +if k +emitc c +end +end +emit nl +end +fn main to int +strip msg set1 +ret 0 +end + +{{out}} +
+Sh ws  soul strppr. Sh took my hrt!
+
diff --git a/drafts/rosettacode/strip-comments-from-a-string.wiki b/drafts/rosettacode/strip-comments-from-a-string.wiki new file mode 100644 index 0000000..2411319 --- /dev/null +++ b/drafts/rosettacode/strip-comments-from-a-string.wiki @@ -0,0 +1,64 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text lq "<<<" +text rq ">>>" +text s1 "apples, pears # and bananas" +text s2 "apples, pears ; and bananas" +text s3 " no comment here " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note cut at the first '#' or ';', then drop whitespace (space, tab) at both ends of what is left +fn strip a view uchar * to void +e = len a +for i ulong len a +c = get a i +if and eq e len a or eq c 35 eq c 59 +e = i +end +end +s = ulong 0 +while lt s e bound 4096 +c = get a s +if and ne c 32 ne c 9 +break +end +s = add s 1 +end +while gt e s bound 4096 +c = get a sub e 1 +if and ne c 32 ne c 9 +break +end +e = sub e 1 +end +emit lq +emit part a s sub e s +emit rq +emit nl +end +fn main to int +strip s1 +strip s2 +strip s3 +ret 0 +end + +{{out}} +
+<<<apples, pears>>>
+<<<apples, pears>>>
+<<<no comment here>>>
+
diff --git a/drafts/rosettacode/strip-whitespace-from-a-string.wiki b/drafts/rosettacode/strip-whitespace-from-a-string.wiki new file mode 100644 index 0000000..67adf86 --- /dev/null +++ b/drafts/rosettacode/strip-whitespace-from-a-string.wiki @@ -0,0 +1,65 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text lb "[" +text rb "]" +text msg " \t hello world \t\n " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note white: space, tab, newline, vertical tab, form feed, carriage return +fn white c uchar to bool +ret or eq c 32 and ge c 9 le c 13 +end +fn lead a view uchar * to ulong +for i ulong len a +if not white get a i +ret i +end +end +ret len a +end +fn trail a view uchar * to ulong +for i ulong len a +if not white get a sub sub len a 1 i +ret sub len a i +end +end +ret 0 +end +fn show a view uchar * to void +emit lb +emit a +emit rb +emit nl +end +fn main to int +l = lead msg +t = trail msg +show msg +show part msg l sub len msg l +show part msg 0 t +show part msg l sub t l +ret 0 +end + +{{out}} +
+[  	  hello world 	
+ ]
+[hello world 	
+ ]
+[  	  hello world]
+[hello world]
+
diff --git a/drafts/rosettacode/substitution-cipher.wiki b/drafts/rosettacode/substitution-cipher.wiki new file mode 100644 index 0000000..dfbe860 --- /dev/null +++ b/drafts/rosettacode/substitution-cipher.wiki @@ -0,0 +1,65 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +note key: plain letter a..z maps to the letter at the same place here +text key "phqgiumeaylnofdxjkrcvstzwb" +text msg "the quick brown fox jumps over the lazy dog, 42 times" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note enc maps each lower-case letter through key; other bytes are copied +fn enc a view uchar * r edit uchar * to void +for i ulong len a +c = get a i +if and ge c 97 le c 122 +c = get key sub c 97 +end +put r i c +end +end +note dec builds the inverse table first, then maps through it +fn dec a view uchar * r edit uchar * to void +inv = new uchar 26 0 +for j ulong 26 +put inv sub get key j 97 add uchar j 97 +end +for i ulong len a +c = get a i +if and ge c 97 le c 122 +c = get inv sub c 97 +end +put r i c +end +end +fn main to int +n = len msg +e = new uchar 256 0 +d = new uchar 256 0 +emit msg +emit nl +enc msg part e 0 n +emit part e 0 n +emit nl +dec part e 0 n part d 0 n +emit part d 0 n +emit nl +ret 0 +end + +{{out}} +
+the quick brown fox jumps over the lazy dog, 42 times
+cei jvaql hkdtf udz yvoxr dsik cei npbw gdm, 42 caoir
+the quick brown fox jumps over the lazy dog, 42 times
+
diff --git a/drafts/rosettacode/substring.wiki b/drafts/rosettacode/substring.wiki new file mode 100644 index 0000000..17800d3 --- /dev/null +++ b/drafts/rosettacode/substring.wiki @@ -0,0 +1,59 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text str "abcdefgh" +text cd "cd" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note find returns the first index where t occurs in s; a missing t traps 9 +fn find s view uchar * t view uchar * to ulong +for i ulong add sub len s len t 1 +m = true +for j ulong len t +if ne get s add i j get t j +m = false +end +end +if m +ret i +end +end +trap 9 +end +fn line a view uchar * to void +emit a +emit nl +end +fn main to int +n = ulong 2 +m = ulong 3 +note n characters in, m long; from n to the end; all but the last; from d, m long; from cd, m long +line part str n m +line part str n sub len str n +line part str 0 sub len str 1 +c = new uchar 1 100 +line part str find str c m +line part str find str cd m +ret 0 +end + +{{out}} +
+cde
+cdefgh
+abcdefg
+def
+cde
+
diff --git a/drafts/rosettacode/sudoku.wiki b/drafts/rosettacode/sudoku.wiki new file mode 100644 index 0000000..49a4f5f --- /dev/null +++ b/drafts/rosettacode/sudoku.wiki @@ -0,0 +1,139 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text puzzle "850002400720000009004000000000107002305000900040000000000080070017000000000036040" +text tbar "------+-------+------\n" +text tsep "| " +text tsolved "solved:\n" +global grid ulong 81 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note ok says whether digit d may go in cell c: not already in its row, column or 3x3 box +fn ok c ulong d ulong to bool +r = div c 9 +k = rem c 9 +b = add mul mul div r 3 3 9 mul div k 3 3 +for i ulong 9 +if eq get grid add mul r 9 i d +ret false +end +if eq get grid add mul i 9 k d +ret false +end +q = add b add mul div i 3 9 rem i 3 +if eq get grid q d +ret false +end +end +ret true +end +note solve fills the first empty cell at or after c with each digit that fits and recurses; on a dead +note end it empties the cell again (backtracking); the recursion is at most 81 deep +fn solve c ulong to bool +p = c +while lt p 81 bound 81 +if eq get grid p 0 +break +end +p = add p 1 +end +if eq p 81 +ret true +end +for x ulong 9 +d = add x 1 +if ok p d +put grid p d +if solve add p 1 +ret true +end +end +end +put grid p 0 +ret false +end +fn show to void +for r ulong 9 +if and gt r 0 eq rem r 3 0 +emit tbar +end +for k ulong 9 +if and gt k 0 eq rem k 3 0 +emit tsep +end +emitu get grid add mul r 9 k +if lt k 8 +emit sp +end +end +emit nl +end +end +fn main to int +for i ulong 81 +put grid i sub ulong get puzzle i 48 +end +show +if not solve 0 +exit 1 +end +emit tsolved +show +ret 0 +end + +{{out}} +
+8 5 0 | 0 0 2 | 4 0 0
+7 2 0 | 0 0 0 | 0 0 9
+0 0 4 | 0 0 0 | 0 0 0
+------+-------+------
+0 0 0 | 1 0 7 | 0 0 2
+3 0 5 | 0 0 0 | 9 0 0
+0 4 0 | 0 0 0 | 0 0 0
+------+-------+------
+0 0 0 | 0 8 0 | 0 7 0
+0 1 7 | 0 0 0 | 0 0 0
+0 0 0 | 0 3 6 | 0 4 0
+solved:
+8 5 9 | 6 1 2 | 4 3 7
+7 2 3 | 8 5 4 | 1 6 9
+1 6 4 | 3 7 9 | 5 2 8
+------+-------+------
+9 8 6 | 1 4 7 | 3 5 2
+3 7 5 | 2 6 8 | 9 1 4
+2 4 1 | 5 9 3 | 7 8 6
+------+-------+------
+4 3 2 | 9 8 1 | 6 7 5
+6 1 7 | 4 2 5 | 8 9 3
+5 9 8 | 7 3 6 | 2 4 1
+
diff --git a/drafts/rosettacode/sum-and-product-of-an-array.wiki b/drafts/rosettacode/sum-and-product-of-an-array.wiki new file mode 100644 index 0000000..db53825 --- /dev/null +++ b/drafts/rosettacode/sum-and-product-of-an-array.wiki @@ -0,0 +1,59 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text tsum "sum " +text tprod "product " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn main to int +a = new ulong 10 0 +for i ulong 10 +put a i add i 1 +end +s = ulong 0 +p = ulong 1 +for i ulong 10 +s = add s get a i +p = mul p get a i +end +emit tsum +emitu s +emit nl +emit tprod +emitu p +emit nl +ret 0 +end + +{{out}} +
+sum 55
+product 3628800
+
diff --git a/drafts/rosettacode/sum-digits-of-an-integer.wiki b/drafts/rosettacode/sum-digits-of-an-integer.wiki new file mode 100644 index 0000000..2824540 --- /dev/null +++ b/drafts/rosettacode/sum-digits-of-an-integer.wiki @@ -0,0 +1,95 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text dig "0123456789abcdefghijklmnopqrstuvwxyz" +text bs " (base " +text eqs "): digit sum " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitc writes the single byte c +fn emitc c uchar to void +b = new uchar 1 c +emit b +end +note emitb writes n in base b (2..36); a ulong has at most 64 digits in base 2 +fn emitb n ulong b ulong to void +buf = new uchar 64 0 +k = ulong 0 +v = n +while true bound 64 +put buf k get dig rem v b +k = add k 1 +v = div v b +if eq v 0 +break +end +end +for i ulong k +emitc get buf sub sub k 1 i +end +end +fn dsum n ulong b ulong to ulong +s = ulong 0 +v = n +while gt v 0 bound 64 +s = add s rem v b +v = div v b +end +ret s +end +fn tell n ulong b ulong to void +emitb n b +emit bs +emitu b +emit eqs +emitu dsum n b +emit nl +end +fn main to int +tell 1 10 +tell 1234 10 +tell 0xfe 16 +tell 0xf0e 16 +tell 255 8 +tell 18446744073709551615 2 +tell 18446744073709551615 36 +ret 0 +end + +{{out}} +
+1 (base 10): digit sum 1
+1234 (base 10): digit sum 10
+fe (base 16): digit sum 29
+f0e (base 16): digit sum 29
+377 (base 8): digit sum 17
+1111111111111111111111111111111111111111111111111111111111111111 (base 2): digit sum 64
+3w5e11264sgsf (base 36): digit sum 155
+
diff --git a/drafts/rosettacode/sum-multiples-of-3-and-5.wiki b/drafts/rosettacode/sum-multiples-of-3-and-5.wiki new file mode 100644 index 0000000..d2abd88 --- /dev/null +++ b/drafts/rosettacode/sum-multiples-of-3-and-5.wiki @@ -0,0 +1,76 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text loop "by loop, below 1000: " +text pre "by formula, below 10^" +text col ": " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note the multiples of k below n add up to k * m * (m + 1) / 2 with m = (n - 1) / k; inclusion-exclusion +note removes the multiples of 15 counted twice. Below 10^9 every product stays under 2^64 +fn sm k ulong n ulong to ulong +m = div sub n 1 k +ret div mul mul k m add m 1 2 +end +fn main to int +s = ulong 0 +for i ulong 1000 +if or eq rem i 3 0 eq rem i 5 0 +s = add s i +end +end +emit loop +emitu s +emit nl +n = ulong 10 +for e ulong 9 +emit pre +emitu add e 1 +emit col +emitu sub add sm 3 n sm 5 n sm 15 n +emit nl +n = mul n 10 +end +ret 0 +end + +{{out}} +
+by loop, below 1000: 233168
+by formula, below 10^1: 23
+by formula, below 10^2: 2318
+by formula, below 10^3: 233168
+by formula, below 10^4: 23331668
+by formula, below 10^5: 2333316668
+by formula, below 10^6: 233333166668
+by formula, below 10^7: 23333331666668
+by formula, below 10^8: 2333333316666668
+by formula, below 10^9: 233333333166666668
+
diff --git a/drafts/rosettacode/temperature-conversion.wiki b/drafts/rosettacode/temperature-conversion.wiki new file mode 100644 index 0000000..119eb9f --- /dev/null +++ b/drafts/rosettacode/temperature-conversion.wiki @@ -0,0 +1,108 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text tk "K " +text tc "C " +text tf "F " +text tr "R " +text minus "-" +text dot "." +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitc2 writes v hundredths (signed) as a decimal with two places +fn emitc2 v long to void +u = ulong 0 +if lt v 0 +emit minus +u = ulong neg v +else +u = ulong v +end +emitu div u 100 +emit dot +f = rem u 100 +if lt f 10 +emitu 0 +end +emitu f +end +note k is kelvin in hundredths, a multiple of 5 so every scale below is exact in hundredths: +note C = K - 273.15, F = K * 9/5 - 459.67, R = K * 9/5 +fn show k long to void +emit tk +emitc2 k +emit nl +emit tc +emitc2 sub k 27315 +emit nl +emit tf +emitc2 sub div mul k 9 5 45967 +emit nl +emit tr +emitc2 div mul k 9 5 +emit nl +emit nl +end +fn main to int +show 2100 +show 0 +show 27315 +show 37315 +show 31015 +ret 0 +end + +{{out}} +
+K  21.00
+C  -252.15
+F  -421.87
+R  37.80
+
+K  0.00
+C  -273.15
+F  -459.67
+R  0.00
+
+K  273.15
+C  0.00
+F  32.00
+R  491.67
+
+K  373.15
+C  100.00
+F  212.00
+R  671.67
+
+K  310.15
+C  37.00
+F  98.60
+R  558.27
+
diff --git a/drafts/rosettacode/the-twelve-days-of-christmas.wiki b/drafts/rosettacode/the-twelve-days-of-christmas.wiki new file mode 100644 index 0000000..309eb61 --- /dev/null +++ b/drafts/rosettacode/the-twelve-days-of-christmas.wiki @@ -0,0 +1,90 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text on "On the " +text day " day of Christmas,\nMy true love sent to me:\n" +text last "And a partridge in a pear tree." +text days "first\nsecond\nthird\nfourth\nfifth\nsixth\nseventh\neighth\nninth\ntenth\neleventh\ntwelfth\n" +text gifts "A partridge in a pear tree.\nTwo turtle doves,\nThree french hens,\nFour calling birds,\nFive golden rings,\nSix geese a-laying,\nSeven swans a-swimming,\nEight maids a-milking,\nNine ladies dancing,\nTen lords a-leaping,\nEleven pipers piping,\nTwelve drummers drumming,\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note word writes element k of a newline-terminated list, without the newline +fn word t view uchar * k ulong to void +s = ulong 0 +m = ulong 0 +for i ulong len t +if eq get t i 10 +if eq m k +emit part t s sub i s +ret +end +m = add m 1 +s = add i 1 +end +end +trap 9 +end +note verse d lists gifts d down to 0; from the second day on the partridge line starts "And a" +fn verse d ulong to void +emit on +word days d +emit day +for k ulong add d 1 +g = sub d k +if and eq g 0 gt d 0 +emit last +else +word gifts g +end +emit nl +end +end +fn main to int +for d ulong 12 +if gt d 0 +emit nl +end +verse d +end +ret 0 +end + +{{out}} +
+On the first day of Christmas,
+My true love sent to me:
+A partridge in a pear tree.
+
+On the second day of Christmas,
+My true love sent to me:
+Two turtle doves,
+And a partridge in a pear tree.
+
+On the third day of Christmas,
+My true love sent to me:
+Three french hens,
+Two turtle doves,
+And a partridge in a pear tree.
+
+...
+Eight maids a-milking,
+Seven swans a-swimming,
+Six geese a-laying,
+Five golden rings,
+Four calling birds,
+Three french hens,
+Two turtle doves,
+And a partridge in a pear tree.
+
diff --git a/drafts/rosettacode/thue-morse.wiki b/drafts/rosettacode/thue-morse.wiki new file mode 100644 index 0000000..90523a0 --- /dev/null +++ b/drafts/rosettacode/thue-morse.wiki @@ -0,0 +1,51 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes the single byte c +fn emitc c uchar to void +b = new uchar 1 c +emit b +end +note each step appends the bitwise complement of the sequence so far; 7 steps reach 64 terms +fn main to int +t = new uchar 64 0 +n = ulong 1 +for s ulong 7 +for i ulong n +emitc add get t i uchar 48 +end +emit nl +if lt n 64 +for i ulong n +put t add n i sub uchar 1 get t i +end +n = mul n 2 +end +end +ret 0 +end + +{{out}} +
+0
+01
+0110
+01101001
+0110100110010110
+01101001100101101001011001101001
+0110100110010110100101100110100110010110011010010110100110010110
+
diff --git a/drafts/rosettacode/tokenize-a-string.wiki b/drafts/rosettacode/tokenize-a-string.wiki new file mode 100644 index 0000000..01b80bd --- /dev/null +++ b/drafts/rosettacode/tokenize-a-string.wiki @@ -0,0 +1,47 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text dot "." +text msg "Hello,How,Are,You,Today" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note splits at every comma, keeps the pieces as (start, length) pairs, then joins them with periods +fn main to int +st = new ulong 64 0 +ln = new ulong 64 0 +k = ulong 0 +s = ulong 0 +for i ulong add len msg 1 +if or eq i len msg eq get msg rem i len msg 44 +put st k s +put ln k sub i s +k = add k 1 +s = add i 1 +end +end +for j ulong k +if gt j 0 +emit dot +end +emit part msg get st j get ln j +end +emit nl +ret 0 +end + +{{out}} +
+Hello.How.Are.You.Today
+
diff --git a/drafts/rosettacode/topological-sort.wiki b/drafts/rosettacode/topological-sort.wiki new file mode 100644 index 0000000..affa3aa --- /dev/null +++ b/drafts/rosettacode/topological-sort.wiki @@ -0,0 +1,169 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text data "des_system_lib std synopsys std_cell_lib des_system_lib dw02 dw01 ramlib ieee;dw01 ieee dw01 dware gtech;dw02 ieee dw02 dware;dw03 std synopsys dware dw03 dw02 dw01 ieee gtech;dw04 dw04 ieee dw01 dware gtech;dw05 dw05 ieee dware;dw06 dw06 ieee dware;dw07 ieee dware;dware ieee dware;gtech ieee gtech;ramlib std ieee;std_cell_lib ieee std_cell_lib;synopsys;" +text tcyc "un-orderable:" +global nst ulong 32 +global nln ulong 32 +global dep uchar 1024 +global indeg ulong 32 +global sent uchar 32 +global nn ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note data lists one library per ;-terminated group: the library, then what it depends on; a library +note that names itself is ignored, as the task asks. Names are kept as (start, length) in data. +note same says whether the names at (a, la) and (b, lb) in data are the same bytes +fn same a ulong la ulong b ulong lb ulong to bool +if ne la lb +ret false +end +for i ulong la +if ne get data add a i get data add b i +ret false +end +end +ret true +end +note before says whether name i sorts before name j, comparing bytes as C's strcmp does +fn before i ulong j ulong to bool +a = get nst i +b = get nst j +la = get nln i +lb = get nln j +for k ulong la +if ge k lb +ret false +end +if ne get data add a k get data add b k +ret lt get data add a k get data add b k +end +end +ret lt la lb +end +note intern returns the number of the name at (s, l), adding it to the table if it is new +fn intern s ulong l ulong to ulong +n = get nn 0 +for i ulong n +if same get nst i get nln i s l +ret i +end +end +put nst n s +put nln n l +put nn 0 add n 1 +ret n +end +note load reads every group: the first word is the library u, each later word v adds the edge v -> u +fn load to void +first = true +u = ulong 0 +st = ulong 0 +for i ulong len data +c = get data i +if or eq c 32 eq c 59 +if gt i st +v = intern st sub i st +if first +u = v +first = false +else +addedge v u +end +end +st = add i 1 +if eq c 59 +first = true +end +end +end +end +fn addedge v ulong u ulong to void +if ne v u +if eq get dep add mul v 32 u 0 +put dep add mul v 32 u 1 +put indeg u add get indeg u 1 +end +end +end +fn name i ulong to void +emit part data get nst i get nln i +end +note Kahn's algorithm: output the smallest ready name (in-degree 0), then release what depends on it; +note each round outputs one name, so nn rounds suffice; names never output are part of a cycle +fn main to int +load +n = get nn 0 +for r ulong n +b = n +for i ulong n +if and eq get sent i 0 eq get indeg i 0 +if eq b n +b = i +else +if before i b +b = i +end +end +end +end +if eq b n +break +end +put sent b 1 +if gt r 0 +emit sp +end +name b +for j ulong n +if eq get dep add mul b 32 j 1 +put indeg j sub get indeg j 1 +end +end +end +emit nl +emit tcyc +for i ulong n +if eq get sent i 0 +emit sp +name i +end +end +emit nl +ret 0 +end + +{{out}} +
+ieee dware dw02 dw05 dw06 dw07 gtech dw01 dw04 std ramlib std_cell_lib synopsys des_system_lib dw03
+un-orderable:
+
diff --git a/drafts/rosettacode/topswops.wiki b/drafts/rosettacode/topswops.wiki new file mode 100644 index 0000000..8c847fd --- /dev/null +++ b/drafts/rosettacode/topswops.wiki @@ -0,0 +1,147 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text thead "n most swops\n" +global perm ulong 10 +global deck ulong 10 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt w sub 20 i +i = sub 20 w +end +emit part d i sub 20 i +end +note nextp: the next permutation in lexicographic order, false after the last +fn nextp n ulong to bool +if lt n 2 +ret false +end +i = sub n 1 +while gt i 0 bound 10 +if lt get perm sub i 1 get perm i +break +end +i = sub i 1 +end +if eq i 0 +ret false +end +j = sub n 1 +while le get perm j get perm sub i 1 bound 10 +j = sub j 1 +end +t = get perm sub i 1 +put perm sub i 1 get perm j +put perm j t +k = sub n 1 +while lt i k bound 10 +t2 = get perm i +put perm i get perm k +put perm k t2 +i = add i 1 +k = sub k 1 +end +ret true +end +note swops plays the game on a copy of perm (cards 1..n): while the top card is m > 1, reverse the top +note m cards; it ends because the deck's lexicographic order rises with each reversal, and the known +note maxima for n <= 9 are at most 30, so 1000 rounds are plenty +fn swops n ulong to ulong +for i ulong n +put deck i get perm i +end +c = ulong 0 +while ne get deck 0 1 bound 1000 +m = get deck 0 +for x ulong shr m 1 +t = get deck x +put deck x get deck sub sub m x 1 +put deck sub sub m x 1 t +end +c = add c 1 +end +ret c +end +note most tries every one of the n! decks (9! = 362880 at most) +fn most n ulong to ulong +for i ulong n +put perm i add i 1 +end +b = ulong 0 +while true bound 362880 +s = swops n +if gt s b +b = s +end +if not nextp n +break +end +end +ret b +end +fn main to int +emit thead +for x ulong 9 +n = add x 1 +emitu n +emitw most n 12 +emit nl +end +ret 0 +end + +{{out}} +
+n  most swops
+1           0
+2           1
+3           2
+4           4
+5           7
+6          10
+7          16
+8          22
+9          30
+
diff --git a/drafts/rosettacode/totient-function.wiki b/drafts/rosettacode/totient-function.wiki new file mode 100644 index 0000000..25359ae --- /dev/null +++ b/drafts/rosettacode/totient-function.wiki @@ -0,0 +1,117 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text phi "phi(" +text eqs ") = " +text prm ", prime" +text cnt "primes up to " +text col ": " +note ph n is Euler's totient of n for n up to 100000 +global ph ulong 100001 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note sieve: start with ph n = n; each prime p (still ph p = p when reached) scales its multiples by (1 - 1/p) +note bound: each p touches at most 100000 / p multiples +fn sieve to void +for i ulong 100001 +put ph i i +end +p = ulong 2 +while le p 100000 bound 100000 +if eq get ph p p +j = p +while le j 100000 bound 100000 +put ph j sub get ph j div get ph j p +j = add j p +end +end +p = add p 1 +end +end +note n > 1 is prime exactly when phi(n) = n - 1 +fn main to int +sieve +for i ulong 25 +n = add i 1 +emit phi +emitu n +emit eqs +emitu get ph n +if eq get ph n sub n 1 +emit prm +end +emit nl +end +lim = ulong 100 +c = ulong 0 +for i ulong 99999 +n = add i 2 +if eq get ph n sub n 1 +c = add c 1 +end +if eq n lim +emit cnt +emitu lim +emit col +emitu c +emit nl +lim = mul lim 10 +end +end +ret 0 +end + +{{out}} +
+phi(1) = 1
+phi(2) = 1, prime
+phi(3) = 2, prime
+phi(4) = 2
+phi(5) = 4, prime
+phi(6) = 2
+phi(7) = 6, prime
+phi(8) = 4
+phi(9) = 6
+phi(10) = 4
+phi(11) = 10, prime
+phi(12) = 4
+phi(13) = 12, prime
+phi(14) = 6
+phi(15) = 8
+...
+phi(22) = 10
+phi(23) = 22, prime
+phi(24) = 8
+phi(25) = 20
+primes up to 100: 25
+primes up to 1000: 168
+primes up to 10000: 1229
+primes up to 100000: 9592
+
diff --git a/drafts/rosettacode/tree-traversal.wiki b/drafts/rosettacode/tree-traversal.wiki new file mode 100644 index 0000000..9521e9e --- /dev/null +++ b/drafts/rosettacode/tree-traversal.wiki @@ -0,0 +1,128 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tpre "preorder: " +text tin "inorder: " +text tpost "postorder: " +text tlevel "level-order:" +global lft ulong 10 +global rgt ulong 10 +global queue ulong 10 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note the task's tree, nodes 1..9 with 0 for no child: +note 1 +note / \ +note 2 3 +note / \ / +note 4 5 6 +note / / \ +note 7 8 9 +fn kids n ulong l ulong r ulong to void +put lft n l +put rgt n r +end +fn pre n ulong to void +if eq n 0 +ret +end +emit sp +emitu n +pre get lft n +pre get rgt n +end +fn ino n ulong to void +if eq n 0 +ret +end +ino get lft n +emit sp +emitu n +ino get rgt n +end +fn post n ulong to void +if eq n 0 +ret +end +post get lft n +post get rgt n +emit sp +emitu n +end +note level order reads nodes from a queue; each node enters it once, so 9 passes suffice +fn level n ulong to void +put queue 0 n +h = ulong 0 +t = ulong 1 +while lt h t bound 9 +v = get queue h +h = add h 1 +emit sp +emitu v +if ne get lft v 0 +put queue t get lft v +t = add t 1 +end +if ne get rgt v 0 +put queue t get rgt v +t = add t 1 +end +end +end +fn main to int +kids 1 2 3 +kids 2 4 5 +kids 3 6 0 +kids 4 7 0 +kids 6 8 9 +emit tpre +pre 1 +emit nl +emit tin +ino 1 +emit nl +emit tpost +post 1 +emit nl +emit tlevel +level 1 +emit nl +ret 0 +end + +{{out}} +
+preorder:    1 2 4 7 5 3 6 8 9
+inorder:     7 4 2 5 1 8 6 9 3
+postorder:   7 4 5 2 8 9 6 3 1
+level-order: 1 2 3 4 5 6 7 8 9
+
diff --git a/drafts/rosettacode/twin-primes.wiki b/drafts/rosettacode/twin-primes.wiki new file mode 100644 index 0000000..70cc209 --- /dev/null +++ b/drafts/rosettacode/twin-primes.wiki @@ -0,0 +1,102 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text pre "twin prime pairs below " +text colon ": " +note odd-only bit sieve below 10^8: odd q is bit q >> 1, set when q is composite; 5 * 10^7 bits in 6250000 bytes +global cb uchar 6250000 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn mark q ulong to void +b = shr q 1 +put cb shr b 3 or get cb shr b 3 shl uchar 1 and b 7 +end +note isp is for odd q below 10^8 only (3, 5, 7, ...) +fn isp q ulong to bool +b = shr q 1 +ret eq and get cb shr b 3 shl uchar 1 and b 7 0 +end +note bound: odd p with p * p < 10^8 means p < 10^4; an odd prime p marks at most 10^8 / (2p) < 1.7 * 10^7 multiples, +note done as up to 1000 chunks of 999990 marks each +fn sieve to void +mark 1 +p = ulong 3 +while lt mul p p 100000000 bound 5000 +if isp p +s = mul p 2 +m = mul p p +while lt m 100000000 bound 1000 +c = ulong 0 +while and lt m 100000000 lt c 999990 bound 999999 +mark m +m = add m s +c = add c 1 +end +end +end +p = add p 2 +end +end +note a pair (p, p + 2) counts below lim when p + 2 < lim; p = 2j + 3 walks every odd p from 3 +fn main to int +sieve +lim = ulong 10 +cnt = ulong 0 +for j ulong 50000000 +p = add mul j 2 3 +while ge add p 2 lim bound 9 +emit pre +emitu lim +emit colon +emitu cnt +emit nl +lim = mul lim 10 +end +if gt lim 100000000 +break +end +if and isp p isp add p 2 +cnt = add cnt 1 +end +end +ret 0 +end + +{{out}} +
+twin prime pairs below 10: 2
+twin prime pairs below 100: 8
+twin prime pairs below 1000: 35
+twin prime pairs below 10000: 205
+twin prime pairs below 100000: 1224
+twin prime pairs below 1000000: 8169
+twin prime pairs below 10000000: 58980
+twin prime pairs below 100000000: 440312
+
diff --git a/drafts/rosettacode/url-decoding.wiki b/drafts/rosettacode/url-decoding.wiki new file mode 100644 index 0000000..3aa2ff9 --- /dev/null +++ b/drafts/rosettacode/url-decoding.wiki @@ -0,0 +1,65 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text u1 "http%3A%2F%2Ffoo%20bar%2F" +text u2 "google.com/search?q=%60Abdu%27l-Bah%C3%A1" +text u3 "%25%32%35" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note hexv gives the value of one hex digit, either case; anything else traps 9 +fn hexv c uchar to uchar +if and ge c 48 le c 57 +ret sub c 48 +end +if and ge c 65 le c 70 +ret sub c 55 +end +if and ge c 97 le c 102 +ret sub c 87 +end +trap 9 +end +note dec turns every %XY into the byte 0xXY; the result bytes are written as they are (here UTF-8) +fn dec a view uchar * to void +o = new uchar 256 0 +p = ulong 0 +i = ulong 0 +while lt i len a bound 4096 +c = get a i +if eq c 37 +c = or shl hexv get a add i 1 4 hexv get a add i 2 +i = add i 3 +else +i = add i 1 +end +put o p c +p = add p 1 +end +emit part o 0 p +emit nl +end +fn main to int +dec u1 +dec u2 +dec u3 +ret 0 +end + +{{out}} +
+http://foo bar/
+google.com/search?q=`Abdu'l-Bahá
+%25
+
diff --git a/drafts/rosettacode/url-encoding.wiki b/drafts/rosettacode/url-encoding.wiki new file mode 100644 index 0000000..2be0982 --- /dev/null +++ b/drafts/rosettacode/url-encoding.wiki @@ -0,0 +1,66 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text pct "%" +text hexd "0123456789ABCDEF" +text u1 "http://foo bar/" +text u2 "mailto:joe@example.com?subject=a&b c~d_e.f-g" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note keep says whether c is written as itself: letters and digits always, - . _ ~ only in the +note RFC 3986 variant (rfc true); every other byte becomes % and two upper-case hex digits +fn keep c uchar rfc bool to bool +if or or and ge c 48 le c 57 and ge c 65 le c 90 and ge c 97 le c 122 +ret true +end +if and rfc or or eq c 45 eq c 46 or eq c 95 eq c 126 +ret true +end +ret false +end +fn enc a view uchar * rfc bool to void +for i ulong len a +c = get a i +if keep c rfc +emitc c +else +emit pct +emitc get hexd shr c 4 +emitc get hexd and c 15 +end +end +emit nl +end +fn main to int +enc u1 false +enc u1 true +enc u2 false +enc u2 true +ret 0 +end + +{{out}} +
+http%3A%2F%2Ffoo%20bar%2F
+http%3A%2F%2Ffoo%20bar%2F
+mailto%3Ajoe%40example%2Ecom%3Fsubject%3Da%26b%20c%7Ed%5Fe%2Ef%2Dg
+mailto%3Ajoe%40example.com%3Fsubject%3Da%26b%20c~d_e.f-g
+
diff --git a/drafts/rosettacode/van-eck-sequence.wiki b/drafts/rosettacode/van-eck-sequence.wiki new file mode 100644 index 0000000..d67101a --- /dev/null +++ b/drafts/rosettacode/van-eck-sequence.wiki @@ -0,0 +1,68 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text first "terms 1 to 10:" +text last "terms 991 to 1000:" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a(0) = 0; a(n+1) = n - m where m < n is the last earlier index with a(m) = a(n), else 0 +note ls v is one more than the last index holding v (0: not seen); every term is below 1000, its index +fn main to int +a = new ulong 1000 0 +ls = new ulong 1000 0 +for n ulong 999 +v = get a n +w = get ls v +if gt w 0 +put a add n 1 sub n sub w 1 +end +put ls v add n 1 +end +emit first +for i ulong 10 +emit sp +emitu get a i +end +emit nl +emit last +for i ulong 10 +emit sp +emitu get a add 990 i +end +emit nl +ret 0 +end + +{{out}} +
+terms 1 to 10: 0 0 1 0 2 0 2 2 1 6
+terms 991 to 1000: 4 7 30 25 67 225 488 0 10 136
+
diff --git a/drafts/rosettacode/vigenere-cipher.wiki b/drafts/rosettacode/vigenere-cipher.wiki new file mode 100644 index 0000000..709b1dd --- /dev/null +++ b/drafts/rosettacode/vigenere-cipher.wiki @@ -0,0 +1,69 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text key "VIGENERECIPHER" +text msg "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note clean keeps the letters of a, upper-cased, in r and returns how many +fn clean a view uchar * r edit uchar * to ulong +n = ulong 0 +for i ulong len a +c = get a i +if and ge c 97 le c 122 +c = sub c 32 +end +if and ge c 65 le c 90 +put r n c +n = add n 1 +end +end +ret n +end +note vig adds (dir 0) or subtracts (dir 1) the key letters mod 26, the key repeating +fn vig a view uchar * r edit uchar * dir ulong to void +for i ulong len a +k = sub get key rem i len key 65 +c = sub get a i 65 +if eq dir 0 +c = rem add c k 26 +else +c = rem sub add c 26 k 26 +end +put r i add c 65 +end +end +fn main to int +t = new uchar 256 0 +e = new uchar 256 0 +d = new uchar 256 0 +n = clean msg t +emit part t 0 n +emit nl +vig part t 0 n part e 0 n 0 +emit part e 0 n +emit nl +vig part e 0 n part d 0 n 1 +emit part d 0 n +emit nl +ret 0 +end + +{{out}} +
+BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH
+WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY
+BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH
+
diff --git a/drafts/rosettacode/water-collected-between-towers.wiki b/drafts/rosettacode/water-collected-between-towers.wiki new file mode 100644 index 0000000..1702c2d --- /dev/null +++ b/drafts/rosettacode/water-collected-between-towers.wiki @@ -0,0 +1,176 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text d0 "1 5 3 7 2" +text d1 "5 3 7 2 6 4 5 9 1 2" +text d2 "2 6 3 5 2 8 1 4 2 2 5 3 5 7 4 1" +text d3 "5 5 5 5" +text d4 "5 6 7 8" +text d5 "8 7 7 6" +text d6 "6 7 10 7 6" +text twater "water: " +global vals long 16 +global lmax long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note water: a column holds min(highest to its left, highest to its right) minus its own height; +note lmax[i] is the running maximum from the left, the right maximum is kept while walking back +fn water n ulong to long +m = long 0 +for i ulong n +if gt get vals i m +m = get vals i +end +put lmax i m +end +r = long 0 +w = long 0 +for x ulong n +i = sub sub n x 1 +if gt get vals i r +r = get vals i +end +h = get lmax i +if lt r h +h = r +end +w = add w sub h get vals i +end +ret w +end +fn main to int +n = parse d0 +show n +emit twater +emiti water n +emit nl +n = parse d1 +show n +emit twater +emiti water n +emit nl +n = parse d2 +show n +emit twater +emiti water n +emit nl +n = parse d3 +show n +emit twater +emiti water n +emit nl +n = parse d4 +show n +emit twater +emiti water n +emit nl +n = parse d5 +show n +emit twater +emiti water n +emit nl +n = parse d6 +show n +emit twater +emiti water n +emit nl +ret 0 +end + +{{out}} +
+1 5 3 7 2
+water: 2
+5 3 7 2 6 4 5 9 1 2
+water: 14
+2 6 3 5 2 8 1 4 2 2 5 3 5 7 4 1
+water: 35
+5 5 5 5
+water: 0
+5 6 7 8
+water: 0
+8 7 7 6
+water: 0
+6 7 10 7 6
+water: 0
+
diff --git a/drafts/rosettacode/wireworld.wiki b/drafts/rosettacode/wireworld.wiki new file mode 100644 index 0000000..0164eba --- /dev/null +++ b/drafts/rosettacode/wireworld.wiki @@ -0,0 +1,133 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +text tstart "tH.........|. . | ... |. . |Ht.. ......" +text tgen "generation " +global cur uchar 55 +global nxt uchar 55 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitb writes the single byte c +fn emitb c ulong to void +d = new uchar 1 0 +put d 0 uchar c +emit d +end +note the 5 x 11 board from the task: H electron head, t tail, . conductor, space empty; a head becomes a +note tail, a tail a conductor, and a conductor a head when 1 or 2 of its 8 neighbours are heads +fn heads r long c long to ulong +n = ulong 0 +for a ulong 3 +for b ulong 3 +y = sub add r long a 1 +x = sub add c long b 1 +if and and ge y 0 lt y 5 and ge x 0 lt x 11 +if eq get cur add mul ulong y 11 ulong x 72 +n = add n 1 +end +end +end +end +ret n +end +fn rule k ulong n ulong to uchar +v = get cur k +if eq v 72 +ret 116 +end +if eq v 116 +ret 46 +end +if and eq v 46 or eq n 1 eq n 2 +ret 72 +end +ret v +end +fn step to void +for r ulong 5 +for c ulong 11 +k = add mul r 11 c +put nxt k rule k heads long r long c +end +end +for i ulong 55 +put cur i get nxt i +end +end +fn main to int +for r ulong 5 +for c ulong 11 +put cur add mul r 11 c get tstart add mul r 12 c +end +end +for g ulong 10 +emit tgen +emitu g +emit nl +for i ulong 55 +emitb ulong get cur i +if eq rem i 11 10 +emit nl +end +end +step +end +ret 0 +end + +{{out}} +
+generation 0
+tH.........
+.   .      
+   ...     
+.   .      
+Ht.. ......
+generation 1
+.tH........
+H   .      
+   ...     
+H   .      
+t... ......
+generation 2
+H.tH.......
+t   .      
+...
+t   .      
+.H.t ......
+generation 9
+tH.tH.tH.tH
+.   H      
+   ...     
+.   .      
+HtH. ......
+
diff --git a/drafts/rosettacode/word-wrap.wiki b/drafts/rosettacode/word-wrap.wiki new file mode 100644 index 0000000..b01fbe5 --- /dev/null +++ b/drafts/rosettacode/word-wrap.wiki @@ -0,0 +1,87 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text rule "----\n" +text para "In olden times when wishing still helped one, there lived a king whose daughters were all beautiful, but the youngest was so beautiful that the sun itself, which has seen so much, was astonished whenever it shone in her face. Close by the king's castle lay a great dark forest, and under an old lime-tree in the forest was a well, and when the day was very warm, the king's child went out into the forest and sat down by the side of the cool fountain, and when she was bored she took a golden ball, and threw it up on high and caught it, and this ball was her favorite plaything." +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note greedy wrap: a word goes on the current line when it still fits in width w, else starts a new one +fn wrap a view uchar * w ulong to void +cur = ulong 0 +i = ulong 0 +while lt i len a bound 4096 +if eq get a i 32 +i = add i 1 +else +j = i +while lt j len a bound 4096 +if eq get a j 32 +break +end +j = add j 1 +end +l = sub j i +if eq cur 0 +cur = l +else +if le add add cur 1 l w +emit sp +cur = add add cur 1 l +else +emit nl +cur = l +end +end +emit part a i l +i = j +end +end +emit nl +end +fn main to int +wrap para 72 +emit rule +wrap para 40 +ret 0 +end + +{{out}} +
+In olden times when wishing still helped one, there lived a king whose
+daughters were all beautiful, but the youngest was so beautiful that the
+sun itself, which has seen so much, was astonished whenever it shone in
+her face. Close by the king's castle lay a great dark forest, and under
+an old lime-tree in the forest was a well, and when the day was very
+warm, the king's child went out into the forest and sat down by the side
+of the cool fountain, and when she was bored she took a golden ball, and
+threw it up on high and caught it, and this ball was her favorite
+plaything.
+----
+In olden times when wishing still helped
+one, there lived a king whose daughters
+were all beautiful, but the youngest was
+so beautiful that the sun itself, which
+has seen so much, was astonished
+...
+forest, and under an old lime-tree in
+the forest was a well, and when the day
+was very warm, the king's child went out
+into the forest and sat down by the side
+of the cool fountain, and when she was
+bored she took a golden ball, and threw
+it up on high and caught it, and this
+ball was her favorite plaything.
+
diff --git a/drafts/rosettacode/zig-zag-matrix.wiki b/drafts/rosettacode/zig-zag-matrix.wiki new file mode 100644 index 0000000..17aa8d3 --- /dev/null +++ b/drafts/rosettacode/zig-zag-matrix.wiki @@ -0,0 +1,102 @@ +=={{header|Omegese}}== +{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} +Omega has no imports, so the small output helpers (emit and friends) are part of the program. + +om2 +text nl "\n" +text sp " " +text minus "-" +global grid ulong 64 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt w sub 20 i +i = sub 20 w +end +emit part d i sub 20 i +end +note number the cells along the anti-diagonals d = i + j; even diagonals run up and to the right +note (i falling), odd ones down and to the left (i rising) +fn zigzag n ulong to void +k = ulong 0 +for d ulong sub mul n 2 1 +for x ulong add d 1 +i = x +if eq rem d 2 0 +i = sub d x +end +if and lt i n le sub d i sub n 1 +put grid add mul i n sub d i k +k = add k 1 +end +end +end +end +fn show n ulong to void +for i ulong n +for j ulong n +emitw get grid add mul i n j 3 +end +emit nl +end +end +fn main to int +zigzag 5 +show 5 +emit nl +zigzag 6 +show 6 +ret 0 +end + +{{out}} +
+  0  1  5  6 14
+  2  4  7 13 15
+  3  8 12 16 21
+  9 11 17 20 22
+ 10 18 19 23 24
+
+  0  1  5  6 14 15
+  2  4  7 13 16 25
+  3  8 12 17 24 26
+  9 11 18 23 27 32
+ 10 19 22 28 31 33
+ 20 21 29 30 34 35
+
diff --git a/exercises/acronym/.meta/example.om b/exercises/acronym/.meta/example.om new file mode 100644 index 0000000..20115e8 --- /dev/null +++ b/exercises/acronym/.meta/example.om @@ -0,0 +1,28 @@ +note Exercism-style exercise "acronym": https://exercism.org/tracks/python/exercises/acronym +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note abbreviate writes the upper-case acronym of phrase s into o and returns its length. Words are +note separated by spaces, hyphens and other punctuation; an apostrophe does not start a new word. +note Check it with: sh exercise.sh exercises/acronym +om2 +fn abbreviate s view uchar * o edit uchar * to ulong +p = ulong 0 +prev = false +for i ulong len s +c = get s i +up = and ge c 65 le c 90 +lo = and ge c 97 le c 122 +if or up lo +if not prev +if lo +c = sub c 32 +end +put o p c +p = add p 1 +end +prev = true +else +prev = eq c 39 +end +end +ret p +end diff --git a/exercises/acronym/acronym.om b/exercises/acronym/acronym.om new file mode 100644 index 0000000..fef75bc --- /dev/null +++ b/exercises/acronym/acronym.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "acronym": https://exercism.org/tracks/python/exercises/acronym +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note abbreviate writes the upper-case acronym of phrase s into o and returns its length. Words are +note separated by spaces, hyphens and other punctuation; an apostrophe does not start a new word. +note Check it with: sh exercise.sh exercises/acronym +om2 +fn abbreviate s view uchar * o edit uchar * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/acronym/test.om b/exercises/acronym/test.om new file mode 100644 index 0000000..1c3adde --- /dev/null +++ b/exercises/acronym/test.om @@ -0,0 +1,153 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/acronym/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 9 cases kept, 0 skipped +fn tbeq a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text s0 "Portable Network Graphics" +text e1 "PNG" +text c2 "abbreviate: basic" +text s3 "Ruby on Rails" +text e4 "ROR" +text c5 "abbreviate: lowercase words" +text s6 "First In, First Out" +text e7 "FIFO" +text c8 "abbreviate: punctuation" +text s9 "GNU Image Manipulation Program" +text e10 "GIMP" +text c11 "abbreviate: all caps word" +text s12 "Complementary metal-oxide semiconductor" +text e13 "CMOS" +text c14 "abbreviate: punctuation without whitespace" +text s15 "Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me" +text e16 "ROTFLSHTMDCOALM" +text c17 "abbreviate: very long abbreviation" +text s18 "Something - I made up from thin air" +text e19 "SIMUFTA" +text c20 "abbreviate: consecutive delimiters" +text s21 "Halley's Comet" +text e22 "HC" +text c23 "abbreviate: apostrophes" +text s24 "The Road _Not_ Taken" +text e25 "TRNT" +text c26 "abbreviate: underscore emphasis" +note canonical case 1e22cceb-c5e4-4562-9afe-aef07ad1eaf4 +fn tc0 to bool +o = new uchar 1024 0 +n = abbreviate s0 o +ret tbeq part o 0 n e1 +end +note canonical case 79ae3889-a5c0-4b01-baf0-232d31180c08 +fn tc1 to bool +o = new uchar 1024 0 +n = abbreviate s3 o +ret tbeq part o 0 n e4 +end +note canonical case ec7000a7-3931-4a17-890e-33ca2073a548 +fn tc2 to bool +o = new uchar 1024 0 +n = abbreviate s6 o +ret tbeq part o 0 n e7 +end +note canonical case 32dd261c-0c92-469a-9c5c-b192e94a63b0 +fn tc3 to bool +o = new uchar 1024 0 +n = abbreviate s9 o +ret tbeq part o 0 n e10 +end +note canonical case ae2ac9fa-a606-4d05-8244-3bcc4659c1d4 +fn tc4 to bool +o = new uchar 1024 0 +n = abbreviate s12 o +ret tbeq part o 0 n e13 +end +note canonical case 0e4b1e7c-1a6d-48fb-81a7-bf65eb9e69f9 +fn tc5 to bool +o = new uchar 1024 0 +n = abbreviate s15 o +ret tbeq part o 0 n e16 +end +note canonical case 6a078f49-c68d-4b7b-89af-33a1a98c28cc +fn tc6 to bool +o = new uchar 1024 0 +n = abbreviate s18 o +ret tbeq part o 0 n e19 +end +note canonical case 5118b4b1-4572-434c-8d57-5b762e57973e +fn tc7 to bool +o = new uchar 1024 0 +n = abbreviate s21 o +ret tbeq part o 0 n e22 +end +note canonical case adc12eab-ec2d-414f-b48c-66a4fc06cdef +fn tc8 to bool +o = new uchar 1024 0 +n = abbreviate s24 o +ret tbeq part o 0 n e25 +end +fn main to int +tcheck tc0 c2 +tcheck tc1 c5 +tcheck tc2 c8 +tcheck tc3 c11 +tcheck tc4 c14 +tcheck tc5 c17 +tcheck tc6 c20 +tcheck tc7 c23 +tcheck tc8 c26 +ret int get tfails 0 +end diff --git a/exercises/all-your-base/.meta/example.om b/exercises/all-your-base/.meta/example.om new file mode 100644 index 0000000..892e8de --- /dev/null +++ b/exercises/all-your-base/.meta/example.om @@ -0,0 +1,37 @@ +note Exercism-style exercise "all-your-base": https://exercism.org/tracks/python/exercises/all-your-base +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note rebase converts the number whose digits ds are in base ib (most significant first) to base ob, +note writes its digits into o and returns how many (the number 0 is the single digit 0); or -1 when +note ib < 2, -3 when ob < 2, -2 when a digit is outside 0 .. ib-1. +note Check it with: sh exercise.sh exercises/all-your-base +om2 +fn rebase ib long ds view long * ob long o edit long * to long +if lt ib 2 +ret -1 +end +if lt ob 2 +ret -3 +end +v = long 0 +for i ulong len ds +d = get ds i +if or lt d 0 ge d ib +ret -2 +end +v = add mul v ib d +end +tmp = new long 64 0 +k = ulong 0 +while true bound 64 +put tmp k rem v ob +k = add k 1 +v = div v ob +if eq v 0 +break +end +end +for j ulong k +put o j get tmp sub sub k 1 j +end +ret long k +end diff --git a/exercises/all-your-base/all-your-base.om b/exercises/all-your-base/all-your-base.om new file mode 100644 index 0000000..685c9fa --- /dev/null +++ b/exercises/all-your-base/all-your-base.om @@ -0,0 +1,11 @@ +note Exercism-style exercise "all-your-base": https://exercism.org/tracks/python/exercises/all-your-base +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note rebase converts the number whose digits ds are in base ib (most significant first) to base ob, +note writes its digits into o and returns how many (the number 0 is the single digit 0); or -1 when +note ib < 2, -3 when ob < 2, -2 when a digit is outside 0 .. ib-1. +note Check it with: sh exercise.sh exercises/all-your-base +om2 +fn rebase ib long ds view long * ob long o edit long * to long +note replace this line with your solution +trap 13 +end diff --git a/exercises/all-your-base/test.om b/exercises/all-your-base/test.om new file mode 100644 index 0000000..d5603f9 --- /dev/null +++ b/exercises/all-your-base/test.om @@ -0,0 +1,370 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/all-your-base/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 21 cases kept, 0 skipped +fn tleq a view long * b view long * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text c0 "rebase: single bit one to decimal" +text c1 "rebase: binary to single decimal" +text c2 "rebase: single decimal to binary" +text c3 "rebase: binary to multiple decimal" +text c4 "rebase: decimal to binary" +text c5 "rebase: trinary to hexadecimal" +text c6 "rebase: hexadecimal to trinary" +text c7 "rebase: 15-bit integer" +text c8 "rebase: empty list" +text c9 "rebase: single zero" +text c10 "rebase: multiple zeros" +text c11 "rebase: leading zeros" +text c12 "rebase: input base is one" +text c13 "rebase: input base is zero" +text c14 "rebase: input base is negative" +text c15 "rebase: negative digit" +text c16 "rebase: invalid positive digit" +text c17 "rebase: output base is one" +text c18 "rebase: output base is zero" +text c19 "rebase: output base is negative" +text c20 "rebase: both bases are negative" +note canonical case 5ce422f9-7a4b-4f44-ad29-49c67cb32d2c +fn tc0 to bool +a0 = new long 1 0 +put a0 0 1 +o = new long 256 0 +n = rebase 2 a0 10 o +if lt n 0 +ret false +end +a1 = new long 1 0 +put a1 0 1 +ret tleq part o 0 ulong n a1 +end +note canonical case 0cc3fea8-bb79-46ac-a2ab-5a2c93051033 +fn tc1 to bool +a0 = new long 3 0 +put a0 0 1 +put a0 1 0 +put a0 2 1 +o = new long 256 0 +n = rebase 2 a0 10 o +if lt n 0 +ret false +end +a1 = new long 1 0 +put a1 0 5 +ret tleq part o 0 ulong n a1 +end +note canonical case f12db0f9-0d3d-42c2-b3ba-e38cb375a2b8 +fn tc2 to bool +a0 = new long 1 0 +put a0 0 5 +o = new long 256 0 +n = rebase 10 a0 2 o +if lt n 0 +ret false +end +a1 = new long 3 0 +put a1 0 1 +put a1 1 0 +put a1 2 1 +ret tleq part o 0 ulong n a1 +end +note canonical case 2c45cf54-6da3-4748-9733-5a3c765d925b +fn tc3 to bool +a0 = new long 6 0 +put a0 0 1 +put a0 1 0 +put a0 2 1 +put a0 3 0 +put a0 4 1 +put a0 5 0 +o = new long 256 0 +n = rebase 2 a0 10 o +if lt n 0 +ret false +end +a1 = new long 2 0 +put a1 0 4 +put a1 1 2 +ret tleq part o 0 ulong n a1 +end +note canonical case 65ddb8b4-8899-4fcc-8618-181b2cf0002d +fn tc4 to bool +a0 = new long 2 0 +put a0 0 4 +put a0 1 2 +o = new long 256 0 +n = rebase 10 a0 2 o +if lt n 0 +ret false +end +a1 = new long 6 0 +put a1 0 1 +put a1 1 0 +put a1 2 1 +put a1 3 0 +put a1 4 1 +put a1 5 0 +ret tleq part o 0 ulong n a1 +end +note canonical case 8d418419-02a7-4824-8b7a-352d33c6987e +fn tc5 to bool +a0 = new long 4 0 +put a0 0 1 +put a0 1 1 +put a0 2 2 +put a0 3 0 +o = new long 256 0 +n = rebase 3 a0 16 o +if lt n 0 +ret false +end +a1 = new long 2 0 +put a1 0 2 +put a1 1 10 +ret tleq part o 0 ulong n a1 +end +note canonical case d3901c80-8190-41b9-bd86-38d988efa956 +fn tc6 to bool +a0 = new long 2 0 +put a0 0 2 +put a0 1 10 +o = new long 256 0 +n = rebase 16 a0 3 o +if lt n 0 +ret false +end +a1 = new long 4 0 +put a1 0 1 +put a1 1 1 +put a1 2 2 +put a1 3 0 +ret tleq part o 0 ulong n a1 +end +note canonical case 5d42f85e-21ad-41bd-b9be-a3e8e4258bbf +fn tc7 to bool +a0 = new long 3 0 +put a0 0 3 +put a0 1 46 +put a0 2 60 +o = new long 256 0 +n = rebase 97 a0 73 o +if lt n 0 +ret false +end +a1 = new long 3 0 +put a1 0 6 +put a1 1 10 +put a1 2 45 +ret tleq part o 0 ulong n a1 +end +note canonical case d68788f7-66dd-43f8-a543-f15b6d233f83 +fn tc8 to bool +a0 = new long 1 0 +o = new long 256 0 +n = rebase 2 part a0 0 0 10 o +if lt n 0 +ret false +end +a1 = new long 1 0 +put a1 0 0 +ret tleq part o 0 ulong n a1 +end +note canonical case 5e27e8da-5862-4c5f-b2a9-26c0382b6be7 +fn tc9 to bool +a0 = new long 1 0 +put a0 0 0 +o = new long 256 0 +n = rebase 10 a0 2 o +if lt n 0 +ret false +end +a1 = new long 1 0 +put a1 0 0 +ret tleq part o 0 ulong n a1 +end +note canonical case 2e1c2573-77e4-4b9c-8517-6c56c5bcfdf2 +fn tc10 to bool +a0 = new long 3 0 +put a0 0 0 +put a0 1 0 +put a0 2 0 +o = new long 256 0 +n = rebase 10 a0 2 o +if lt n 0 +ret false +end +a1 = new long 1 0 +put a1 0 0 +ret tleq part o 0 ulong n a1 +end +note canonical case 3530cd9f-8d6d-43f5-bc6e-b30b1db9629b +fn tc11 to bool +a0 = new long 3 0 +put a0 0 0 +put a0 1 6 +put a0 2 0 +o = new long 256 0 +n = rebase 7 a0 10 o +if lt n 0 +ret false +end +a1 = new long 2 0 +put a1 0 4 +put a1 1 2 +ret tleq part o 0 ulong n a1 +end +note canonical case a6b476a1-1901-4f2a-92c4-4d91917ae023 +fn tc12 to bool +a0 = new long 1 0 +put a0 0 0 +o = new long 256 0 +ret eq rebase 1 a0 10 o -1 +end +note canonical case e21a693a-7a69-450b-b393-27415c26a016 +fn tc13 to bool +a0 = new long 1 0 +o = new long 256 0 +ret eq rebase 0 part a0 0 0 10 o -1 +end +note canonical case 54a23be5-d99e-41cc-88e0-a650ffe5fcc2 +fn tc14 to bool +a0 = new long 1 0 +put a0 0 1 +o = new long 256 0 +ret eq rebase -2 a0 10 o -1 +end +note canonical case 9eccf60c-dcc9-407b-95d8-c37b8be56bb6 +fn tc15 to bool +a0 = new long 6 0 +put a0 0 1 +put a0 1 -1 +put a0 2 1 +put a0 3 0 +put a0 4 1 +put a0 5 0 +o = new long 256 0 +ret eq rebase 2 a0 10 o -2 +end +note canonical case 232fa4a5-e761-4939-ba0c-ed046cd0676a +fn tc16 to bool +a0 = new long 6 0 +put a0 0 1 +put a0 1 2 +put a0 2 1 +put a0 3 0 +put a0 4 1 +put a0 5 0 +o = new long 256 0 +ret eq rebase 2 a0 10 o -2 +end +note canonical case 14238f95-45da-41dc-95ce-18f860b30ad3 +fn tc17 to bool +a0 = new long 6 0 +put a0 0 1 +put a0 1 0 +put a0 2 1 +put a0 3 0 +put a0 4 1 +put a0 5 0 +o = new long 256 0 +ret eq rebase 2 a0 1 o -3 +end +note canonical case 73dac367-da5c-4a37-95fe-c87fad0a4047 +fn tc18 to bool +a0 = new long 1 0 +put a0 0 7 +o = new long 256 0 +ret eq rebase 10 a0 0 o -3 +end +note canonical case 13f81f42-ff53-4e24-89d9-37603a48ebd9 +fn tc19 to bool +a0 = new long 1 0 +put a0 0 1 +o = new long 256 0 +ret eq rebase 2 a0 -7 o -3 +end +note canonical case 0e6c895d-8a5d-4868-a345-309d094cfe8d +fn tc20 to bool +a0 = new long 1 0 +put a0 0 1 +o = new long 256 0 +ret eq rebase -2 a0 -7 o -1 +end +fn main to int +tcheck tc0 c0 +tcheck tc1 c1 +tcheck tc2 c2 +tcheck tc3 c3 +tcheck tc4 c4 +tcheck tc5 c5 +tcheck tc6 c6 +tcheck tc7 c7 +tcheck tc8 c8 +tcheck tc9 c9 +tcheck tc10 c10 +tcheck tc11 c11 +tcheck tc12 c12 +tcheck tc13 c13 +tcheck tc14 c14 +tcheck tc15 c15 +tcheck tc16 c16 +tcheck tc17 c17 +tcheck tc18 c18 +tcheck tc19 c19 +tcheck tc20 c20 +ret int get tfails 0 +end diff --git a/exercises/allergies/.meta/example.om b/exercises/allergies/.meta/example.om new file mode 100644 index 0000000..6809734 --- /dev/null +++ b/exercises/allergies/.meta/example.om @@ -0,0 +1,106 @@ +note Exercism-style exercise "allergies": https://exercism.org/tracks/python/exercises/allergies +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note The allergens in score order are eggs 1, peanuts 2, shellfish 4, strawberries 8, tomatoes 16, +note chocolate 32, pollen 64, cats 128; a score is the sum of the allergens (higher bits are ignored). +note allergicto is true when score includes item; list writes the allergens of score, in that order, +note joined by commas, into o and returns the number of bytes written. +note Check it with: sh exercise.sh exercises/allergies +om2 +text al0 "eggs" +text al1 "peanuts" +text al2 "shellfish" +text al3 "strawberries" +text al4 "tomatoes" +text al5 "chocolate" +text al6 "pollen" +text al7 "cats" +fn same a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +fn app o edit uchar * p ulong s view uchar * to ulong +q = p +for i ulong len s +put o q get s i +q = add q 1 +end +ret q +end +fn itemidx item view uchar * to ulong +if same item al0 +ret 0 +end +if same item al1 +ret 1 +end +if same item al2 +ret 2 +end +if same item al3 +ret 3 +end +if same item al4 +ret 4 +end +if same item al5 +ret 5 +end +if same item al6 +ret 6 +end +if same item al7 +ret 7 +end +ret 8 +end +fn allergicto item view uchar * score ulong to bool +k = itemidx item +if eq k 8 +ret false +end +ret ne and shr score k 1 0 +end +fn appitem o edit uchar * p ulong k ulong to ulong +if eq k 0 +ret app o p al0 +end +if eq k 1 +ret app o p al1 +end +if eq k 2 +ret app o p al2 +end +if eq k 3 +ret app o p al3 +end +if eq k 4 +ret app o p al4 +end +if eq k 5 +ret app o p al5 +end +if eq k 6 +ret app o p al6 +end +ret app o p al7 +end +fn list score ulong o edit uchar * to ulong +p = ulong 0 +for k ulong 8 +if ne and shr score k 1 0 +if gt p 0 +put o p 44 +p = add p 1 +end +p = appitem o p k +end +end +ret p +end diff --git a/exercises/allergies/allergies.om b/exercises/allergies/allergies.om new file mode 100644 index 0000000..14d33e2 --- /dev/null +++ b/exercises/allergies/allergies.om @@ -0,0 +1,16 @@ +note Exercism-style exercise "allergies": https://exercism.org/tracks/python/exercises/allergies +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note The allergens in score order are eggs 1, peanuts 2, shellfish 4, strawberries 8, tomatoes 16, +note chocolate 32, pollen 64, cats 128; a score is the sum of the allergens (higher bits are ignored). +note allergicto is true when score includes item; list writes the allergens of score, in that order, +note joined by commas, into o and returns the number of bytes written. +note Check it with: sh exercise.sh exercises/allergies +om2 +fn allergicto item view uchar * score ulong to bool +note replace this line with your solution +trap 13 +end +fn list score ulong o edit uchar * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/allergies/test.om b/exercises/allergies/test.om new file mode 100644 index 0000000..af99d8d --- /dev/null +++ b/exercises/allergies/test.om @@ -0,0 +1,432 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/allergies/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 50 cases kept, 0 skipped +fn tbeq a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text s0 "eggs" +text c1 "allergicTo: testing for eggs allergy / not allergic to anything" +text s2 "eggs" +text c3 "allergicTo: testing for eggs allergy / allergic only to eggs" +text s4 "eggs" +text c5 "allergicTo: testing for eggs allergy / allergic to eggs and something else" +text s6 "eggs" +text c7 "allergicTo: testing for eggs allergy / allergic to something, but not eggs" +text s8 "eggs" +text c9 "allergicTo: testing for eggs allergy / allergic to everything" +text s10 "peanuts" +text c11 "allergicTo: testing for peanuts allergy / not allergic to anything" +text s12 "peanuts" +text c13 "allergicTo: testing for peanuts allergy / allergic only to peanuts" +text s14 "peanuts" +text c15 "allergicTo: testing for peanuts allergy / allergic to peanuts and something else" +text s16 "peanuts" +text c17 "allergicTo: testing for peanuts allergy / allergic to something, but not peanuts" +text s18 "peanuts" +text c19 "allergicTo: testing for peanuts allergy / allergic to everything" +text s20 "shellfish" +text c21 "allergicTo: testing for shellfish allergy / not allergic to anything" +text s22 "shellfish" +text c23 "allergicTo: testing for shellfish allergy / allergic only to shellfish" +text s24 "shellfish" +text c25 "allergicTo: testing for shellfish allergy / allergic to shellfish and something else" +text s26 "shellfish" +text c27 "allergicTo: testing for shellfish allergy / allergic to something, but not shellfish" +text s28 "shellfish" +text c29 "allergicTo: testing for shellfish allergy / allergic to everything" +text s30 "strawberries" +text c31 "allergicTo: testing for strawberries allergy / not allergic to anything" +text s32 "strawberries" +text c33 "allergicTo: testing for strawberries allergy / allergic only to strawberries" +text s34 "strawberries" +text c35 "allergicTo: testing for strawberries allergy / allergic to strawberries and something else" +text s36 "strawberries" +text c37 "allergicTo: testing for strawberries allergy / allergic to something, but not strawberries" +text s38 "strawberries" +text c39 "allergicTo: testing for strawberries allergy / allergic to everything" +text s40 "tomatoes" +text c41 "allergicTo: testing for tomatoes allergy / not allergic to anything" +text s42 "tomatoes" +text c43 "allergicTo: testing for tomatoes allergy / allergic only to tomatoes" +text s44 "tomatoes" +text c45 "allergicTo: testing for tomatoes allergy / allergic to tomatoes and something else" +text s46 "tomatoes" +text c47 "allergicTo: testing for tomatoes allergy / allergic to something, but not tomatoes" +text s48 "tomatoes" +text c49 "allergicTo: testing for tomatoes allergy / allergic to everything" +text s50 "chocolate" +text c51 "allergicTo: testing for chocolate allergy / not allergic to anything" +text s52 "chocolate" +text c53 "allergicTo: testing for chocolate allergy / allergic only to chocolate" +text s54 "chocolate" +text c55 "allergicTo: testing for chocolate allergy / allergic to chocolate and something else" +text s56 "chocolate" +text c57 "allergicTo: testing for chocolate allergy / allergic to something, but not chocolate" +text s58 "chocolate" +text c59 "allergicTo: testing for chocolate allergy / allergic to everything" +text s60 "pollen" +text c61 "allergicTo: testing for pollen allergy / not allergic to anything" +text s62 "pollen" +text c63 "allergicTo: testing for pollen allergy / allergic only to pollen" +text s64 "pollen" +text c65 "allergicTo: testing for pollen allergy / allergic to pollen and something else" +text s66 "pollen" +text c67 "allergicTo: testing for pollen allergy / allergic to something, but not pollen" +text s68 "pollen" +text c69 "allergicTo: testing for pollen allergy / allergic to everything" +text s70 "cats" +text c71 "allergicTo: testing for cats allergy / not allergic to anything" +text s72 "cats" +text c73 "allergicTo: testing for cats allergy / allergic only to cats" +text s74 "cats" +text c75 "allergicTo: testing for cats allergy / allergic to cats and something else" +text s76 "cats" +text c77 "allergicTo: testing for cats allergy / allergic to something, but not cats" +text s78 "cats" +text c79 "allergicTo: testing for cats allergy / allergic to everything" +text c80 "list: list when: / no allergies" +text e81 "eggs" +text c82 "list: list when: / just eggs" +text e83 "peanuts" +text c84 "list: list when: / just peanuts" +text e85 "strawberries" +text c86 "list: list when: / just strawberries" +text e87 "eggs,peanuts" +text c88 "list: list when: / eggs and peanuts" +text e89 "eggs,shellfish" +text c90 "list: list when: / more than eggs but not peanuts" +text e91 "strawberries,tomatoes,chocolate,pollen,cats" +text c92 "list: list when: / lots of stuff" +text e93 "eggs,peanuts,shellfish,strawberries,tomatoes,chocolate,pollen,cats" +text c94 "list: list when: / everything" +text e95 "eggs,shellfish,strawberries,tomatoes,chocolate,pollen,cats" +text c96 "list: list when: / no allergen score parts" +text e97 "eggs" +text c98 "list: list when: / no allergen score parts without highest valid score" +note canonical case 17fc7296-2440-4ac4-ad7b-d07c321bc5a0 +fn tc0 to bool +ret eq allergicto s0 0 false +end +note canonical case 07ced27b-1da5-4c2e-8ae2-cb2791437546 +fn tc1 to bool +ret eq allergicto s2 1 true +end +note canonical case 5035b954-b6fa-4b9b-a487-dae69d8c5f96 +fn tc2 to bool +ret eq allergicto s4 3 true +end +note canonical case 64a6a83a-5723-4b5b-a896-663307403310 +fn tc3 to bool +ret eq allergicto s6 2 false +end +note canonical case 90c8f484-456b-41c4-82ba-2d08d93231c6 +fn tc4 to bool +ret eq allergicto s8 255 true +end +note canonical case d266a59a-fccc-413b-ac53-d57cb1f0db9d +fn tc5 to bool +ret eq allergicto s10 0 false +end +note canonical case ea210a98-860d-46b2-a5bf-50d8995b3f2a +fn tc6 to bool +ret eq allergicto s12 2 true +end +note canonical case eac69ae9-8d14-4291-ac4b-7fd2c73d3a5b +fn tc7 to bool +ret eq allergicto s14 7 true +end +note canonical case 9152058c-ce39-4b16-9b1d-283ec6d25085 +fn tc8 to bool +ret eq allergicto s16 5 false +end +note canonical case d2d71fd8-63d5-40f9-a627-fbdaf88caeab +fn tc9 to bool +ret eq allergicto s18 255 true +end +note canonical case b948b0a1-cbf7-4b28-a244-73ff56687c80 +fn tc10 to bool +ret eq allergicto s20 0 false +end +note canonical case 9ce9a6f3-53e9-4923-85e0-73019047c567 +fn tc11 to bool +ret eq allergicto s22 4 true +end +note canonical case b272fca5-57ba-4b00-bd0c-43a737ab2131 +fn tc12 to bool +ret eq allergicto s24 14 true +end +note canonical case 21ef8e17-c227-494e-8e78-470a1c59c3d8 +fn tc13 to bool +ret eq allergicto s26 10 false +end +note canonical case cc789c19-2b5e-4c67-b146-625dc8cfa34e +fn tc14 to bool +ret eq allergicto s28 255 true +end +note canonical case 651bde0a-2a74-46c4-ab55-02a0906ca2f5 +fn tc15 to bool +ret eq allergicto s30 0 false +end +note canonical case b649a750-9703-4f5f-b7f7-91da2c160ece +fn tc16 to bool +ret eq allergicto s32 8 true +end +note canonical case 50f5f8f3-3bac-47e6-8dba-2d94470a4bc6 +fn tc17 to bool +ret eq allergicto s34 28 true +end +note canonical case 23dd6952-88c9-48d7-a7d5-5d0343deb18d +fn tc18 to bool +ret eq allergicto s36 20 false +end +note canonical case 74afaae2-13b6-43a2-837a-286cd42e7d7e +fn tc19 to bool +ret eq allergicto s38 255 true +end +note canonical case c49a91ef-6252-415e-907e-a9d26ef61723 +fn tc20 to bool +ret eq allergicto s40 0 false +end +note canonical case b69c5131-b7d0-41ad-a32c-e1b2cc632df8 +fn tc21 to bool +ret eq allergicto s42 16 true +end +note canonical case 1ca50eb1-f042-4ccf-9050-341521b929ec +fn tc22 to bool +ret eq allergicto s44 56 true +end +note canonical case e9846baa-456b-4eff-8025-034b9f77bd8e +fn tc23 to bool +ret eq allergicto s46 40 false +end +note canonical case b2414f01-f3ad-4965-8391-e65f54dad35f +fn tc24 to bool +ret eq allergicto s48 255 true +end +note canonical case 978467ab-bda4-49f7-b004-1d011ead947c +fn tc25 to bool +ret eq allergicto s50 0 false +end +note canonical case 59cf4e49-06ea-4139-a2c1-d7aad28f8cbc +fn tc26 to bool +ret eq allergicto s52 32 true +end +note canonical case b0a7c07b-2db7-4f73-a180-565e07040ef1 +fn tc27 to bool +ret eq allergicto s54 112 true +end +note canonical case f5506893-f1ae-482a-b516-7532ba5ca9d2 +fn tc28 to bool +ret eq allergicto s56 80 false +end +note canonical case 02debb3d-d7e2-4376-a26b-3c974b6595c6 +fn tc29 to bool +ret eq allergicto s58 255 true +end +note canonical case 17f4a42b-c91e-41b8-8a76-4797886c2d96 +fn tc30 to bool +ret eq allergicto s60 0 false +end +note canonical case 7696eba7-1837-4488-882a-14b7b4e3e399 +fn tc31 to bool +ret eq allergicto s62 64 true +end +note canonical case 9a49aec5-fa1f-405d-889e-4dfc420db2b6 +fn tc32 to bool +ret eq allergicto s64 224 true +end +note canonical case 3cb8e79f-d108-4712-b620-aa146b1954a9 +fn tc33 to bool +ret eq allergicto s66 160 false +end +note canonical case 1dc3fe57-7c68-4043-9d51-5457128744b2 +fn tc34 to bool +ret eq allergicto s68 255 true +end +note canonical case d3f523d6-3d50-419b-a222-d4dfd62ce314 +fn tc35 to bool +ret eq allergicto s70 0 false +end +note canonical case eba541c3-c886-42d3-baef-c048cb7fcd8f +fn tc36 to bool +ret eq allergicto s72 128 true +end +note canonical case ba718376-26e0-40b7-bbbe-060287637ea5 +fn tc37 to bool +ret eq allergicto s74 192 true +end +note canonical case 3c6dbf4a-5277-436f-8b88-15a206f2d6c4 +fn tc38 to bool +ret eq allergicto s76 64 false +end +note canonical case 1faabb05-2b98-4995-9046-d83e4a48a7c1 +fn tc39 to bool +ret eq allergicto s78 255 true +end +note canonical case f9c1b8e7-7dc5-4887-aa93-cebdcc29dd8f +fn tc40 to bool +o = new uchar 1024 0 +n = list 0 o +ret eq n 0 +end +note canonical case 9e1a4364-09a6-4d94-990f-541a94a4c1e8 +fn tc41 to bool +o = new uchar 1024 0 +n = list 1 o +ret tbeq part o 0 n e81 +end +note canonical case 8851c973-805e-4283-9e01-d0c0da0e4695 +fn tc42 to bool +o = new uchar 1024 0 +n = list 2 o +ret tbeq part o 0 n e83 +end +note canonical case 2c8943cb-005e-435f-ae11-3e8fb558ea98 +fn tc43 to bool +o = new uchar 1024 0 +n = list 8 o +ret tbeq part o 0 n e85 +end +note canonical case 6fa95d26-044c-48a9-8a7b-9ee46ec32c5c +fn tc44 to bool +o = new uchar 1024 0 +n = list 3 o +ret tbeq part o 0 n e87 +end +note canonical case 19890e22-f63f-4c5c-a9fb-fb6eacddfe8e +fn tc45 to bool +o = new uchar 1024 0 +n = list 5 o +ret tbeq part o 0 n e89 +end +note canonical case 4b68f470-067c-44e4-889f-c9fe28917d2f +fn tc46 to bool +o = new uchar 1024 0 +n = list 248 o +ret tbeq part o 0 n e91 +end +note canonical case 0881b7c5-9efa-4530-91bd-68370d054bc7 +fn tc47 to bool +o = new uchar 1024 0 +n = list 255 o +ret tbeq part o 0 n e93 +end +note canonical case 12ce86de-b347-42a0-ab7c-2e0570f0c65b +fn tc48 to bool +o = new uchar 1024 0 +n = list 509 o +ret tbeq part o 0 n e95 +end +note canonical case 93c2df3e-4f55-4fed-8116-7513092819cd +fn tc49 to bool +o = new uchar 1024 0 +n = list 257 o +ret tbeq part o 0 n e97 +end +fn main to int +tcheck tc0 c1 +tcheck tc1 c3 +tcheck tc2 c5 +tcheck tc3 c7 +tcheck tc4 c9 +tcheck tc5 c11 +tcheck tc6 c13 +tcheck tc7 c15 +tcheck tc8 c17 +tcheck tc9 c19 +tcheck tc10 c21 +tcheck tc11 c23 +tcheck tc12 c25 +tcheck tc13 c27 +tcheck tc14 c29 +tcheck tc15 c31 +tcheck tc16 c33 +tcheck tc17 c35 +tcheck tc18 c37 +tcheck tc19 c39 +tcheck tc20 c41 +tcheck tc21 c43 +tcheck tc22 c45 +tcheck tc23 c47 +tcheck tc24 c49 +tcheck tc25 c51 +tcheck tc26 c53 +tcheck tc27 c55 +tcheck tc28 c57 +tcheck tc29 c59 +tcheck tc30 c61 +tcheck tc31 c63 +tcheck tc32 c65 +tcheck tc33 c67 +tcheck tc34 c69 +tcheck tc35 c71 +tcheck tc36 c73 +tcheck tc37 c75 +tcheck tc38 c77 +tcheck tc39 c79 +tcheck tc40 c80 +tcheck tc41 c82 +tcheck tc42 c84 +tcheck tc43 c86 +tcheck tc44 c88 +tcheck tc45 c90 +tcheck tc46 c92 +tcheck tc47 c94 +tcheck tc48 c96 +tcheck tc49 c98 +ret int get tfails 0 +end diff --git a/exercises/atbash-cipher/.meta/example.om b/exercises/atbash-cipher/.meta/example.om new file mode 100644 index 0000000..dfadbc2 --- /dev/null +++ b/exercises/atbash-cipher/.meta/example.om @@ -0,0 +1,55 @@ +note Exercism-style exercise "atbash-cipher": https://exercism.org/tracks/python/exercises/atbash-cipher +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note encode writes the Atbash cipher of s into o (a to z, b to y, ...; letters lower case, digits +note kept, everything else dropped) in groups of 5 separated by one space; decode writes the plain +note text of s (spaces dropped). Both return the number of bytes written. +note Check it with: sh exercise.sh exercises/atbash-cipher +om2 +fn encode s view uchar * o edit uchar * to ulong +p = ulong 0 +k = ulong 0 +for i ulong len s +c = get s i +if and ge c 65 le c 90 +c = add c 32 +end +keep = false +if and ge c 97 le c 122 +c = sub 219 c +keep = true +else +if and ge c 48 le c 57 +keep = true +end +end +if keep +if and gt k 0 eq rem k 5 0 +put o p 32 +p = add p 1 +end +put o p c +p = add p 1 +k = add k 1 +end +end +ret p +end +fn decode s view uchar * o edit uchar * to ulong +p = ulong 0 +for i ulong len s +c = get s i +if and ge c 65 le c 90 +c = add c 32 +end +if and ge c 97 le c 122 +put o p sub 219 c +p = add p 1 +else +if and ge c 48 le c 57 +put o p c +p = add p 1 +end +end +end +ret p +end diff --git a/exercises/atbash-cipher/atbash-cipher.om b/exercises/atbash-cipher/atbash-cipher.om new file mode 100644 index 0000000..d1ddbb1 --- /dev/null +++ b/exercises/atbash-cipher/atbash-cipher.om @@ -0,0 +1,15 @@ +note Exercism-style exercise "atbash-cipher": https://exercism.org/tracks/python/exercises/atbash-cipher +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note encode writes the Atbash cipher of s into o (a to z, b to y, ...; letters lower case, digits +note kept, everything else dropped) in groups of 5 separated by one space; decode writes the plain +note text of s (spaces dropped). Both return the number of bytes written. +note Check it with: sh exercise.sh exercises/atbash-cipher +om2 +fn encode s view uchar * o edit uchar * to ulong +note replace this line with your solution +trap 13 +end +fn decode s view uchar * o edit uchar * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/atbash-cipher/test.om b/exercises/atbash-cipher/test.om new file mode 100644 index 0000000..18bb836 --- /dev/null +++ b/exercises/atbash-cipher/test.om @@ -0,0 +1,203 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/atbash-cipher/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 14 cases kept, 0 skipped +fn tbeq a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text s0 "yes" +text e1 "bvh" +text c2 "encode: encode / encode yes" +text s3 "no" +text e4 "ml" +text c5 "encode: encode / encode no" +text s6 "OMG" +text e7 "lnt" +text c8 "encode: encode / encode OMG" +text s9 "O M G" +text e10 "lnt" +text c11 "encode: encode / encode spaces" +text s12 "mindblowingly" +text e13 "nrmwy oldrm tob" +text c14 "encode: encode / encode mindblowingly" +text s15 "Testing,1 2 3, testing." +text e16 "gvhgr mt123 gvhgr mt" +text c17 "encode: encode / encode numbers" +text s18 "Truth is fiction." +text e19 "gifgs rhurx grlm" +text c20 "encode: encode / encode deep thought" +text s21 "The quick brown fox jumps over the lazy dog." +text e22 "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt" +text c23 "encode: encode / encode all the letters" +text s24 "vcvix rhn" +text e25 "exercism" +text c26 "decode: decode / decode exercism" +text s27 "zmlyh gzxov rhlug vmzhg vkkrm thglm v" +text e28 "anobstacleisoftenasteppingstone" +text c29 "decode: decode / decode a sentence" +text s30 "gvhgr mt123 gvhgr mt" +text e31 "testing123testing" +text c32 "decode: decode / decode numbers" +text s33 "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt" +text e34 "thequickbrownfoxjumpsoverthelazydog" +text c35 "decode: decode / decode all the letters" +text s36 "vc vix r hn" +text e37 "exercism" +text c38 "decode: decode / decode with too many spaces" +text s39 "zmlyhgzxovrhlugvmzhgvkkrmthglmv" +text e40 "anobstacleisoftenasteppingstone" +text c41 "decode: decode / decode with no spaces" +note canonical case 2f47ebe1-eab9-4d6b-b3c6-627562a31c77 +fn tc0 to bool +o = new uchar 1024 0 +n = encode s0 o +ret tbeq part o 0 n e1 +end +note canonical case b4ffe781-ea81-4b74-b268-cc58ba21c739 +fn tc1 to bool +o = new uchar 1024 0 +n = encode s3 o +ret tbeq part o 0 n e4 +end +note canonical case 10e48927-24ab-4c4d-9d3f-3067724ace00 +fn tc2 to bool +o = new uchar 1024 0 +n = encode s6 o +ret tbeq part o 0 n e7 +end +note canonical case d59b8bc3-509a-4a9a-834c-6f501b98750b +fn tc3 to bool +o = new uchar 1024 0 +n = encode s9 o +ret tbeq part o 0 n e10 +end +note canonical case 31d44b11-81b7-4a94-8b43-4af6a2449429 +fn tc4 to bool +o = new uchar 1024 0 +n = encode s12 o +ret tbeq part o 0 n e13 +end +note canonical case d503361a-1433-48c0-aae0-d41b5baa33ff +fn tc5 to bool +o = new uchar 1024 0 +n = encode s15 o +ret tbeq part o 0 n e16 +end +note canonical case 79c8a2d5-0772-42d4-b41b-531d0b5da926 +fn tc6 to bool +o = new uchar 1024 0 +n = encode s18 o +ret tbeq part o 0 n e19 +end +note canonical case 9ca13d23-d32a-4967-a1fd-6100b8742bab +fn tc7 to bool +o = new uchar 1024 0 +n = encode s21 o +ret tbeq part o 0 n e22 +end +note canonical case bb50e087-7fdf-48e7-9223-284fe7e69851 +fn tc8 to bool +o = new uchar 1024 0 +n = decode s24 o +ret tbeq part o 0 n e25 +end +note canonical case ac021097-cd5d-4717-8907-b0814b9e292c +fn tc9 to bool +o = new uchar 1024 0 +n = decode s27 o +ret tbeq part o 0 n e28 +end +note canonical case 18729de3-de74-49b8-b68c-025eaf77f851 +fn tc10 to bool +o = new uchar 1024 0 +n = decode s30 o +ret tbeq part o 0 n e31 +end +note canonical case 0f30325f-f53b-415d-ad3e-a7a4f63de034 +fn tc11 to bool +o = new uchar 1024 0 +n = decode s33 o +ret tbeq part o 0 n e34 +end +note canonical case 39640287-30c6-4c8c-9bac-9d613d1a5674 +fn tc12 to bool +o = new uchar 1024 0 +n = decode s36 o +ret tbeq part o 0 n e37 +end +note canonical case b34edf13-34c0-49b5-aa21-0768928000d5 +fn tc13 to bool +o = new uchar 1024 0 +n = decode s39 o +ret tbeq part o 0 n e40 +end +fn main to int +tcheck tc0 c2 +tcheck tc1 c5 +tcheck tc2 c8 +tcheck tc3 c11 +tcheck tc4 c14 +tcheck tc5 c17 +tcheck tc6 c20 +tcheck tc7 c23 +tcheck tc8 c26 +tcheck tc9 c29 +tcheck tc10 c32 +tcheck tc11 c35 +tcheck tc12 c38 +tcheck tc13 c41 +ret int get tfails 0 +end diff --git a/exercises/binary-search/.meta/example.om b/exercises/binary-search/.meta/example.om new file mode 100644 index 0000000..c25f3a3 --- /dev/null +++ b/exercises/binary-search/.meta/example.om @@ -0,0 +1,23 @@ +note Exercism-style exercise "binary-search": https://exercism.org/tracks/python/exercises/binary-search +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note find is the index of v in the ascending array a, found by binary search, or -1 when v is not +note in a. +note Check it with: sh exercise.sh exercises/binary-search +om2 +fn find a view ulong * v ulong to long +lo = ulong 0 +hi = len a +while lt lo hi bound 64 +m = add lo div sub hi lo 2 +x = get a m +if eq x v +ret long m +end +if lt x v +lo = add m 1 +else +hi = m +end +end +ret -1 +end diff --git a/exercises/binary-search/binary-search.om b/exercises/binary-search/binary-search.om new file mode 100644 index 0000000..f598143 --- /dev/null +++ b/exercises/binary-search/binary-search.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "binary-search": https://exercism.org/tracks/python/exercises/binary-search +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note find is the index of v in the ascending array a, found by binary search, or -1 when v is not +note in a. +note Check it with: sh exercise.sh exercises/binary-search +om2 +fn find a view ulong * v ulong to long +note replace this line with your solution +trap 13 +end diff --git a/exercises/binary-search/test.om b/exercises/binary-search/test.om new file mode 100644 index 0000000..908ae2b --- /dev/null +++ b/exercises/binary-search/test.om @@ -0,0 +1,199 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/binary-search/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 11 cases kept, 0 skipped +text c0 "find: finds a value in an array with one element" +text c1 "find: finds a value in the middle of an array" +text c2 "find: finds a value at the beginning of an array" +text c3 "find: finds a value at the end of an array" +text c4 "find: finds a value in an array of odd length" +text c5 "find: finds a value in an array of even length" +text c6 "find: identifies that a value is not included in the array" +text c7 "find: a value smaller than the array's smallest value is not found" +text c8 "find: a value larger than the array's largest value is not found" +text c9 "find: nothing is found in an empty array" +text c10 "find: nothing is found when the left and right bounds cross" +note canonical case b55c24a9-a98d-4379-a08c-2adcf8ebeee8 +fn tc0 to bool +a0 = new ulong 1 0 +put a0 0 6 +ret eq find a0 6 0 +end +note canonical case 73469346-b0a0-4011-89bf-989e443d503d +fn tc1 to bool +a0 = new ulong 7 0 +put a0 0 1 +put a0 1 3 +put a0 2 4 +put a0 3 6 +put a0 4 8 +put a0 5 9 +put a0 6 11 +ret eq find a0 6 3 +end +note canonical case 327bc482-ab85-424e-a724-fb4658e66ddb +fn tc2 to bool +a0 = new ulong 7 0 +put a0 0 1 +put a0 1 3 +put a0 2 4 +put a0 3 6 +put a0 4 8 +put a0 5 9 +put a0 6 11 +ret eq find a0 1 0 +end +note canonical case f9f94b16-fe5e-472c-85ea-c513804c7d59 +fn tc3 to bool +a0 = new ulong 7 0 +put a0 0 1 +put a0 1 3 +put a0 2 4 +put a0 3 6 +put a0 4 8 +put a0 5 9 +put a0 6 11 +ret eq find a0 11 6 +end +note canonical case f0068905-26e3-4342-856d-ad153cadb338 +fn tc4 to bool +a0 = new ulong 13 0 +put a0 0 1 +put a0 1 3 +put a0 2 5 +put a0 3 8 +put a0 4 13 +put a0 5 21 +put a0 6 34 +put a0 7 55 +put a0 8 89 +put a0 9 144 +put a0 10 233 +put a0 11 377 +put a0 12 634 +ret eq find a0 144 9 +end +note canonical case fc316b12-c8b3-4f5e-9e89-532b3389de8c +fn tc5 to bool +a0 = new ulong 12 0 +put a0 0 1 +put a0 1 3 +put a0 2 5 +put a0 3 8 +put a0 4 13 +put a0 5 21 +put a0 6 34 +put a0 7 55 +put a0 8 89 +put a0 9 144 +put a0 10 233 +put a0 11 377 +ret eq find a0 21 5 +end +note canonical case da7db20a-354f-49f7-a6a1-650a54998aa6 +fn tc6 to bool +a0 = new ulong 7 0 +put a0 0 1 +put a0 1 3 +put a0 2 4 +put a0 3 6 +put a0 4 8 +put a0 5 9 +put a0 6 11 +ret eq find a0 7 -1 +end +note canonical case 95d869ff-3daf-4c79-b622-6e805c675f97 +fn tc7 to bool +a0 = new ulong 7 0 +put a0 0 1 +put a0 1 3 +put a0 2 4 +put a0 3 6 +put a0 4 8 +put a0 5 9 +put a0 6 11 +ret eq find a0 0 -1 +end +note canonical case 8b24ef45-6e51-4a94-9eac-c2bf38fdb0ba +fn tc8 to bool +a0 = new ulong 7 0 +put a0 0 1 +put a0 1 3 +put a0 2 4 +put a0 3 6 +put a0 4 8 +put a0 5 9 +put a0 6 11 +ret eq find a0 13 -1 +end +note canonical case f439a0fa-cf42-4262-8ad1-64bf41ce566a +fn tc9 to bool +a0 = new ulong 1 0 +ret eq find part a0 0 0 1 -1 +end +note canonical case 2c353967-b56d-40b8-acff-ce43115eed64 +fn tc10 to bool +a0 = new ulong 2 0 +put a0 0 1 +put a0 1 2 +ret eq find a0 0 -1 +end +fn main to int +tcheck tc0 c0 +tcheck tc1 c1 +tcheck tc2 c2 +tcheck tc3 c3 +tcheck tc4 c4 +tcheck tc5 c5 +tcheck tc6 c6 +tcheck tc7 c7 +tcheck tc8 c8 +tcheck tc9 c9 +tcheck tc10 c10 +ret int get tfails 0 +end diff --git a/exercises/bob/.meta/example.om b/exercises/bob/.meta/example.om new file mode 100644 index 0000000..b9472ee --- /dev/null +++ b/exercises/bob/.meta/example.om @@ -0,0 +1,60 @@ +note Exercism-style exercise "bob": https://exercism.org/tracks/python/exercises/bob +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note response writes Bob's reply to s into o and returns its length. Silence (only spaces, tabs, +note CR, LF): "Fine. Be that way!". A yelled question: "Calm down, I know what I'm doing!". Yelling +note (some letter, no lower-case letter): "Whoa, chill out!". A question (last non-space byte is +note "?"): "Sure.". Anything else: "Whatever.". +note Check it with: sh exercise.sh exercises/bob +om2 +text bfine "Fine. Be that way!" +text bcalm "Calm down, I know what I'm doing!" +text bwhoa "Whoa, chill out!" +text bsure "Sure." +text bwhat "Whatever." +fn app o edit uchar * p ulong s view uchar * to ulong +q = p +for i ulong len s +put o q get s i +q = add q 1 +end +ret q +end +fn lastbyte s view uchar * to uchar +last = uchar 0 +for i ulong len s +c = get s i +if not or or or eq c 32 eq c 9 eq c 10 eq c 13 +last = c +end +end +ret last +end +fn hasrange s view uchar * lo uchar hi uchar to bool +for i ulong len s +c = get s i +if and ge c lo le c hi +ret true +end +end +ret false +end +fn response s view uchar * o edit uchar * to ulong +last = lastbyte s +up = hasrange s 65 90 +lo = hasrange s 97 122 +if eq last 0 +ret app o 0 bfine +end +yell = and up not lo +q = eq last 63 +if and yell q +ret app o 0 bcalm +end +if yell +ret app o 0 bwhoa +end +if q +ret app o 0 bsure +end +ret app o 0 bwhat +end diff --git a/exercises/bob/bob.om b/exercises/bob/bob.om new file mode 100644 index 0000000..3042fc9 --- /dev/null +++ b/exercises/bob/bob.om @@ -0,0 +1,12 @@ +note Exercism-style exercise "bob": https://exercism.org/tracks/python/exercises/bob +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note response writes Bob's reply to s into o and returns its length. Silence (only spaces, tabs, +note CR, LF): "Fine. Be that way!". A yelled question: "Calm down, I know what I'm doing!". Yelling +note (some letter, no lower-case letter): "Whoa, chill out!". A question (last non-space byte is +note "?"): "Sure.". Anything else: "Whatever.". +note Check it with: sh exercise.sh exercises/bob +om2 +fn response s view uchar * o edit uchar * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/bob/test.om b/exercises/bob/test.om new file mode 100644 index 0000000..7f1043c --- /dev/null +++ b/exercises/bob/test.om @@ -0,0 +1,327 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/bob/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 26 cases kept, 0 skipped, 1 superseded (reimplemented) cases omitted +fn tbeq a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text s0 "Does this cryogenic chamber make me look fat?" +text e1 "Sure." +text c2 "response: asking a question" +text s3 "WATCH OUT!" +text e4 "Whoa, chill out!" +text c5 "response: shouting" +text s6 "WHAT'S GOING ON?" +text e7 "Calm down, I know what I'm doing!" +text c8 "response: forceful question" +text z9 "z" +text e10 "Fine. Be that way!" +text c11 "response: silence" +text s12 "Tom-ay-to, tom-aaaah-to." +text e13 "Whatever." +text c14 "response: stating something" +text s15 "You are, what, like 15?" +text e16 "Sure." +text c17 "response: asking a numeric question" +text s18 "fffbbcbeab?" +text e19 "Sure." +text c20 "response: asking gibberish" +text s21 "4?" +text e22 "Sure." +text c23 "response: question with no letters" +text s24 ":) ?" +text e25 "Sure." +text c26 "response: non-letters with question" +text s27 "Wait! Hang on. Are you going to be OK?" +text e28 "Sure." +text c29 "response: prattling on" +text s30 "Okay if like my spacebar quite a bit? " +text e31 "Sure." +text c32 "response: ending with whitespace" +text s33 "\nDoes this cryogenic chamber make\n me look fat?" +text e34 "Sure." +text c35 "response: multiple line question" +text s36 "FCECDFCAAB" +text e37 "Whoa, chill out!" +text c38 "response: shouting gibberish" +text s39 "DO LIONS EAT PEOPLE? AHHHHH." +text e40 "Whoa, chill out!" +text c41 "response: shouting a statement containing a question mark" +text s42 "1, 2, 3 GO!" +text e43 "Whoa, chill out!" +text c44 "response: shouting numbers" +text s45 "ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!" +text e46 "Whoa, chill out!" +text c47 "response: shouting with special characters" +text s48 "I HATE THE DENTIST" +text e49 "Whoa, chill out!" +text c50 "response: shouting with no exclamation mark" +text s51 " " +text e52 "Fine. Be that way!" +text c53 "response: prolonged silence" +text s54 "\t\t\t\t\t\t\t\t\t\t" +text e55 "Fine. Be that way!" +text c56 "response: alternate silence" +text e57 "Fine. Be that way!" +text c58 "response: other whitespace" +text s59 "Hi there!" +text e60 "Whatever." +text c61 "response: talking forcefully" +text s62 "It's OK if you don't want to go work for NASA." +text e63 "Whatever." +text c64 "response: using acronyms in regular speech" +text s65 "1, 2, 3" +text e66 "Whatever." +text c67 "response: no letters" +text s68 "Ending with ? means a question." +text e69 "Whatever." +text c70 "response: statement containing question mark" +text s71 " hmmmmmmm..." +text e72 "Whatever." +text c73 "response: starting with whitespace" +text s74 "This is a statement ending with whitespace " +text e75 "Whatever." +text c76 "response: non-question ending with whitespace" +note canonical case 8a2e771d-d6f1-4e3f-b6c6-b41495556e37 +fn tc0 to bool +o = new uchar 1024 0 +n = response s0 o +ret tbeq part o 0 n e1 +end +note canonical case 73a966dc-8017-47d6-bb32-cf07d1a5fcd9 +fn tc1 to bool +o = new uchar 1024 0 +n = response s3 o +ret tbeq part o 0 n e4 +end +note canonical case a5193c61-4a92-4f68-93e2-f554eb385ec6 +fn tc2 to bool +o = new uchar 1024 0 +n = response s6 o +ret tbeq part o 0 n e7 +end +note canonical case bc39f7c6-f543-41be-9a43-fd1c2f753fc0 +fn tc3 to bool +o = new uchar 1024 0 +n = response part z9 0 0 o +ret tbeq part o 0 n e10 +end +note canonical case e162fead-606f-437a-a166-d051915cea8e +fn tc4 to bool +o = new uchar 1024 0 +n = response s12 o +ret tbeq part o 0 n e13 +end +note canonical case 81080c62-4e4d-4066-b30a-48d8d76920d9 +fn tc5 to bool +o = new uchar 1024 0 +n = response s15 o +ret tbeq part o 0 n e16 +end +note canonical case 2a02716d-685b-4e2e-a804-2adaf281c01e +fn tc6 to bool +o = new uchar 1024 0 +n = response s18 o +ret tbeq part o 0 n e19 +end +note canonical case bb0011c5-cd52-4a5b-8bfb-a87b6283b0e2 +fn tc7 to bool +o = new uchar 1024 0 +n = response s21 o +ret tbeq part o 0 n e22 +end +note canonical case 9bfc677d-ea3a-45f2-be44-35bc8fa3753e +fn tc8 to bool +o = new uchar 1024 0 +n = response s24 o +ret tbeq part o 0 n e25 +end +note canonical case 8608c508-f7de-4b17-985b-811878b3cf45 +fn tc9 to bool +o = new uchar 1024 0 +n = response s27 o +ret tbeq part o 0 n e28 +end +note canonical case 05b304d6-f83b-46e7-81e0-4cd3ca647900 +fn tc10 to bool +o = new uchar 1024 0 +n = response s30 o +ret tbeq part o 0 n e31 +end +note canonical case 2c7278ac-f955-4eb4-bf8f-e33eb4116a15 +fn tc11 to bool +o = new uchar 1024 0 +n = response s33 o +ret tbeq part o 0 n e34 +end +note canonical case d6c98afd-df35-4806-b55e-2c457c3ab748 +fn tc12 to bool +o = new uchar 1024 0 +n = response s36 o +ret tbeq part o 0 n e37 +end +note canonical case 3c954328-86fb-4c71-8961-e18d6a5e2517 +fn tc13 to bool +o = new uchar 1024 0 +n = response s39 o +ret tbeq part o 0 n e40 +end +note canonical case a20e0c54-2224-4dde-8b10-bd2cdd4f61bc +fn tc14 to bool +o = new uchar 1024 0 +n = response s42 o +ret tbeq part o 0 n e43 +end +note canonical case 496143c8-1c31-4c01-8a08-88427af85c66 +fn tc15 to bool +o = new uchar 1024 0 +n = response s45 o +ret tbeq part o 0 n e46 +end +note canonical case e6793c1c-43bd-4b8d-bc11-499aea73925f +fn tc16 to bool +o = new uchar 1024 0 +n = response s48 o +ret tbeq part o 0 n e49 +end +note canonical case d6c47565-372b-4b09-b1dd-c40552b8378b +fn tc17 to bool +o = new uchar 1024 0 +n = response s51 o +ret tbeq part o 0 n e52 +end +note canonical case 4428f28d-4100-4d85-a902-e5a78cb0ecd3 +fn tc18 to bool +o = new uchar 1024 0 +n = response s54 o +ret tbeq part o 0 n e55 +end +note canonical case 72bd5ad3-9b2f-4931-a988-dce1f5771de2 +fn tc19 to bool +a0 = new uchar 4 0 +put a0 0 10 +put a0 1 13 +put a0 2 32 +put a0 3 9 +o = new uchar 1024 0 +n = response a0 o +ret tbeq part o 0 n e57 +end +note canonical case c02f9179-ab16-4aa7-a8dc-940145c385f7 +fn tc20 to bool +o = new uchar 1024 0 +n = response s59 o +ret tbeq part o 0 n e60 +end +note canonical case 153c0e25-9bb5-4ec5-966e-598463658bcd +fn tc21 to bool +o = new uchar 1024 0 +n = response s62 o +ret tbeq part o 0 n e63 +end +note canonical case f7bc4b92-bdff-421e-a238-ae97f230ccac +fn tc22 to bool +o = new uchar 1024 0 +n = response s65 o +ret tbeq part o 0 n e66 +end +note canonical case aa8097cc-c548-4951-8856-14a404dd236a +fn tc23 to bool +o = new uchar 1024 0 +n = response s68 o +ret tbeq part o 0 n e69 +end +note canonical case 5371ef75-d9ea-4103-bcfa-2da973ddec1b +fn tc24 to bool +o = new uchar 1024 0 +n = response s71 o +ret tbeq part o 0 n e72 +end +note canonical case 12983553-8601-46a8-92fa-fcaa3bc4a2a0 +fn tc25 to bool +o = new uchar 1024 0 +n = response s74 o +ret tbeq part o 0 n e75 +end +fn main to int +tcheck tc0 c2 +tcheck tc1 c5 +tcheck tc2 c8 +tcheck tc3 c11 +tcheck tc4 c14 +tcheck tc5 c17 +tcheck tc6 c20 +tcheck tc7 c23 +tcheck tc8 c26 +tcheck tc9 c29 +tcheck tc10 c32 +tcheck tc11 c35 +tcheck tc12 c38 +tcheck tc13 c41 +tcheck tc14 c44 +tcheck tc15 c47 +tcheck tc16 c50 +tcheck tc17 c53 +tcheck tc18 c56 +tcheck tc19 c58 +tcheck tc20 c61 +tcheck tc21 c64 +tcheck tc22 c67 +tcheck tc23 c70 +tcheck tc24 c73 +tcheck tc25 c76 +ret int get tfails 0 +end diff --git a/exercises/change/.meta/example.om b/exercises/change/.meta/example.om new file mode 100644 index 0000000..a0fa115 --- /dev/null +++ b/exercises/change/.meta/example.om @@ -0,0 +1,64 @@ +note Exercism-style exercise "change": https://exercism.org/tracks/python/exercises/change +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note findfewestcoins writes the fewest coins from the denominations coins (each usable any number +note of times) that add up to target (at most 1000) into o, in ascending order, and returns how many; +note or -1 when target cannot be made, -2 when target is negative. +note Check it with: sh exercise.sh exercises/change +om2 +const CMAX ulong 1001 +const CINF ulong 1000000 +global cbest ulong CMAX +global clast ulong CMAX +fn fill coins view ulong * t ulong to void +put cbest 0 0 +for v ulong t +w = add v 1 +put cbest w CINF +for j ulong len coins +c = get coins j +if and gt c 0 le c w +b = add get cbest sub w c 1 +if lt b get cbest w +put cbest w b +put clast w c +end +end +end +end +end +fn findfewestcoins coins view ulong * target long o edit ulong * to long +if lt target 0 +ret -2 +end +t = ulong target +fill coins t +if ge get cbest t CINF +ret -1 +end +k = trace o t +sortasc o k +ret long k +end +fn trace o edit ulong * t ulong to ulong +k = ulong 0 +r = t +while gt r 0 bound 1001 +c = get clast r +put o k c +k = add k 1 +r = sub r c +end +ret k +end +fn sortasc o edit ulong * k ulong to void +for a ulong k +for b ulong sub sub k 1 a +x = get o b +y = get o add b 1 +if gt x y +put o b y +put o add b 1 x +end +end +end +end diff --git a/exercises/change/change.om b/exercises/change/change.om new file mode 100644 index 0000000..560794e --- /dev/null +++ b/exercises/change/change.om @@ -0,0 +1,11 @@ +note Exercism-style exercise "change": https://exercism.org/tracks/python/exercises/change +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note findfewestcoins writes the fewest coins from the denominations coins (each usable any number +note of times) that add up to target (at most 1000) into o, in ascending order, and returns how many; +note or -1 when target cannot be made, -2 when target is negative. +note Check it with: sh exercise.sh exercises/change +om2 +fn findfewestcoins coins view ulong * target long o edit ulong * to long +note replace this line with your solution +trap 13 +end diff --git a/exercises/change/test.om b/exercises/change/test.om new file mode 100644 index 0000000..984b52d --- /dev/null +++ b/exercises/change/test.om @@ -0,0 +1,307 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/change/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 13 cases kept, 0 skipped +fn tueq a view ulong * b view ulong * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text c0 "findFewestCoins: change for 1 cent" +text c1 "findFewestCoins: single coin change" +text c2 "findFewestCoins: multiple coin change" +text c3 "findFewestCoins: change with Lilliputian Coins" +text c4 "findFewestCoins: change with Lower Elbonia Coins" +text c5 "findFewestCoins: large target values" +text c6 "findFewestCoins: possible change without unit coins available" +text c7 "findFewestCoins: another possible change without unit coins available" +text c8 "findFewestCoins: a greedy approach is not optimal" +text c9 "findFewestCoins: no coins make 0 change" +text c10 "findFewestCoins: error testing for change smaller than the smallest of coins" +text c11 "findFewestCoins: error if no combination can add up to target" +text c12 "findFewestCoins: cannot find negative change values" +note canonical case d0ebd0e1-9d27-4609-a654-df5c0ba1d83a +fn tc0 to bool +a0 = new ulong 4 0 +put a0 0 1 +put a0 1 5 +put a0 2 10 +put a0 3 25 +o = new ulong 256 0 +n = findfewestcoins a0 1 o +if lt n 0 +ret false +end +a1 = new ulong 1 0 +put a1 0 1 +ret tueq part o 0 ulong n a1 +end +note canonical case 36887bea-7f92-4a9c-b0cc-c0e886b3ecc8 +fn tc1 to bool +a0 = new ulong 5 0 +put a0 0 1 +put a0 1 5 +put a0 2 10 +put a0 3 25 +put a0 4 100 +o = new ulong 256 0 +n = findfewestcoins a0 25 o +if lt n 0 +ret false +end +a1 = new ulong 1 0 +put a1 0 25 +ret tueq part o 0 ulong n a1 +end +note canonical case cef21ccc-0811-4e6e-af44-f011e7eab6c6 +fn tc2 to bool +a0 = new ulong 5 0 +put a0 0 1 +put a0 1 5 +put a0 2 10 +put a0 3 25 +put a0 4 100 +o = new ulong 256 0 +n = findfewestcoins a0 15 o +if lt n 0 +ret false +end +a1 = new ulong 2 0 +put a1 0 5 +put a1 1 10 +ret tueq part o 0 ulong n a1 +end +note canonical case d60952bc-0c1a-4571-bf0c-41be72690cb3 +fn tc3 to bool +a0 = new ulong 5 0 +put a0 0 1 +put a0 1 4 +put a0 2 15 +put a0 3 20 +put a0 4 50 +o = new ulong 256 0 +n = findfewestcoins a0 23 o +if lt n 0 +ret false +end +a1 = new ulong 3 0 +put a1 0 4 +put a1 1 4 +put a1 2 15 +ret tueq part o 0 ulong n a1 +end +note canonical case 408390b9-fafa-4bb9-b608-ffe6036edb6c +fn tc4 to bool +a0 = new ulong 5 0 +put a0 0 1 +put a0 1 5 +put a0 2 10 +put a0 3 21 +put a0 4 25 +o = new ulong 256 0 +n = findfewestcoins a0 63 o +if lt n 0 +ret false +end +a1 = new ulong 3 0 +put a1 0 21 +put a1 1 21 +put a1 2 21 +ret tueq part o 0 ulong n a1 +end +note canonical case 7421a4cb-1c48-4bf9-99c7-7f049689132f +fn tc5 to bool +a0 = new ulong 7 0 +put a0 0 1 +put a0 1 2 +put a0 2 5 +put a0 3 10 +put a0 4 20 +put a0 5 50 +put a0 6 100 +o = new ulong 256 0 +n = findfewestcoins a0 999 o +if lt n 0 +ret false +end +a1 = new ulong 15 0 +put a1 0 2 +put a1 1 2 +put a1 2 5 +put a1 3 20 +put a1 4 20 +put a1 5 50 +put a1 6 100 +put a1 7 100 +put a1 8 100 +put a1 9 100 +put a1 10 100 +put a1 11 100 +put a1 12 100 +put a1 13 100 +put a1 14 100 +ret tueq part o 0 ulong n a1 +end +note canonical case f79d2e9b-0ae3-4d6a-bb58-dc978b0dba28 +fn tc6 to bool +a0 = new ulong 5 0 +put a0 0 2 +put a0 1 5 +put a0 2 10 +put a0 3 20 +put a0 4 50 +o = new ulong 256 0 +n = findfewestcoins a0 21 o +if lt n 0 +ret false +end +a1 = new ulong 5 0 +put a1 0 2 +put a1 1 2 +put a1 2 2 +put a1 3 5 +put a1 4 10 +ret tueq part o 0 ulong n a1 +end +note canonical case 9a166411-d35d-4f7f-a007-6724ac266178 +fn tc7 to bool +a0 = new ulong 2 0 +put a0 0 4 +put a0 1 5 +o = new ulong 256 0 +n = findfewestcoins a0 27 o +if lt n 0 +ret false +end +a1 = new ulong 6 0 +put a1 0 4 +put a1 1 4 +put a1 2 4 +put a1 3 5 +put a1 4 5 +put a1 5 5 +ret tueq part o 0 ulong n a1 +end +note canonical case ce0f80d5-51c3-469d-818c-3e69dbd25f75 +fn tc8 to bool +a0 = new ulong 3 0 +put a0 0 1 +put a0 1 10 +put a0 2 11 +o = new ulong 256 0 +n = findfewestcoins a0 20 o +if lt n 0 +ret false +end +a1 = new ulong 2 0 +put a1 0 10 +put a1 1 10 +ret tueq part o 0 ulong n a1 +end +note canonical case bbbcc154-e9e9-4209-a4db-dd6d81ec26bb +fn tc9 to bool +a0 = new ulong 5 0 +put a0 0 1 +put a0 1 5 +put a0 2 10 +put a0 3 21 +put a0 4 25 +o = new ulong 256 0 +n = findfewestcoins a0 0 o +if lt n 0 +ret false +end +ret eq ulong n 0 +end +note canonical case c8b81d5a-49bd-4b61-af73-8ee5383a2ce1 +fn tc10 to bool +a0 = new ulong 2 0 +put a0 0 5 +put a0 1 10 +o = new ulong 256 0 +ret eq findfewestcoins a0 3 o -1 +end +note canonical case 3c43e3e4-63f9-46ac-9476-a67516e98f68 +fn tc11 to bool +a0 = new ulong 2 0 +put a0 0 5 +put a0 1 10 +o = new ulong 256 0 +ret eq findfewestcoins a0 94 o -1 +end +note canonical case 8fe1f076-9b2d-4f44-89fe-8a6ccd63c8f3 +fn tc12 to bool +a0 = new ulong 3 0 +put a0 0 1 +put a0 1 2 +put a0 2 5 +o = new ulong 256 0 +ret eq findfewestcoins a0 -5 o -2 +end +fn main to int +tcheck tc0 c0 +tcheck tc1 c1 +tcheck tc2 c2 +tcheck tc3 c3 +tcheck tc4 c4 +tcheck tc5 c5 +tcheck tc6 c6 +tcheck tc7 c7 +tcheck tc8 c8 +tcheck tc9 c9 +tcheck tc10 c10 +tcheck tc11 c11 +tcheck tc12 c12 +ret int get tfails 0 +end diff --git a/exercises/darts/.meta/example.om b/exercises/darts/.meta/example.om new file mode 100644 index 0000000..fb49fba --- /dev/null +++ b/exercises/darts/.meta/example.om @@ -0,0 +1,19 @@ +note Exercism-style exercise "darts": https://exercism.org/tracks/python/exercises/darts +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note score is the points of a dart landing at (x, y): 10 inside or on the circle of radius 1 around +note the centre, 5 within radius 5, 1 within radius 10, 0 outside. +note Check it with: sh exercise.sh exercises/darts +om2 +fn score x f64 y f64 to ulong +d = add mul x x mul y y +if le d 1.0 +ret 10 +end +if le d 25.0 +ret 5 +end +if le d 100.0 +ret 1 +end +ret 0 +end diff --git a/exercises/darts/darts.om b/exercises/darts/darts.om new file mode 100644 index 0000000..9bd8728 --- /dev/null +++ b/exercises/darts/darts.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "darts": https://exercism.org/tracks/python/exercises/darts +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note score is the points of a dart landing at (x, y): 10 inside or on the circle of radius 1 around +note the centre, 5 within radius 5, 1 within radius 10, 0 outside. +note Check it with: sh exercise.sh exercises/darts +om2 +fn score x f64 y f64 to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/darts/test.om b/exercises/darts/test.om new file mode 100644 index 0000000..41fafc6 --- /dev/null +++ b/exercises/darts/test.om @@ -0,0 +1,130 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/darts/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 13 cases kept, 0 skipped +text c0 "score: Missed target" +text c1 "score: On the outer circle" +text c2 "score: On the middle circle" +text c3 "score: On the inner circle" +text c4 "score: Exactly on center" +text c5 "score: Near the center" +text c6 "score: Just within the inner circle" +text c7 "score: Just outside the inner circle" +text c8 "score: Just within the middle circle" +text c9 "score: Just outside the middle circle" +text c10 "score: Just within the outer circle" +text c11 "score: Just outside the outer circle" +text c12 "score: Asymmetric position between the inner and middle circles" +note canonical case 9033f731-0a3a-4d9c-b1c0-34a1c8362afb +fn tc0 to bool +ret eq score -9.0 9.0 0 +end +note canonical case 4c9f6ff4-c489-45fd-be8a-1fcb08b4d0ba +fn tc1 to bool +ret eq score 0.0 10.0 1 +end +note canonical case 14378687-ee58-4c9b-a323-b089d5274be8 +fn tc2 to bool +ret eq score -5.0 0.0 5 +end +note canonical case 849e2e63-85bd-4fed-bc3b-781ae962e2c9 +fn tc3 to bool +ret eq score 0.0 -1.0 10 +end +note canonical case 1c5ffd9f-ea66-462f-9f06-a1303de5a226 +fn tc4 to bool +ret eq score 0.0 0.0 10 +end +note canonical case b65abce3-a679-4550-8115-4b74bda06088 +fn tc5 to bool +ret eq score -0.1 -0.1 10 +end +note canonical case 66c29c1d-44f5-40cf-9927-e09a1305b399 +fn tc6 to bool +ret eq score 0.7 0.7 10 +end +note canonical case d1012f63-c97c-4394-b944-7beb3d0b141a +fn tc7 to bool +ret eq score 0.8 -0.8 5 +end +note canonical case ab2b5666-b0b4-49c3-9b27-205e790ed945 +fn tc8 to bool +ret eq score -3.5 3.5 5 +end +note canonical case 70f1424e-d690-4860-8caf-9740a52c0161 +fn tc9 to bool +ret eq score -3.6 -3.6 1 +end +note canonical case a7dbf8db-419c-4712-8a7f-67602b69b293 +fn tc10 to bool +ret eq score -7.0 7.0 1 +end +note canonical case e0f39315-9f9a-4546-96e4-a9475b885aa7 +fn tc11 to bool +ret eq score 7.1 -7.1 0 +end +note canonical case 045d7d18-d863-4229-818e-b50828c75d19 +fn tc12 to bool +ret eq score 0.5 -4.0 5 +end +fn main to int +tcheck tc0 c0 +tcheck tc1 c1 +tcheck tc2 c2 +tcheck tc3 c3 +tcheck tc4 c4 +tcheck tc5 c5 +tcheck tc6 c6 +tcheck tc7 c7 +tcheck tc8 c8 +tcheck tc9 c9 +tcheck tc10 c10 +tcheck tc11 c11 +tcheck tc12 c12 +ret int get tfails 0 +end diff --git a/exercises/eliuds-eggs/.meta/example.om b/exercises/eliuds-eggs/.meta/example.om new file mode 100644 index 0000000..9091eb5 --- /dev/null +++ b/exercises/eliuds-eggs/.meta/example.om @@ -0,0 +1,14 @@ +note Exercism-style exercise "eliuds-eggs": https://exercism.org/tracks/python/exercises/eliuds-eggs +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note eggcount is the number of 1 bits in the binary representation of n. +note Check it with: sh exercise.sh exercises/eliuds-eggs +om2 +fn eggcount n ulong to ulong +c = ulong 0 +v = n +while ne v 0 bound 64 +c = add c and v 1 +v = shr v 1 +end +ret c +end diff --git a/exercises/eliuds-eggs/eliuds-eggs.om b/exercises/eliuds-eggs/eliuds-eggs.om new file mode 100644 index 0000000..e2ff427 --- /dev/null +++ b/exercises/eliuds-eggs/eliuds-eggs.om @@ -0,0 +1,9 @@ +note Exercism-style exercise "eliuds-eggs": https://exercism.org/tracks/python/exercises/eliuds-eggs +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note eggcount is the number of 1 bits in the binary representation of n. +note Check it with: sh exercise.sh exercises/eliuds-eggs +om2 +fn eggcount n ulong to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/eliuds-eggs/test.om b/exercises/eliuds-eggs/test.om new file mode 100644 index 0000000..f15db5f --- /dev/null +++ b/exercises/eliuds-eggs/test.om @@ -0,0 +1,76 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/eliuds-eggs/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 4 cases kept, 0 skipped +text c0 "eggCount: 0 eggs" +text c1 "eggCount: 1 egg" +text c2 "eggCount: 4 eggs" +text c3 "eggCount: 13 eggs" +note canonical case 559e789d-07d1-4422-9004-3b699f83bca3 +fn tc0 to bool +ret eq eggcount 0 0 +end +note canonical case 97223282-f71e-490c-92f0-b3ec9e275aba +fn tc1 to bool +ret eq eggcount 16 1 +end +note canonical case 1f8fd18f-26e9-4144-9a0e-57cdfc4f4ff5 +fn tc2 to bool +ret eq eggcount 89 4 +end +note canonical case 0c18be92-a498-4ef2-bcbb-28ac4b06cb81 +fn tc3 to bool +ret eq eggcount 2000000000 13 +end +fn main to int +tcheck tc0 c0 +tcheck tc1 c1 +tcheck tc2 c2 +tcheck tc3 c3 +ret int get tfails 0 +end diff --git a/exercises/hamming/.meta/example.om b/exercises/hamming/.meta/example.om new file mode 100644 index 0000000..bdc5c7b --- /dev/null +++ b/exercises/hamming/.meta/example.om @@ -0,0 +1,18 @@ +note Exercism-style exercise "hamming": https://exercism.org/tracks/python/exercises/hamming +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note distance is the number of positions at which DNA strands a and b differ, +note or -1 when they are not of equal length. +note Check it with: sh exercise.sh exercises/hamming +om2 +fn distance a view uchar * b view uchar * to long +if ne len a len b +ret -1 +end +d = long 0 +for i ulong len a +if ne get a i get b i +d = add d 1 +end +end +ret d +end diff --git a/exercises/hamming/hamming.om b/exercises/hamming/hamming.om new file mode 100644 index 0000000..3649679 --- /dev/null +++ b/exercises/hamming/hamming.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "hamming": https://exercism.org/tracks/python/exercises/hamming +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note distance is the number of positions at which DNA strands a and b differ, +note or -1 when they are not of equal length. +note Check it with: sh exercise.sh exercises/hamming +om2 +fn distance a view uchar * b view uchar * to long +note replace this line with your solution +trap 13 +end diff --git a/exercises/hamming/test.om b/exercises/hamming/test.om new file mode 100644 index 0000000..1dd028a --- /dev/null +++ b/exercises/hamming/test.om @@ -0,0 +1,124 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/hamming/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 9 cases kept, 0 skipped, 6 superseded (reimplemented) cases omitted +text z0 "z" +text z1 "z" +text c2 "distance: empty strands" +text s3 "A" +text s4 "A" +text c5 "distance: single letter identical strands" +text s6 "G" +text s7 "T" +text c8 "distance: single letter different strands" +text s9 "GGACTGAAATCTG" +text s10 "GGACTGAAATCTG" +text c11 "distance: long identical strands" +text s12 "GGACGGATTCTG" +text s13 "AGGACGGATTCT" +text c14 "distance: long different strands" +text s15 "AATG" +text s16 "AAA" +text c17 "distance: disallow first strand longer" +text s18 "ATA" +text s19 "AGTG" +text c20 "distance: disallow second strand longer" +text z21 "z" +text s22 "G" +text c23 "distance: disallow empty first strand" +text s24 "G" +text z25 "z" +text c26 "distance: disallow empty second strand" +note canonical case f6dcb64f-03b0-4b60-81b1-3c9dbf47e887 +fn tc0 to bool +ret eq distance part z0 0 0 part z1 0 0 0 +end +note canonical case 54681314-eee2-439a-9db0-b0636c656156 +fn tc1 to bool +ret eq distance s3 s4 0 +end +note canonical case 294479a3-a4c8-478f-8d63-6209815a827b +fn tc2 to bool +ret eq distance s6 s7 1 +end +note canonical case 9aed5f34-5693-4344-9b31-40c692fb5592 +fn tc3 to bool +ret eq distance s9 s10 0 +end +note canonical case cd2273a5-c576-46c8-a52b-dee251c3e6e5 +fn tc4 to bool +ret eq distance s12 s13 9 +end +note canonical case b9228bb1-465f-4141-b40f-1f99812de5a8 +fn tc5 to bool +ret eq distance s15 s16 -1 +end +note canonical case dab38838-26bb-4fff-acbe-3b0a9bfeba2d +fn tc6 to bool +ret eq distance s18 s19 -1 +end +note canonical case b764d47c-83ff-4de2-ab10-6cfe4b15c0f3 +fn tc7 to bool +ret eq distance part z21 0 0 s22 -1 +end +note canonical case 9ab9262f-3521-4191-81f5-0ed184a5aa89 +fn tc8 to bool +ret eq distance s24 part z25 0 0 -1 +end +fn main to int +tcheck tc0 c2 +tcheck tc1 c5 +tcheck tc2 c8 +tcheck tc3 c11 +tcheck tc4 c14 +tcheck tc5 c17 +tcheck tc6 c20 +tcheck tc7 c23 +tcheck tc8 c26 +ret int get tfails 0 +end diff --git a/exercises/hello-world/.meta/example.om b/exercises/hello-world/.meta/example.om new file mode 100644 index 0000000..527eee8 --- /dev/null +++ b/exercises/hello-world/.meta/example.om @@ -0,0 +1,12 @@ +note Exercism-style exercise "hello-world": https://exercism.org/tracks/python/exercises/hello-world +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note hello writes the greeting "Hello, World!" into o and returns its length in bytes. +note Check it with: sh exercise.sh exercises/hello-world +om2 +text hw "Hello, World!" +fn hello o edit uchar * to ulong +for i ulong len hw +put o i get hw i +end +ret len hw +end diff --git a/exercises/hello-world/hello-world.om b/exercises/hello-world/hello-world.om new file mode 100644 index 0000000..4b1f051 --- /dev/null +++ b/exercises/hello-world/hello-world.om @@ -0,0 +1,9 @@ +note Exercism-style exercise "hello-world": https://exercism.org/tracks/python/exercises/hello-world +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note hello writes the greeting "Hello, World!" into o and returns its length in bytes. +note Check it with: sh exercise.sh exercises/hello-world +om2 +fn hello o edit uchar * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/hello-world/test.om b/exercises/hello-world/test.om new file mode 100644 index 0000000..1d7d72a --- /dev/null +++ b/exercises/hello-world/test.om @@ -0,0 +1,72 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/hello-world/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 1 cases kept, 0 skipped +fn tbeq a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text e0 "Hello, World!" +text c1 "hello: Say Hi!" +note canonical case af9ffe10-dc13-42d8-a742-e7bdafac449d +fn tc0 to bool +o = new uchar 1024 0 +n = hello o +ret tbeq part o 0 n e0 +end +fn main to int +tcheck tc0 c1 +ret int get tfails 0 +end diff --git a/exercises/isbn-verifier/.meta/example.om b/exercises/isbn-verifier/.meta/example.om new file mode 100644 index 0000000..22dd9f2 --- /dev/null +++ b/exercises/isbn-verifier/.meta/example.om @@ -0,0 +1,32 @@ +note Exercism-style exercise "isbn-verifier": https://exercism.org/tracks/python/exercises/isbn-verifier +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note isvalid is true when isbn, with its hyphens removed, is a valid ISBN-10: nine digits and a check +note digit or X (10), such that d1*10 + d2*9 + ... + d10*1 is divisible by 11. +note Check it with: sh exercise.sh exercises/isbn-verifier +om2 +fn isvalid isbn view uchar * to bool +k = ulong 0 +t = ulong 0 +for i ulong len isbn +c = get isbn i +if eq c 45 +continue +end +d = ulong 0 +if and ge c 48 le c 57 +d = ulong sub c 48 +else +if and eq c 88 eq k 9 +d = ulong 10 +else +ret false +end +end +if ge k 10 +ret false +end +t = add t mul d sub 10 k +k = add k 1 +end +ret and eq k 10 eq rem t 11 0 +end diff --git a/exercises/isbn-verifier/isbn-verifier.om b/exercises/isbn-verifier/isbn-verifier.om new file mode 100644 index 0000000..ad12955 --- /dev/null +++ b/exercises/isbn-verifier/isbn-verifier.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "isbn-verifier": https://exercism.org/tracks/python/exercises/isbn-verifier +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note isvalid is true when isbn, with its hyphens removed, is a valid ISBN-10: nine digits and a check +note digit or X (10), such that d1*10 + d2*9 + ... + d10*1 is divisible by 11. +note Check it with: sh exercise.sh exercises/isbn-verifier +om2 +fn isvalid isbn view uchar * to bool +note replace this line with your solution +trap 13 +end diff --git a/exercises/isbn-verifier/test.om b/exercises/isbn-verifier/test.om new file mode 100644 index 0000000..9c43932 --- /dev/null +++ b/exercises/isbn-verifier/test.om @@ -0,0 +1,199 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/isbn-verifier/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 21 cases kept, 0 skipped +text s0 "3-598-21508-8" +text c1 "isValid: valid isbn" +text s2 "3-598-21508-9" +text c3 "isValid: invalid isbn check digit" +text s4 "3-598-21507-X" +text c5 "isValid: valid isbn with a check digit of 10" +text s6 "3-598-21507-A" +text c7 "isValid: check digit is a character other than X" +text s8 "4-598-21507-B" +text c9 "isValid: invalid check digit in isbn is not treated as zero" +text s10 "3-598-P1581-X" +text c11 "isValid: invalid character in isbn is not treated as zero" +text s12 "3-598-2X507-9" +text c13 "isValid: X is only valid as a check digit" +text s14 "3-598-21508-96" +text c15 "isValid: only one check digit is allowed" +text s16 "3-598-2X507-5" +text c17 "isValid: X is not substituted by the value 10" +text s18 "3598215088" +text c19 "isValid: valid isbn without separating dashes" +text s20 "359821507X" +text c21 "isValid: isbn without separating dashes and X as check digit" +text s22 "359821507" +text c23 "isValid: isbn without check digit and dashes" +text s24 "3598215078X" +text c25 "isValid: too long isbn and no dashes" +text s26 "00" +text c27 "isValid: too short isbn" +text s28 "3-598-21507" +text c29 "isValid: isbn without check digit" +text s30 "3-598-21515-X" +text c31 "isValid: check digit of X should not be used for 0" +text z32 "z" +text c33 "isValid: empty isbn" +text s34 "134456729" +text c35 "isValid: input is 9 characters" +text s36 "3132P34035" +text c37 "isValid: invalid characters are not ignored after checking length" +text s38 "3598P215088" +text c39 "isValid: invalid characters are not ignored before checking length" +text s40 "98245726788" +text c41 "isValid: input is too long but contains a valid isbn" +note canonical case 0caa3eac-d2e3-4c29-8df8-b188bc8c9292 +fn tc0 to bool +ret eq isvalid s0 true +end +note canonical case 19f76b53-7c24-45f8-87b8-4604d0ccd248 +fn tc1 to bool +ret eq isvalid s2 false +end +note canonical case 4164bfee-fb0a-4a1c-9f70-64c6a1903dcd +fn tc2 to bool +ret eq isvalid s4 true +end +note canonical case 3ed50db1-8982-4423-a993-93174a20825c +fn tc3 to bool +ret eq isvalid s6 false +end +note canonical case 9416f4a5-fe01-4b61-a07b-eb75892ef562 +fn tc4 to bool +ret eq isvalid s8 false +end +note canonical case c19ba0c4-014f-4dc3-a63f-ff9aefc9b5ec +fn tc5 to bool +ret eq isvalid s10 false +end +note canonical case 28025280-2c39-4092-9719-f3234b89c627 +fn tc6 to bool +ret eq isvalid s12 false +end +note canonical case 8005b57f-f194-44ee-88d2-a77ac4142591 +fn tc7 to bool +ret eq isvalid s14 false +end +note canonical case fdb14c99-4cf8-43c5-b06d-eb1638eff343 +fn tc8 to bool +ret eq isvalid s16 false +end +note canonical case f6294e61-7e79-46b3-977b-f48789a4945b +fn tc9 to bool +ret eq isvalid s18 true +end +note canonical case 185ab99b-3a1b-45f3-aeec-b80d80b07f0b +fn tc10 to bool +ret eq isvalid s20 true +end +note canonical case 7725a837-ec8e-4528-a92a-d981dd8cf3e2 +fn tc11 to bool +ret eq isvalid s22 false +end +note canonical case 47e4dfba-9c20-46ed-9958-4d3190630bdf +fn tc12 to bool +ret eq isvalid s24 false +end +note canonical case 737f4e91-cbba-4175-95bf-ae630b41fb60 +fn tc13 to bool +ret eq isvalid s26 false +end +note canonical case 5458a128-a9b6-4ff8-8afb-674e74567cef +fn tc14 to bool +ret eq isvalid s28 false +end +note canonical case 70b6ad83-d0a2-4ca7-a4d5-a9ab731800f7 +fn tc15 to bool +ret eq isvalid s30 false +end +note canonical case 94610459-55ab-4c35-9b93-ff6ea1a8e562 +fn tc16 to bool +ret eq isvalid part z32 0 0 false +end +note canonical case 7bff28d4-d770-48cc-80d6-b20b3a0fb46c +fn tc17 to bool +ret eq isvalid s34 false +end +note canonical case ed6e8d1b-382c-4081-8326-8b772c581fec +fn tc18 to bool +ret eq isvalid s36 false +end +note canonical case daad3e58-ce00-4395-8a8e-e3eded1cdc86 +fn tc19 to bool +ret eq isvalid s38 false +end +note canonical case fb5e48d8-7c03-4bfb-a088-b101df16fdc3 +fn tc20 to bool +ret eq isvalid s40 false +end +fn main to int +tcheck tc0 c1 +tcheck tc1 c3 +tcheck tc2 c5 +tcheck tc3 c7 +tcheck tc4 c9 +tcheck tc5 c11 +tcheck tc6 c13 +tcheck tc7 c15 +tcheck tc8 c17 +tcheck tc9 c19 +tcheck tc10 c21 +tcheck tc11 c23 +tcheck tc12 c25 +tcheck tc13 c27 +tcheck tc14 c29 +tcheck tc15 c31 +tcheck tc16 c33 +tcheck tc17 c35 +tcheck tc18 c37 +tcheck tc19 c39 +tcheck tc20 c41 +ret int get tfails 0 +end diff --git a/exercises/isogram/.meta/example.om b/exercises/isogram/.meta/example.om new file mode 100644 index 0000000..ac32939 --- /dev/null +++ b/exercises/isogram/.meta/example.om @@ -0,0 +1,22 @@ +note Exercism-style exercise "isogram": https://exercism.org/tracks/python/exercises/isogram +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note isisogram is true when no letter occurs twice in s, ignoring case; spaces and hyphens may repeat. +note Check it with: sh exercise.sh exercises/isogram +om2 +fn isisogram s view uchar * to bool +seen = new uchar 26 0 +for i ulong len s +c = get s i +if and ge c 65 le c 90 +c = add c 32 +end +if and ge c 97 le c 122 +k = ulong sub c 97 +if ne get seen k 0 +ret false +end +put seen k 1 +end +end +ret true +end diff --git a/exercises/isogram/isogram.om b/exercises/isogram/isogram.om new file mode 100644 index 0000000..36c05d2 --- /dev/null +++ b/exercises/isogram/isogram.om @@ -0,0 +1,9 @@ +note Exercism-style exercise "isogram": https://exercism.org/tracks/python/exercises/isogram +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note isisogram is true when no letter occurs twice in s, ignoring case; spaces and hyphens may repeat. +note Check it with: sh exercise.sh exercises/isogram +om2 +fn isisogram s view uchar * to bool +note replace this line with your solution +trap 13 +end diff --git a/exercises/isogram/test.om b/exercises/isogram/test.om new file mode 100644 index 0000000..8c3018c --- /dev/null +++ b/exercises/isogram/test.om @@ -0,0 +1,150 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/isogram/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 14 cases kept, 0 skipped +text z0 "z" +text c1 "isIsogram: empty string" +text s2 "isogram" +text c3 "isIsogram: isogram with only lower case characters" +text s4 "eleven" +text c5 "isIsogram: word with one duplicated character" +text s6 "zzyzx" +text c7 "isIsogram: word with one duplicated character from the end of the alphabet" +text s8 "subdermatoglyphic" +text c9 "isIsogram: longest reported english isogram" +text s10 "Alphabet" +text c11 "isIsogram: word with duplicated character in mixed case" +text s12 "alphAbet" +text c13 "isIsogram: word with duplicated character in mixed case, lowercase first" +text s14 "thumbscrew-japingly" +text c15 "isIsogram: hypothetical isogrammic word with hyphen" +text s16 "thumbscrew-jappingly" +text c17 "isIsogram: hypothetical word with duplicated character following hyphen" +text s18 "six-year-old" +text c19 "isIsogram: isogram with duplicated hyphen" +text s20 "Emily Jung Schwartzkopf" +text c21 "isIsogram: made-up name that is an isogram" +text s22 "accentor" +text c23 "isIsogram: duplicated character in the middle" +text s24 "angola" +text c25 "isIsogram: same first and last characters" +text s26 "up-to-date" +text c27 "isIsogram: word with duplicated character and with two hyphens" +note canonical case a0e97d2d-669e-47c7-8134-518a1e2c4555 +fn tc0 to bool +ret eq isisogram part z0 0 0 true +end +note canonical case 9a001b50-f194-4143-bc29-2af5ec1ef652 +fn tc1 to bool +ret eq isisogram s2 true +end +note canonical case 8ddb0ca3-276e-4f8b-89da-d95d5bae78a4 +fn tc2 to bool +ret eq isisogram s4 false +end +note canonical case 6450b333-cbc2-4b24-a723-0b459b34fe18 +fn tc3 to bool +ret eq isisogram s6 false +end +note canonical case a15ff557-dd04-4764-99e7-02cc1a385863 +fn tc4 to bool +ret eq isisogram s8 true +end +note canonical case f1a7f6c7-a42f-4915-91d7-35b2ea11c92e +fn tc5 to bool +ret eq isisogram s10 false +end +note canonical case 14a4f3c1-3b47-4695-b645-53d328298942 +fn tc6 to bool +ret eq isisogram s12 false +end +note canonical case 423b850c-7090-4a8a-b057-97f1cadd7c42 +fn tc7 to bool +ret eq isisogram s14 true +end +note canonical case 93dbeaa0-3c5a-45c2-8b25-428b8eacd4f2 +fn tc8 to bool +ret eq isisogram s16 false +end +note canonical case 36b30e5c-173f-49c6-a515-93a3e825553f +fn tc9 to bool +ret eq isisogram s18 true +end +note canonical case cdabafa0-c9f4-4c1f-b142-689c6ee17d93 +fn tc10 to bool +ret eq isisogram s20 true +end +note canonical case 5fc61048-d74e-48fd-bc34-abfc21552d4d +fn tc11 to bool +ret eq isisogram s22 false +end +note canonical case 310ac53d-8932-47bc-bbb4-b2b94f25a83e +fn tc12 to bool +ret eq isisogram s24 false +end +note canonical case 0d0b8644-0a1e-4a31-a432-2b3ee270d847 +fn tc13 to bool +ret eq isisogram s26 false +end +fn main to int +tcheck tc0 c1 +tcheck tc1 c3 +tcheck tc2 c5 +tcheck tc3 c7 +tcheck tc4 c9 +tcheck tc5 c11 +tcheck tc6 c13 +tcheck tc7 c15 +tcheck tc8 c17 +tcheck tc9 c19 +tcheck tc10 c21 +tcheck tc11 c23 +tcheck tc12 c25 +tcheck tc13 c27 +ret int get tfails 0 +end diff --git a/exercises/knapsack/.meta/example.om b/exercises/knapsack/.meta/example.om new file mode 100644 index 0000000..ec2b059 --- /dev/null +++ b/exercises/knapsack/.meta/example.om @@ -0,0 +1,29 @@ +note Exercism-style exercise "knapsack": https://exercism.org/tracks/python/exercises/knapsack +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note Item i weighs ws[i] and is worth vs[i]. maximumvalue is the largest total worth of items (each +note taken at most once) whose total weight is at most cap (cap at most 1000). +note Check it with: sh exercise.sh exercises/knapsack +om2 +const KMAX ulong 1001 +global kdp ulong KMAX +fn additem w ulong v ulong cap ulong to void +for k ulong add sub cap w 1 +d = sub cap k +x = add get kdp sub d w v +if gt x get kdp d +put kdp d x +end +end +end +fn maximumvalue ws view ulong * vs view ulong * cap ulong to ulong +for c ulong add cap 1 +put kdp c 0 +end +for i ulong len ws +w = get ws i +if le w cap +additem w get vs i cap +end +end +ret get kdp cap +end diff --git a/exercises/knapsack/knapsack.om b/exercises/knapsack/knapsack.om new file mode 100644 index 0000000..8e9d353 --- /dev/null +++ b/exercises/knapsack/knapsack.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "knapsack": https://exercism.org/tracks/python/exercises/knapsack +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note Item i weighs ws[i] and is worth vs[i]. maximumvalue is the largest total worth of items (each +note taken at most once) whose total weight is at most cap (cap at most 1000). +note Check it with: sh exercise.sh exercises/knapsack +om2 +fn maximumvalue ws view ulong * vs view ulong * cap ulong to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/knapsack/test.om b/exercises/knapsack/test.om new file mode 100644 index 0000000..f51db07 --- /dev/null +++ b/exercises/knapsack/test.om @@ -0,0 +1,184 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/knapsack/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 7 cases kept, 0 skipped, 1 superseded (reimplemented) cases omitted +text c0 "maximumValue: no items" +text c1 "maximumValue: one item, too heavy" +text c2 "maximumValue: five items (cannot be greedy by weight)" +text c3 "maximumValue: five items (cannot be greedy by value)" +text c4 "maximumValue: example knapsack" +text c5 "maximumValue: 8 items" +text c6 "maximumValue: 15 items" +note canonical case 3993a824-c20e-493d-b3c9-ee8a7753ee59 +fn tc0 to bool +a0 = new ulong 1 0 +a1 = new ulong 1 0 +ret eq maximumvalue part a0 0 0 part a1 0 0 100 0 +end +note canonical case 1d39e98c-6249-4a8b-912f-87cb12e506b0 +fn tc1 to bool +a0 = new ulong 1 0 +put a0 0 100 +a1 = new ulong 1 0 +put a1 0 1 +ret eq maximumvalue a0 a1 10 0 +end +note canonical case 833ea310-6323-44f2-9d27-a278740ffbd8 +fn tc2 to bool +a0 = new ulong 5 0 +put a0 0 2 +put a0 1 2 +put a0 2 2 +put a0 3 2 +put a0 4 10 +a1 = new ulong 5 0 +put a1 0 5 +put a1 1 5 +put a1 2 5 +put a1 3 5 +put a1 4 21 +ret eq maximumvalue a0 a1 10 21 +end +note canonical case 277cdc52-f835-4c7d-872b-bff17bab2456 +fn tc3 to bool +a0 = new ulong 5 0 +put a0 0 2 +put a0 1 2 +put a0 2 2 +put a0 3 2 +put a0 4 10 +a1 = new ulong 5 0 +put a1 0 20 +put a1 1 20 +put a1 2 20 +put a1 3 20 +put a1 4 50 +ret eq maximumvalue a0 a1 10 80 +end +note canonical case 81d8e679-442b-4f7a-8a59-7278083916c9 +fn tc4 to bool +a0 = new ulong 4 0 +put a0 0 5 +put a0 1 4 +put a0 2 6 +put a0 3 4 +a1 = new ulong 4 0 +put a1 0 10 +put a1 1 40 +put a1 2 30 +put a1 3 50 +ret eq maximumvalue a0 a1 10 90 +end +note canonical case f23a2449-d67c-4c26-bf3e-cde020f27ecc +fn tc5 to bool +a0 = new ulong 8 0 +put a0 0 25 +put a0 1 35 +put a0 2 45 +put a0 3 5 +put a0 4 25 +put a0 5 3 +put a0 6 2 +put a0 7 2 +a1 = new ulong 8 0 +put a1 0 350 +put a1 1 400 +put a1 2 450 +put a1 3 20 +put a1 4 70 +put a1 5 8 +put a1 6 5 +put a1 7 5 +ret eq maximumvalue a0 a1 104 900 +end +note canonical case 7c682ae9-c385-4241-a197-d2fa02c81a11 +fn tc6 to bool +a0 = new ulong 15 0 +put a0 0 70 +put a0 1 73 +put a0 2 77 +put a0 3 80 +put a0 4 82 +put a0 5 87 +put a0 6 90 +put a0 7 94 +put a0 8 98 +put a0 9 106 +put a0 10 110 +put a0 11 113 +put a0 12 115 +put a0 13 118 +put a0 14 120 +a1 = new ulong 15 0 +put a1 0 135 +put a1 1 139 +put a1 2 149 +put a1 3 150 +put a1 4 156 +put a1 5 163 +put a1 6 173 +put a1 7 184 +put a1 8 192 +put a1 9 201 +put a1 10 210 +put a1 11 214 +put a1 12 221 +put a1 13 229 +put a1 14 240 +ret eq maximumvalue a0 a1 750 1458 +end +fn main to int +tcheck tc0 c0 +tcheck tc1 c1 +tcheck tc2 c2 +tcheck tc3 c3 +tcheck tc4 c4 +tcheck tc5 c5 +tcheck tc6 c6 +ret int get tfails 0 +end diff --git a/exercises/largest-series-product/.meta/example.om b/exercises/largest-series-product/.meta/example.om new file mode 100644 index 0000000..e9cc53d --- /dev/null +++ b/exercises/largest-series-product/.meta/example.om @@ -0,0 +1,43 @@ +note Exercism-style exercise "largest-series-product": https://exercism.org/tracks/python/exercises/largest-series-product +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note largestproduct is the largest product of span adjacent decimal digits of s (1 for span 0); or +note -1 when span is negative, -2 when span is longer than s, -3 when s holds a non-digit. +note Check it with: sh exercise.sh exercises/largest-series-product +om2 +fn alldigits s view uchar * to bool +for i ulong len s +c = get s i +if or lt c 48 gt c 57 +ret false +end +end +ret true +end +fn window s view uchar * k ulong w ulong to long +p = long 1 +for j ulong w +p = mul p long sub get s add k j 48 +end +ret p +end +fn largestproduct s view uchar * span long to long +if lt span 0 +ret -1 +end +w = ulong span +n = len s +if gt w n +ret -2 +end +if not alldigits s +ret -3 +end +best = long 0 +for k ulong add sub n w 1 +p = window s k w +if gt p best +best = p +end +end +ret best +end diff --git a/exercises/largest-series-product/largest-series-product.om b/exercises/largest-series-product/largest-series-product.om new file mode 100644 index 0000000..d064bc7 --- /dev/null +++ b/exercises/largest-series-product/largest-series-product.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "largest-series-product": https://exercism.org/tracks/python/exercises/largest-series-product +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note largestproduct is the largest product of span adjacent decimal digits of s (1 for span 0); or +note -1 when span is negative, -2 when span is longer than s, -3 when s holds a non-digit. +note Check it with: sh exercise.sh exercises/largest-series-product +om2 +fn largestproduct s view uchar * span long to long +note replace this line with your solution +trap 13 +end diff --git a/exercises/largest-series-product/test.om b/exercises/largest-series-product/test.om new file mode 100644 index 0000000..9c5f208 --- /dev/null +++ b/exercises/largest-series-product/test.om @@ -0,0 +1,157 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/largest-series-product/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 15 cases kept, 0 skipped, 3 superseded (reimplemented) cases omitted +text s0 "29" +text c1 "largestProduct: finds the largest product if span equals length" +text s2 "0123456789" +text c3 "largestProduct: can find the largest product of 2 with numbers in order" +text s4 "576802143" +text c5 "largestProduct: can find the largest product of 2" +text s6 "0123456789" +text c7 "largestProduct: can find the largest product of 3 with numbers in order" +text s8 "1027839564" +text c9 "largestProduct: can find the largest product of 3" +text s10 "0123456789" +text c11 "largestProduct: can find the largest product of 5 with numbers in order" +text s12 "73167176531330624919225119674426574742355349194934" +text c13 "largestProduct: can get the largest product of a big number" +text s14 "0000" +text c15 "largestProduct: reports zero if the only digits are zero" +text s16 "99099" +text c17 "largestProduct: reports zero if all spans include zero" +text s18 "123" +text c19 "largestProduct: rejects span longer than string length" +text z20 "z" +text c21 "largestProduct: reports 1 for empty string and empty product (0 span)" +text s22 "123" +text c23 "largestProduct: reports 1 for nonempty string and empty product (0 span)" +text z24 "z" +text c25 "largestProduct: rejects empty string and nonzero span" +text s26 "1234a5" +text c27 "largestProduct: rejects invalid character in digits" +text s28 "12345" +text c29 "largestProduct: rejects negative span" +note canonical case 7c82f8b7-e347-48ee-8a22-f672323324d4 +fn tc0 to bool +ret eq largestproduct s0 2 18 +end +note canonical case 88523f65-21ba-4458-a76a-b4aaf6e4cb5e +fn tc1 to bool +ret eq largestproduct s2 2 72 +end +note canonical case f1376b48-1157-419d-92c2-1d7e36a70b8a +fn tc2 to bool +ret eq largestproduct s4 2 48 +end +note canonical case 46356a67-7e02-489e-8fea-321c2fa7b4a4 +fn tc3 to bool +ret eq largestproduct s6 3 504 +end +note canonical case a2dcb54b-2b8f-4993-92dd-5ce56dece64a +fn tc4 to bool +ret eq largestproduct s8 3 270 +end +note canonical case 673210a3-33cd-4708-940b-c482d7a88f9d +fn tc5 to bool +ret eq largestproduct s10 5 15120 +end +note canonical case 02acd5a6-3bbf-46df-8282-8b313a80a7c9 +fn tc6 to bool +ret eq largestproduct s12 6 23520 +end +note canonical case 76dcc407-21e9-424c-a98e-609f269622b5 +fn tc7 to bool +ret eq largestproduct s14 2 0 +end +note canonical case 6ef0df9f-52d4-4a5d-b210-f6fae5f20e19 +fn tc8 to bool +ret eq largestproduct s16 3 0 +end +note canonical case 0ae1ce53-d9ba-41bb-827f-2fceb64f058b +fn tc9 to bool +ret eq largestproduct s18 4 -2 +end +note canonical case 06bc8b90-0c51-4c54-ac22-3ec3893a079e +fn tc10 to bool +ret eq largestproduct part z20 0 0 0 1 +end +note canonical case 3ec0d92e-f2e2-4090-a380-70afee02f4c0 +fn tc11 to bool +ret eq largestproduct s22 0 1 +end +note canonical case 6cf66098-a6af-4223-aab1-26aeeefc7402 +fn tc12 to bool +ret eq largestproduct part z24 0 0 1 -2 +end +note canonical case 7a38f2d6-3c35-45f6-8d6f-12e6e32d4d74 +fn tc13 to bool +ret eq largestproduct s26 2 -3 +end +note canonical case c859f34a-9bfe-4897-9c2f-6d7f8598e7f0 +fn tc14 to bool +ret eq largestproduct s28 -1 -1 +end +fn main to int +tcheck tc0 c1 +tcheck tc1 c3 +tcheck tc2 c5 +tcheck tc3 c7 +tcheck tc4 c9 +tcheck tc5 c11 +tcheck tc6 c13 +tcheck tc7 c15 +tcheck tc8 c17 +tcheck tc9 c19 +tcheck tc10 c21 +tcheck tc11 c23 +tcheck tc12 c25 +tcheck tc13 c27 +tcheck tc14 c29 +ret int get tfails 0 +end diff --git a/exercises/luhn/.meta/example.om b/exercises/luhn/.meta/example.om new file mode 100644 index 0000000..6c27a1c --- /dev/null +++ b/exercises/luhn/.meta/example.om @@ -0,0 +1,29 @@ +note Exercism-style exercise "luhn": https://exercism.org/tracks/python/exercises/luhn +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note valid is true when s, after removing spaces, is at least two decimal digits (and nothing else) +note that pass the Luhn checksum. +note Check it with: sh exercise.sh exercises/luhn +om2 +fn valid s view uchar * to bool +n = ulong 0 +t = ulong 0 +for i ulong len s +c = get s sub sub len s 1 i +if eq c 32 +continue +end +if or lt c 48 gt c 57 +ret false +end +d = ulong sub c 48 +if eq rem n 2 1 +d = mul d 2 +if gt d 9 +d = sub d 9 +end +end +t = add t d +n = add n 1 +end +ret and gt n 1 eq rem t 10 0 +end diff --git a/exercises/luhn/luhn.om b/exercises/luhn/luhn.om new file mode 100644 index 0000000..61c2ff3 --- /dev/null +++ b/exercises/luhn/luhn.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "luhn": https://exercism.org/tracks/python/exercises/luhn +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note valid is true when s, after removing spaces, is at least two decimal digits (and nothing else) +note that pass the Luhn checksum. +note Check it with: sh exercise.sh exercises/luhn +om2 +fn valid s view uchar * to bool +note replace this line with your solution +trap 13 +end diff --git a/exercises/luhn/test.om b/exercises/luhn/test.om new file mode 100644 index 0000000..4361699 --- /dev/null +++ b/exercises/luhn/test.om @@ -0,0 +1,206 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/luhn/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 22 cases kept, 0 skipped +text s0 "1" +text c1 "valid: single digit strings can not be valid" +text s2 "0" +text c3 "valid: a single zero is invalid" +text s4 "059" +text c5 "valid: a simple valid SIN that remains valid if reversed" +text s6 "59" +text c7 "valid: a simple valid SIN that becomes invalid if reversed" +text s8 "055 444 285" +text c9 "valid: a valid Canadian SIN" +text s10 "055 444 286" +text c11 "valid: invalid Canadian SIN" +text s12 "8273 1232 7352 0569" +text c13 "valid: invalid credit card" +text s14 "1 2345 6789 1234 5678 9012" +text c15 "valid: invalid long number with an even remainder" +text s16 "1 2345 6789 1234 5678 9013" +text c17 "valid: invalid long number with a remainder divisible by 5" +text s18 "095 245 88" +text c19 "valid: valid number with an even number of digits" +text s20 "234 567 891 234" +text c21 "valid: valid number with an odd number of spaces" +text s22 "059a" +text c23 "valid: valid strings with a non-digit added at the end become invalid" +text s24 "055-444-285" +text c25 "valid: valid strings with punctuation included become invalid" +text s26 "055# 444$ 285" +text c27 "valid: valid strings with symbols included become invalid" +text s28 " 0" +text c29 "valid: single zero with space is invalid" +text s30 "0000 0" +text c31 "valid: more than a single zero is valid" +text s32 "091" +text c33 "valid: input digit 9 is correctly converted to output digit 9" +text s34 "9999999999 9999999999 9999999999 9999999999" +text c35 "valid: very long input is valid" +text s36 "109" +text c37 "valid: valid luhn with an odd number of digits and non zero first digit" +text s38 "055b 444 285" +text c39 "valid: using ascii value for non-doubled non-digit isn't allowed" +text s40 ":9" +text c41 "valid: using ascii value for doubled non-digit isn't allowed" +text s42 "59%59" +text c43 "valid: non-numeric, non-space char in the middle with a sum that's divisible by 10 isn't allowed" +note canonical case 792a7082-feb7-48c7-b88b-bbfec160865e +fn tc0 to bool +ret eq valid s0 false +end +note canonical case 698a7924-64d4-4d89-8daa-32e1aadc271e +fn tc1 to bool +ret eq valid s2 false +end +note canonical case 73c2f62b-9b10-4c9f-9a04-83cee7367965 +fn tc2 to bool +ret eq valid s4 true +end +note canonical case 9369092e-b095-439f-948d-498bd076be11 +fn tc3 to bool +ret eq valid s6 true +end +note canonical case 8f9f2350-1faf-4008-ba84-85cbb93ffeca +fn tc4 to bool +ret eq valid s8 true +end +note canonical case 1cdcf269-6560-44fc-91f6-5819a7548737 +fn tc5 to bool +ret eq valid s10 false +end +note canonical case 656c48c1-34e8-4e60-9a5a-aad8a367810a +fn tc6 to bool +ret eq valid s12 false +end +note canonical case 20e67fad-2121-43ed-99a8-14b5b856adb9 +fn tc7 to bool +ret eq valid s14 false +end +note canonical case 7e7c9fc1-d994-457c-811e-d390d52fba5e +fn tc8 to bool +ret eq valid s16 false +end +note canonical case ad2a0c5f-84ed-4e5b-95da-6011d6f4f0aa +fn tc9 to bool +ret eq valid s18 true +end +note canonical case ef081c06-a41f-4761-8492-385e13c8202d +fn tc10 to bool +ret eq valid s20 true +end +note canonical case bef66f64-6100-4cbb-8f94-4c9713c5e5b2 +fn tc11 to bool +ret eq valid s22 false +end +note canonical case 2177e225-9ce7-40f6-b55d-fa420e62938e +fn tc12 to bool +ret eq valid s24 false +end +note canonical case ebf04f27-9698-45e1-9afe-7e0851d0fe8d +fn tc13 to bool +ret eq valid s26 false +end +note canonical case 08195c5e-ce7f-422c-a5eb-3e45fece68ba +fn tc14 to bool +ret eq valid s28 false +end +note canonical case 12e63a3c-f866-4a79-8c14-b359fc386091 +fn tc15 to bool +ret eq valid s30 true +end +note canonical case ab56fa80-5de8-4735-8a4a-14dae588663e +fn tc16 to bool +ret eq valid s32 true +end +note canonical case b9887ee8-8337-46c5-bc45-3bcab51bc36f +fn tc17 to bool +ret eq valid s34 true +end +note canonical case 8a7c0e24-85ea-4154-9cf1-c2db90eabc08 +fn tc18 to bool +ret eq valid s36 true +end +note canonical case 39a06a5a-5bad-4e0f-b215-b042d46209b1 +fn tc19 to bool +ret eq valid s38 false +end +note canonical case f94cf191-a62f-4868-bc72-7253114aa157 +fn tc20 to bool +ret eq valid s40 false +end +note canonical case 8b72ad26-c8be-49a2-b99c-bcc3bf631b33 +fn tc21 to bool +ret eq valid s42 false +end +fn main to int +tcheck tc0 c1 +tcheck tc1 c3 +tcheck tc2 c5 +tcheck tc3 c7 +tcheck tc4 c9 +tcheck tc5 c11 +tcheck tc6 c13 +tcheck tc7 c15 +tcheck tc8 c17 +tcheck tc9 c19 +tcheck tc10 c21 +tcheck tc11 c23 +tcheck tc12 c25 +tcheck tc13 c27 +tcheck tc14 c29 +tcheck tc15 c31 +tcheck tc16 c33 +tcheck tc17 c35 +tcheck tc18 c37 +tcheck tc19 c39 +tcheck tc20 c41 +tcheck tc21 c43 +ret int get tfails 0 +end diff --git a/exercises/nth-prime/.meta/example.om b/exercises/nth-prime/.meta/example.om new file mode 100644 index 0000000..4fb93be --- /dev/null +++ b/exercises/nth-prime/.meta/example.om @@ -0,0 +1,29 @@ +note Exercism-style exercise "nth-prime": https://exercism.org/tracks/python/exercises/nth-prime +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note prime is the n-th prime number (prime 1 is 2), or 0 for n = 0, which has no prime. +note Check it with: sh exercise.sh exercises/nth-prime +om2 +fn isprime n ulong to bool +d = ulong 2 +while le mul d d n bound 999999 +if eq rem n d 0 +ret false +end +d = add d 1 +end +ret true +end +fn prime n ulong to ulong +if eq n 0 +ret 0 +end +c = ulong 0 +v = ulong 1 +while lt c n bound 999999 +v = add v 1 +if isprime v +c = add c 1 +end +end +ret v +end diff --git a/exercises/nth-prime/nth-prime.om b/exercises/nth-prime/nth-prime.om new file mode 100644 index 0000000..7760d73 --- /dev/null +++ b/exercises/nth-prime/nth-prime.om @@ -0,0 +1,9 @@ +note Exercism-style exercise "nth-prime": https://exercism.org/tracks/python/exercises/nth-prime +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note prime is the n-th prime number (prime 1 is 2), or 0 for n = 0, which has no prime. +note Check it with: sh exercise.sh exercises/nth-prime +om2 +fn prime n ulong to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/nth-prime/test.om b/exercises/nth-prime/test.om new file mode 100644 index 0000000..d79dccd --- /dev/null +++ b/exercises/nth-prime/test.om @@ -0,0 +1,82 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/nth-prime/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 5 cases kept, 0 skipped +text c0 "prime: first prime" +text c1 "prime: second prime" +text c2 "prime: sixth prime" +text c3 "prime: big prime" +text c4 "prime: there is no zeroth prime" +note canonical case 75c65189-8aef-471a-81de-0a90c728160c +fn tc0 to bool +ret eq prime 1 2 +end +note canonical case 2c38804c-295f-4701-b728-56dea34fd1a0 +fn tc1 to bool +ret eq prime 2 3 +end +note canonical case 56692534-781e-4e8c-b1f9-3e82c1640259 +fn tc2 to bool +ret eq prime 6 13 +end +note canonical case fce1e979-0edb-412d-93aa-2c744e8f50ff +fn tc3 to bool +ret eq prime 10001 104743 +end +note canonical case bd0a9eae-6df7-485b-a144-80e13c7d55b2 +fn tc4 to bool +ret eq prime 0 0 +end +fn main to int +tcheck tc0 c0 +tcheck tc1 c1 +tcheck tc2 c2 +tcheck tc3 c3 +tcheck tc4 c4 +ret int get tfails 0 +end diff --git a/exercises/nucleotide-count/.meta/example.om b/exercises/nucleotide-count/.meta/example.om new file mode 100644 index 0000000..d698447 --- /dev/null +++ b/exercises/nucleotide-count/.meta/example.om @@ -0,0 +1,32 @@ +note Exercism-style exercise "nucleotide-count": https://exercism.org/tracks/python/exercises/nucleotide-count +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note nucleotidecounts writes how many A, C, G and T the DNA strand holds into o[0] .. o[3] and is +note true; it is false when the strand holds any other byte. +note Check it with: sh exercise.sh exercises/nucleotide-count +om2 +fn nucleotidecounts strand view uchar * o edit ulong * to bool +for j ulong 4 +put o j 0 +end +for i ulong len strand +c = get strand i +k = ulong 4 +if eq c 65 +k = ulong 0 +end +if eq c 67 +k = ulong 1 +end +if eq c 71 +k = ulong 2 +end +if eq c 84 +k = ulong 3 +end +if eq k 4 +ret false +end +put o k add get o k 1 +end +ret true +end diff --git a/exercises/nucleotide-count/nucleotide-count.om b/exercises/nucleotide-count/nucleotide-count.om new file mode 100644 index 0000000..7a75a96 --- /dev/null +++ b/exercises/nucleotide-count/nucleotide-count.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "nucleotide-count": https://exercism.org/tracks/python/exercises/nucleotide-count +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note nucleotidecounts writes how many A, C, G and T the DNA strand holds into o[0] .. o[3] and is +note true; it is false when the strand holds any other byte. +note Check it with: sh exercise.sh exercises/nucleotide-count +om2 +fn nucleotidecounts strand view uchar * o edit ulong * to bool +note replace this line with your solution +trap 13 +end diff --git a/exercises/nucleotide-count/test.om b/exercises/nucleotide-count/test.om new file mode 100644 index 0000000..6c22233 --- /dev/null +++ b/exercises/nucleotide-count/test.om @@ -0,0 +1,139 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/nucleotide-count/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 5 cases kept, 0 skipped +fn tueq a view ulong * b view ulong * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text z0 "z" +text c1 "nucleotideCounts: empty strand" +text s2 "G" +text c3 "nucleotideCounts: can count one nucleotide in single-character input" +text s4 "GGGGGGG" +text c5 "nucleotideCounts: strand with repeated nucleotide" +text s6 "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC" +text c7 "nucleotideCounts: strand with multiple nucleotides" +text s8 "AGXXACT" +text c9 "nucleotideCounts: strand with invalid nucleotides" +note canonical case 3e5c30a8-87e2-4845-a815-a49671ade970 +fn tc0 to bool +o = new ulong 4 0 +a0 = new ulong 4 0 +put a0 0 0 +put a0 1 0 +put a0 2 0 +put a0 3 0 +ok = nucleotidecounts part z0 0 0 o +if not ok +ret false +end +ret tueq o a0 +end +note canonical case a0ea42a6-06d9-4ac6-828c-7ccaccf98fec +fn tc1 to bool +o = new ulong 4 0 +a0 = new ulong 4 0 +put a0 0 0 +put a0 1 0 +put a0 2 1 +put a0 3 0 +ok = nucleotidecounts s2 o +if not ok +ret false +end +ret tueq o a0 +end +note canonical case eca0d565-ed8c-43e7-9033-6cefbf5115b5 +fn tc2 to bool +o = new ulong 4 0 +a0 = new ulong 4 0 +put a0 0 0 +put a0 1 0 +put a0 2 7 +put a0 3 0 +ok = nucleotidecounts s4 o +if not ok +ret false +end +ret tueq o a0 +end +note canonical case 40a45eac-c83f-4740-901a-20b22d15a39f +fn tc3 to bool +o = new ulong 4 0 +a0 = new ulong 4 0 +put a0 0 20 +put a0 1 12 +put a0 2 17 +put a0 3 21 +ok = nucleotidecounts s6 o +if not ok +ret false +end +ret tueq o a0 +end +note canonical case b4c47851-ee9e-4b0a-be70-a86e343bd851 +fn tc4 to bool +o = new ulong 4 0 +ret not nucleotidecounts s8 o +end +fn main to int +tcheck tc0 c1 +tcheck tc1 c3 +tcheck tc2 c5 +tcheck tc3 c7 +tcheck tc4 c9 +ret int get tfails 0 +end diff --git a/exercises/pangram/.meta/example.om b/exercises/pangram/.meta/example.om new file mode 100644 index 0000000..425b232 --- /dev/null +++ b/exercises/pangram/.meta/example.om @@ -0,0 +1,23 @@ +note Exercism-style exercise "pangram": https://exercism.org/tracks/python/exercises/pangram +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note ispangram is true when s contains every letter a .. z at least once, ignoring case. +note Check it with: sh exercise.sh exercises/pangram +om2 +fn ispangram s view uchar * to bool +seen = new uchar 26 0 +n = ulong 0 +for i ulong len s +c = get s i +if and ge c 65 le c 90 +c = add c 32 +end +if and ge c 97 le c 122 +k = ulong sub c 97 +if eq get seen k 0 +put seen k 1 +n = add n 1 +end +end +end +ret eq n 26 +end diff --git a/exercises/pangram/pangram.om b/exercises/pangram/pangram.om new file mode 100644 index 0000000..aefaec1 --- /dev/null +++ b/exercises/pangram/pangram.om @@ -0,0 +1,9 @@ +note Exercism-style exercise "pangram": https://exercism.org/tracks/python/exercises/pangram +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note ispangram is true when s contains every letter a .. z at least once, ignoring case. +note Check it with: sh exercise.sh exercises/pangram +om2 +fn ispangram s view uchar * to bool +note replace this line with your solution +trap 13 +end diff --git a/exercises/pangram/test.om b/exercises/pangram/test.om new file mode 100644 index 0000000..4d7ae2f --- /dev/null +++ b/exercises/pangram/test.om @@ -0,0 +1,122 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/pangram/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 10 cases kept, 0 skipped, 1 superseded (reimplemented) cases omitted +text z0 "z" +text c1 "isPangram: empty sentence" +text s2 "abcdefghijklmnopqrstuvwxyz" +text c3 "isPangram: perfect lower case" +text s4 "the quick brown fox jumps over the lazy dog" +text c5 "isPangram: only lower case" +text s6 "a quick movement of the enemy will jeopardize five gunboats" +text c7 "isPangram: missing the letter 'x'" +text s8 "five boxing wizards jump quickly at it" +text c9 "isPangram: missing the letter 'h'" +text s10 "the_quick_brown_fox_jumps_over_the_lazy_dog" +text c11 "isPangram: with underscores" +text s12 "the 1 quick brown fox jumps over the 2 lazy dogs" +text c13 "isPangram: with numbers" +text s14 "7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog" +text c15 "isPangram: missing letters replaced by numbers" +text s16 "\"Five quacking Zephyrs jolt my wax bed.\"" +text c17 "isPangram: mixed case and punctuation" +text s18 "abcdefghijklm ABCDEFGHIJKLM" +text c19 "isPangram: a-m and A-M are 26 different characters but not a pangram" +note canonical case 64f61791-508e-4f5c-83ab-05de042b0149 +fn tc0 to bool +ret eq ispangram part z0 0 0 false +end +note canonical case 74858f80-4a4d-478b-8a5e-c6477e4e4e84 +fn tc1 to bool +ret eq ispangram s2 true +end +note canonical case 61288860-35ca-4abe-ba08-f5df76ecbdcd +fn tc2 to bool +ret eq ispangram s4 true +end +note canonical case 6564267d-8ac5-4d29-baf2-e7d2e304a743 +fn tc3 to bool +ret eq ispangram s6 false +end +note canonical case c79af1be-d715-4cdb-a5f2-b2fa3e7e0de0 +fn tc4 to bool +ret eq ispangram s8 false +end +note canonical case d835ec38-bc8f-48e4-9e36-eb232427b1df +fn tc5 to bool +ret eq ispangram s10 true +end +note canonical case 8cc1e080-a178-4494-b4b3-06982c9be2a8 +fn tc6 to bool +ret eq ispangram s12 true +end +note canonical case bed96b1c-ff95-45b8-9731-fdbdcb6ede9a +fn tc7 to bool +ret eq ispangram s14 false +end +note canonical case 938bd5d8-ade5-40e2-a2d9-55a338a01030 +fn tc8 to bool +ret eq ispangram s16 true +end +note canonical case 7138e389-83e4-4c6e-8413-1e40a0076951 +fn tc9 to bool +ret eq ispangram s18 false +end +fn main to int +tcheck tc0 c1 +tcheck tc1 c3 +tcheck tc2 c5 +tcheck tc3 c7 +tcheck tc4 c9 +tcheck tc5 c11 +tcheck tc6 c13 +tcheck tc7 c15 +tcheck tc8 c17 +tcheck tc9 c19 +ret int get tfails 0 +end diff --git a/exercises/pascals-triangle/.meta/example.om b/exercises/pascals-triangle/.meta/example.om new file mode 100644 index 0000000..6ee90da --- /dev/null +++ b/exercises/pascals-triangle/.meta/example.om @@ -0,0 +1,20 @@ +note Exercism-style exercise "pascals-triangle": https://exercism.org/tracks/python/exercises/pascals-triangle +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note rows writes the first count rows of Pascal's triangle into o, row after row (1, 1 1, 1 2 1, ...), +note and returns how many numbers it wrote. +note Check it with: sh exercise.sh exercises/pascals-triangle +om2 +fn rows count ulong o edit ulong * to ulong +for r ulong count +s = div mul r add r 1 2 +for c ulong add r 1 +v = ulong 1 +if and gt c 0 lt c r +b = sub sub add s c r 1 +v = add get o b get o add b 1 +end +put o add s c v +end +end +ret div mul count add count 1 2 +end diff --git a/exercises/pascals-triangle/pascals-triangle.om b/exercises/pascals-triangle/pascals-triangle.om new file mode 100644 index 0000000..ddc7a72 --- /dev/null +++ b/exercises/pascals-triangle/pascals-triangle.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "pascals-triangle": https://exercism.org/tracks/python/exercises/pascals-triangle +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note rows writes the first count rows of Pascal's triangle into o, row after row (1, 1 1, 1 2 1, ...), +note and returns how many numbers it wrote. +note Check it with: sh exercise.sh exercises/pascals-triangle +om2 +fn rows count ulong o edit ulong * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/pascals-triangle/test.om b/exercises/pascals-triangle/test.om new file mode 100644 index 0000000..94455c6 --- /dev/null +++ b/exercises/pascals-triangle/test.om @@ -0,0 +1,245 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/pascals-triangle/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 8 cases kept, 0 skipped +fn tueq a view ulong * b view ulong * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text c0 "rows: zero rows" +text c1 "rows: single row" +text c2 "rows: two rows" +text c3 "rows: three rows" +text c4 "rows: four rows" +text c5 "rows: five rows" +text c6 "rows: six rows" +text c7 "rows: ten rows" +note canonical case 9920ce55-9629-46d5-85d6-4201f4a4234d +fn tc0 to bool +o = new ulong 256 0 +n = rows 0 o +ret eq n 0 +end +note canonical case 70d643ce-a46d-4e93-af58-12d88dd01f21 +fn tc1 to bool +o = new ulong 256 0 +n = rows 1 o +a0 = new ulong 1 0 +put a0 0 1 +ret tueq part o 0 n a0 +end +note canonical case a6e5a2a2-fc9a-4b47-9f4f-ed9ad9fbe4bd +fn tc2 to bool +o = new ulong 256 0 +n = rows 2 o +a0 = new ulong 3 0 +put a0 0 1 +put a0 1 1 +put a0 2 1 +ret tueq part o 0 n a0 +end +note canonical case 97206a99-79ba-4b04-b1c5-3c0fa1e16925 +fn tc3 to bool +o = new ulong 256 0 +n = rows 3 o +a0 = new ulong 6 0 +put a0 0 1 +put a0 1 1 +put a0 2 1 +put a0 3 1 +put a0 4 2 +put a0 5 1 +ret tueq part o 0 n a0 +end +note canonical case 565a0431-c797-417c-a2c8-2935e01ce306 +fn tc4 to bool +o = new ulong 256 0 +n = rows 4 o +a0 = new ulong 10 0 +put a0 0 1 +put a0 1 1 +put a0 2 1 +put a0 3 1 +put a0 4 2 +put a0 5 1 +put a0 6 1 +put a0 7 3 +put a0 8 3 +put a0 9 1 +ret tueq part o 0 n a0 +end +note canonical case 06f9ea50-9f51-4eb2-b9a9-c00975686c27 +fn tc5 to bool +o = new ulong 256 0 +n = rows 5 o +a0 = new ulong 15 0 +put a0 0 1 +put a0 1 1 +put a0 2 1 +put a0 3 1 +put a0 4 2 +put a0 5 1 +put a0 6 1 +put a0 7 3 +put a0 8 3 +put a0 9 1 +put a0 10 1 +put a0 11 4 +put a0 12 6 +put a0 13 4 +put a0 14 1 +ret tueq part o 0 n a0 +end +note canonical case c3912965-ddb4-46a9-848e-3363e6b00b13 +fn tc6 to bool +o = new ulong 256 0 +n = rows 6 o +a0 = new ulong 21 0 +put a0 0 1 +put a0 1 1 +put a0 2 1 +put a0 3 1 +put a0 4 2 +put a0 5 1 +put a0 6 1 +put a0 7 3 +put a0 8 3 +put a0 9 1 +put a0 10 1 +put a0 11 4 +put a0 12 6 +put a0 13 4 +put a0 14 1 +put a0 15 1 +put a0 16 5 +put a0 17 10 +put a0 18 10 +put a0 19 5 +put a0 20 1 +ret tueq part o 0 n a0 +end +note canonical case 6cb26c66-7b57-4161-962c-81ec8c99f16b +fn tc7 to bool +o = new ulong 256 0 +n = rows 10 o +a0 = new ulong 55 0 +put a0 0 1 +put a0 1 1 +put a0 2 1 +put a0 3 1 +put a0 4 2 +put a0 5 1 +put a0 6 1 +put a0 7 3 +put a0 8 3 +put a0 9 1 +put a0 10 1 +put a0 11 4 +put a0 12 6 +put a0 13 4 +put a0 14 1 +put a0 15 1 +put a0 16 5 +put a0 17 10 +put a0 18 10 +put a0 19 5 +put a0 20 1 +put a0 21 1 +put a0 22 6 +put a0 23 15 +put a0 24 20 +put a0 25 15 +put a0 26 6 +put a0 27 1 +put a0 28 1 +put a0 29 7 +put a0 30 21 +put a0 31 35 +put a0 32 35 +put a0 33 21 +put a0 34 7 +put a0 35 1 +put a0 36 1 +put a0 37 8 +put a0 38 28 +put a0 39 56 +put a0 40 70 +put a0 41 56 +put a0 42 28 +put a0 43 8 +put a0 44 1 +put a0 45 1 +put a0 46 9 +put a0 47 36 +put a0 48 84 +put a0 49 126 +put a0 50 126 +put a0 51 84 +put a0 52 36 +put a0 53 9 +put a0 54 1 +ret tueq part o 0 n a0 +end +fn main to int +tcheck tc0 c0 +tcheck tc1 c1 +tcheck tc2 c2 +tcheck tc3 c3 +tcheck tc4 c4 +tcheck tc5 c5 +tcheck tc6 c6 +tcheck tc7 c7 +ret int get tfails 0 +end diff --git a/exercises/perfect-numbers/.meta/example.om b/exercises/perfect-numbers/.meta/example.om new file mode 100644 index 0000000..25ad266 --- /dev/null +++ b/exercises/perfect-numbers/.meta/example.om @@ -0,0 +1,32 @@ +note Exercism-style exercise "perfect-numbers": https://exercism.org/tracks/python/exercises/perfect-numbers +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note classify compares n with its aliquot sum (the sum of its divisors other than n): 1 perfect +note (equal), 2 abundant (greater), 3 deficient (smaller), 0 when n is not a positive integer. +note Check it with: sh exercise.sh exercises/perfect-numbers +om2 +fn classify n long to int +if le n 0 +ret 0 +end +s = long 0 +d = long 1 +while le mul d d n bound 999999 +if eq rem n d 0 +if ne d n +s = add s d +end +e = div n d +if and ne e d ne e n +s = add s e +end +end +d = add d 1 +end +if eq s n +ret 1 +end +if gt s n +ret 2 +end +ret 3 +end diff --git a/exercises/perfect-numbers/perfect-numbers.om b/exercises/perfect-numbers/perfect-numbers.om new file mode 100644 index 0000000..fd036f7 --- /dev/null +++ b/exercises/perfect-numbers/perfect-numbers.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "perfect-numbers": https://exercism.org/tracks/python/exercises/perfect-numbers +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note classify compares n with its aliquot sum (the sum of its divisors other than n): 1 perfect +note (equal), 2 abundant (greater), 3 deficient (smaller), 0 when n is not a positive integer. +note Check it with: sh exercise.sh exercises/perfect-numbers +om2 +fn classify n long to int +note replace this line with your solution +trap 13 +end diff --git a/exercises/perfect-numbers/test.om b/exercises/perfect-numbers/test.om new file mode 100644 index 0000000..5883ca8 --- /dev/null +++ b/exercises/perfect-numbers/test.om @@ -0,0 +1,136 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/perfect-numbers/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 14 cases kept, 0 skipped +text c0 "classify: Perfect numbers / Smallest perfect number is classified correctly" +text c1 "classify: Perfect numbers / Medium perfect number is classified correctly" +text c2 "classify: Perfect numbers / Large perfect number is classified correctly" +text c3 "classify: Abundant numbers / Smallest abundant number is classified correctly" +text c4 "classify: Abundant numbers / Medium abundant number is classified correctly" +text c5 "classify: Abundant numbers / Large abundant number is classified correctly" +text c6 "classify: Abundant numbers / Perfect square abundant number is classified correctly" +text c7 "classify: Deficient numbers / Smallest prime deficient number is classified correctly" +text c8 "classify: Deficient numbers / Smallest non-prime deficient number is classified correctly" +text c9 "classify: Deficient numbers / Medium deficient number is classified correctly" +text c10 "classify: Deficient numbers / Large deficient number is classified correctly" +text c11 "classify: Deficient numbers / Edge case (no factors other than itself) is classified correctly" +text c12 "classify: Invalid inputs / Zero is rejected (as it is not a positive integer)" +text c13 "classify: Invalid inputs / Negative integer is rejected (as it is not a positive integer)" +note canonical case 163e8e86-7bfd-4ee2-bd68-d083dc3381a3 +fn tc0 to bool +ret eq classify 6 1 +end +note canonical case 169a7854-0431-4ae0-9815-c3b6d967436d +fn tc1 to bool +ret eq classify 28 1 +end +note canonical case ee3627c4-7b36-4245-ba7c-8727d585f402 +fn tc2 to bool +ret eq classify 33550336 1 +end +note canonical case 80ef7cf8-9ea8-49b9-8b2d-d9cb3db3ed7e +fn tc3 to bool +ret eq classify 12 2 +end +note canonical case 3e300e0d-1a12-4f11-8c48-d1027165ab60 +fn tc4 to bool +ret eq classify 30 2 +end +note canonical case ec7792e6-8786-449c-b005-ce6dd89a772b +fn tc5 to bool +ret eq classify 33550335 2 +end +note canonical case 05f15b93-849c-45e9-9c7d-1ea131ef7d10 +fn tc6 to bool +ret eq classify 196 2 +end +note canonical case e610fdc7-2b6e-43c3-a51c-b70fb37413ba +fn tc7 to bool +ret eq classify 2 3 +end +note canonical case 0beb7f66-753a-443f-8075-ad7fbd9018f3 +fn tc8 to bool +ret eq classify 4 3 +end +note canonical case 1c802e45-b4c6-4962-93d7-1cad245821ef +fn tc9 to bool +ret eq classify 32 3 +end +note canonical case 47dd569f-9e5a-4a11-9a47-a4e91c8c28aa +fn tc10 to bool +ret eq classify 33550337 3 +end +note canonical case a696dec8-6147-4d68-afad-d38de5476a56 +fn tc11 to bool +ret eq classify 1 3 +end +note canonical case 72445cee-660c-4d75-8506-6c40089dc302 +fn tc12 to bool +ret eq classify 0 0 +end +note canonical case 2d72ce2c-6802-49ac-8ece-c790ba3dae13 +fn tc13 to bool +ret eq classify -1 0 +end +fn main to int +tcheck tc0 c0 +tcheck tc1 c1 +tcheck tc2 c2 +tcheck tc3 c3 +tcheck tc4 c4 +tcheck tc5 c5 +tcheck tc6 c6 +tcheck tc7 c7 +tcheck tc8 c8 +tcheck tc9 c9 +tcheck tc10 c10 +tcheck tc11 c11 +tcheck tc12 c12 +tcheck tc13 c13 +ret int get tfails 0 +end diff --git a/exercises/prime-factors/.meta/example.om b/exercises/prime-factors/.meta/example.om new file mode 100644 index 0000000..f6cddcc --- /dev/null +++ b/exercises/prime-factors/.meta/example.om @@ -0,0 +1,25 @@ +note Exercism-style exercise "prime-factors": https://exercism.org/tracks/python/exercises/prime-factors +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note factors writes the prime factors of value into o, smallest first and with repetition, and +note returns how many it wrote. +note Check it with: sh exercise.sh exercises/prime-factors +om2 +fn factors value ulong o edit ulong * to ulong +n = value +k = ulong 0 +d = ulong 2 +while le mul d d n bound 999999 +if eq rem n d 0 +put o k d +k = add k 1 +n = div n d +else +d = add d 1 +end +end +if gt n 1 +put o k n +k = add k 1 +end +ret k +end diff --git a/exercises/prime-factors/prime-factors.om b/exercises/prime-factors/prime-factors.om new file mode 100644 index 0000000..466a567 --- /dev/null +++ b/exercises/prime-factors/prime-factors.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "prime-factors": https://exercism.org/tracks/python/exercises/prime-factors +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note factors writes the prime factors of value into o, smallest first and with repetition, and +note returns how many it wrote. +note Check it with: sh exercise.sh exercises/prime-factors +om2 +fn factors value ulong o edit ulong * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/prime-factors/test.om b/exercises/prime-factors/test.om new file mode 100644 index 0000000..afc39e7 --- /dev/null +++ b/exercises/prime-factors/test.om @@ -0,0 +1,198 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/prime-factors/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 12 cases kept, 0 skipped +fn tueq a view ulong * b view ulong * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text c0 "factors: no factors" +text c1 "factors: prime number" +text c2 "factors: another prime number" +text c3 "factors: square of a prime" +text c4 "factors: product of first prime" +text c5 "factors: cube of a prime" +text c6 "factors: product of second prime" +text c7 "factors: product of third prime" +text c8 "factors: product of first and second prime" +text c9 "factors: product of primes and non-primes" +text c10 "factors: product of primes" +text c11 "factors: factors include a large prime" +note canonical case 924fc966-a8f5-4288-82f2-6b9224819ccd +fn tc0 to bool +o = new ulong 256 0 +n = factors 1 o +ret eq n 0 +end +note canonical case 17e30670-b105-4305-af53-ddde182cb6ad +fn tc1 to bool +o = new ulong 256 0 +n = factors 2 o +a0 = new ulong 1 0 +put a0 0 2 +ret tueq part o 0 n a0 +end +note canonical case 238d57c8-4c12-42ef-af34-ae4929f94789 +fn tc2 to bool +o = new ulong 256 0 +n = factors 3 o +a0 = new ulong 1 0 +put a0 0 3 +ret tueq part o 0 n a0 +end +note canonical case f59b8350-a180-495a-8fb1-1712fbee1158 +fn tc3 to bool +o = new ulong 256 0 +n = factors 9 o +a0 = new ulong 2 0 +put a0 0 3 +put a0 1 3 +ret tueq part o 0 n a0 +end +note canonical case 756949d3-3158-4e3d-91f2-c4f9f043ee70 +fn tc4 to bool +o = new ulong 256 0 +n = factors 4 o +a0 = new ulong 2 0 +put a0 0 2 +put a0 1 2 +ret tueq part o 0 n a0 +end +note canonical case bc8c113f-9580-4516-8669-c5fc29512ceb +fn tc5 to bool +o = new ulong 256 0 +n = factors 8 o +a0 = new ulong 3 0 +put a0 0 2 +put a0 1 2 +put a0 2 2 +ret tueq part o 0 n a0 +end +note canonical case 7d6a3300-a4cb-4065-bd33-0ced1de6cb44 +fn tc6 to bool +o = new ulong 256 0 +n = factors 27 o +a0 = new ulong 3 0 +put a0 0 3 +put a0 1 3 +put a0 2 3 +ret tueq part o 0 n a0 +end +note canonical case 073ac0b2-c915-4362-929d-fc45f7b9a9e4 +fn tc7 to bool +o = new ulong 256 0 +n = factors 625 o +a0 = new ulong 4 0 +put a0 0 5 +put a0 1 5 +put a0 2 5 +put a0 3 5 +ret tueq part o 0 n a0 +end +note canonical case 6e0e4912-7fb6-47f3-a9ad-dbcd79340c75 +fn tc8 to bool +o = new ulong 256 0 +n = factors 6 o +a0 = new ulong 2 0 +put a0 0 2 +put a0 1 3 +ret tueq part o 0 n a0 +end +note canonical case 00485cd3-a3fe-4fbe-a64a-a4308fc1f870 +fn tc9 to bool +o = new ulong 256 0 +n = factors 12 o +a0 = new ulong 3 0 +put a0 0 2 +put a0 1 2 +put a0 2 3 +ret tueq part o 0 n a0 +end +note canonical case 02251d54-3ca1-4a9b-85e1-b38f4b0ccb91 +fn tc10 to bool +o = new ulong 256 0 +n = factors 901255 o +a0 = new ulong 4 0 +put a0 0 5 +put a0 1 17 +put a0 2 23 +put a0 3 461 +ret tueq part o 0 n a0 +end +note canonical case 070cf8dc-e202-4285-aa37-8d775c9cd473 +fn tc11 to bool +o = new ulong 256 0 +n = factors 93819012551 o +a0 = new ulong 3 0 +put a0 0 11 +put a0 1 9539 +put a0 2 894119 +ret tueq part o 0 n a0 +end +fn main to int +tcheck tc0 c0 +tcheck tc1 c1 +tcheck tc2 c2 +tcheck tc3 c3 +tcheck tc4 c4 +tcheck tc5 c5 +tcheck tc6 c6 +tcheck tc7 c7 +tcheck tc8 c8 +tcheck tc9 c9 +tcheck tc10 c10 +tcheck tc11 c11 +ret int get tfails 0 +end diff --git a/exercises/protein-translation/.meta/example.om b/exercises/protein-translation/.meta/example.om new file mode 100644 index 0000000..21a0b5b --- /dev/null +++ b/exercises/protein-translation/.meta/example.om @@ -0,0 +1,105 @@ +note Exercism-style exercise "protein-translation": https://exercism.org/tracks/python/exercises/protein-translation +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note proteins translates the RNA strand codon by codon into o, protein names joined by commas: +note AUG Methionine; UUU UUC Phenylalanine; UUA UUG Leucine; UCU UCC UCA UCG Serine; UAU UAC +note Tyrosine; UGU UGC Cysteine; UGG Tryptophan; UAA UAG UGA stop translation. Returns the number +note of bytes written, or -1 when a codon before the first stop is invalid or incomplete. +note Check it with: sh exercise.sh exercises/protein-translation +om2 +text pn0 "Methionine" +text pn1 "Phenylalanine" +text pn2 "Leucine" +text pn3 "Serine" +text pn4 "Tyrosine" +text pn5 "Cysteine" +text pn6 "Tryptophan" +fn app o edit uchar * p ulong s view uchar * to ulong +q = p +for i ulong len s +put o q get s i +q = add q 1 +end +ret q +end +note codon id: 0 .. 6 a protein as in pn0 .. pn6, 7 stop, 8 invalid +fn codon a uchar b uchar c uchar to ulong +if and and eq a 65 eq b 85 eq c 71 +ret 0 +end +if ne a 85 +ret 8 +end +if not or or or eq c 85 eq c 67 eq c 65 eq c 71 +ret 8 +end +if eq b 85 +if or eq c 85 eq c 67 +ret 1 +end +ret 2 +end +if eq b 67 +ret 3 +end +if eq b 65 +if or eq c 85 eq c 67 +ret 4 +end +ret 7 +end +if eq b 71 +if or eq c 85 eq c 67 +ret 5 +end +if eq c 71 +ret 6 +end +ret 7 +end +ret 8 +end +fn appname o edit uchar * p ulong k ulong to ulong +if eq k 0 +ret app o p pn0 +end +if eq k 1 +ret app o p pn1 +end +if eq k 2 +ret app o p pn2 +end +if eq k 3 +ret app o p pn3 +end +if eq k 4 +ret app o p pn4 +end +if eq k 5 +ret app o p pn5 +end +ret app o p pn6 +end +fn proteins strand view uchar * o edit uchar * to long +p = ulong 0 +i = ulong 0 +n = len strand +while lt i n bound 999999 +if gt add i 3 n +ret -1 +end +k = codon get strand i get strand add i 1 get strand add i 2 +if eq k 8 +ret -1 +end +if eq k 7 +break +end +if gt p 0 +put o p 44 +p = add p 1 +end +p = appname o p k +i = add i 3 +end +ret long p +end diff --git a/exercises/protein-translation/protein-translation.om b/exercises/protein-translation/protein-translation.om new file mode 100644 index 0000000..0a844aa --- /dev/null +++ b/exercises/protein-translation/protein-translation.om @@ -0,0 +1,12 @@ +note Exercism-style exercise "protein-translation": https://exercism.org/tracks/python/exercises/protein-translation +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note proteins translates the RNA strand codon by codon into o, protein names joined by commas: +note AUG Methionine; UUU UUC Phenylalanine; UUA UUG Leucine; UCU UCC UCA UCG Serine; UAU UAC +note Tyrosine; UGU UGC Cysteine; UGG Tryptophan; UAA UAG UGA stop translation. Returns the number +note of bytes written, or -1 when a codon before the first stop is invalid or incomplete. +note Check it with: sh exercise.sh exercises/protein-translation +om2 +fn proteins strand view uchar * o edit uchar * to long +note replace this line with your solution +trap 13 +end diff --git a/exercises/protein-translation/test.om b/exercises/protein-translation/test.om new file mode 100644 index 0000000..354e7da --- /dev/null +++ b/exercises/protein-translation/test.om @@ -0,0 +1,438 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/protein-translation/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 30 cases kept, 0 skipped, 1 superseded (reimplemented) cases omitted +fn tbeq a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text z0 "z" +text c1 "proteins: Empty RNA sequence results in no proteins" +text s2 "AUG" +text e3 "Methionine" +text c4 "proteins: Methionine RNA sequence" +text s5 "UUU" +text e6 "Phenylalanine" +text c7 "proteins: Phenylalanine RNA sequence 1" +text s8 "UUC" +text e9 "Phenylalanine" +text c10 "proteins: Phenylalanine RNA sequence 2" +text s11 "UUA" +text e12 "Leucine" +text c13 "proteins: Leucine RNA sequence 1" +text s14 "UUG" +text e15 "Leucine" +text c16 "proteins: Leucine RNA sequence 2" +text s17 "UCU" +text e18 "Serine" +text c19 "proteins: Serine RNA sequence 1" +text s20 "UCC" +text e21 "Serine" +text c22 "proteins: Serine RNA sequence 2" +text s23 "UCA" +text e24 "Serine" +text c25 "proteins: Serine RNA sequence 3" +text s26 "UCG" +text e27 "Serine" +text c28 "proteins: Serine RNA sequence 4" +text s29 "UAU" +text e30 "Tyrosine" +text c31 "proteins: Tyrosine RNA sequence 1" +text s32 "UAC" +text e33 "Tyrosine" +text c34 "proteins: Tyrosine RNA sequence 2" +text s35 "UGU" +text e36 "Cysteine" +text c37 "proteins: Cysteine RNA sequence 1" +text s38 "UGC" +text e39 "Cysteine" +text c40 "proteins: Cysteine RNA sequence 2" +text s41 "UGG" +text e42 "Tryptophan" +text c43 "proteins: Tryptophan RNA sequence" +text s44 "UAA" +text c45 "proteins: STOP codon RNA sequence 1" +text s46 "UAG" +text c47 "proteins: STOP codon RNA sequence 2" +text s48 "UGA" +text c49 "proteins: STOP codon RNA sequence 3" +text s50 "UUUUUU" +text e51 "Phenylalanine,Phenylalanine" +text c52 "proteins: Sequence of two protein codons translates into proteins" +text s53 "UUAUUG" +text e54 "Leucine,Leucine" +text c55 "proteins: Sequence of two different protein codons translates into proteins" +text s56 "AUGUUUUGG" +text e57 "Methionine,Phenylalanine,Tryptophan" +text c58 "proteins: Translate RNA strand into correct protein list" +text s59 "UAGUGG" +text c60 "proteins: Translation stops if STOP codon at beginning of sequence" +text s61 "UGGUAG" +text e62 "Tryptophan" +text c63 "proteins: Translation stops if STOP codon at end of two-codon sequence" +text s64 "AUGUUUUAA" +text e65 "Methionine,Phenylalanine" +text c66 "proteins: Translation stops if STOP codon at end of three-codon sequence" +text s67 "UGGUAGUGG" +text e68 "Tryptophan" +text c69 "proteins: Translation stops if STOP codon in middle of three-codon sequence" +text s70 "UGGUGUUAUUAAUGGUUU" +text e71 "Tryptophan,Cysteine,Tyrosine" +text c72 "proteins: Translation stops if STOP codon in middle of six-codon sequence" +text s73 "AUGAUG" +text e74 "Methionine,Methionine" +text c75 "proteins: Sequence of two non-STOP codons does not translate to a STOP codon" +text s76 "XYZ" +text c77 "proteins: Unknown amino acids, not part of a codon, can't translate" +text s78 "AUGU" +text c79 "proteins: Incomplete RNA sequence can't translate" +text s80 "UUCUUCUAAUGGU" +text e81 "Phenylalanine,Phenylalanine" +text c82 "proteins: Incomplete RNA sequence can translate if valid until a STOP codon" +note canonical case 2c44f7bf-ba20-43f7-a3bf-f2219c0c3f98 +fn tc0 to bool +o = new uchar 1024 0 +n = proteins part z0 0 0 o +if lt n 0 +ret false +end +ret eq ulong n 0 +end +note canonical case 96d3d44f-34a2-4db4-84cd-fff523e069be +fn tc1 to bool +o = new uchar 1024 0 +n = proteins s2 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e3 +end +note canonical case 1b4c56d8-d69f-44eb-be0e-7b17546143d9 +fn tc2 to bool +o = new uchar 1024 0 +n = proteins s5 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e6 +end +note canonical case 81b53646-bd57-4732-b2cb-6b1880e36d11 +fn tc3 to bool +o = new uchar 1024 0 +n = proteins s8 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e9 +end +note canonical case 42f69d4f-19d2-4d2c-a8b0-f0ae9ee1b6b4 +fn tc4 to bool +o = new uchar 1024 0 +n = proteins s11 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e12 +end +note canonical case ac5edadd-08ed-40a3-b2b9-d82bb50424c4 +fn tc5 to bool +o = new uchar 1024 0 +n = proteins s14 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e15 +end +note canonical case 8bc36e22-f984-44c3-9f6b-ee5d4e73f120 +fn tc6 to bool +o = new uchar 1024 0 +n = proteins s17 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e18 +end +note canonical case 5c3fa5da-4268-44e5-9f4b-f016ccf90131 +fn tc7 to bool +o = new uchar 1024 0 +n = proteins s20 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e21 +end +note canonical case 00579891-b594-42b4-96dc-7ff8bf519606 +fn tc8 to bool +o = new uchar 1024 0 +n = proteins s23 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e24 +end +note canonical case 08c61c3b-fa34-4950-8c4a-133945570ef6 +fn tc9 to bool +o = new uchar 1024 0 +n = proteins s26 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e27 +end +note canonical case 54e1e7d8-63c0-456d-91d2-062c72f8eef5 +fn tc10 to bool +o = new uchar 1024 0 +n = proteins s29 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e30 +end +note canonical case 47bcfba2-9d72-46ad-bbce-22f7666b7eb1 +fn tc11 to bool +o = new uchar 1024 0 +n = proteins s32 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e33 +end +note canonical case 3a691829-fe72-43a7-8c8e-1bd083163f72 +fn tc12 to bool +o = new uchar 1024 0 +n = proteins s35 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e36 +end +note canonical case 1b6f8a26-ca2f-43b8-8262-3ee446021767 +fn tc13 to bool +o = new uchar 1024 0 +n = proteins s38 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e39 +end +note canonical case 1e91c1eb-02c0-48a0-9e35-168ad0cb5f39 +fn tc14 to bool +o = new uchar 1024 0 +n = proteins s41 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e42 +end +note canonical case e547af0b-aeab-49c7-9f13-801773a73557 +fn tc15 to bool +o = new uchar 1024 0 +n = proteins s44 o +if lt n 0 +ret false +end +ret eq ulong n 0 +end +note canonical case 67640947-ff02-4f23-a2ef-816f8a2ba72e +fn tc16 to bool +o = new uchar 1024 0 +n = proteins s46 o +if lt n 0 +ret false +end +ret eq ulong n 0 +end +note canonical case 9c2ad527-ebc9-4ace-808b-2b6447cb54cb +fn tc17 to bool +o = new uchar 1024 0 +n = proteins s48 o +if lt n 0 +ret false +end +ret eq ulong n 0 +end +note canonical case f4d9d8ee-00a8-47bf-a1e3-1641d4428e54 +fn tc18 to bool +o = new uchar 1024 0 +n = proteins s50 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e51 +end +note canonical case dd22eef3-b4f1-4ad6-bb0b-27093c090a9d +fn tc19 to bool +o = new uchar 1024 0 +n = proteins s53 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e54 +end +note canonical case d0f295df-fb70-425c-946c-ec2ec185388e +fn tc20 to bool +o = new uchar 1024 0 +n = proteins s56 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e57 +end +note canonical case e30e8505-97ec-4e5f-a73e-5726a1faa1f4 +fn tc21 to bool +o = new uchar 1024 0 +n = proteins s59 o +if lt n 0 +ret false +end +ret eq ulong n 0 +end +note canonical case 5358a20b-6f4c-4893-bce4-f929001710f3 +fn tc22 to bool +o = new uchar 1024 0 +n = proteins s61 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e62 +end +note canonical case ba16703a-1a55-482f-bb07-b21eef5093a3 +fn tc23 to bool +o = new uchar 1024 0 +n = proteins s64 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e65 +end +note canonical case 4089bb5a-d5b4-4e71-b79e-b8d1f14a2911 +fn tc24 to bool +o = new uchar 1024 0 +n = proteins s67 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e68 +end +note canonical case 2c2a2a60-401f-4a80-b977-e0715b23b93d +fn tc25 to bool +o = new uchar 1024 0 +n = proteins s70 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e71 +end +note canonical case f6f92714-769f-4187-9524-e353e8a41a80 +fn tc26 to bool +o = new uchar 1024 0 +n = proteins s73 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e74 +end +note canonical case 9eac93f3-627a-4c90-8653-6d0a0595bc6f +fn tc27 to bool +o = new uchar 1024 0 +ret eq proteins s76 o -1 +end +note canonical case 9d73899f-e68e-4291-b1e2-7bf87c00f024 +fn tc28 to bool +o = new uchar 1024 0 +ret eq proteins s78 o -1 +end +note canonical case 43945cf7-9968-402d-ab9f-b8a28750b050 +fn tc29 to bool +o = new uchar 1024 0 +n = proteins s80 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e81 +end +fn main to int +tcheck tc0 c1 +tcheck tc1 c4 +tcheck tc2 c7 +tcheck tc3 c10 +tcheck tc4 c13 +tcheck tc5 c16 +tcheck tc6 c19 +tcheck tc7 c22 +tcheck tc8 c25 +tcheck tc9 c28 +tcheck tc10 c31 +tcheck tc11 c34 +tcheck tc12 c37 +tcheck tc13 c40 +tcheck tc14 c43 +tcheck tc15 c45 +tcheck tc16 c47 +tcheck tc17 c49 +tcheck tc18 c52 +tcheck tc19 c55 +tcheck tc20 c58 +tcheck tc21 c60 +tcheck tc22 c63 +tcheck tc23 c66 +tcheck tc24 c69 +tcheck tc25 c72 +tcheck tc26 c75 +tcheck tc27 c77 +tcheck tc28 c79 +tcheck tc29 c82 +ret int get tfails 0 +end diff --git a/exercises/queen-attack/.meta/example.om b/exercises/queen-attack/.meta/example.om new file mode 100644 index 0000000..5f14011 --- /dev/null +++ b/exercises/queen-attack/.meta/example.om @@ -0,0 +1,33 @@ +note Exercism-style exercise "queen-attack": https://exercism.org/tracks/python/exercises/queen-attack +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note Rows and columns run 0 .. 7. create checks one queen's position: 0 valid, 1 row negative, +note 2 row past the board, 3 column negative, 4 column past the board (row checked first). +note canattack is true when queens at (wr, wc) and (br, bc) share a row, column or diagonal. +note Check it with: sh exercise.sh exercises/queen-attack +om2 +fn create row long col long to int +if lt row 0 +ret 1 +end +if gt row 7 +ret 2 +end +if lt col 0 +ret 3 +end +if gt col 7 +ret 4 +end +ret 0 +end +fn canattack wr long wc long br long bc long to bool +dr = sub wr br +dc = sub wc bc +if lt dr 0 +dr = neg dr +end +if lt dc 0 +dc = neg dc +end +ret or or eq dr 0 eq dc 0 eq dr dc +end diff --git a/exercises/queen-attack/queen-attack.om b/exercises/queen-attack/queen-attack.om new file mode 100644 index 0000000..d35f74d --- /dev/null +++ b/exercises/queen-attack/queen-attack.om @@ -0,0 +1,15 @@ +note Exercism-style exercise "queen-attack": https://exercism.org/tracks/python/exercises/queen-attack +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note Rows and columns run 0 .. 7. create checks one queen's position: 0 valid, 1 row negative, +note 2 row past the board, 3 column negative, 4 column past the board (row checked first). +note canattack is true when queens at (wr, wc) and (br, bc) share a row, column or diagonal. +note Check it with: sh exercise.sh exercises/queen-attack +om2 +fn create row long col long to int +note replace this line with your solution +trap 13 +end +fn canattack wr long wc long br long bc long to bool +note replace this line with your solution +trap 13 +end diff --git a/exercises/queen-attack/test.om b/exercises/queen-attack/test.om new file mode 100644 index 0000000..0fb9c27 --- /dev/null +++ b/exercises/queen-attack/test.om @@ -0,0 +1,130 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/queen-attack/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 13 cases kept, 0 skipped +text c0 "create: Test creation of Queens with valid and invalid positions / queen with a valid position" +text c1 "create: Test creation of Queens with valid and invalid positions / queen must have positive row" +text c2 "create: Test creation of Queens with valid and invalid positions / queen must have row on board" +text c3 "create: Test creation of Queens with valid and invalid positions / queen must have positive column" +text c4 "create: Test creation of Queens with valid and invalid positions / queen must have column on board" +text c5 "canAttack: Test the ability of one queen to attack another / cannot attack" +text c6 "canAttack: Test the ability of one queen to attack another / can attack on same row" +text c7 "canAttack: Test the ability of one queen to attack another / can attack on same column" +text c8 "canAttack: Test the ability of one queen to attack another / can attack on first diagonal" +text c9 "canAttack: Test the ability of one queen to attack another / can attack on second diagonal" +text c10 "canAttack: Test the ability of one queen to attack another / can attack on third diagonal" +text c11 "canAttack: Test the ability of one queen to attack another / can attack on fourth diagonal" +text c12 "canAttack: Test the ability of one queen to attack another / cannot attack if falling diagonals are only the same when reflected across the longest falling diagonal" +note canonical case 3ac4f735-d36c-44c4-a3e2-316f79704203 +fn tc0 to bool +ret eq create 2 2 0 +end +note canonical case 4e812d5d-b974-4e38-9a6b-8e0492bfa7be +fn tc1 to bool +ret eq create -2 2 1 +end +note canonical case f07b7536-b66b-4f08-beb9-4d70d891d5c8 +fn tc2 to bool +ret eq create 8 4 2 +end +note canonical case 15a10794-36d9-4907-ae6b-e5a0d4c54ebe +fn tc3 to bool +ret eq create 2 -2 3 +end +note canonical case 6907762d-0e8a-4c38-87fb-12f2f65f0ce4 +fn tc4 to bool +ret eq create 4 8 4 +end +note canonical case 33ae4113-d237-42ee-bac1-e1e699c0c007 +fn tc5 to bool +ret eq canattack 2 4 6 6 false +end +note canonical case eaa65540-ea7c-4152-8c21-003c7a68c914 +fn tc6 to bool +ret eq canattack 2 4 2 6 true +end +note canonical case bae6f609-2c0e-4154-af71-af82b7c31cea +fn tc7 to bool +ret eq canattack 4 5 2 5 true +end +note canonical case 0e1b4139-b90d-4562-bd58-dfa04f1746c7 +fn tc8 to bool +ret eq canattack 2 2 0 4 true +end +note canonical case ff9b7ed4-e4b6-401b-8d16-bc894d6d3dcd +fn tc9 to bool +ret eq canattack 2 2 3 1 true +end +note canonical case 0a71e605-6e28-4cc2-aa47-d20a2e71037a +fn tc10 to bool +ret eq canattack 2 2 1 1 true +end +note canonical case 0790b588-ae73-4f1f-a968-dd0b34f45f86 +fn tc11 to bool +ret eq canattack 1 7 0 6 true +end +note canonical case 543f8fd4-2597-4aad-8d77-cbdab63619f8 +fn tc12 to bool +ret eq canattack 4 1 2 5 false +end +fn main to int +tcheck tc0 c0 +tcheck tc1 c1 +tcheck tc2 c2 +tcheck tc3 c3 +tcheck tc4 c4 +tcheck tc5 c5 +tcheck tc6 c6 +tcheck tc7 c7 +tcheck tc8 c8 +tcheck tc9 c9 +tcheck tc10 c10 +tcheck tc11 c11 +tcheck tc12 c12 +ret int get tfails 0 +end diff --git a/exercises/raindrops/.meta/example.om b/exercises/raindrops/.meta/example.om new file mode 100644 index 0000000..251a17e --- /dev/null +++ b/exercises/raindrops/.meta/example.om @@ -0,0 +1,47 @@ +note Exercism-style exercise "raindrops": https://exercism.org/tracks/python/exercises/raindrops +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note convert writes "Pling" if n is divisible by 3, then "Plang" if by 5, then "Plong" if by 7, +note or the decimal digits of n when it is divisible by none of them, into o; returns the length. +note Check it with: sh exercise.sh exercises/raindrops +om2 +text pling "Pling" +text plang "Plang" +text plong "Plong" +fn app o edit uchar * p ulong s view uchar * to ulong +q = p +for i ulong len s +put o q get s i +q = add q 1 +end +ret q +end +fn appnum o edit uchar * p ulong n ulong to ulong +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +ret app o p part d i sub 20 i +end +fn convert n ulong o edit uchar * to ulong +p = ulong 0 +if eq rem n 3 0 +p = app o p pling +end +if eq rem n 5 0 +p = app o p plang +end +if eq rem n 7 0 +p = app o p plong +end +if eq p 0 +p = appnum o p n +end +ret p +end diff --git a/exercises/raindrops/raindrops.om b/exercises/raindrops/raindrops.om new file mode 100644 index 0000000..9dc5091 --- /dev/null +++ b/exercises/raindrops/raindrops.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "raindrops": https://exercism.org/tracks/python/exercises/raindrops +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note convert writes "Pling" if n is divisible by 3, then "Plang" if by 5, then "Plong" if by 7, +note or the decimal digits of n when it is divisible by none of them, into o; returns the length. +note Check it with: sh exercise.sh exercises/raindrops +om2 +fn convert n ulong o edit uchar * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/raindrops/test.om b/exercises/raindrops/test.om new file mode 100644 index 0000000..c0667f5 --- /dev/null +++ b/exercises/raindrops/test.om @@ -0,0 +1,225 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/raindrops/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 18 cases kept, 0 skipped +fn tbeq a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text e0 "1" +text c1 "convert: the sound for 1 is 1" +text e2 "Pling" +text c3 "convert: the sound for 3 is Pling" +text e4 "Plang" +text c5 "convert: the sound for 5 is Plang" +text e6 "Plong" +text c7 "convert: the sound for 7 is Plong" +text e8 "Pling" +text c9 "convert: the sound for 6 is Pling as it has a factor 3" +text e10 "8" +text c11 "convert: 2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base" +text e12 "Pling" +text c13 "convert: the sound for 9 is Pling as it has a factor 3" +text e14 "Plang" +text c15 "convert: the sound for 10 is Plang as it has a factor 5" +text e16 "Plong" +text c17 "convert: the sound for 14 is Plong as it has a factor of 7" +text e18 "PlingPlang" +text c19 "convert: the sound for 15 is PlingPlang as it has factors 3 and 5" +text e20 "PlingPlong" +text c21 "convert: the sound for 21 is PlingPlong as it has factors 3 and 7" +text e22 "Plang" +text c23 "convert: the sound for 25 is Plang as it has a factor 5" +text e24 "Pling" +text c25 "convert: the sound for 27 is Pling as it has a factor 3" +text e26 "PlangPlong" +text c27 "convert: the sound for 35 is PlangPlong as it has factors 5 and 7" +text e28 "Plong" +text c29 "convert: the sound for 49 is Plong as it has a factor 7" +text e30 "52" +text c31 "convert: the sound for 52 is 52" +text e32 "PlingPlangPlong" +text c33 "convert: the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7" +text e34 "Plang" +text c35 "convert: the sound for 3125 is Plang as it has a factor 5" +note canonical case 1575d549-e502-46d4-a8e1-6b7bec6123d8 +fn tc0 to bool +o = new uchar 1024 0 +n = convert 1 o +ret tbeq part o 0 n e0 +end +note canonical case 1f51a9f9-4895-4539-b182-d7b0a5ab2913 +fn tc1 to bool +o = new uchar 1024 0 +n = convert 3 o +ret tbeq part o 0 n e2 +end +note canonical case 2d9bfae5-2b21-4bcd-9629-c8c0e388f3e0 +fn tc2 to bool +o = new uchar 1024 0 +n = convert 5 o +ret tbeq part o 0 n e4 +end +note canonical case d7e60daa-32ef-4c23-b688-2abff46c4806 +fn tc3 to bool +o = new uchar 1024 0 +n = convert 7 o +ret tbeq part o 0 n e6 +end +note canonical case 6bb4947b-a724-430c-923f-f0dc3d62e56a +fn tc4 to bool +o = new uchar 1024 0 +n = convert 6 o +ret tbeq part o 0 n e8 +end +note canonical case ce51e0e8-d9d4-446d-9949-96eac4458c2d +fn tc5 to bool +o = new uchar 1024 0 +n = convert 8 o +ret tbeq part o 0 n e10 +end +note canonical case 0dd66175-e3e2-47fc-8750-d01739856671 +fn tc6 to bool +o = new uchar 1024 0 +n = convert 9 o +ret tbeq part o 0 n e12 +end +note canonical case 022c44d3-2182-4471-95d7-c575af225c96 +fn tc7 to bool +o = new uchar 1024 0 +n = convert 10 o +ret tbeq part o 0 n e14 +end +note canonical case 37ab74db-fed3-40ff-b7b9-04acdfea8edf +fn tc8 to bool +o = new uchar 1024 0 +n = convert 14 o +ret tbeq part o 0 n e16 +end +note canonical case 31f92999-6afb-40ee-9aa4-6d15e3334d0f +fn tc9 to bool +o = new uchar 1024 0 +n = convert 15 o +ret tbeq part o 0 n e18 +end +note canonical case ff9bb95d-6361-4602-be2c-653fe5239b54 +fn tc10 to bool +o = new uchar 1024 0 +n = convert 21 o +ret tbeq part o 0 n e20 +end +note canonical case d2e75317-b72e-40ab-8a64-6734a21dece1 +fn tc11 to bool +o = new uchar 1024 0 +n = convert 25 o +ret tbeq part o 0 n e22 +end +note canonical case a09c4c58-c662-4e32-97fe-f1501ef7125c +fn tc12 to bool +o = new uchar 1024 0 +n = convert 27 o +ret tbeq part o 0 n e24 +end +note canonical case bdf061de-8564-4899-a843-14b48b722789 +fn tc13 to bool +o = new uchar 1024 0 +n = convert 35 o +ret tbeq part o 0 n e26 +end +note canonical case c4680bee-69ba-439d-99b5-70c5fd1a7a83 +fn tc14 to bool +o = new uchar 1024 0 +n = convert 49 o +ret tbeq part o 0 n e28 +end +note canonical case 17f2bc9a-b65a-4d23-8ccd-266e8c271444 +fn tc15 to bool +o = new uchar 1024 0 +n = convert 52 o +ret tbeq part o 0 n e30 +end +note canonical case e46677ed-ff1a-419f-a740-5c713d2830e4 +fn tc16 to bool +o = new uchar 1024 0 +n = convert 105 o +ret tbeq part o 0 n e32 +end +note canonical case 13c6837a-0fcd-4b86-a0eb-20572f7deb0b +fn tc17 to bool +o = new uchar 1024 0 +n = convert 3125 o +ret tbeq part o 0 n e34 +end +fn main to int +tcheck tc0 c1 +tcheck tc1 c3 +tcheck tc2 c5 +tcheck tc3 c7 +tcheck tc4 c9 +tcheck tc5 c11 +tcheck tc6 c13 +tcheck tc7 c15 +tcheck tc8 c17 +tcheck tc9 c19 +tcheck tc10 c21 +tcheck tc11 c23 +tcheck tc12 c25 +tcheck tc13 c27 +tcheck tc14 c29 +tcheck tc15 c31 +tcheck tc16 c33 +tcheck tc17 c35 +ret int get tfails 0 +end diff --git a/exercises/resistor-color-duo/.meta/example.om b/exercises/resistor-color-duo/.meta/example.om new file mode 100644 index 0000000..00e7e21 --- /dev/null +++ b/exercises/resistor-color-duo/.meta/example.om @@ -0,0 +1,82 @@ +note Exercism-style exercise "resistor-color-duo": https://exercism.org/tracks/python/exercises/resistor-color-duo +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note colors holds band color names joined by commas. value is the two-digit number given by the +note first two bands (black 0, brown 1, red 2, orange 3, yellow 4, green 5, blue 6, violet 7, grey 8, +note white 9); further bands are ignored. +note Check it with: sh exercise.sh exercises/resistor-color-duo +om2 +text rc0 "black" +text rc1 "brown" +text rc2 "red" +text rc3 "orange" +text rc4 "yellow" +text rc5 "green" +text rc6 "blue" +text rc7 "violet" +text rc8 "grey" +text rc9 "white" +fn same a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +fn code color view uchar * to ulong +if same color rc0 +ret 0 +end +if same color rc1 +ret 1 +end +if same color rc2 +ret 2 +end +if same color rc3 +ret 3 +end +if same color rc4 +ret 4 +end +if same color rc5 +ret 5 +end +if same color rc6 +ret 6 +end +if same color rc7 +ret 7 +end +if same color rc8 +ret 8 +end +if same color rc9 +ret 9 +end +ret 10 +end +fn band cs view uchar * n ulong to ulong +s = ulong 0 +f = ulong 0 +for i ulong add len cs 1 +stop = eq i len cs +if not stop +stop = eq get cs i 44 +end +if stop +if eq f n +ret code part cs s sub i s +end +f = add f 1 +s = add i 1 +end +end +ret 10 +end +fn value colors view uchar * to ulong +ret add mul band colors 0 10 band colors 1 +end diff --git a/exercises/resistor-color-duo/resistor-color-duo.om b/exercises/resistor-color-duo/resistor-color-duo.om new file mode 100644 index 0000000..8880f62 --- /dev/null +++ b/exercises/resistor-color-duo/resistor-color-duo.om @@ -0,0 +1,11 @@ +note Exercism-style exercise "resistor-color-duo": https://exercism.org/tracks/python/exercises/resistor-color-duo +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note colors holds band color names joined by commas. value is the two-digit number given by the +note first two bands (black 0, brown 1, red 2, orange 3, yellow 4, green 5, blue 6, violet 7, grey 8, +note white 9); further bands are ignored. +note Check it with: sh exercise.sh exercises/resistor-color-duo +om2 +fn value colors view uchar * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/resistor-color-duo/test.om b/exercises/resistor-color-duo/test.om new file mode 100644 index 0000000..d437312 --- /dev/null +++ b/exercises/resistor-color-duo/test.om @@ -0,0 +1,101 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/resistor-color-duo/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 7 cases kept, 0 skipped +text s0 "brown,black" +text c1 "value: Brown and black" +text s2 "blue,grey" +text c3 "value: Blue and grey" +text s4 "yellow,violet" +text c5 "value: Yellow and violet" +text s6 "white,red" +text c7 "value: White and red" +text s8 "orange,orange" +text c9 "value: Orange and orange" +text s10 "green,brown,orange" +text c11 "value: Ignore additional colors" +text s12 "black,brown" +text c13 "value: Black and brown, one-digit" +note canonical case ce11995a-5b93-4950-a5e9-93423693b2fc +fn tc0 to bool +ret eq value s0 10 +end +note canonical case 7bf82f7a-af23-48ba-a97d-38d59406a920 +fn tc1 to bool +ret eq value s2 68 +end +note canonical case f1886361-fdfd-4693-acf8-46726fe24e0c +fn tc2 to bool +ret eq value s4 47 +end +note canonical case b7a6cbd2-ae3c-470a-93eb-56670b305640 +fn tc3 to bool +ret eq value s6 92 +end +note canonical case 77a8293d-2a83-4016-b1af-991acc12b9fe +fn tc4 to bool +ret eq value s8 33 +end +note canonical case 0c4fb44f-db7c-4d03-afa8-054350f156a8 +fn tc5 to bool +ret eq value s10 51 +end +note canonical case 4a8ceec5-0ab4-4904-88a4-daf953a5e818 +fn tc6 to bool +ret eq value s12 1 +end +fn main to int +tcheck tc0 c1 +tcheck tc1 c3 +tcheck tc2 c5 +tcheck tc3 c7 +tcheck tc4 c9 +tcheck tc5 c11 +tcheck tc6 c13 +ret int get tfails 0 +end diff --git a/exercises/resistor-color-trio/.meta/example.om b/exercises/resistor-color-trio/.meta/example.om new file mode 100644 index 0000000..55861ed --- /dev/null +++ b/exercises/resistor-color-trio/.meta/example.om @@ -0,0 +1,130 @@ +note Exercism-style exercise "resistor-color-trio": https://exercism.org/tracks/python/exercises/resistor-color-trio +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note colors holds band color names joined by commas (black 0, brown 1, red 2, orange 3, yellow 4, +note green 5, blue 6, violet 7, grey 8, white 9). The first two bands give a two-digit number, the +note third the count of zeros after it; further bands are ignored. label writes the resistance as +note " " into o, with the largest of ohms, kiloohms, megaohms, gigaohms that keeps the +note number whole, and returns the number of bytes written. +note Check it with: sh exercise.sh exercises/resistor-color-trio +om2 +text rc0 "black" +text rc1 "brown" +text rc2 "red" +text rc3 "orange" +text rc4 "yellow" +text rc5 "green" +text rc6 "blue" +text rc7 "violet" +text rc8 "grey" +text rc9 "white" +text u0 " ohms" +text u1 " kiloohms" +text u2 " megaohms" +text u3 " gigaohms" +fn same a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +fn app o edit uchar * p ulong s view uchar * to ulong +q = p +for i ulong len s +put o q get s i +q = add q 1 +end +ret q +end +fn appnum o edit uchar * p ulong n ulong to ulong +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +ret app o p part d i sub 20 i +end +fn code color view uchar * to ulong +if same color rc0 +ret 0 +end +if same color rc1 +ret 1 +end +if same color rc2 +ret 2 +end +if same color rc3 +ret 3 +end +if same color rc4 +ret 4 +end +if same color rc5 +ret 5 +end +if same color rc6 +ret 6 +end +if same color rc7 +ret 7 +end +if same color rc8 +ret 8 +end +if same color rc9 +ret 9 +end +ret 10 +end +fn band cs view uchar * n ulong to ulong +s = ulong 0 +f = ulong 0 +for i ulong add len cs 1 +stop = eq i len cs +if not stop +stop = eq get cs i 44 +end +if stop +if eq f n +ret code part cs s sub i s +end +f = add f 1 +s = add i 1 +end +end +ret 10 +end +fn label colors view uchar * o edit uchar * to ulong +v = add mul band colors 0 10 band colors 1 +z = band colors 2 +for i ulong z +v = mul v 10 +end +u = ulong 0 +while and and ge v 1000 eq rem v 1000 0 lt u 3 bound 4 +v = div v 1000 +u = add u 1 +end +p = appnum o 0 v +if eq u 0 +ret app o p u0 +end +if eq u 1 +ret app o p u1 +end +if eq u 2 +ret app o p u2 +end +ret app o p u3 +end diff --git a/exercises/resistor-color-trio/resistor-color-trio.om b/exercises/resistor-color-trio/resistor-color-trio.om new file mode 100644 index 0000000..46cfd8b --- /dev/null +++ b/exercises/resistor-color-trio/resistor-color-trio.om @@ -0,0 +1,13 @@ +note Exercism-style exercise "resistor-color-trio": https://exercism.org/tracks/python/exercises/resistor-color-trio +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note colors holds band color names joined by commas (black 0, brown 1, red 2, orange 3, yellow 4, +note green 5, blue 6, violet 7, grey 8, white 9). The first two bands give a two-digit number, the +note third the count of zeros after it; further bands are ignored. label writes the resistance as +note " " into o, with the largest of ohms, kiloohms, megaohms, gigaohms that keeps the +note number whole, and returns the number of bytes written. +note Check it with: sh exercise.sh exercises/resistor-color-trio +om2 +fn label colors view uchar * o edit uchar * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/resistor-color-trio/test.om b/exercises/resistor-color-trio/test.om new file mode 100644 index 0000000..b2ff704 --- /dev/null +++ b/exercises/resistor-color-trio/test.om @@ -0,0 +1,163 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/resistor-color-trio/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 10 cases kept, 0 skipped +fn tbeq a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text s0 "orange,orange,black" +text e1 "33 ohms" +text c2 "label: Orange and orange and black" +text s3 "blue,grey,brown" +text e4 "680 ohms" +text c5 "label: Blue and grey and brown" +text s6 "red,black,red" +text e7 "2 kiloohms" +text c8 "label: Red and black and red" +text s9 "green,brown,orange" +text e10 "51 kiloohms" +text c11 "label: Green and brown and orange" +text s12 "yellow,violet,yellow" +text e13 "470 kiloohms" +text c14 "label: Yellow and violet and yellow" +text s15 "blue,violet,blue" +text e16 "67 megaohms" +text c17 "label: Blue and violet and blue" +text s18 "black,black,black" +text e19 "0 ohms" +text c20 "label: Minimum possible value" +text s21 "white,white,white" +text e22 "99 gigaohms" +text c23 "label: Maximum possible value" +text s24 "black,grey,black" +text e25 "8 ohms" +text c26 "label: First two colors make an invalid octal number" +text s27 "blue,green,yellow,orange" +text e28 "650 kiloohms" +text c29 "label: Ignore extra colors" +note canonical case d6863355-15b7-40bb-abe0-bfb1a25512ed +fn tc0 to bool +o = new uchar 1024 0 +n = label s0 o +ret tbeq part o 0 n e1 +end +note canonical case 1224a3a9-8c8e-4032-843a-5224e04647d6 +fn tc1 to bool +o = new uchar 1024 0 +n = label s3 o +ret tbeq part o 0 n e4 +end +note canonical case b8bda7dc-6b95-4539-abb2-2ad51d66a207 +fn tc2 to bool +o = new uchar 1024 0 +n = label s6 o +ret tbeq part o 0 n e7 +end +note canonical case 5b1e74bc-d838-4eda-bbb3-eaba988e733b +fn tc3 to bool +o = new uchar 1024 0 +n = label s9 o +ret tbeq part o 0 n e10 +end +note canonical case f5d37ef9-1919-4719-a90d-a33c5a6934c9 +fn tc4 to bool +o = new uchar 1024 0 +n = label s12 o +ret tbeq part o 0 n e13 +end +note canonical case 5f6404a7-5bb3-4283-877d-3d39bcc33854 +fn tc5 to bool +o = new uchar 1024 0 +n = label s15 o +ret tbeq part o 0 n e16 +end +note canonical case 7d3a6ab8-e40e-46c3-98b1-91639fff2344 +fn tc6 to bool +o = new uchar 1024 0 +n = label s18 o +ret tbeq part o 0 n e19 +end +note canonical case ca0aa0ac-3825-42de-9f07-dac68cc580fd +fn tc7 to bool +o = new uchar 1024 0 +n = label s21 o +ret tbeq part o 0 n e22 +end +note canonical case 0061a76c-903a-4714-8ce2-f26ce23b0e09 +fn tc8 to bool +o = new uchar 1024 0 +n = label s24 o +ret tbeq part o 0 n e25 +end +note canonical case 30872c92-f567-4b69-a105-8455611c10c4 +fn tc9 to bool +o = new uchar 1024 0 +n = label s27 o +ret tbeq part o 0 n e28 +end +fn main to int +tcheck tc0 c2 +tcheck tc1 c5 +tcheck tc2 c8 +tcheck tc3 c11 +tcheck tc4 c14 +tcheck tc5 c17 +tcheck tc6 c20 +tcheck tc7 c23 +tcheck tc8 c26 +tcheck tc9 c29 +ret int get tfails 0 +end diff --git a/exercises/resistor-color/.meta/example.om b/exercises/resistor-color/.meta/example.om new file mode 100644 index 0000000..7ef518f --- /dev/null +++ b/exercises/resistor-color/.meta/example.om @@ -0,0 +1,91 @@ +note Exercism-style exercise "resistor-color": https://exercism.org/tracks/python/exercises/resistor-color +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note The band colors in value order are black brown red orange yellow green blue violet grey white +note (0 .. 9). colorcode is the value of color (10 when it is not a band color); colors writes all +note the band colors in value order, joined by commas, into o and returns the number of bytes written. +note Check it with: sh exercise.sh exercises/resistor-color +om2 +text rc0 "black" +text rc1 "brown" +text rc2 "red" +text rc3 "orange" +text rc4 "yellow" +text rc5 "green" +text rc6 "blue" +text rc7 "violet" +text rc8 "grey" +text rc9 "white" +fn same a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +fn app o edit uchar * p ulong s view uchar * to ulong +q = p +for i ulong len s +put o q get s i +q = add q 1 +end +ret q +end +fn colorcode color view uchar * to ulong +if same color rc0 +ret 0 +end +if same color rc1 +ret 1 +end +if same color rc2 +ret 2 +end +if same color rc3 +ret 3 +end +if same color rc4 +ret 4 +end +if same color rc5 +ret 5 +end +if same color rc6 +ret 6 +end +if same color rc7 +ret 7 +end +if same color rc8 +ret 8 +end +if same color rc9 +ret 9 +end +ret 10 +end +fn colors o edit uchar * to ulong +p = app o 0 rc0 +put o p 44 +p = app o add p 1 rc1 +put o p 44 +p = app o add p 1 rc2 +put o p 44 +p = app o add p 1 rc3 +put o p 44 +p = app o add p 1 rc4 +put o p 44 +p = app o add p 1 rc5 +put o p 44 +p = app o add p 1 rc6 +put o p 44 +p = app o add p 1 rc7 +put o p 44 +p = app o add p 1 rc8 +put o p 44 +p = app o add p 1 rc9 +ret p +end diff --git a/exercises/resistor-color/resistor-color.om b/exercises/resistor-color/resistor-color.om new file mode 100644 index 0000000..d2cfdca --- /dev/null +++ b/exercises/resistor-color/resistor-color.om @@ -0,0 +1,15 @@ +note Exercism-style exercise "resistor-color": https://exercism.org/tracks/python/exercises/resistor-color +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note The band colors in value order are black brown red orange yellow green blue violet grey white +note (0 .. 9). colorcode is the value of color (10 when it is not a band color); colors writes all +note the band colors in value order, joined by commas, into o and returns the number of bytes written. +note Check it with: sh exercise.sh exercises/resistor-color +om2 +fn colorcode color view uchar * to ulong +note replace this line with your solution +trap 13 +end +fn colors o edit uchar * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/resistor-color/test.om b/exercises/resistor-color/test.om new file mode 100644 index 0000000..c8708ca --- /dev/null +++ b/exercises/resistor-color/test.om @@ -0,0 +1,93 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/resistor-color/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 4 cases kept, 0 skipped +fn tbeq a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text s0 "black" +text c1 "colorCode: Color codes / Black" +text s2 "white" +text c3 "colorCode: Color codes / White" +text s4 "orange" +text c5 "colorCode: Color codes / Orange" +text e6 "black,brown,red,orange,yellow,green,blue,violet,grey,white" +text c7 "colors: Colors" +note canonical case 49eb31c5-10a8-4180-9f7f-fea632ab87ef +fn tc0 to bool +ret eq colorcode s0 0 +end +note canonical case 0a4df94b-92da-4579-a907-65040ce0b3fc +fn tc1 to bool +ret eq colorcode s2 9 +end +note canonical case 5f81608d-f36f-4190-8084-f45116b6f380 +fn tc2 to bool +ret eq colorcode s4 3 +end +note canonical case 581d68fa-f968-4be2-9f9d-880f2fb73cf7 +fn tc3 to bool +o = new uchar 1024 0 +n = colors o +ret tbeq part o 0 n e6 +end +fn main to int +tcheck tc0 c1 +tcheck tc1 c3 +tcheck tc2 c5 +tcheck tc3 c7 +ret int get tfails 0 +end diff --git a/exercises/reverse-string/.meta/example.om b/exercises/reverse-string/.meta/example.om new file mode 100644 index 0000000..9b5b705 --- /dev/null +++ b/exercises/reverse-string/.meta/example.om @@ -0,0 +1,12 @@ +note Exercism-style exercise "reverse-string": https://exercism.org/tracks/python/exercises/reverse-string +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note reverse writes the bytes of s into o in reverse order and returns the length. +note Check it with: sh exercise.sh exercises/reverse-string +om2 +fn reverse s view uchar * o edit uchar * to ulong +n = len s +for i ulong n +put o i get s sub sub n 1 i +end +ret n +end diff --git a/exercises/reverse-string/reverse-string.om b/exercises/reverse-string/reverse-string.om new file mode 100644 index 0000000..3d144e2 --- /dev/null +++ b/exercises/reverse-string/reverse-string.om @@ -0,0 +1,9 @@ +note Exercism-style exercise "reverse-string": https://exercism.org/tracks/python/exercises/reverse-string +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note reverse writes the bytes of s into o in reverse order and returns the length. +note Check it with: sh exercise.sh exercises/reverse-string +om2 +fn reverse s view uchar * o edit uchar * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/reverse-string/test.om b/exercises/reverse-string/test.om new file mode 100644 index 0000000..e05b9f3 --- /dev/null +++ b/exercises/reverse-string/test.om @@ -0,0 +1,122 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/reverse-string/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 6 cases kept, 3 skipped: "wide characters" (needs Unicode grapheme handling (non-ASCII input)); "grapheme cluster with pre-combined form" (needs Unicode grapheme handling (non-ASCII input)); "grapheme clusters" (needs Unicode grapheme handling (non-ASCII input)) +fn tbeq a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text z0 "z" +text c1 "reverse: an empty string" +text s2 "robot" +text e3 "tobor" +text c4 "reverse: a word" +text s5 "Ramen" +text e6 "nemaR" +text c7 "reverse: a capitalized word" +text s8 "I'm hungry!" +text e9 "!yrgnuh m'I" +text c10 "reverse: a sentence with punctuation" +text s11 "racecar" +text e12 "racecar" +text c13 "reverse: a palindrome" +text s14 "drawer" +text e15 "reward" +text c16 "reverse: an even-sized word" +note canonical case c3b7d806-dced-49ee-8543-933fd1719b1c +fn tc0 to bool +o = new uchar 1024 0 +n = reverse part z0 0 0 o +ret eq n 0 +end +note canonical case 01ebf55b-bebb-414e-9dec-06f7bb0bee3c +fn tc1 to bool +o = new uchar 1024 0 +n = reverse s2 o +ret tbeq part o 0 n e3 +end +note canonical case 0f7c07e4-efd1-4aaa-a07a-90b49ce0b746 +fn tc2 to bool +o = new uchar 1024 0 +n = reverse s5 o +ret tbeq part o 0 n e6 +end +note canonical case 71854b9c-f200-4469-9f5c-1e8e5eff5614 +fn tc3 to bool +o = new uchar 1024 0 +n = reverse s8 o +ret tbeq part o 0 n e9 +end +note canonical case 1f8ed2f3-56f3-459b-8f3e-6d8d654a1f6c +fn tc4 to bool +o = new uchar 1024 0 +n = reverse s11 o +ret tbeq part o 0 n e12 +end +note canonical case b9e7dec1-c6df-40bd-9fa3-cd7ded010c4c +fn tc5 to bool +o = new uchar 1024 0 +n = reverse s14 o +ret tbeq part o 0 n e15 +end +fn main to int +tcheck tc0 c1 +tcheck tc1 c4 +tcheck tc2 c7 +tcheck tc3 c10 +tcheck tc4 c13 +tcheck tc5 c16 +ret int get tfails 0 +end diff --git a/exercises/rna-transcription/.meta/example.om b/exercises/rna-transcription/.meta/example.om new file mode 100644 index 0000000..85c349f --- /dev/null +++ b/exercises/rna-transcription/.meta/example.om @@ -0,0 +1,26 @@ +note Exercism-style exercise "rna-transcription": https://exercism.org/tracks/python/exercises/rna-transcription +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note torna writes the RNA complement of DNA strand dna into o (G to C, C to G, T to A, A to U) +note and returns its length. +note Check it with: sh exercise.sh exercises/rna-transcription +om2 +fn torna dna view uchar * o edit uchar * to ulong +for i ulong len dna +c = get dna i +r = uchar 0 +if eq c 71 +r = uchar 67 +end +if eq c 67 +r = uchar 71 +end +if eq c 84 +r = uchar 65 +end +if eq c 65 +r = uchar 85 +end +put o i r +end +ret len dna +end diff --git a/exercises/rna-transcription/rna-transcription.om b/exercises/rna-transcription/rna-transcription.om new file mode 100644 index 0000000..fd2e56a --- /dev/null +++ b/exercises/rna-transcription/rna-transcription.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "rna-transcription": https://exercism.org/tracks/python/exercises/rna-transcription +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note torna writes the RNA complement of DNA strand dna into o (G to C, C to G, T to A, A to U) +note and returns its length. +note Check it with: sh exercise.sh exercises/rna-transcription +om2 +fn torna dna view uchar * o edit uchar * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/rna-transcription/test.om b/exercises/rna-transcription/test.om new file mode 100644 index 0000000..9bc5980 --- /dev/null +++ b/exercises/rna-transcription/test.om @@ -0,0 +1,122 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/rna-transcription/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 6 cases kept, 0 skipped +fn tbeq a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text z0 "z" +text c1 "toRna: Empty RNA sequence" +text s2 "C" +text e3 "G" +text c4 "toRna: RNA complement of cytosine is guanine" +text s5 "G" +text e6 "C" +text c7 "toRna: RNA complement of guanine is cytosine" +text s8 "T" +text e9 "A" +text c10 "toRna: RNA complement of thymine is adenine" +text s11 "A" +text e12 "U" +text c13 "toRna: RNA complement of adenine is uracil" +text s14 "ACGTGGTCTTAA" +text e15 "UGCACCAGAAUU" +text c16 "toRna: RNA complement" +note canonical case b4631f82-c98c-4a2f-90b3-c5c2b6c6f661 +fn tc0 to bool +o = new uchar 1024 0 +n = torna part z0 0 0 o +ret eq n 0 +end +note canonical case a9558a3c-318c-4240-9256-5d5ed47005a6 +fn tc1 to bool +o = new uchar 1024 0 +n = torna s2 o +ret tbeq part o 0 n e3 +end +note canonical case 6eedbb5c-12cb-4c8b-9f51-f8320b4dc2e7 +fn tc2 to bool +o = new uchar 1024 0 +n = torna s5 o +ret tbeq part o 0 n e6 +end +note canonical case 870bd3ec-8487-471d-8d9a-a25046488d3e +fn tc3 to bool +o = new uchar 1024 0 +n = torna s8 o +ret tbeq part o 0 n e9 +end +note canonical case aade8964-02e1-4073-872f-42d3ffd74c5f +fn tc4 to bool +o = new uchar 1024 0 +n = torna s11 o +ret tbeq part o 0 n e12 +end +note canonical case 79ed2757-f018-4f47-a1d7-34a559392dbf +fn tc5 to bool +o = new uchar 1024 0 +n = torna s14 o +ret tbeq part o 0 n e15 +end +fn main to int +tcheck tc0 c1 +tcheck tc1 c4 +tcheck tc2 c7 +tcheck tc3 c10 +tcheck tc4 c13 +tcheck tc5 c16 +ret int get tfails 0 +end diff --git a/exercises/rotational-cipher/.meta/example.om b/exercises/rotational-cipher/.meta/example.om new file mode 100644 index 0000000..bb2916a --- /dev/null +++ b/exercises/rotational-cipher/.meta/example.om @@ -0,0 +1,21 @@ +note Exercism-style exercise "rotational-cipher": https://exercism.org/tracks/python/exercises/rotational-cipher +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note rotate writes s into o with every letter shifted key places along the alphabet (keeping its +note case, wrapping z to a) and every other byte unchanged; returns the length. +note Check it with: sh exercise.sh exercises/rotational-cipher +om2 +fn rotate s view uchar * key ulong o edit uchar * to ulong +k = uchar rem key 26 +for i ulong len s +c = get s i +if and ge c 65 le c 90 +c = add 65 rem add sub c 65 k 26 +else +if and ge c 97 le c 122 +c = add 97 rem add sub c 97 k 26 +end +end +put o i c +end +ret len s +end diff --git a/exercises/rotational-cipher/rotational-cipher.om b/exercises/rotational-cipher/rotational-cipher.om new file mode 100644 index 0000000..9fdc4d4 --- /dev/null +++ b/exercises/rotational-cipher/rotational-cipher.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "rotational-cipher": https://exercism.org/tracks/python/exercises/rotational-cipher +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note rotate writes s into o with every letter shifted key places along the alphabet (keeping its +note case, wrapping z to a) and every other byte unchanged; returns the length. +note Check it with: sh exercise.sh exercises/rotational-cipher +om2 +fn rotate s view uchar * key ulong o edit uchar * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/rotational-cipher/test.om b/exercises/rotational-cipher/test.om new file mode 100644 index 0000000..f177dd6 --- /dev/null +++ b/exercises/rotational-cipher/test.om @@ -0,0 +1,163 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/rotational-cipher/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 10 cases kept, 0 skipped +fn tbeq a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text s0 "a" +text e1 "a" +text c2 "rotate: rotate a by 0, same output as input" +text s3 "a" +text e4 "b" +text c5 "rotate: rotate a by 1" +text s6 "a" +text e7 "a" +text c8 "rotate: rotate a by 26, same output as input" +text s9 "m" +text e10 "z" +text c11 "rotate: rotate m by 13" +text s12 "n" +text e13 "a" +text c14 "rotate: rotate n by 13 with wrap around alphabet" +text s15 "OMG" +text e16 "TRL" +text c17 "rotate: rotate capital letters" +text s18 "O M G" +text e19 "T R L" +text c20 "rotate: rotate spaces" +text s21 "Testing 1 2 3 testing" +text e22 "Xiwxmrk 1 2 3 xiwxmrk" +text c23 "rotate: rotate numbers" +text s24 "Let's eat, Grandma!" +text e25 "Gzo'n zvo, Bmviyhv!" +text c26 "rotate: rotate punctuation" +text s27 "The quick brown fox jumps over the lazy dog." +text e28 "Gur dhvpx oebja sbk whzcf bire gur ynml qbt." +text c29 "rotate: rotate all letters" +note canonical case 74e58a38-e484-43f1-9466-877a7515e10f +fn tc0 to bool +o = new uchar 1024 0 +n = rotate s0 0 o +ret tbeq part o 0 n e1 +end +note canonical case 7ee352c6-e6b0-4930-b903-d09943ecb8f5 +fn tc1 to bool +o = new uchar 1024 0 +n = rotate s3 1 o +ret tbeq part o 0 n e4 +end +note canonical case edf0a733-4231-4594-a5ee-46a4009ad764 +fn tc2 to bool +o = new uchar 1024 0 +n = rotate s6 26 o +ret tbeq part o 0 n e7 +end +note canonical case e3e82cb9-2a5b-403f-9931-e43213879300 +fn tc3 to bool +o = new uchar 1024 0 +n = rotate s9 13 o +ret tbeq part o 0 n e10 +end +note canonical case 19f9eb78-e2ad-4da4-8fe3-9291d47c1709 +fn tc4 to bool +o = new uchar 1024 0 +n = rotate s12 13 o +ret tbeq part o 0 n e13 +end +note canonical case a116aef4-225b-4da9-884f-e8023ca6408a +fn tc5 to bool +o = new uchar 1024 0 +n = rotate s15 5 o +ret tbeq part o 0 n e16 +end +note canonical case 71b541bb-819c-4dc6-a9c3-132ef9bb737b +fn tc6 to bool +o = new uchar 1024 0 +n = rotate s18 5 o +ret tbeq part o 0 n e19 +end +note canonical case ef32601d-e9ef-4b29-b2b5-8971392282e6 +fn tc7 to bool +o = new uchar 1024 0 +n = rotate s21 4 o +ret tbeq part o 0 n e22 +end +note canonical case 32dd74f6-db2b-41a6-b02c-82eb4f93e549 +fn tc8 to bool +o = new uchar 1024 0 +n = rotate s24 21 o +ret tbeq part o 0 n e25 +end +note canonical case 9fb93fe6-42b0-46e6-9ec1-0bf0a062d8c9 +fn tc9 to bool +o = new uchar 1024 0 +n = rotate s27 13 o +ret tbeq part o 0 n e28 +end +fn main to int +tcheck tc0 c2 +tcheck tc1 c5 +tcheck tc2 c8 +tcheck tc3 c11 +tcheck tc4 c14 +tcheck tc5 c17 +tcheck tc6 c20 +tcheck tc7 c23 +tcheck tc8 c26 +tcheck tc9 c29 +ret int get tfails 0 +end diff --git a/exercises/run-length-encoding/.meta/example.om b/exercises/run-length-encoding/.meta/example.om new file mode 100644 index 0000000..5f51702 --- /dev/null +++ b/exercises/run-length-encoding/.meta/example.om @@ -0,0 +1,71 @@ +note Exercism-style exercise "run-length-encoding": https://exercism.org/tracks/python/exercises/run-length-encoding +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note encode writes the run-length encoding of s into o: each run of 2 or more equal bytes becomes its +note decimal count followed by the byte, a single byte stays as it is. decode reverses it. Both +note return the number of bytes written. +note Check it with: sh exercise.sh exercises/run-length-encoding +om2 +fn app o edit uchar * p ulong s view uchar * to ulong +q = p +for i ulong len s +put o q get s i +q = add q 1 +end +ret q +end +fn appnum o edit uchar * p ulong n ulong to ulong +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +ret app o p part d i sub 20 i +end +fn encode s view uchar * o edit uchar * to ulong +p = ulong 0 +i = ulong 0 +n = len s +while lt i n bound 999999 +c = get s i +r = ulong 1 +while lt add i r n bound 999999 +if ne get s add i r c +break +end +r = add r 1 +end +if gt r 1 +p = appnum o p r +end +put o p c +p = add p 1 +i = add i r +end +ret p +end +fn decode s view uchar * o edit uchar * to ulong +p = ulong 0 +r = ulong 0 +for i ulong len s +c = get s i +if and ge c 48 le c 57 +r = add mul r 10 ulong sub c 48 +else +if eq r 0 +r = ulong 1 +end +for j ulong r +put o p c +p = add p 1 +end +r = ulong 0 +end +end +ret p +end diff --git a/exercises/run-length-encoding/run-length-encoding.om b/exercises/run-length-encoding/run-length-encoding.om new file mode 100644 index 0000000..d9818f9 --- /dev/null +++ b/exercises/run-length-encoding/run-length-encoding.om @@ -0,0 +1,15 @@ +note Exercism-style exercise "run-length-encoding": https://exercism.org/tracks/python/exercises/run-length-encoding +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note encode writes the run-length encoding of s into o: each run of 2 or more equal bytes becomes its +note decimal count followed by the byte, a single byte stays as it is. decode reverses it. Both +note return the number of bytes written. +note Check it with: sh exercise.sh exercises/run-length-encoding +om2 +fn encode s view uchar * o edit uchar * to ulong +note replace this line with your solution +trap 13 +end +fn decode s view uchar * o edit uchar * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/run-length-encoding/test.om b/exercises/run-length-encoding/test.om new file mode 100644 index 0000000..f4fcc0c --- /dev/null +++ b/exercises/run-length-encoding/test.om @@ -0,0 +1,193 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/run-length-encoding/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 13 cases kept, 0 skipped +fn tbeq a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text z0 "z" +text c1 "encode: run-length encode a string / empty string" +text s2 "XYZ" +text e3 "XYZ" +text c4 "encode: run-length encode a string / single characters only are encoded without count" +text s5 "AABBBCCCC" +text e6 "2A3B4C" +text c7 "encode: run-length encode a string / string with no single characters" +text s8 "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" +text e9 "12WB12W3B24WB" +text c10 "encode: run-length encode a string / single characters mixed with repeated characters" +text s11 " hsqq qww " +text e12 "2 hs2q q2w2 " +text c13 "encode: run-length encode a string / multiple whitespace mixed in string" +text s14 "aabbbcccc" +text e15 "2a3b4c" +text c16 "encode: run-length encode a string / lowercase characters" +text z17 "z" +text c18 "decode: run-length decode a string / empty string" +text s19 "XYZ" +text e20 "XYZ" +text c21 "decode: run-length decode a string / single characters only" +text s22 "2A3B4C" +text e23 "AABBBCCCC" +text c24 "decode: run-length decode a string / string with no single characters" +text s25 "12WB12W3B24WB" +text e26 "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" +text c27 "decode: run-length decode a string / single characters with repeated characters" +text s28 "2 hs2q q2w2 " +text e29 " hsqq qww " +text c30 "decode: run-length decode a string / multiple whitespace mixed in string" +text s31 "2a3b4c" +text e32 "aabbbcccc" +text c33 "decode: run-length decode a string / lowercase string" +text s34 "zzz ZZ zZ" +text e35 "zzz ZZ zZ" +text c36 "consistency: encode and then decode / encode followed by decode gives original string" +note canonical case ad53b61b-6ffc-422f-81a6-61f7df92a231 +fn tc0 to bool +o = new uchar 1024 0 +n = encode part z0 0 0 o +ret eq n 0 +end +note canonical case 52012823-b7e6-4277-893c-5b96d42f82de +fn tc1 to bool +o = new uchar 1024 0 +n = encode s2 o +ret tbeq part o 0 n e3 +end +note canonical case b7868492-7e3a-415f-8da3-d88f51f80409 +fn tc2 to bool +o = new uchar 1024 0 +n = encode s5 o +ret tbeq part o 0 n e6 +end +note canonical case 859b822b-6e9f-44d6-9c46-6091ee6ae358 +fn tc3 to bool +o = new uchar 1024 0 +n = encode s8 o +ret tbeq part o 0 n e9 +end +note canonical case 1b34de62-e152-47be-bc88-469746df63b3 +fn tc4 to bool +o = new uchar 1024 0 +n = encode s11 o +ret tbeq part o 0 n e12 +end +note canonical case abf176e2-3fbd-40ad-bb2f-2dd6d4df721a +fn tc5 to bool +o = new uchar 1024 0 +n = encode s14 o +ret tbeq part o 0 n e15 +end +note canonical case 7ec5c390-f03c-4acf-ac29-5f65861cdeb5 +fn tc6 to bool +o = new uchar 1024 0 +n = decode part z17 0 0 o +ret eq n 0 +end +note canonical case ad23f455-1ac2-4b0e-87d0-b85b10696098 +fn tc7 to bool +o = new uchar 1024 0 +n = decode s19 o +ret tbeq part o 0 n e20 +end +note canonical case 21e37583-5a20-4a0e-826c-3dee2c375f54 +fn tc8 to bool +o = new uchar 1024 0 +n = decode s22 o +ret tbeq part o 0 n e23 +end +note canonical case 1389ad09-c3a8-4813-9324-99363fba429c +fn tc9 to bool +o = new uchar 1024 0 +n = decode s25 o +ret tbeq part o 0 n e26 +end +note canonical case 3f8e3c51-6aca-4670-b86c-a213bf4706b0 +fn tc10 to bool +o = new uchar 1024 0 +n = decode s28 o +ret tbeq part o 0 n e29 +end +note canonical case 29f721de-9aad-435f-ba37-7662df4fb551 +fn tc11 to bool +o = new uchar 1024 0 +n = decode s31 o +ret tbeq part o 0 n e32 +end +note canonical case 2a762efd-8695-4e04-b0d6-9736899fbc16 +fn tc12 to bool +o = new uchar 1024 0 +o2 = new uchar 1024 0 +n = encode s34 o +m = decode part o 0 n o2 +ret tbeq part o2 0 m e35 +end +fn main to int +tcheck tc0 c1 +tcheck tc1 c4 +tcheck tc2 c7 +tcheck tc3 c10 +tcheck tc4 c13 +tcheck tc5 c16 +tcheck tc6 c18 +tcheck tc7 c21 +tcheck tc8 c24 +tcheck tc9 c27 +tcheck tc10 c30 +tcheck tc11 c33 +tcheck tc12 c36 +ret int get tfails 0 +end diff --git a/exercises/saddle-points/.meta/example.om b/exercises/saddle-points/.meta/example.om new file mode 100644 index 0000000..57b4c68 --- /dev/null +++ b/exercises/saddle-points/.meta/example.om @@ -0,0 +1,38 @@ +note Exercism-style exercise "saddle-points": https://exercism.org/tracks/python/exercises/saddle-points +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note m is a matrix of nr rows and nc columns, row after row. A saddle point is a value that is the +note largest in its row and the smallest in its column. saddlepoints writes the 1-based row and +note column of each saddle point into o as pairs, in row-major order, and returns the number of points. +note Check it with: sh exercise.sh exercises/saddle-points +om2 +fn isrowmax m view ulong * nc ulong r ulong c ulong to bool +v = get m add mul r nc c +for j ulong nc +if gt get m add mul r nc j v +ret false +end +end +ret true +end +fn iscolmin m view ulong * nr ulong nc ulong r ulong c ulong to bool +v = get m add mul r nc c +for i ulong nr +if lt get m add mul i nc c v +ret false +end +end +ret true +end +fn saddlepoints m view ulong * nr ulong nc ulong o edit ulong * to ulong +k = ulong 0 +for r ulong nr +for c ulong nc +if and isrowmax m nc r c iscolmin m nr nc r c +put o mul k 2 add r 1 +put o add mul k 2 1 add c 1 +k = add k 1 +end +end +end +ret k +end diff --git a/exercises/saddle-points/saddle-points.om b/exercises/saddle-points/saddle-points.om new file mode 100644 index 0000000..bbed334 --- /dev/null +++ b/exercises/saddle-points/saddle-points.om @@ -0,0 +1,11 @@ +note Exercism-style exercise "saddle-points": https://exercism.org/tracks/python/exercises/saddle-points +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note m is a matrix of nr rows and nc columns, row after row. A saddle point is a value that is the +note largest in its row and the smallest in its column. saddlepoints writes the 1-based row and +note column of each saddle point into o as pairs, in row-major order, and returns the number of points. +note Check it with: sh exercise.sh exercises/saddle-points +om2 +fn saddlepoints m view ulong * nr ulong nc ulong o edit ulong * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/saddle-points/test.om b/exercises/saddle-points/test.om new file mode 100644 index 0000000..66f5527 --- /dev/null +++ b/exercises/saddle-points/test.om @@ -0,0 +1,238 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/saddle-points/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 9 cases kept, 0 skipped +fn tueq a view ulong * b view ulong * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text c0 "saddlePoints: Can identify single saddle point" +text c1 "saddlePoints: Can identify that empty matrix has no saddle points" +text c2 "saddlePoints: Can identify lack of saddle points when there are none" +text c3 "saddlePoints: Can identify multiple saddle points in a column" +text c4 "saddlePoints: Can identify multiple saddle points in a row" +text c5 "saddlePoints: Can identify saddle point in bottom right corner" +text c6 "saddlePoints: Can identify saddle points in a non square matrix" +text c7 "saddlePoints: Can identify that saddle points in a single column matrix are those with the minimum value" +text c8 "saddlePoints: Can identify that saddle points in a single row matrix are those with the maximum value" +note canonical case 3e374e63-a2e0-4530-a39a-d53c560382bd +fn tc0 to bool +a0 = new ulong 9 0 +put a0 0 9 +put a0 1 8 +put a0 2 7 +put a0 3 5 +put a0 4 3 +put a0 5 2 +put a0 6 6 +put a0 7 6 +put a0 8 7 +o = new ulong 256 0 +n = saddlepoints a0 3 3 o +a1 = new ulong 2 0 +put a1 0 2 +put a1 1 1 +ret tueq part o 0 mul n 2 a1 +end +note canonical case 6b501e2b-6c1f-491f-b1bb-7f278f760534 +fn tc1 to bool +a0 = new ulong 1 0 +o = new ulong 256 0 +n = saddlepoints part a0 0 0 1 0 o +ret eq mul n 2 0 +end +note canonical case 8c27cc64-e573-4fcb-a099-f0ae863fb02f +fn tc2 to bool +a0 = new ulong 9 0 +put a0 0 1 +put a0 1 2 +put a0 2 3 +put a0 3 3 +put a0 4 1 +put a0 5 2 +put a0 6 2 +put a0 7 3 +put a0 8 1 +o = new ulong 256 0 +n = saddlepoints a0 3 3 o +ret eq mul n 2 0 +end +note canonical case 6d1399bd-e105-40fd-a2c9-c6609507d7a3 +fn tc3 to bool +a0 = new ulong 9 0 +put a0 0 4 +put a0 1 5 +put a0 2 4 +put a0 3 3 +put a0 4 5 +put a0 5 5 +put a0 6 1 +put a0 7 5 +put a0 8 4 +o = new ulong 256 0 +n = saddlepoints a0 3 3 o +a1 = new ulong 6 0 +put a1 0 1 +put a1 1 2 +put a1 2 2 +put a1 3 2 +put a1 4 3 +put a1 5 2 +ret tueq part o 0 mul n 2 a1 +end +note canonical case 3e81dce9-53b3-44e6-bf26-e328885fd5d1 +fn tc4 to bool +a0 = new ulong 9 0 +put a0 0 6 +put a0 1 7 +put a0 2 8 +put a0 3 5 +put a0 4 5 +put a0 5 5 +put a0 6 7 +put a0 7 5 +put a0 8 6 +o = new ulong 256 0 +n = saddlepoints a0 3 3 o +a1 = new ulong 6 0 +put a1 0 2 +put a1 1 1 +put a1 2 2 +put a1 3 2 +put a1 4 2 +put a1 5 3 +ret tueq part o 0 mul n 2 a1 +end +note canonical case 88868621-b6f4-4837-bb8b-3fad8b25d46b +fn tc5 to bool +a0 = new ulong 9 0 +put a0 0 8 +put a0 1 7 +put a0 2 9 +put a0 3 6 +put a0 4 7 +put a0 5 6 +put a0 6 3 +put a0 7 2 +put a0 8 5 +o = new ulong 256 0 +n = saddlepoints a0 3 3 o +a1 = new ulong 2 0 +put a1 0 3 +put a1 1 3 +ret tueq part o 0 mul n 2 a1 +end +note canonical case 5b9499ca-fcea-4195-830a-9c4584a0ee79 +fn tc6 to bool +a0 = new ulong 6 0 +put a0 0 3 +put a0 1 1 +put a0 2 3 +put a0 3 3 +put a0 4 2 +put a0 5 4 +o = new ulong 256 0 +n = saddlepoints a0 2 3 o +a1 = new ulong 4 0 +put a1 0 1 +put a1 1 1 +put a1 2 1 +put a1 3 3 +ret tueq part o 0 mul n 2 a1 +end +note canonical case ee99ccd2-a1f1-4283-ad39-f8c70f0cf594 +fn tc7 to bool +a0 = new ulong 4 0 +put a0 0 2 +put a0 1 1 +put a0 2 4 +put a0 3 1 +o = new ulong 256 0 +n = saddlepoints a0 4 1 o +a1 = new ulong 4 0 +put a1 0 2 +put a1 1 1 +put a1 2 4 +put a1 3 1 +ret tueq part o 0 mul n 2 a1 +end +note canonical case 63abf709-a84b-407f-a1b3-456638689713 +fn tc8 to bool +a0 = new ulong 4 0 +put a0 0 2 +put a0 1 5 +put a0 2 3 +put a0 3 5 +o = new ulong 256 0 +n = saddlepoints a0 1 4 o +a1 = new ulong 4 0 +put a1 0 1 +put a1 1 2 +put a1 2 1 +put a1 3 4 +ret tueq part o 0 mul n 2 a1 +end +fn main to int +tcheck tc0 c0 +tcheck tc1 c1 +tcheck tc2 c2 +tcheck tc3 c3 +tcheck tc4 c4 +tcheck tc5 c5 +tcheck tc6 c6 +tcheck tc7 c7 +tcheck tc8 c8 +ret int get tfails 0 +end diff --git a/exercises/scrabble-score/.meta/example.om b/exercises/scrabble-score/.meta/example.om new file mode 100644 index 0000000..b9a0418 --- /dev/null +++ b/exercises/scrabble-score/.meta/example.om @@ -0,0 +1,21 @@ +note Exercism-style exercise "scrabble-score": https://exercism.org/tracks/python/exercises/scrabble-score +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note score is the Scrabble score of word, ignoring case: AEIOULNRST 1, DG 2, BCMP 3, FHVWY 4, K 5, +note JX 8, QZ 10. +note Check it with: sh exercise.sh exercises/scrabble-score +om2 +note letter values A .. Z, each stored as 48 + value (":" is 10) +text sv "1332142418513113:11114484:" +fn score word view uchar * to ulong +t = ulong 0 +for i ulong len word +c = get word i +if and ge c 97 le c 122 +c = sub c 32 +end +if and ge c 65 le c 90 +t = add t ulong sub get sv ulong sub c 65 48 +end +end +ret t +end diff --git a/exercises/scrabble-score/scrabble-score.om b/exercises/scrabble-score/scrabble-score.om new file mode 100644 index 0000000..0842d12 --- /dev/null +++ b/exercises/scrabble-score/scrabble-score.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "scrabble-score": https://exercism.org/tracks/python/exercises/scrabble-score +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note score is the Scrabble score of word, ignoring case: AEIOULNRST 1, DG 2, BCMP 3, FHVWY 4, K 5, +note JX 8, QZ 10. +note Check it with: sh exercise.sh exercises/scrabble-score +om2 +fn score word view uchar * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/scrabble-score/test.om b/exercises/scrabble-score/test.om new file mode 100644 index 0000000..b68f142 --- /dev/null +++ b/exercises/scrabble-score/test.om @@ -0,0 +1,129 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/scrabble-score/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 11 cases kept, 0 skipped +text s0 "a" +text c1 "score: lowercase letter" +text s2 "A" +text c3 "score: uppercase letter" +text s4 "f" +text c5 "score: valuable letter" +text s6 "at" +text c7 "score: short word" +text s8 "zoo" +text c9 "score: short, valuable word" +text s10 "street" +text c11 "score: medium word" +text s12 "quirky" +text c13 "score: medium, valuable word" +text s14 "OxyphenButazone" +text c15 "score: long, mixed-case word" +text s16 "pinata" +text c17 "score: english-like word" +text z18 "z" +text c19 "score: empty input" +text s20 "abcdefghijklmnopqrstuvwxyz" +text c21 "score: entire alphabet available" +note canonical case f46cda29-1ca5-4ef2-bd45-388a767e3db2 +fn tc0 to bool +ret eq score s0 1 +end +note canonical case f7794b49-f13e-45d1-a933-4e48459b2201 +fn tc1 to bool +ret eq score s2 1 +end +note canonical case eaba9c76-f9fa-49c9-a1b0-d1ba3a5b31fa +fn tc2 to bool +ret eq score s4 4 +end +note canonical case f3c8c94e-bb48-4da2-b09f-e832e103151e +fn tc3 to bool +ret eq score s6 2 +end +note canonical case 71e3d8fa-900d-4548-930e-68e7067c4615 +fn tc4 to bool +ret eq score s8 12 +end +note canonical case d3088ad9-570c-4b51-8764-c75d5a430e99 +fn tc5 to bool +ret eq score s10 6 +end +note canonical case fa20c572-ad86-400a-8511-64512daac352 +fn tc6 to bool +ret eq score s12 22 +end +note canonical case 9336f0ba-9c2b-4fa0-bd1c-2e2d328cf967 +fn tc7 to bool +ret eq score s14 41 +end +note canonical case 1e34e2c3-e444-4ea7-b598-3c2b46fd2c10 +fn tc8 to bool +ret eq score s16 8 +end +note canonical case 4efe3169-b3b6-4334-8bae-ff4ef24a7e4f +fn tc9 to bool +ret eq score part z18 0 0 0 +end +note canonical case 3b305c1c-f260-4e15-a5b5-cb7d3ea7c3d7 +fn tc10 to bool +ret eq score s20 87 +end +fn main to int +tcheck tc0 c1 +tcheck tc1 c3 +tcheck tc2 c5 +tcheck tc3 c7 +tcheck tc4 c9 +tcheck tc5 c11 +tcheck tc6 c13 +tcheck tc7 c15 +tcheck tc8 c17 +tcheck tc9 c19 +tcheck tc10 c21 +ret int get tfails 0 +end diff --git a/exercises/secret-handshake/.meta/example.om b/exercises/secret-handshake/.meta/example.om new file mode 100644 index 0000000..6542d52 --- /dev/null +++ b/exercises/secret-handshake/.meta/example.om @@ -0,0 +1,49 @@ +note Exercism-style exercise "secret-handshake": https://exercism.org/tracks/python/exercises/secret-handshake +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note commands writes the handshake of the low five bits of n into o, joined by commas: 1 wink, +note 2 double blink, 4 close your eyes, 8 jump, in that order, reversed when bit 16 is set; returns +note the number of bytes written. +note Check it with: sh exercise.sh exercises/secret-handshake +om2 +text hs0 "wink" +text hs1 "double blink" +text hs2 "close your eyes" +text hs3 "jump" +fn app o edit uchar * p ulong s view uchar * to ulong +q = p +for i ulong len s +put o q get s i +q = add q 1 +end +ret q +end +fn appcmd o edit uchar * p ulong k ulong to ulong +if eq k 0 +ret app o p hs0 +end +if eq k 1 +ret app o p hs1 +end +if eq k 2 +ret app o p hs2 +end +ret app o p hs3 +end +fn commands n ulong o edit uchar * to ulong +p = ulong 0 +rev = ne and n 16 0 +for i ulong 4 +k = i +if rev +k = sub 3 i +end +if ne and shr n k 1 0 +if gt p 0 +put o p 44 +p = add p 1 +end +p = appcmd o p k +end +end +ret p +end diff --git a/exercises/secret-handshake/secret-handshake.om b/exercises/secret-handshake/secret-handshake.om new file mode 100644 index 0000000..7f262fb --- /dev/null +++ b/exercises/secret-handshake/secret-handshake.om @@ -0,0 +1,11 @@ +note Exercism-style exercise "secret-handshake": https://exercism.org/tracks/python/exercises/secret-handshake +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note commands writes the handshake of the low five bits of n into o, joined by commas: 1 wink, +note 2 double blink, 4 close your eyes, 8 jump, in that order, reversed when bit 16 is set; returns +note the number of bytes written. +note Check it with: sh exercise.sh exercises/secret-handshake +om2 +fn commands n ulong o edit uchar * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/secret-handshake/test.om b/exercises/secret-handshake/test.om new file mode 100644 index 0000000..fa93ca2 --- /dev/null +++ b/exercises/secret-handshake/test.om @@ -0,0 +1,160 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/secret-handshake/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 11 cases kept, 0 skipped +fn tbeq a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text e0 "wink" +text c1 "commands: wink for 1" +text e2 "double blink" +text c3 "commands: double blink for 10" +text e4 "close your eyes" +text c5 "commands: close your eyes for 100" +text e6 "jump" +text c7 "commands: jump for 1000" +text e8 "wink,double blink" +text c9 "commands: combine two actions" +text e10 "double blink,wink" +text c11 "commands: reverse two actions" +text e12 "jump" +text c13 "commands: reversing one action gives the same action" +text c14 "commands: reversing no actions still gives no actions" +text e15 "wink,double blink,close your eyes,jump" +text c16 "commands: all possible actions" +text e17 "jump,close your eyes,double blink,wink" +text c18 "commands: reverse all possible actions" +text c19 "commands: do nothing for zero" +note canonical case b8496fbd-6778-468c-8054-648d03c4bb23 +fn tc0 to bool +o = new uchar 1024 0 +n = commands 1 o +ret tbeq part o 0 n e0 +end +note canonical case 83ec6c58-81a9-4fd1-bfaf-0160514fc0e3 +fn tc1 to bool +o = new uchar 1024 0 +n = commands 2 o +ret tbeq part o 0 n e2 +end +note canonical case 0e20e466-3519-4134-8082-5639d85fef71 +fn tc2 to bool +o = new uchar 1024 0 +n = commands 4 o +ret tbeq part o 0 n e4 +end +note canonical case b339ddbb-88b7-4b7d-9b19-4134030d9ac0 +fn tc3 to bool +o = new uchar 1024 0 +n = commands 8 o +ret tbeq part o 0 n e6 +end +note canonical case 40499fb4-e60c-43d7-8b98-0de3ca44e0eb +fn tc4 to bool +o = new uchar 1024 0 +n = commands 3 o +ret tbeq part o 0 n e8 +end +note canonical case 9730cdd5-ef27-494b-afd3-5c91ad6c3d9d +fn tc5 to bool +o = new uchar 1024 0 +n = commands 19 o +ret tbeq part o 0 n e10 +end +note canonical case 0b828205-51ca-45cd-90d5-f2506013f25f +fn tc6 to bool +o = new uchar 1024 0 +n = commands 24 o +ret tbeq part o 0 n e12 +end +note canonical case 9949e2ac-6c9c-4330-b685-2089ab28b05f +fn tc7 to bool +o = new uchar 1024 0 +n = commands 16 o +ret eq n 0 +end +note canonical case 23fdca98-676b-4848-970d-cfed7be39f81 +fn tc8 to bool +o = new uchar 1024 0 +n = commands 15 o +ret tbeq part o 0 n e15 +end +note canonical case ae8fe006-d910-4d6f-be00-54b7c3799e79 +fn tc9 to bool +o = new uchar 1024 0 +n = commands 31 o +ret tbeq part o 0 n e17 +end +note canonical case 3d36da37-b31f-4cdb-a396-d93a2ee1c4a5 +fn tc10 to bool +o = new uchar 1024 0 +n = commands 0 o +ret eq n 0 +end +fn main to int +tcheck tc0 c1 +tcheck tc1 c3 +tcheck tc2 c5 +tcheck tc3 c7 +tcheck tc4 c9 +tcheck tc5 c11 +tcheck tc6 c13 +tcheck tc7 c14 +tcheck tc8 c16 +tcheck tc9 c18 +tcheck tc10 c19 +ret int get tfails 0 +end diff --git a/exercises/series/.meta/example.om b/exercises/series/.meta/example.om new file mode 100644 index 0000000..7e51748 --- /dev/null +++ b/exercises/series/.meta/example.om @@ -0,0 +1,35 @@ +note Exercism-style exercise "series": https://exercism.org/tracks/python/exercises/series +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note slices writes every contiguous run of k bytes of s, in order, joined by commas, into o and +note returns the number of bytes written; or -1 when s is empty, -2 when k is negative, -3 when k is +note zero, -4 when k is longer than s. +note Check it with: sh exercise.sh exercises/series +om2 +fn slices s view uchar * k long o edit uchar * to long +n = len s +if eq n 0 +ret -1 +end +if lt k 0 +ret -2 +end +if eq k 0 +ret -3 +end +w = ulong k +if gt w n +ret -4 +end +p = ulong 0 +for i ulong add sub n w 1 +if gt i 0 +put o p 44 +p = add p 1 +end +for j ulong w +put o p get s add i j +p = add p 1 +end +end +ret long p +end diff --git a/exercises/series/series.om b/exercises/series/series.om new file mode 100644 index 0000000..5bffb62 --- /dev/null +++ b/exercises/series/series.om @@ -0,0 +1,11 @@ +note Exercism-style exercise "series": https://exercism.org/tracks/python/exercises/series +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note slices writes every contiguous run of k bytes of s, in order, joined by commas, into o and +note returns the number of bytes written; or -1 when s is empty, -2 when k is negative, -3 when k is +note zero, -4 when k is longer than s. +note Check it with: sh exercise.sh exercises/series +om2 +fn slices s view uchar * k long o edit uchar * to long +note replace this line with your solution +trap 13 +end diff --git a/exercises/series/test.om b/exercises/series/test.om new file mode 100644 index 0000000..b116dd9 --- /dev/null +++ b/exercises/series/test.om @@ -0,0 +1,181 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/series/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 11 cases kept, 0 skipped +fn tbeq a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text s0 "1" +text e1 "1" +text c2 "slices: slices of one from one" +text s3 "12" +text e4 "1,2" +text c5 "slices: slices of one from two" +text s6 "35" +text e7 "35" +text c8 "slices: slices of two" +text s9 "9142" +text e10 "91,14,42" +text c11 "slices: slices of two overlap" +text s12 "777777" +text e13 "777,777,777,777" +text c14 "slices: slices can include duplicates" +text s15 "918493904243" +text e16 "91849,18493,84939,49390,93904,39042,90424,04243" +text c17 "slices: slices of a long series" +text s18 "12345" +text c19 "slices: slice length is too large" +text s20 "12345" +text c21 "slices: slice length is way too large" +text s22 "12345" +text c23 "slices: slice length cannot be zero" +text s24 "123" +text c25 "slices: slice length cannot be negative" +text z26 "z" +text c27 "slices: empty series is invalid" +note canonical case 7ae7a46a-d992-4c2a-9c15-a112d125ebad +fn tc0 to bool +o = new uchar 1024 0 +n = slices s0 1 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e1 +end +note canonical case 3143b71d-f6a5-4221-aeae-619f906244d2 +fn tc1 to bool +o = new uchar 1024 0 +n = slices s3 1 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e4 +end +note canonical case dbb68ff5-76c5-4ccd-895a-93dbec6d5805 +fn tc2 to bool +o = new uchar 1024 0 +n = slices s6 2 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e7 +end +note canonical case 19bbea47-c987-4e11-a7d1-e103442adf86 +fn tc3 to bool +o = new uchar 1024 0 +n = slices s9 2 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e10 +end +note canonical case 8e17148d-ba0a-4007-a07f-d7f87015d84c +fn tc4 to bool +o = new uchar 1024 0 +n = slices s12 3 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e13 +end +note canonical case bd5b085e-f612-4f81-97a8-6314258278b0 +fn tc5 to bool +o = new uchar 1024 0 +n = slices s15 5 o +if lt n 0 +ret false +end +ret tbeq part o 0 ulong n e16 +end +note canonical case 6d235d85-46cf-4fae-9955-14b6efef27cd +fn tc6 to bool +o = new uchar 1024 0 +ret eq slices s18 6 o -4 +end +note canonical case d7957455-346d-4e47-8e4b-87ed1564c6d7 +fn tc7 to bool +o = new uchar 1024 0 +ret eq slices s20 42 o -4 +end +note canonical case d34004ad-8765-4c09-8ba1-ada8ce776806 +fn tc8 to bool +o = new uchar 1024 0 +ret eq slices s22 0 o -3 +end +note canonical case 10ab822d-8410-470a-a85d-23fbeb549e54 +fn tc9 to bool +o = new uchar 1024 0 +ret eq slices s24 -1 o -2 +end +note canonical case c7ed0812-0e4b-4bf3-99c4-28cbbfc246a2 +fn tc10 to bool +o = new uchar 1024 0 +ret eq slices part z26 0 0 1 o -1 +end +fn main to int +tcheck tc0 c2 +tcheck tc1 c5 +tcheck tc2 c8 +tcheck tc3 c11 +tcheck tc4 c14 +tcheck tc5 c17 +tcheck tc6 c19 +tcheck tc7 c21 +tcheck tc8 c23 +tcheck tc9 c25 +tcheck tc10 c27 +ret int get tfails 0 +end diff --git a/exercises/sieve/.meta/example.om b/exercises/sieve/.meta/example.om new file mode 100644 index 0000000..d985540 --- /dev/null +++ b/exercises/sieve/.meta/example.om @@ -0,0 +1,25 @@ +note Exercism-style exercise "sieve": https://exercism.org/tracks/python/exercises/sieve +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note primes writes every prime from 2 up to and including limit (limit at most 1000) into o, in +note increasing order, using the sieve of Eratosthenes, and returns how many it wrote. +note Check it with: sh exercise.sh exercises/sieve +om2 +const SMAX ulong 1001 +fn primes limit ulong o edit ulong * to ulong +comp = new uchar SMAX 0 +k = ulong 0 +for i ulong add limit 1 +if ge i 2 +if eq get comp i 0 +put o k i +k = add k 1 +j = mul i i +while le j limit bound 1000 +put comp j 1 +j = add j i +end +end +end +end +ret k +end diff --git a/exercises/sieve/sieve.om b/exercises/sieve/sieve.om new file mode 100644 index 0000000..c6b8fd1 --- /dev/null +++ b/exercises/sieve/sieve.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "sieve": https://exercism.org/tracks/python/exercises/sieve +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note primes writes every prime from 2 up to and including limit (limit at most 1000) into o, in +note increasing order, using the sieve of Eratosthenes, and returns how many it wrote. +note Check it with: sh exercise.sh exercises/sieve +om2 +fn primes limit ulong o edit ulong * to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/sieve/test.om b/exercises/sieve/test.om new file mode 100644 index 0000000..364d254 --- /dev/null +++ b/exercises/sieve/test.om @@ -0,0 +1,286 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/sieve/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 5 cases kept, 0 skipped +fn tueq a view ulong * b view ulong * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text c0 "primes: no primes under two" +text c1 "primes: find first prime" +text c2 "primes: find primes up to 10" +text c3 "primes: limit is prime" +text c4 "primes: find primes up to 1000" +note canonical case 88529125-c4ce-43cc-bb36-1eb4ddd7b44f +fn tc0 to bool +o = new ulong 256 0 +n = primes 1 o +ret eq n 0 +end +note canonical case 4afe9474-c705-4477-9923-840e1024cc2b +fn tc1 to bool +o = new ulong 256 0 +n = primes 2 o +a0 = new ulong 1 0 +put a0 0 2 +ret tueq part o 0 n a0 +end +note canonical case 974945d8-8cd9-4f00-9463-7d813c7f17b7 +fn tc2 to bool +o = new ulong 256 0 +n = primes 10 o +a0 = new ulong 4 0 +put a0 0 2 +put a0 1 3 +put a0 2 5 +put a0 3 7 +ret tueq part o 0 n a0 +end +note canonical case 2e2417b7-3f3a-452a-8594-b9af08af6d82 +fn tc3 to bool +o = new ulong 256 0 +n = primes 13 o +a0 = new ulong 6 0 +put a0 0 2 +put a0 1 3 +put a0 2 5 +put a0 3 7 +put a0 4 11 +put a0 5 13 +ret tueq part o 0 n a0 +end +note canonical case 92102a05-4c7c-47de-9ed0-b7d5fcd00f21 +fn tc4 to bool +o = new ulong 256 0 +n = primes 1000 o +a0 = new ulong 168 0 +put a0 0 2 +put a0 1 3 +put a0 2 5 +put a0 3 7 +put a0 4 11 +put a0 5 13 +put a0 6 17 +put a0 7 19 +put a0 8 23 +put a0 9 29 +put a0 10 31 +put a0 11 37 +put a0 12 41 +put a0 13 43 +put a0 14 47 +put a0 15 53 +put a0 16 59 +put a0 17 61 +put a0 18 67 +put a0 19 71 +put a0 20 73 +put a0 21 79 +put a0 22 83 +put a0 23 89 +put a0 24 97 +put a0 25 101 +put a0 26 103 +put a0 27 107 +put a0 28 109 +put a0 29 113 +put a0 30 127 +put a0 31 131 +put a0 32 137 +put a0 33 139 +put a0 34 149 +put a0 35 151 +put a0 36 157 +put a0 37 163 +put a0 38 167 +put a0 39 173 +put a0 40 179 +put a0 41 181 +put a0 42 191 +put a0 43 193 +put a0 44 197 +put a0 45 199 +put a0 46 211 +put a0 47 223 +put a0 48 227 +put a0 49 229 +put a0 50 233 +put a0 51 239 +put a0 52 241 +put a0 53 251 +put a0 54 257 +put a0 55 263 +put a0 56 269 +put a0 57 271 +put a0 58 277 +put a0 59 281 +put a0 60 283 +put a0 61 293 +put a0 62 307 +put a0 63 311 +put a0 64 313 +put a0 65 317 +put a0 66 331 +put a0 67 337 +put a0 68 347 +put a0 69 349 +put a0 70 353 +put a0 71 359 +put a0 72 367 +put a0 73 373 +put a0 74 379 +put a0 75 383 +put a0 76 389 +put a0 77 397 +put a0 78 401 +put a0 79 409 +put a0 80 419 +put a0 81 421 +put a0 82 431 +put a0 83 433 +put a0 84 439 +put a0 85 443 +put a0 86 449 +put a0 87 457 +put a0 88 461 +put a0 89 463 +put a0 90 467 +put a0 91 479 +put a0 92 487 +put a0 93 491 +put a0 94 499 +put a0 95 503 +put a0 96 509 +put a0 97 521 +put a0 98 523 +put a0 99 541 +put a0 100 547 +put a0 101 557 +put a0 102 563 +put a0 103 569 +put a0 104 571 +put a0 105 577 +put a0 106 587 +put a0 107 593 +put a0 108 599 +put a0 109 601 +put a0 110 607 +put a0 111 613 +put a0 112 617 +put a0 113 619 +put a0 114 631 +put a0 115 641 +put a0 116 643 +put a0 117 647 +put a0 118 653 +put a0 119 659 +put a0 120 661 +put a0 121 673 +put a0 122 677 +put a0 123 683 +put a0 124 691 +put a0 125 701 +put a0 126 709 +put a0 127 719 +put a0 128 727 +put a0 129 733 +put a0 130 739 +put a0 131 743 +put a0 132 751 +put a0 133 757 +put a0 134 761 +put a0 135 769 +put a0 136 773 +put a0 137 787 +put a0 138 797 +put a0 139 809 +put a0 140 811 +put a0 141 821 +put a0 142 823 +put a0 143 827 +put a0 144 829 +put a0 145 839 +put a0 146 853 +put a0 147 857 +put a0 148 859 +put a0 149 863 +put a0 150 877 +put a0 151 881 +put a0 152 883 +put a0 153 887 +put a0 154 907 +put a0 155 911 +put a0 156 919 +put a0 157 929 +put a0 158 937 +put a0 159 941 +put a0 160 947 +put a0 161 953 +put a0 162 967 +put a0 163 971 +put a0 164 977 +put a0 165 983 +put a0 166 991 +put a0 167 997 +ret tueq part o 0 n a0 +end +fn main to int +tcheck tc0 c0 +tcheck tc1 c1 +tcheck tc2 c2 +tcheck tc3 c3 +tcheck tc4 c4 +ret int get tfails 0 +end diff --git a/exercises/space-age/.meta/example.om b/exercises/space-age/.meta/example.om new file mode 100644 index 0000000..805715b --- /dev/null +++ b/exercises/space-age/.meta/example.om @@ -0,0 +1,61 @@ +note Exercism-style exercise "space-age": https://exercism.org/tracks/python/exercises/space-age +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note age is how old someone of the given seconds is in years of planet: one Earth year is 31557600 +note seconds; orbital periods in Earth years are Mercury 0.2408467, Venus 0.61519726, Earth 1.0, +note Mars 1.8808158, Jupiter 11.862615, Saturn 29.447498, Uranus 84.016846, Neptune 164.79132. +note age is -1.0 when planet is not one of these names. +note Check it with: sh exercise.sh exercises/space-age +om2 +text pl0 "Mercury" +text pl1 "Venus" +text pl2 "Earth" +text pl3 "Mars" +text pl4 "Jupiter" +text pl5 "Saturn" +text pl6 "Uranus" +text pl7 "Neptune" +fn same a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +fn period planet view uchar * to f64 +if same planet pl0 +ret 0.2408467 +end +if same planet pl1 +ret 0.61519726 +end +if same planet pl2 +ret 1.0 +end +if same planet pl3 +ret 1.8808158 +end +if same planet pl4 +ret 11.862615 +end +if same planet pl5 +ret 29.447498 +end +if same planet pl6 +ret 84.016846 +end +if same planet pl7 +ret 164.79132 +end +ret -1.0 +end +fn age planet view uchar * seconds f64 to f64 +p = period planet +if lt p 0.0 +ret -1.0 +end +ret div div seconds 31557600.0 p +end diff --git a/exercises/space-age/space-age.om b/exercises/space-age/space-age.om new file mode 100644 index 0000000..19bcf65 --- /dev/null +++ b/exercises/space-age/space-age.om @@ -0,0 +1,12 @@ +note Exercism-style exercise "space-age": https://exercism.org/tracks/python/exercises/space-age +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note age is how old someone of the given seconds is in years of planet: one Earth year is 31557600 +note seconds; orbital periods in Earth years are Mercury 0.2408467, Venus 0.61519726, Earth 1.0, +note Mars 1.8808158, Jupiter 11.862615, Saturn 29.447498, Uranus 84.016846, Neptune 164.79132. +note age is -1.0 when planet is not one of these names. +note Check it with: sh exercise.sh exercises/space-age +om2 +fn age planet view uchar * seconds f64 to f64 +note replace this line with your solution +trap 13 +end diff --git a/exercises/space-age/test.om b/exercises/space-age/test.om new file mode 100644 index 0000000..bf60493 --- /dev/null +++ b/exercises/space-age/test.om @@ -0,0 +1,123 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/space-age/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 9 cases kept, 0 skipped +text s0 "Earth" +text c1 "age: age on Earth" +text s2 "Mercury" +text c3 "age: age on Mercury" +text s4 "Venus" +text c5 "age: age on Venus" +text s6 "Mars" +text c7 "age: age on Mars" +text s8 "Jupiter" +text c9 "age: age on Jupiter" +text s10 "Saturn" +text c11 "age: age on Saturn" +text s12 "Uranus" +text c13 "age: age on Uranus" +text s14 "Neptune" +text c15 "age: age on Neptune" +text s16 "Sun" +text c17 "age: invalid planet causes error" +note canonical case 84f609af-5a91-4d68-90a3-9e32d8a5cd34 +fn tc0 to bool +v = age s0 1000000000.0 +ret lt fabs sub v 31.69 0.005 +end +note canonical case ca20c4e9-6054-458c-9312-79679ffab40b +fn tc1 to bool +v = age s2 2134835688.0 +ret lt fabs sub v 280.88 0.005 +end +note canonical case 502c6529-fd1b-41d3-8fab-65e03082b024 +fn tc2 to bool +v = age s4 189839836.0 +ret lt fabs sub v 9.78 0.005 +end +note canonical case 9ceadf5e-a0d5-4388-9d40-2c459227ceb8 +fn tc3 to bool +v = age s6 2129871239.0 +ret lt fabs sub v 35.88 0.005 +end +note canonical case 42927dc3-fe5e-4f76-a5b5-f737fc19bcde +fn tc4 to bool +v = age s8 901876382.0 +ret lt fabs sub v 2.41 0.005 +end +note canonical case 8469b332-7837-4ada-b27c-00ee043ebcad +fn tc5 to bool +v = age s10 2000000000.0 +ret lt fabs sub v 2.15 0.005 +end +note canonical case 999354c1-76f8-4bb5-a672-f317b6436743 +fn tc6 to bool +v = age s12 1210123456.0 +ret lt fabs sub v 0.46 0.005 +end +note canonical case 80096d30-a0d4-4449-903e-a381178355d8 +fn tc7 to bool +v = age s14 1821023456.0 +ret lt fabs sub v 0.35 0.005 +end +note canonical case 57b96e2a-1178-40b7-b34d-f3c9c34e4bf4 +fn tc8 to bool +ret lt age s16 680804807.0 0.0 +end +fn main to int +tcheck tc0 c1 +tcheck tc1 c3 +tcheck tc2 c5 +tcheck tc3 c7 +tcheck tc4 c9 +tcheck tc5 c11 +tcheck tc6 c13 +tcheck tc7 c15 +tcheck tc8 c17 +ret int get tfails 0 +end diff --git a/exercises/square-root/.meta/example.om b/exercises/square-root/.meta/example.om new file mode 100644 index 0000000..fa9da86 --- /dev/null +++ b/exercises/square-root/.meta/example.om @@ -0,0 +1,18 @@ +note Exercism-style exercise "square-root": https://exercism.org/tracks/python/exercises/square-root +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note squareroot is the integer square root of n: the largest r with r * r <= n. +note Check it with: sh exercise.sh exercises/square-root +om2 +fn squareroot n ulong to ulong +lo = ulong 0 +hi = add div n 2 1 +while lt lo hi bound 64 +m = div add add lo hi 1 2 +if le m div n m +lo = m +else +hi = sub m 1 +end +end +ret lo +end diff --git a/exercises/square-root/square-root.om b/exercises/square-root/square-root.om new file mode 100644 index 0000000..8680285 --- /dev/null +++ b/exercises/square-root/square-root.om @@ -0,0 +1,9 @@ +note Exercism-style exercise "square-root": https://exercism.org/tracks/python/exercises/square-root +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note squareroot is the integer square root of n: the largest r with r * r <= n. +note Check it with: sh exercise.sh exercises/square-root +om2 +fn squareroot n ulong to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/square-root/test.om b/exercises/square-root/test.om new file mode 100644 index 0000000..94308f8 --- /dev/null +++ b/exercises/square-root/test.om @@ -0,0 +1,88 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/square-root/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 6 cases kept, 0 skipped +text c0 "squareRoot: root of 1" +text c1 "squareRoot: root of 4" +text c2 "squareRoot: root of 25" +text c3 "squareRoot: root of 81" +text c4 "squareRoot: root of 196" +text c5 "squareRoot: root of 65025" +note canonical case 9b748478-7b0a-490c-b87a-609dacf631fd +fn tc0 to bool +ret eq squareroot 1 1 +end +note canonical case 7d3aa9ba-9ac6-4e93-a18b-2e8b477139bb +fn tc1 to bool +ret eq squareroot 4 2 +end +note canonical case 6624aabf-3659-4ae0-a1c8-25ae7f33c6ef +fn tc2 to bool +ret eq squareroot 25 5 +end +note canonical case 93beac69-265e-4429-abb1-94506b431f81 +fn tc3 to bool +ret eq squareroot 81 9 +end +note canonical case fbddfeda-8c4f-4bc4-87ca-6991af35360e +fn tc4 to bool +ret eq squareroot 196 14 +end +note canonical case c03d0532-8368-4734-a8e0-f96a9eb7fc1d +fn tc5 to bool +ret eq squareroot 65025 255 +end +fn main to int +tcheck tc0 c0 +tcheck tc1 c1 +tcheck tc2 c2 +tcheck tc3 c3 +tcheck tc4 c4 +tcheck tc5 c5 +ret int get tfails 0 +end diff --git a/exercises/sum-of-multiples/.meta/example.om b/exercises/sum-of-multiples/.meta/example.om new file mode 100644 index 0000000..f5b1182 --- /dev/null +++ b/exercises/sum-of-multiples/.meta/example.om @@ -0,0 +1,24 @@ +note Exercism-style exercise "sum-of-multiples": https://exercism.org/tracks/python/exercises/sum-of-multiples +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note sum is the sum of every distinct number below limit that is a multiple of at least one of the +note factors fs (a factor 0 has no multiples here). +note Check it with: sh exercise.sh exercises/sum-of-multiples +om2 +fn sum fs view ulong * limit ulong to ulong +t = ulong 0 +for m ulong limit +hit = false +for j ulong len fs +f = get fs j +if ne f 0 +if eq rem m f 0 +hit = true +end +end +end +if and hit ne m 0 +t = add t m +end +end +ret t +end diff --git a/exercises/sum-of-multiples/sum-of-multiples.om b/exercises/sum-of-multiples/sum-of-multiples.om new file mode 100644 index 0000000..6aa92e4 --- /dev/null +++ b/exercises/sum-of-multiples/sum-of-multiples.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "sum-of-multiples": https://exercism.org/tracks/python/exercises/sum-of-multiples +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note sum is the sum of every distinct number below limit that is a multiple of at least one of the +note factors fs (a factor 0 has no multiples here). +note Check it with: sh exercise.sh exercises/sum-of-multiples +om2 +fn sum fs view ulong * limit ulong to ulong +note replace this line with your solution +trap 13 +end diff --git a/exercises/sum-of-multiples/test.om b/exercises/sum-of-multiples/test.om new file mode 100644 index 0000000..c199126 --- /dev/null +++ b/exercises/sum-of-multiples/test.om @@ -0,0 +1,196 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/sum-of-multiples/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 16 cases kept, 0 skipped +text c0 "sum: no multiples within limit" +text c1 "sum: one factor has multiples within limit" +text c2 "sum: more than one multiple within limit" +text c3 "sum: more than one factor with multiples within limit" +text c4 "sum: each multiple is only counted once" +text c5 "sum: a much larger limit" +text c6 "sum: three factors" +text c7 "sum: factors not relatively prime" +text c8 "sum: some pairs of factors relatively prime and some not" +text c9 "sum: one factor is a multiple of another" +text c10 "sum: much larger factors" +text c11 "sum: all numbers are multiples of 1" +text c12 "sum: no factors means an empty sum" +text c13 "sum: the only multiple of 0 is 0" +text c14 "sum: the factor 0 does not affect the sum of multiples of other factors" +text c15 "sum: solutions using include-exclude must extend to cardinality greater than 3" +note canonical case 54aaab5a-ce86-4edc-8b40-d3ab2400a279 +fn tc0 to bool +a0 = new ulong 2 0 +put a0 0 3 +put a0 1 5 +ret eq sum a0 1 0 +end +note canonical case 361e4e50-c89b-4f60-95ef-5bc5c595490a +fn tc1 to bool +a0 = new ulong 2 0 +put a0 0 3 +put a0 1 5 +ret eq sum a0 4 3 +end +note canonical case e644e070-040e-4ae0-9910-93c69fc3f7ce +fn tc2 to bool +a0 = new ulong 1 0 +put a0 0 3 +ret eq sum a0 7 9 +end +note canonical case 607d6eb9-535c-41ce-91b5-3a61da3fa57f +fn tc3 to bool +a0 = new ulong 2 0 +put a0 0 3 +put a0 1 5 +ret eq sum a0 10 23 +end +note canonical case f47e8209-c0c5-4786-b07b-dc273bf86b9b +fn tc4 to bool +a0 = new ulong 2 0 +put a0 0 3 +put a0 1 5 +ret eq sum a0 100 2318 +end +note canonical case 28c4b267-c980-4054-93e9-07723db615ac +fn tc5 to bool +a0 = new ulong 2 0 +put a0 0 3 +put a0 1 5 +ret eq sum a0 1000 233168 +end +note canonical case 09c4494d-ff2d-4e0f-8421-f5532821ee12 +fn tc6 to bool +a0 = new ulong 3 0 +put a0 0 7 +put a0 1 13 +put a0 2 17 +ret eq sum a0 20 51 +end +note canonical case 2d0d5faa-f177-4ad6-bde9-ebb865083751 +fn tc7 to bool +a0 = new ulong 2 0 +put a0 0 4 +put a0 1 6 +ret eq sum a0 15 30 +end +note canonical case ece8f2e8-96aa-4166-bbb7-6ce71261e354 +fn tc8 to bool +a0 = new ulong 3 0 +put a0 0 5 +put a0 1 6 +put a0 2 8 +ret eq sum a0 150 4419 +end +note canonical case 624fdade-6ffb-400e-8472-456a38c171c0 +fn tc9 to bool +a0 = new ulong 2 0 +put a0 0 5 +put a0 1 25 +ret eq sum a0 51 275 +end +note canonical case 949ee7eb-db51-479c-b5cb-4a22b40ac057 +fn tc10 to bool +a0 = new ulong 2 0 +put a0 0 43 +put a0 1 47 +ret eq sum a0 10000 2203160 +end +note canonical case 41093673-acbd-482c-ab80-d00a0cbedecd +fn tc11 to bool +a0 = new ulong 1 0 +put a0 0 1 +ret eq sum a0 100 4950 +end +note canonical case 1730453b-baaa-438e-a9c2-d754497b2a76 +fn tc12 to bool +a0 = new ulong 1 0 +ret eq sum part a0 0 0 10000 0 +end +note canonical case 214a01e9-f4bf-45bb-80f1-1dce9fbb0310 +fn tc13 to bool +a0 = new ulong 1 0 +put a0 0 0 +ret eq sum a0 1 0 +end +note canonical case c423ae21-a0cb-4ec7-aeb1-32971af5b510 +fn tc14 to bool +a0 = new ulong 2 0 +put a0 0 3 +put a0 1 0 +ret eq sum a0 4 3 +end +note canonical case 17053ba9-112f-4ac0-aadb-0519dd836342 +fn tc15 to bool +a0 = new ulong 5 0 +put a0 0 2 +put a0 1 3 +put a0 2 5 +put a0 3 7 +put a0 4 11 +ret eq sum a0 10000 39614537 +end +fn main to int +tcheck tc0 c0 +tcheck tc1 c1 +tcheck tc2 c2 +tcheck tc3 c3 +tcheck tc4 c4 +tcheck tc5 c5 +tcheck tc6 c6 +tcheck tc7 c7 +tcheck tc8 c8 +tcheck tc9 c9 +tcheck tc10 c10 +tcheck tc11 c11 +tcheck tc12 c12 +tcheck tc13 c13 +tcheck tc14 c14 +tcheck tc15 c15 +ret int get tfails 0 +end diff --git a/exercises/triangle/.meta/example.om b/exercises/triangle/.meta/example.om new file mode 100644 index 0000000..88a1c30 --- /dev/null +++ b/exercises/triangle/.meta/example.om @@ -0,0 +1,22 @@ +note Exercism-style exercise "triangle": https://exercism.org/tracks/python/exercises/triangle +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note The sides a b c form a triangle when each is greater than 0 and the sum of any two is at least +note the third. equilateral: a triangle with all three sides equal; isosceles: a triangle with at +note least two sides equal; scalene: a triangle with all sides different. +note Check it with: sh exercise.sh exercises/triangle +om2 +fn istri a f64 b f64 c f64 to bool +if or or le a 0.0 le b 0.0 le c 0.0 +ret false +end +ret and and ge add a b c ge add b c a ge add a c b +end +fn equilateral a f64 b f64 c f64 to bool +ret and istri a b c and eq a b eq b c +end +fn isosceles a f64 b f64 c f64 to bool +ret and istri a b c or or eq a b eq b c eq a c +end +fn scalene a f64 b f64 c f64 to bool +ret and istri a b c and and ne a b ne b c ne a c +end diff --git a/exercises/triangle/test.om b/exercises/triangle/test.om new file mode 100644 index 0000000..09140e9 --- /dev/null +++ b/exercises/triangle/test.om @@ -0,0 +1,178 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/triangle/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 21 cases kept, 0 skipped +text c0 "equilateral: equilateral triangle / all sides are equal" +text c1 "equilateral: equilateral triangle / any side is unequal" +text c2 "equilateral: equilateral triangle / no sides are equal" +text c3 "equilateral: equilateral triangle / all zero sides is not a triangle" +text c4 "equilateral: equilateral triangle / sides may be floats" +text c5 "isosceles: isosceles triangle / last two sides are equal" +text c6 "isosceles: isosceles triangle / first two sides are equal" +text c7 "isosceles: isosceles triangle / first and last sides are equal" +text c8 "isosceles: isosceles triangle / equilateral triangles are also isosceles" +text c9 "isosceles: isosceles triangle / no sides are equal" +text c10 "isosceles: isosceles triangle / first triangle inequality violation" +text c11 "isosceles: isosceles triangle / second triangle inequality violation" +text c12 "isosceles: isosceles triangle / third triangle inequality violation" +text c13 "isosceles: isosceles triangle / sides may be floats" +text c14 "scalene: scalene triangle / no sides are equal" +text c15 "scalene: scalene triangle / all sides are equal" +text c16 "scalene: scalene triangle / first and second sides are equal" +text c17 "scalene: scalene triangle / first and third sides are equal" +text c18 "scalene: scalene triangle / second and third sides are equal" +text c19 "scalene: scalene triangle / may not violate triangle inequality" +text c20 "scalene: scalene triangle / sides may be floats" +note canonical case 8b2c43ac-7257-43f9-b552-7631a91988af +fn tc0 to bool +ret eq equilateral 2.0 2.0 2.0 true +end +note canonical case 33eb6f87-0498-4ccf-9573-7f8c3ce92b7b +fn tc1 to bool +ret eq equilateral 2.0 3.0 2.0 false +end +note canonical case c6585b7d-a8c0-4ad8-8a34-e21d36f7ad87 +fn tc2 to bool +ret eq equilateral 5.0 4.0 6.0 false +end +note canonical case 16e8ceb0-eadb-46d1-b892-c50327479251 +fn tc3 to bool +ret eq equilateral 0.0 0.0 0.0 false +end +note canonical case 3022f537-b8e5-4cc1-8f12-fd775827a00c +fn tc4 to bool +ret eq equilateral 0.5 0.5 0.5 true +end +note canonical case cbc612dc-d75a-4c1c-87fc-e2d5edd70b71 +fn tc5 to bool +ret eq isosceles 3.0 4.0 4.0 true +end +note canonical case e388ce93-f25e-4daf-b977-4b7ede992217 +fn tc6 to bool +ret eq isosceles 4.0 4.0 3.0 true +end +note canonical case d2080b79-4523-4c3f-9d42-2da6e81ab30f +fn tc7 to bool +ret eq isosceles 4.0 3.0 4.0 true +end +note canonical case 8d71e185-2bd7-4841-b7e1-71689a5491d8 +fn tc8 to bool +ret eq isosceles 4.0 4.0 4.0 true +end +note canonical case 840ed5f8-366f-43c5-ac69-8f05e6f10bbb +fn tc9 to bool +ret eq isosceles 2.0 3.0 4.0 false +end +note canonical case 2eba0cfb-6c65-4c40-8146-30b608905eae +fn tc10 to bool +ret eq isosceles 1.0 1.0 3.0 false +end +note canonical case 278469cb-ac6b-41f0-81d4-66d9b828f8ac +fn tc11 to bool +ret eq isosceles 1.0 3.0 1.0 false +end +note canonical case 90efb0c7-72bb-4514-b320-3a3892e278ff +fn tc12 to bool +ret eq isosceles 3.0 1.0 1.0 false +end +note canonical case adb4ee20-532f-43dc-8d31-e9271b7ef2bc +fn tc13 to bool +ret eq isosceles 0.5 0.4 0.5 true +end +note canonical case e8b5f09c-ec2e-47c1-abec-f35095733afb +fn tc14 to bool +ret eq scalene 5.0 4.0 6.0 true +end +note canonical case 2510001f-b44d-4d18-9872-2303e7977dc1 +fn tc15 to bool +ret eq scalene 4.0 4.0 4.0 false +end +note canonical case c6e15a92-90d9-4fb3-90a2-eef64f8d3e1e +fn tc16 to bool +ret eq scalene 4.0 4.0 3.0 false +end +note canonical case 3da23a91-a166-419a-9abf-baf4868fd985 +fn tc17 to bool +ret eq scalene 3.0 4.0 3.0 false +end +note canonical case b6a75d98-1fef-4c42-8e9a-9db854ba0a4d +fn tc18 to bool +ret eq scalene 4.0 3.0 3.0 false +end +note canonical case 70ad5154-0033-48b7-af2c-b8d739cd9fdc +fn tc19 to bool +ret eq scalene 7.0 3.0 2.0 false +end +note canonical case 26d9d59d-f8f1-40d3-ad58-ae4d54123d7d +fn tc20 to bool +ret eq scalene 0.5 0.4 0.6 true +end +fn main to int +tcheck tc0 c0 +tcheck tc1 c1 +tcheck tc2 c2 +tcheck tc3 c3 +tcheck tc4 c4 +tcheck tc5 c5 +tcheck tc6 c6 +tcheck tc7 c7 +tcheck tc8 c8 +tcheck tc9 c9 +tcheck tc10 c10 +tcheck tc11 c11 +tcheck tc12 c12 +tcheck tc13 c13 +tcheck tc14 c14 +tcheck tc15 c15 +tcheck tc16 c16 +tcheck tc17 c17 +tcheck tc18 c18 +tcheck tc19 c19 +tcheck tc20 c20 +ret int get tfails 0 +end diff --git a/exercises/triangle/triangle.om b/exercises/triangle/triangle.om new file mode 100644 index 0000000..f8fab54 --- /dev/null +++ b/exercises/triangle/triangle.om @@ -0,0 +1,19 @@ +note Exercism-style exercise "triangle": https://exercism.org/tracks/python/exercises/triangle +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note The sides a b c form a triangle when each is greater than 0 and the sum of any two is at least +note the third. equilateral: a triangle with all three sides equal; isosceles: a triangle with at +note least two sides equal; scalene: a triangle with all sides different. +note Check it with: sh exercise.sh exercises/triangle +om2 +fn equilateral a f64 b f64 c f64 to bool +note replace this line with your solution +trap 13 +end +fn isosceles a f64 b f64 c f64 to bool +note replace this line with your solution +trap 13 +end +fn scalene a f64 b f64 c f64 to bool +note replace this line with your solution +trap 13 +end diff --git a/exercises/two-fer/.meta/example.om b/exercises/two-fer/.meta/example.om new file mode 100644 index 0000000..0738a71 --- /dev/null +++ b/exercises/two-fer/.meta/example.om @@ -0,0 +1,27 @@ +note Exercism-style exercise "two-fer": https://exercism.org/tracks/python/exercises/two-fer +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note twofer writes "One for X, one for me." into o, X being name, or "you" when name is empty, +note and returns the number of bytes written. +note Check it with: sh exercise.sh exercises/two-fer +om2 +text ofa "One for " +text ofb ", one for me." +text ofy "you" +fn app o edit uchar * p ulong s view uchar * to ulong +q = p +for i ulong len s +put o q get s i +q = add q 1 +end +ret q +end +fn twofer name view uchar * o edit uchar * to ulong +p = app o 0 ofa +if eq len name 0 +p = app o p ofy +else +p = app o p name +end +p = app o p ofb +ret p +end diff --git a/exercises/two-fer/test.om b/exercises/two-fer/test.om new file mode 100644 index 0000000..64e88ef --- /dev/null +++ b/exercises/two-fer/test.om @@ -0,0 +1,93 @@ +note ---- test harness (appended after the solution by exercise.sh) ---- +text tok "ok " +text tnok "not ok " +text tsep " - " +text tnl "\n" +global tfails ulong 1 +global tnum ulong 1 +fn temit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 2 +end +o = add o ulong k +end +end +fn temitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +temit part d i sub 20 i +end +note one TAP-style line per case; main returns the number of failed cases +fn tcheck c bool what view uchar * to void +put tnum 0 add get tnum 0 1 +if c +temit tok +else +temit tnok +put tfails 0 add get tfails 0 1 +end +temitu get tnum 0 +temit tsep +temit what +temit tnl +end +note cases: exercism/problem-specifications exercises/two-fer/canonical-data.json at +note commit 650bebeb39ad40439175288042aece982b0a90a9 (2026-10-19), MIT licence, +note https://github.com/exercism/problem-specifications +note 3 cases kept, 0 skipped +fn tbeq a view uchar * b view uchar * to bool +if ne len a len b +ret false +end +for i ulong len a +if ne get a i get b i +ret false +end +end +ret true +end +text z0 "z" +text e1 "One for you, one for me." +text c2 "twoFer: no name given" +text s3 "Alice" +text e4 "One for Alice, one for me." +text c5 "twoFer: a name given" +text s6 "Bob" +text e7 "One for Bob, one for me." +text c8 "twoFer: another name given" +note canonical case 1cf3e15a-a3d7-4a87-aeb3-ba1b43bc8dce +fn tc0 to bool +o = new uchar 1024 0 +n = twofer part z0 0 0 o +ret tbeq part o 0 n e1 +end +note canonical case b4c6dbb8-b4fb-42c2-bafd-10785abe7709 +fn tc1 to bool +o = new uchar 1024 0 +n = twofer s3 o +ret tbeq part o 0 n e4 +end +note canonical case 3549048d-1a6e-4653-9a79-b0bda163e8d5 +fn tc2 to bool +o = new uchar 1024 0 +n = twofer s6 o +ret tbeq part o 0 n e7 +end +fn main to int +tcheck tc0 c2 +tcheck tc1 c5 +tcheck tc2 c8 +ret int get tfails 0 +end diff --git a/exercises/two-fer/two-fer.om b/exercises/two-fer/two-fer.om new file mode 100644 index 0000000..529c412 --- /dev/null +++ b/exercises/two-fer/two-fer.om @@ -0,0 +1,10 @@ +note Exercism-style exercise "two-fer": https://exercism.org/tracks/python/exercises/two-fer +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +note twofer writes "One for X, one for me." into o, X being name, or "you" when name is empty, +note and returns the number of bytes written. +note Check it with: sh exercise.sh exercises/two-fer +om2 +fn twofer name view uchar * o edit uchar * to ulong +note replace this line with your solution +trap 13 +end diff --git a/tasks/100-doors/100-doors.om b/tasks/100-doors/100-doors.om new file mode 100644 index 0000000..ae42e07 --- /dev/null +++ b/tasks/100-doors/100-doors.om @@ -0,0 +1,55 @@ +note Rosetta Code task "100 doors": https://rosettacode.org/wiki/100_doors +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note pass p (1..100) toggles every p-th door; afterwards print the open doors +fn main to int +d = new uchar 101 0 +for k ulong 100 +p = add k 1 +i = p +while le i 100 bound 101 +put d i xor get d i 1 +i = add i p +end +end +f = true +for i ulong 101 +if eq get d i 1 +if not f +emit sp +end +emitu i +f = false +end +end +emit nl +ret 0 +end diff --git a/tasks/100-doors/expected.txt b/tasks/100-doors/expected.txt new file mode 100644 index 0000000..7277fff --- /dev/null +++ b/tasks/100-doors/expected.txt @@ -0,0 +1 @@ +1 4 9 16 25 36 49 64 81 100 diff --git a/tasks/100-doors/oracle.py b/tasks/100-doors/oracle.py new file mode 100644 index 0000000..f8d11ec --- /dev/null +++ b/tasks/100-doors/oracle.py @@ -0,0 +1,7 @@ +# Independent reference for "100 doors" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +doors = [False] * 101 +for p in range(1, 101): + for i in range(p, 101, p): + doors[i] = not doors[i] +print(" ".join(str(i) for i in range(1, 101) if doors[i])) diff --git a/tasks/abc-problem/abc-problem.om b/tasks/abc-problem/abc-problem.om new file mode 100644 index 0000000..7e066a6 --- /dev/null +++ b/tasks/abc-problem/abc-problem.om @@ -0,0 +1,79 @@ +note Rosetta Code task "ABC problem": https://rosettacode.org/wiki/ABC_problem +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text tyes "yes" +text tno "no" +note the 20 blocks, two letters each, separated by spaces +text blocks "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM" +text w0 "A" +text w1 "BARK" +text w2 "BOOK" +text w3 "TREAT" +text w4 "COMMON" +text w5 "SQUAD" +text w6 "CONFUSE" +global used uchar 20 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +fn emitb b bool to void +if b +emit tyes +else +emit tno +end +end +note onblock: block b carries letter c +fn onblock b ulong c uchar to bool +x = mul b 3 +ret or eq get blocks x c eq get blocks add x 1 c +end +note can: letters i.. of w can be spelled with the unused blocks (depth-first, a block is taken +note back when the rest fails) +fn can w view uchar * i ulong to bool +if eq i len w +ret true +end +c = get w i +for b ulong 20 +if eq get used b 0 +if onblock b c +put used b 1 +if can w add i 1 +ret true +end +put used b 0 +end +end +end +ret false +end +fn show w view uchar * to void +for b ulong 20 +put used b 0 +end +emit w +emit sp +emitb can w 0 +emit nl +end +fn main to int +show w0 +show w1 +show w2 +show w3 +show w4 +show w5 +show w6 +ret 0 +end diff --git a/tasks/abc-problem/expected.txt b/tasks/abc-problem/expected.txt new file mode 100644 index 0000000..e7ea515 --- /dev/null +++ b/tasks/abc-problem/expected.txt @@ -0,0 +1,7 @@ +A yes +BARK yes +BOOK no +TREAT yes +COMMON no +SQUAD yes +CONFUSE yes diff --git a/tasks/abc-problem/oracle.py b/tasks/abc-problem/oracle.py new file mode 100644 index 0000000..da128f8 --- /dev/null +++ b/tasks/abc-problem/oracle.py @@ -0,0 +1,12 @@ +# Independent reference for "ABC problem" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +blocks = "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM".split() +def can(word, used): + if not word: + return True + for i, b in enumerate(blocks): + if i not in used and word[0] in b and can(word[1:], used | {i}): + return True + return False +for w in ['A', 'BARK', 'BOOK', 'TREAT', 'COMMON', 'SQUAD', 'CONFUSE']: + print(w, "yes" if can(w, frozenset()) else "no") diff --git a/tasks/abelian-sandpile-model/abelian-sandpile-model.om b/tasks/abelian-sandpile-model/abelian-sandpile-model.om new file mode 100644 index 0000000..dc0705e --- /dev/null +++ b/tasks/abelian-sandpile-model/abelian-sandpile-model.om @@ -0,0 +1,97 @@ +note Rosetta Code task "Abelian sandpile model": https://rosettacode.org/wiki/Abelian_sandpile_model +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text ttop "topplings: " +global grid ulong 81 +global tops ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a cell holding 4 or more grains topples: it loses 4 and each of its 4 neighbours gains 1; grains +note pushed past the edge are lost; the end state and the number of topplings do not depend on order +fn give k ulong r long c long n ulong to void +if and and ge r 0 lt r long n and ge c 0 lt c long n +j = add mul ulong r n ulong c +put grid j add get grid j 1 +end +end +fn topple r ulong c ulong n ulong to void +k = add mul r n c +put grid k sub get grid k 4 +put tops 0 add get tops 0 1 +give k sub long r 1 long c n +give k add long r 1 long c n +give k long r sub long c 1 n +give k long r add long c 1 n +end +note sweep topples every full cell once and says whether any toppled +fn sweep n ulong to bool +any = false +for r ulong n +for c ulong n +if ge get grid add mul r n c 4 +topple r c n +any = true +end +end +end +ret any +end +note sweeping in this order takes 6, 25 and 91 sweeps for the three cases (counted on the host); +note 10000 bounds them with room to spare +fn pile n ulong grains ulong to void +for i ulong mul n n +put grid i 0 +end +put tops 0 0 +m = div n 2 +put grid add mul m n m grains +while sweep n bound 10000 +end +for r ulong n +for c ulong n +if gt c 0 +emit sp +end +emitu get grid add mul r n c +end +emit nl +end +emit ttop +emitu get tops 0 +emit nl +end +fn main to int +pile 5 16 +emit nl +pile 7 64 +emit nl +pile 9 200 +ret 0 +end diff --git a/tasks/abelian-sandpile-model/expected.txt b/tasks/abelian-sandpile-model/expected.txt new file mode 100644 index 0000000..0e21305 --- /dev/null +++ b/tasks/abelian-sandpile-model/expected.txt @@ -0,0 +1,26 @@ +0 0 1 0 0 +0 2 1 2 0 +1 1 0 1 1 +0 2 1 2 0 +0 0 1 0 0 +topplings: 9 + +0 0 1 2 1 0 0 +0 2 2 2 2 2 0 +1 2 2 2 2 2 1 +2 2 2 0 2 2 2 +1 2 2 2 2 2 1 +0 2 2 2 2 2 0 +0 0 1 2 1 0 0 +topplings: 88 + +0 3 2 3 1 3 2 3 0 +3 0 3 2 0 2 3 0 3 +2 3 2 2 3 2 2 3 2 +3 2 2 2 3 2 2 2 3 +1 0 3 3 0 3 3 0 1 +3 2 2 2 3 2 2 2 3 +2 3 2 2 3 2 2 3 2 +3 0 3 2 0 2 3 0 3 +0 3 2 3 1 3 2 3 0 +topplings: 758 diff --git a/tasks/abelian-sandpile-model/oracle.py b/tasks/abelian-sandpile-model/oracle.py new file mode 100644 index 0000000..b547f6d --- /dev/null +++ b/tasks/abelian-sandpile-model/oracle.py @@ -0,0 +1,26 @@ +# Independent reference for "Abelian sandpile model" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# a work list of full cells, each toppling all its whole fours at once +def pile(n, grains): + g = {(r, c): 0 for r in range(n) for c in range(n)} + g[n // 2, n // 2] = grains + work, tops = [(n // 2, n // 2)], 0 + while work: + r, c = work.pop() + k = g[r, c] // 4 + if k == 0: + continue + g[r, c] -= 4 * k + tops += k + for p in ((r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)): + if p in g: + g[p] += k + if g[p] >= 4: + work.append(p) + for r in range(n): + print(" ".join(str(g[r, c]) for c in range(n))) + print("topplings:", tops) +for t, (n, grains) in enumerate(((5, 16), (7, 64), (9, 200))): + if t: + print() + pile(n, grains) diff --git a/tasks/abundant-deficient-and-perfect-number-classifications/abundant-deficient-and-perfect-number-classifications.om b/tasks/abundant-deficient-and-perfect-number-classifications/abundant-deficient-and-perfect-number-classifications.om new file mode 100644 index 0000000..2b89cdd --- /dev/null +++ b/tasks/abundant-deficient-and-perfect-number-classifications/abundant-deficient-and-perfect-number-classifications.om @@ -0,0 +1,77 @@ +note Rosetta Code task "Abundant, deficient and perfect number classifications": https://rosettacode.org/wiki/Abundant,_deficient_and_perfect_number_classifications +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text tdef "deficient: " +text tper "perfect: " +text tabu "abundant: " +note ds n is the sum of the proper divisors of n, for n up to 20000 +global ds ulong 20001 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note each i adds itself to its multiples 2i, 3i, ... up to 20000; fewer than 20000 multiples per i +fn sieve to void +i = ulong 1 +while le i 10000 bound 10000 +j = mul i 2 +while le j 20000 bound 20000 +put ds j add get ds j i +j = add j i +end +i = add i 1 +end +end +note 1..20000 sorted by comparing each n with the sum of its proper divisors +fn main to int +sieve +d = ulong 0 +p = ulong 0 +a = ulong 0 +for i ulong 20000 +n = add i 1 +s = get ds n +if lt s n +d = add d 1 +end +if eq s n +p = add p 1 +end +if gt s n +a = add a 1 +end +end +emit tdef +emitu d +emit nl +emit tper +emitu p +emit nl +emit tabu +emitu a +emit nl +ret 0 +end diff --git a/tasks/abundant-deficient-and-perfect-number-classifications/expected.txt b/tasks/abundant-deficient-and-perfect-number-classifications/expected.txt new file mode 100644 index 0000000..aae7346 --- /dev/null +++ b/tasks/abundant-deficient-and-perfect-number-classifications/expected.txt @@ -0,0 +1,3 @@ +deficient: 15043 +perfect: 4 +abundant: 4953 diff --git a/tasks/abundant-deficient-and-perfect-number-classifications/oracle.py b/tasks/abundant-deficient-and-perfect-number-classifications/oracle.py new file mode 100644 index 0000000..a1c43fa --- /dev/null +++ b/tasks/abundant-deficient-and-perfect-number-classifications/oracle.py @@ -0,0 +1,18 @@ +# Independent reference for "Abundant, deficient and perfect number classifications" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import math +def sigma_proper(n): + if n < 2: + return 0 + s = 1 + for d in range(2, math.isqrt(n) + 1): + if n % d == 0: + s += d + (n // d if n // d != d else 0) + return s +c = {"deficient": 0, "perfect": 0, "abundant": 0} +for n in range(1, 20001): + s = sigma_proper(n) + c["deficient" if s < n else "perfect" if s == n else "abundant"] += 1 +print(f"deficient: {c['deficient']}") +print(f"perfect: {c['perfect']}") +print(f"abundant: {c['abundant']}") diff --git a/tasks/abundant-deficient-and-perfect-number-classifications/wolfram.txt b/tasks/abundant-deficient-and-perfect-number-classifications/wolfram.txt new file mode 100644 index 0000000..a73073e --- /dev/null +++ b/tasks/abundant-deficient-and-perfect-number-classifications/wolfram.txt @@ -0,0 +1,12 @@ +Wolfram cross-check of tasks/abundant-deficient-and-perfect-number-classifications/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 4eabb54cab93c76c9f863c5fd98f1dcca4cff2ccf3791c77a8713642d852ac69. +agreement: YES +query: +c = Counts[Table[Sign[DivisorSigma[1, n] - 2 n], {n, 1, 20000}]]; +s = "deficient: " <> ToString[c[-1]] <> "\nperfect: " <> ToString[c[0]] <> "\nabundant: " <> ToString[c[1]] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"4eabb54cab93c76c9f863c5fd98f1dcca4cff2ccf3791c77a8713642d852ac69", "deficient: 15043\nperfect: 4\nabundant: 4953\n"} diff --git a/tasks/additive-primes/additive-primes.om b/tasks/additive-primes/additive-primes.om new file mode 100644 index 0000000..69fb519 --- /dev/null +++ b/tasks/additive-primes/additive-primes.om @@ -0,0 +1,92 @@ +note Rosetta Code task "Additive primes": https://rosettacode.org/wiki/Additive_primes +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text hdr "additive primes below 500 (prime, and the digit sum is prime):\n" +text cnt "count: " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n in decimal right-aligned in w columns (w at most 20) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt i sub 20 w +i = sub 20 w +end +emit part d i sub 20 i +end +fn isprime n ulong to bool +if lt n 2 +ret false +end +d = ulong 2 +while le mul d d n bound 32 +if eq rem n d 0 +ret false +end +d = add d 1 +end +ret true +end +fn dsum n ulong to ulong +s = ulong 0 +v = n +while gt v 0 bound 4 +s = add s rem v 10 +v = div v 10 +end +ret s +end +fn main to int +emit hdr +c = ulong 0 +for n ulong 500 +if isprime n +if isprime dsum n +emitw n 4 +c = add c 1 +if eq rem c 10 0 +emit nl +end +end +end +end +emit nl +emit cnt +emitu c +emit nl +ret 0 +end diff --git a/tasks/additive-primes/expected.txt b/tasks/additive-primes/expected.txt new file mode 100644 index 0000000..752c2ea --- /dev/null +++ b/tasks/additive-primes/expected.txt @@ -0,0 +1,8 @@ +additive primes below 500 (prime, and the digit sum is prime): + 2 3 5 7 11 23 29 41 43 47 + 61 67 83 89 101 113 131 137 139 151 + 157 173 179 191 193 197 199 223 227 229 + 241 263 269 281 283 311 313 317 331 337 + 353 359 373 379 397 401 409 421 443 449 + 461 463 467 487 +count: 54 diff --git a/tasks/additive-primes/oracle.py b/tasks/additive-primes/oracle.py new file mode 100644 index 0000000..96d358b --- /dev/null +++ b/tasks/additive-primes/oracle.py @@ -0,0 +1,11 @@ +# Independent reference for "Additive primes" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +sieve = [False, False] + [True] * 498 +for i in range(2, 23): + if sieve[i]: + sieve[i * i::i] = [False] * len(sieve[i * i::i]) +add = [p for p in range(500) if sieve[p] and sieve[sum(map(int, str(p)))]] +print("additive primes below 500 (prime, and the digit sum is prime):") +for i in range(0, len(add), 10): + print("".join(f"{p:4d}" for p in add[i:i + 10])) +print(f"count: {len(add)}") diff --git a/tasks/additive-primes/wolfram.txt b/tasks/additive-primes/wolfram.txt new file mode 100644 index 0000000..4ea50ac --- /dev/null +++ b/tasks/additive-primes/wolfram.txt @@ -0,0 +1,14 @@ +Wolfram cross-check of tasks/additive-primes/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = f27aec6e75dc0416472a47c065183e66bfc1c011b937e7e60f4f97d3f6a7e83b. +agreement: YES +query: +ap = Select[Prime[Range[PrimePi[499]]], PrimeQ[Total[IntegerDigits[#]]] &]; +s = "additive primes below 500 (prime, and the digit sum is prime):\n" <> + StringJoin[(StringJoin[StringPadLeft[ToString[#], 4] & /@ #] <> "\n") & /@ Partition[ap, UpTo[10]]] <> + "count: " <> ToString[Length[ap]] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"f27aec6e75dc0416472a47c065183e66bfc1c011b937e7e60f4f97d3f6a7e83b", "additive primes below 500 (prime, and the digit sum is prime):\n 2 3 5 7 11 23 29 41 43 47\n 61 67 83 89 101 113 131 137 139 151\n 157 173 179 191 193 197 199 223 227 229\n 241 263 269 281 283 311 313 317 331 337\n 353 359 373 379 397 401 409 421 443 449\n 461 463 467 487\ncount: 54\n"} diff --git a/tasks/almost-prime/almost-prime.om b/tasks/almost-prime/almost-prime.om new file mode 100644 index 0000000..3d5d5a2 --- /dev/null +++ b/tasks/almost-prime/almost-prime.om @@ -0,0 +1,72 @@ +note Rosetta Code task "Almost prime": https://rosettacode.org/wiki/Almost_prime +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text kk "k = " +text col ":" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a k-almost prime has exactly k prime factors counted with multiplicity +fn bigo n ulong to ulong +v = n +d = ulong 2 +k = ulong 0 +while le mul d d v bound 64 +if eq rem v d 0 +k = add k 1 +v = div v d +else +d = add d 1 +end +end +if gt v 1 +k = add k 1 +end +ret k +end +note bound: the 10th 5-almost prime is 176, so no list needs more than 200 candidates +fn main to int +for j ulong 5 +k = add j 1 +emit kk +emitu k +emit col +c = ulong 0 +n = ulong 2 +while lt c 10 bound 200 +if eq bigo n k +emit sp +emitu n +c = add c 1 +end +n = add n 1 +end +emit nl +end +ret 0 +end diff --git a/tasks/almost-prime/expected.txt b/tasks/almost-prime/expected.txt new file mode 100644 index 0000000..c2e3d19 --- /dev/null +++ b/tasks/almost-prime/expected.txt @@ -0,0 +1,5 @@ +k = 1: 2 3 5 7 11 13 17 19 23 29 +k = 2: 4 6 9 10 14 15 21 22 25 26 +k = 3: 8 12 18 20 27 28 30 42 44 45 +k = 4: 16 24 36 40 54 56 60 81 84 88 +k = 5: 32 48 72 80 108 112 120 162 168 176 diff --git a/tasks/almost-prime/oracle.py b/tasks/almost-prime/oracle.py new file mode 100644 index 0000000..0841e68 --- /dev/null +++ b/tasks/almost-prime/oracle.py @@ -0,0 +1,17 @@ +# Independent reference for "Almost prime" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def omega(n): + k, p = 0, 2 + while n > 1: + while n % p == 0: + n //= p + k += 1 + p += 1 + return k +for k in range(1, 6): + out, n = [], 1 + while len(out) < 10: + n += 1 + if omega(n) == k: + out.append(n) + print(f"k = {k}:" + "".join(f" {v}" for v in out)) diff --git a/tasks/almost-prime/wolfram.txt b/tasks/almost-prime/wolfram.txt new file mode 100644 index 0000000..ea74e96 --- /dev/null +++ b/tasks/almost-prime/wolfram.txt @@ -0,0 +1,11 @@ +Wolfram cross-check of tasks/almost-prime/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 7630db515363db20a870043e295985483a840ac4790e67618162f873b200d2c3. +agreement: YES +query: +s = StringJoin[Table["k = " <> ToString[k] <> ":" <> StringJoin[(" " <> ToString[#]) & /@ Take[Select[Range[2, 300], PrimeOmega[#] == k &], 10]] <> "\n", {k, 1, 5}]]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"7630db515363db20a870043e295985483a840ac4790e67618162f873b200d2c3", "k = 1: 2 3 5 7 11 13 17 19 23 29\nk = 2: 4 6 9 10 14 15 21 22 25 26\nk = 3: 8 12 18 20 27 28 30 42 44 45\nk = 4: 16 24 36 40 54 56 60 81 84 88\nk = 5: 32 48 72 80 108 112 120 162 168 176\n"} diff --git a/tasks/amicable-pairs/amicable-pairs.om b/tasks/amicable-pairs/amicable-pairs.om new file mode 100644 index 0000000..dd71178 --- /dev/null +++ b/tasks/amicable-pairs/amicable-pairs.om @@ -0,0 +1,60 @@ +note Rosetta Code task "Amicable pairs": https://rosettacode.org/wiki/Amicable_pairs +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +note ds n is the sum of the proper divisors of n, for n below 20000 +global ds ulong 20000 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note each i adds itself to its multiples 2i, 3i, ... below 20000; fewer than 20000 multiples per i +fn sieve to void +i = ulong 1 +while lt i 10000 bound 10000 +j = mul i 2 +while lt j 20000 bound 20000 +put ds j add get ds j i +j = add j i +end +i = add i 1 +end +end +fn main to int +sieve +for n ulong 20000 +m = get ds n +if and gt m n lt m 20000 +if eq get ds m n +emitu n +emit sp +emitu m +emit nl +end +end +end +ret 0 +end diff --git a/tasks/amicable-pairs/expected.txt b/tasks/amicable-pairs/expected.txt new file mode 100644 index 0000000..12dd85e --- /dev/null +++ b/tasks/amicable-pairs/expected.txt @@ -0,0 +1,8 @@ +220 284 +1184 1210 +2620 2924 +5020 5564 +6232 6368 +10744 10856 +12285 14595 +17296 18416 diff --git a/tasks/amicable-pairs/oracle.py b/tasks/amicable-pairs/oracle.py new file mode 100644 index 0000000..39a7cb6 --- /dev/null +++ b/tasks/amicable-pairs/oracle.py @@ -0,0 +1,15 @@ +# Independent reference for "Amicable pairs" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import math +def sigma_proper(n): + if n < 2: + return 0 + s = 1 + for d in range(2, math.isqrt(n) + 1): + if n % d == 0: + s += d + (n // d if n // d != d else 0) + return s +for n in range(1, 20000): + m = sigma_proper(n) + if n < m < 20000 and sigma_proper(m) == n: + print(n, m) diff --git a/tasks/amicable-pairs/wolfram.txt b/tasks/amicable-pairs/wolfram.txt new file mode 100644 index 0000000..141104a --- /dev/null +++ b/tasks/amicable-pairs/wolfram.txt @@ -0,0 +1,13 @@ +Wolfram cross-check of tasks/amicable-pairs/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 887ab0802e179f080fbf921a2a954d846aec410a73c5080a73d9cb5790a2fb65. +agreement: YES +query: +sp[n_] := DivisorSigma[1, n] - n; +pairs = Select[Table[{n, sp[n]}, {n, 1, 19999}], #[[1]] < #[[2]] < 20000 && sp[#[[2]]] == #[[1]] &]; +s = StringJoin[(ToString[#[[1]]] <> " " <> ToString[#[[2]]] <> "\n") & /@ pairs]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"887ab0802e179f080fbf921a2a954d846aec410a73c5080a73d9cb5790a2fb65", "220 284\n1184 1210\n2620 2924\n5020 5564\n6232 6368\n10744 10856\n12285 14595\n17296 18416\n"} diff --git a/tasks/anagrams/anagrams.om b/tasks/anagrams/anagrams.om new file mode 100644 index 0000000..d612f88 --- /dev/null +++ b/tasks/anagrams/anagrams.om @@ -0,0 +1,121 @@ +note Rosetta Code task "Anagrams": https://rosettacode.org/wiki/Anagrams +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +note the word list (Rosetta Code reads unixdict.txt; this is a fixed sample of it), newline-terminated +text dict "abel\nable\nbale\nbela\nelba\nalger\nglare\nlager\nlarge\nregal\nangel\nangle\ngalen\nglean\nlange\ncaret\ncarte\ncater\ncrate\ntrace\nevil\nlevi\nlive\nveil\nvile\nelan\nlane\nlean\nlena\nneal\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note key writes the letters of a sorted (insertion sort) into k; words are at most 16 letters +fn key a view uchar * k edit uchar 16 to void +for i ulong 16 +put k i 0 +end +for i ulong len a +c = get a i +j = i +while gt j 0 bound 16 +if le get k sub j 1 c +break +end +put k j get k sub j 1 +j = sub j 1 +end +put k j c +end +end +fn same x view uchar 16 y view uchar 16 to bool +for i ulong 16 +if ne get x i get y i +ret false +end +end +ret true +end +note index records each word's (start, length) and returns how many words there are +fn index st edit ulong 64 ln edit ulong 64 to ulong +k = ulong 0 +s = ulong 0 +for i ulong len dict +if eq get dict i 10 +put st k s +put ln k sub i s +k = add k 1 +s = add i 1 +end +end +ret k +end +note count: how many words share word w's key +fn count st view ulong 64 ln view ulong 64 n ulong w ulong to ulong +a = new uchar 16 0 +b = new uchar 16 0 +key part dict get st w get ln w a +c = ulong 0 +for v ulong n +key part dict get st v get ln v b +if same a b +c = add c 1 +end +end +ret c +end +note first: no earlier word has word w's key +fn first st view ulong 64 ln view ulong 64 w ulong to bool +a = new uchar 16 0 +b = new uchar 16 0 +key part dict get st w get ln w a +for v ulong w +key part dict get st v get ln v b +if same a b +ret false +end +end +ret true +end +note print every group of the largest size, groups by first appearance, words in list order +fn show st view ulong 64 ln view ulong 64 n ulong best ulong to void +a = new uchar 16 0 +b = new uchar 16 0 +for w ulong n +if and first st ln w eq count st ln n w best +key part dict get st w get ln w a +f = true +for v ulong n +key part dict get st v get ln v b +if same a b +if not f +emit sp +end +emit part dict get st v get ln v +f = false +end +end +emit nl +end +end +end +fn main to int +st = new ulong 64 0 +ln = new ulong 64 0 +n = index st ln +best = ulong 0 +for w ulong n +c = count st ln n w +if gt c best +best = c +end +end +show st ln n best +ret 0 +end diff --git a/tasks/anagrams/expected.txt b/tasks/anagrams/expected.txt new file mode 100644 index 0000000..180c557 --- /dev/null +++ b/tasks/anagrams/expected.txt @@ -0,0 +1,6 @@ +abel able bale bela elba +alger glare lager large regal +angel angle galen glean lange +caret carte cater crate trace +evil levi live veil vile +elan lane lean lena neal diff --git a/tasks/anagrams/oracle.py b/tasks/anagrams/oracle.py new file mode 100644 index 0000000..55edf1a --- /dev/null +++ b/tasks/anagrams/oracle.py @@ -0,0 +1,10 @@ +# Independent reference for "Anagrams" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +words = ['abel', 'able', 'bale', 'bela', 'elba', 'alger', 'glare', 'lager', 'large', 'regal', 'angel', 'angle', 'galen', 'glean', 'lange', 'caret', 'carte', 'cater', 'crate', 'trace', 'evil', 'levi', 'live', 'veil', 'vile', 'elan', 'lane', 'lean', 'lena', 'neal'] +groups = {} +for w in words: + groups.setdefault("".join(sorted(w)), []).append(w) +best = max(map(len, groups.values())) +for g in groups.values(): + if len(g) == best: + print(" ".join(g)) diff --git a/tasks/anti-primes/anti-primes.om b/tasks/anti-primes/anti-primes.om new file mode 100644 index 0000000..7a1ebd1 --- /dev/null +++ b/tasks/anti-primes/anti-primes.om @@ -0,0 +1,66 @@ +note Rosetta Code task "Anti-primes": https://rosettacode.org/wiki/Anti-primes +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text first "first 20 anti-primes:" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note divisor count by pairing d with n / d for d * d <= n +fn ndiv n ulong to ulong +c = ulong 0 +d = ulong 1 +while le mul d d n bound 100 +if eq rem n d 0 +c = add c 2 +if eq mul d d n +c = sub c 1 +end +end +d = add d 1 +end +ret c +end +note an anti-prime has more divisors than every smaller positive integer; the 20th is 7560 +fn main to int +emit first +best = ulong 0 +c = ulong 0 +n = ulong 1 +while lt c 20 bound 7560 +k = ndiv n +if gt k best +best = k +emit sp +emitu n +c = add c 1 +end +n = add n 1 +end +emit nl +ret 0 +end diff --git a/tasks/anti-primes/expected.txt b/tasks/anti-primes/expected.txt new file mode 100644 index 0000000..6cd991f --- /dev/null +++ b/tasks/anti-primes/expected.txt @@ -0,0 +1 @@ +first 20 anti-primes: 1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 diff --git a/tasks/anti-primes/oracle.py b/tasks/anti-primes/oracle.py new file mode 100644 index 0000000..59dc603 --- /dev/null +++ b/tasks/anti-primes/oracle.py @@ -0,0 +1,10 @@ +# Independent reference for "Anti-primes" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +out, best, n = [], 0, 0 +while len(out) < 20: + n += 1 + k = sum(1 for d in range(1, n + 1) if n % d == 0) + if k > best: + best = k + out.append(n) +print("first 20 anti-primes: " + " ".join(map(str, out))) diff --git a/tasks/anti-primes/wolfram.txt b/tasks/anti-primes/wolfram.txt new file mode 100644 index 0000000..41fced4 --- /dev/null +++ b/tasks/anti-primes/wolfram.txt @@ -0,0 +1,13 @@ +Wolfram cross-check of tasks/anti-primes/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 230dc46aeac9165b9b316535e9c91384433aceb1aad903d510f3e469ca15f8a9. +agreement: YES +query: +d = DivisorSigma[0, Range[10000]]; +hc = Flatten[Position[d - Prepend[Most[FoldList[Max, d]], 0], _?Positive]]; +s = "first 20 anti-primes: " <> StringRiffle[ToString /@ Take[hc, 20], " "] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"230dc46aeac9165b9b316535e9c91384433aceb1aad903d510f3e469ca15f8a9", "first 20 anti-primes: 1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560\n"} diff --git a/tasks/array-concatenation/array-concatenation.om b/tasks/array-concatenation/array-concatenation.om new file mode 100644 index 0000000..029ad6b --- /dev/null +++ b/tasks/array-concatenation/array-concatenation.om @@ -0,0 +1,61 @@ +note Rosetta Code task "Array concatenation": https://rosettacode.org/wiki/Array_concatenation +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note cat copies x then y into r and returns the length +fn cat x view ulong * y view ulong * r edit ulong * to ulong +for i ulong len x +put r i get x i +end +for i ulong len y +put r add len x i get y i +end +ret add len x len y +end +fn main to int +a = new ulong 3 0 +b = new ulong 4 0 +c = new ulong 7 0 +for i ulong 3 +put a i add i 1 +end +for i ulong 4 +put b i add i 4 +end +n = cat a b c +for i ulong n +if gt i 0 +emit sp +end +emitu get c i +end +emit nl +ret 0 +end diff --git a/tasks/array-concatenation/expected.txt b/tasks/array-concatenation/expected.txt new file mode 100644 index 0000000..b4457aa --- /dev/null +++ b/tasks/array-concatenation/expected.txt @@ -0,0 +1 @@ +1 2 3 4 5 6 7 diff --git a/tasks/array-concatenation/oracle.py b/tasks/array-concatenation/oracle.py new file mode 100644 index 0000000..f1a7d7f --- /dev/null +++ b/tasks/array-concatenation/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Array concatenation" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [1, 2, 3] +b = [4, 5, 6, 7] +print(" ".join(map(str, a + b))) diff --git a/tasks/attractive-numbers/attractive-numbers.om b/tasks/attractive-numbers/attractive-numbers.om new file mode 100644 index 0000000..1c3e3ac --- /dev/null +++ b/tasks/attractive-numbers/attractive-numbers.om @@ -0,0 +1,85 @@ +note Rosetta Code task "Attractive numbers": https://rosettacode.org/wiki/Attractive_numbers +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text hdr "attractive numbers up to 120 (the count of prime factors, with multiplicity, is prime):\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n in decimal right-aligned in w columns (w at most 20) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt i sub 20 w +i = sub 20 w +end +emit part d i sub 20 i +end +note Omega(n): the number of prime factors of n counted with multiplicity +fn bigo n ulong to ulong +v = n +d = ulong 2 +k = ulong 0 +while le mul d d v bound 64 +if eq rem v d 0 +k = add k 1 +v = div v d +else +d = add d 1 +end +end +if gt v 1 +k = add k 1 +end +ret k +end +note Omega(n) <= 6 for n <= 120, so the primes to test are 2, 3 and 5 +fn main to int +emit hdr +c = ulong 0 +for i ulong 120 +n = add i 1 +k = bigo n +if or or eq k 2 eq k 3 eq k 5 +emitw n 4 +c = add c 1 +if eq rem c 20 0 +emit nl +end +end +end +emit nl +ret 0 +end diff --git a/tasks/attractive-numbers/expected.txt b/tasks/attractive-numbers/expected.txt new file mode 100644 index 0000000..1fa5efd --- /dev/null +++ b/tasks/attractive-numbers/expected.txt @@ -0,0 +1,5 @@ +attractive numbers up to 120 (the count of prime factors, with multiplicity, is prime): + 4 6 8 9 10 12 14 15 18 20 21 22 25 26 27 28 30 32 33 34 + 35 38 39 42 44 45 46 48 49 50 51 52 55 57 58 62 63 65 66 68 + 69 70 72 74 75 76 77 78 80 82 85 86 87 91 92 93 94 95 98 99 + 102 105 106 108 110 111 112 114 115 116 117 118 119 120 diff --git a/tasks/attractive-numbers/oracle.py b/tasks/attractive-numbers/oracle.py new file mode 100644 index 0000000..aa29a40 --- /dev/null +++ b/tasks/attractive-numbers/oracle.py @@ -0,0 +1,14 @@ +# Independent reference for "Attractive numbers" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def omega(n): + k, p = 0, 2 + while n > 1: + while n % p == 0: + n //= p + k += 1 + p += 1 + return k +att = [n for n in range(1, 121) if omega(n) in (2, 3, 5, 7)] +print("attractive numbers up to 120 (the count of prime factors, with multiplicity, is prime):") +for i in range(0, len(att), 20): + print("".join(f"{n:4d}" for n in att[i:i + 20])) diff --git a/tasks/attractive-numbers/wolfram.txt b/tasks/attractive-numbers/wolfram.txt new file mode 100644 index 0000000..68b5726 --- /dev/null +++ b/tasks/attractive-numbers/wolfram.txt @@ -0,0 +1,13 @@ +Wolfram cross-check of tasks/attractive-numbers/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 04bb0d4156a542b468bed492db2afecbbd0dea6cbf79348d7d495e45aff9c5f9. +agreement: YES +query: +att = Select[Range[120], PrimeQ[PrimeOmega[#]] &]; +s = "attractive numbers up to 120 (the count of prime factors, with multiplicity, is prime):\n" <> + StringJoin[(StringJoin[StringPadLeft[ToString[#], 4] & /@ #] <> "\n") & /@ Partition[att, UpTo[20]]]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"04bb0d4156a542b468bed492db2afecbbd0dea6cbf79348d7d495e45aff9c5f9", "attractive numbers up to 120 (the count of prime factors, with multiplicity, is prime):\n 4 6 8 9 10 12 14 15 18 20 21 22 25 26 27 28 30 32 33 34\n 35 38 39 42 44 45 46 48 49 50 51 52 55 57 58 62 63 65 66 68\n 69 70 72 74 75 76 77 78 80 82 85 86 87 91 92 93 94 95 98 99\n 102 105 106 108 110 111 112 114 115 116 117 118 119 120\n"} diff --git a/tasks/bacon-cipher/bacon-cipher.om b/tasks/bacon-cipher/bacon-cipher.om new file mode 100644 index 0000000..beeca0f --- /dev/null +++ b/tasks/bacon-cipher/bacon-cipher.om @@ -0,0 +1,85 @@ +note Rosetta Code task "Bacon cipher": https://rosettacode.org/wiki/Bacon_cipher +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text secret "rosetta code" +text cover "bacon's cipher is a method of steganography devised by francis bacon. a message is concealed in the presentation of text rather than its content." +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note 26-letter variant: letter n (a = 0) is 5 bits, most significant first; a space is 26 (11010). +note The bits ride on the cover text's letters: 0 lower case, 1 upper case; other bytes are copied, +note letters after the last bit stay lower case. Decoding reads the case of the letters 5 at a time. +fn code c uchar to ulong +if eq c 32 +ret 26 +end +ret ulong sub c 97 +end +fn isl c uchar to bool +ret and ge c 97 le c 122 +end +fn enc o edit uchar * to void +nb = mul len secret 5 +b = ulong 0 +for i ulong len cover +c = get cover i +if and isl c lt b nb +v = code get secret div b 5 +if eq and shr v sub 4 rem b 5 1 1 +c = sub c 32 +end +b = add b 1 +end +put o i c +end +end +fn dec e view uchar * n ulong to void +v = ulong 0 +k = ulong 0 +m = ulong 0 +for i ulong len e +c = get e i +if and lt m n or isl c and ge c 65 le c 90 +v = shl v 1 +if le c 90 +v = or v 1 +end +k = add k 1 +if eq k 5 +if eq v 26 +emitc 32 +else +emitc add uchar v 97 +end +v = 0 +k = 0 +m = add m 1 +end +end +end +emit nl +end +fn main to int +o = new uchar 256 0 +n = len cover +enc part o 0 n +emit part o 0 n +emit nl +dec part o 0 n len secret +ret 0 +end diff --git a/tasks/bacon-cipher/expected.txt b/tasks/bacon-cipher/expected.txt new file mode 100644 index 0000000..c34e923 --- /dev/null +++ b/tasks/bacon-cipher/expected.txt @@ -0,0 +1,2 @@ +BacoN's CIPhEr iS a meThoD of STEgaNOgraphY DeVised By fRANcis bACon. A message is concealed in the presentation of text rather than its content. +rosetta code diff --git a/tasks/bacon-cipher/oracle.py b/tasks/bacon-cipher/oracle.py new file mode 100644 index 0000000..d638ed6 --- /dev/null +++ b/tasks/bacon-cipher/oracle.py @@ -0,0 +1,16 @@ +# Independent reference for "Bacon cipher" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +secret = "rosetta code" +cover = "bacon's cipher is a method of steganography devised by francis bacon. a message is concealed in the presentation of text rather than its content." +bits = "".join(format(26 if c == " " else ord(c) - 97, "05b") for c in secret) +out, b = [], 0 +for c in cover: + if "a" <= c <= "z" and b < len(bits): + if bits[b] == "1": + c = c.upper() + b += 1 + out.append(c) +enc = "".join(out) +print(enc) +got = "".join("1" if c.isupper() else "0" for c in enc if c.isalpha())[:len(bits)] +print("".join(" " if int(got[i:i + 5], 2) == 26 else chr(97 + int(got[i:i + 5], 2)) for i in range(0, len(got), 5))) diff --git a/tasks/base64-encode-data/base64-encode-data.om b/tasks/base64-encode-data/base64-encode-data.om new file mode 100644 index 0000000..06ebe5e --- /dev/null +++ b/tasks/base64-encode-data/base64-encode-data.om @@ -0,0 +1,60 @@ +note Rosetta Code task "Base64 encode data": https://rosettacode.org/wiki/Base64_encode_data +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text abc "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" +text m0 "M" +text m1 "Ma" +text m2 "Man" +text m3 "Hello, World!" +text m4 "To err is human, but to really foul things up you need a computer.\n -- Paul R. Ehrlich" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note b64 takes the input 3 bytes (24 bits) at a time and writes 4 alphabet characters, '=' padding +note a short last group; output up to 512 bytes (input up to 384, longer traps 3) +fn b64 a view uchar * to void +o = new uchar 512 0 +p = ulong 0 +i = ulong 0 +while lt i len a bound 200 +r = sub len a i +v = shl ulong get a i 16 +if gt r 1 +v = or v shl ulong get a add i 1 8 +end +if gt r 2 +v = or v ulong get a add i 2 +end +put o p get abc shr v 18 +put o add p 1 get abc and shr v 12 63 +put o add p 2 61 +put o add p 3 61 +if gt r 1 +put o add p 2 get abc and shr v 6 63 +end +if gt r 2 +put o add p 3 get abc and v 63 +end +p = add p 4 +i = add i 3 +end +emit part o 0 p +emit nl +end +fn main to int +b64 m0 +b64 m1 +b64 m2 +b64 m3 +b64 m4 +ret 0 +end diff --git a/tasks/base64-encode-data/expected.txt b/tasks/base64-encode-data/expected.txt new file mode 100644 index 0000000..46ee52d --- /dev/null +++ b/tasks/base64-encode-data/expected.txt @@ -0,0 +1,5 @@ +TQ== +TWE= +TWFu +SGVsbG8sIFdvcmxkIQ== +VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g= diff --git a/tasks/base64-encode-data/oracle.py b/tasks/base64-encode-data/oracle.py new file mode 100644 index 0000000..419a8f7 --- /dev/null +++ b/tasks/base64-encode-data/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Base64 encode data" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import base64 +for s in ['M', 'Ma', 'Man', 'Hello, World!', 'To err is human, but to really foul things up you need a computer.\n -- Paul R. Ehrlich']: + print(base64.b64encode(s.encode()).decode()) diff --git a/tasks/bead-sort/bead-sort.om b/tasks/bead-sort/bead-sort.om new file mode 100644 index 0000000..f0c6a35 --- /dev/null +++ b/tasks/bead-sort/bead-sort.om @@ -0,0 +1,121 @@ +note Rosetta Code task "Sorting algorithms/Bead sort": https://rosettacode.org/wiki/Sorting_algorithms/Bead_sort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "5 3 1 7 4 1 1 20 0 9" +global vals long 16 +global grid uchar 512 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note the abacus has one row per value (at most 16) and one column per unit (at most 32): grid[r*32+c] +note holds a bead when c < vals[r]; the values here are 0 .. 20 +fn bdsort n ulong to void +for r ulong n +for c ulong ulong get vals r +put grid add mul r 32 c 1 +end +end +note gravity: each column's beads fall to the bottom rows +for c ulong 32 +h = ulong 0 +for r ulong n +h = add h ulong get grid add mul r 32 c +put grid add mul r 32 c 0 +end +for x ulong h +put grid add mul sub sub n x 1 32 c 1 +end +end +note row r now holds the r-th smallest value as its bead count, read top to bottom +for r ulong n +v = long 0 +for c ulong 32 +v = add v long get grid add mul r 32 c +end +put vals r v +end +end +fn main to int +n = parse data +show n +bdsort n +show n +ret 0 +end diff --git a/tasks/bead-sort/expected.txt b/tasks/bead-sort/expected.txt new file mode 100644 index 0000000..2951be5 --- /dev/null +++ b/tasks/bead-sort/expected.txt @@ -0,0 +1,2 @@ +5 3 1 7 4 1 1 20 0 9 +0 1 1 1 3 4 5 7 9 20 diff --git a/tasks/bead-sort/oracle.py b/tasks/bead-sort/oracle.py new file mode 100644 index 0000000..3bb02aa --- /dev/null +++ b/tasks/bead-sort/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Sorting algorithms/Bead sort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [5, 3, 1, 7, 4, 1, 1, 20, 0, 9] +print(*a) +print(*sorted(a)) diff --git a/tasks/bell-numbers/bell-numbers.om b/tasks/bell-numbers/bell-numbers.om new file mode 100644 index 0000000..98f5e5b --- /dev/null +++ b/tasks/bell-numbers/bell-numbers.om @@ -0,0 +1,113 @@ +note Rosetta Code task "Bell numbers": https://rosettacode.org/wiki/Bell_numbers +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text tri "first 10 rows of the Bell triangle:\n" +text first "first 15 Bell numbers:" +text b49 "B(49), the 50th Bell number: " +note a triangle row of up to 50 entries; entry e is 6 limbs base 10^9 at 6e..6e+5, least significant first +note 6 limbs hold below 10^54; the largest entry needed, in row 49, is below B(50) < 2 * 10^47 +global pv ulong 300 +global cu ulong 300 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note row i of the Bell triangle starts with the last entry of row i - 1; each next entry adds the entry +note to its left and the entry above that one. Row i - 1 sits in pv and row i is built in cu, then copied +fn bellrow i ulong to void +for l ulong 6 +put cu l get pv add mul sub i 1 6 l +end +for j ulong i +c = ulong 0 +for l ulong 6 +o = add mul j 6 l +s = add get cu o c +s = add s get pv o +put cu add o 6 rem s 1000000000 +c = div s 1000000000 +end +end +for t ulong mul add i 1 6 +put pv t get cu t +end +end +note pv entry 0 in decimal: the top non-zero limb, then each lower limb padded to 9 digits +fn emitb to void +k = ulong 5 +while and gt k 0 eq get pv k 0 bound 6 +k = sub k 1 +end +emitu get pv k +for t ulong k +v = get pv sub sub k 1 t +d = ulong 100000000 +while gt d 0 bound 9 +emitu rem div v d 10 +d = div d 10 +end +end +end +fn main to int +bs = new ulong 15 0 +put pv 0 1 +put bs 0 1 +emit tri +emitu 1 +emit nl +i = ulong 1 +while le i 49 bound 49 +bellrow i +if lt i 10 +for j ulong add i 1 +if gt j 0 +emit sp +end +emitu get pv mul j 6 +end +emit nl +end +if lt i 15 +put bs i get pv 0 +end +if lt i 49 +i = add i 1 +else +break +end +end +emit first +for j ulong 15 +emit sp +emitu get bs j +end +emit nl +emit b49 +emitb +emit nl +ret 0 +end diff --git a/tasks/bell-numbers/expected.txt b/tasks/bell-numbers/expected.txt new file mode 100644 index 0000000..9391c7f --- /dev/null +++ b/tasks/bell-numbers/expected.txt @@ -0,0 +1,13 @@ +first 10 rows of the Bell triangle: +1 +1 2 +2 3 5 +5 7 10 15 +15 20 27 37 52 +52 67 87 114 151 203 +203 255 322 409 523 674 877 +877 1080 1335 1657 2066 2589 3263 4140 +4140 5017 6097 7432 9089 11155 13744 17007 21147 +21147 25287 30304 36401 43833 52922 64077 77821 94828 115975 +first 15 Bell numbers: 1 1 2 5 15 52 203 877 4140 21147 115975 678570 4213597 27644437 190899322 +B(49), the 50th Bell number: 10726137154573358400342215518590002633917247281 diff --git a/tasks/bell-numbers/oracle.py b/tasks/bell-numbers/oracle.py new file mode 100644 index 0000000..f835558 --- /dev/null +++ b/tasks/bell-numbers/oracle.py @@ -0,0 +1,17 @@ +# Independent reference for "Bell numbers" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import math +# Bell numbers by B(n+1) = sum C(n, k) B(k); the triangle rows by the Aitken array rule +B = [1] +for n in range(60): + B.append(sum(math.comb(n, k) * B[k] for k in range(n + 1))) +print("first 10 rows of the Bell triangle:") +r = [1] +for i in range(10): + print(" ".join(map(str, r))) + nr = [r[-1]] + for v in r: + nr.append(nr[-1] + v) + r = nr +print("first 15 Bell numbers: " + " ".join(map(str, B[:15]))) +print(f"B(49), the 50th Bell number: {B[49]}") diff --git a/tasks/bell-numbers/wolfram.txt b/tasks/bell-numbers/wolfram.txt new file mode 100644 index 0000000..f3b1ed4 --- /dev/null +++ b/tasks/bell-numbers/wolfram.txt @@ -0,0 +1,14 @@ +Wolfram cross-check of tasks/bell-numbers/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 473dab5cfebadf6be0108ab423ca0bb4521e2240afff3c6c5b3c6e2049b83382. +agreement: YES +query: +rows = NestList[FoldList[Plus, Last[#], #] &, {1}, 9]; +s = "first 10 rows of the Bell triangle:\n" <> StringJoin[(StringRiffle[ToString /@ #, " "] <> "\n") & /@ rows] <> + "first 15 Bell numbers: " <> StringRiffle[ToString /@ BellB[Range[0, 14]], " "] <> "\n" <> + "B(49), the 50th Bell number: " <> ToString[BellB[49]] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"473dab5cfebadf6be0108ab423ca0bb4521e2240afff3c6c5b3c6e2049b83382", "first 10 rows of the Bell triangle:\n1\n1 2\n2 3 5\n5 7 10 15\n15 20 27 37 52\n52 67 87 114 151 203\n203 255 322 409 523 674 877\n877 1080 1335 1657 2066 2589 3263 4140\n4140 5017 6097 7432 9089 11155 13744 17007 21147\n21147 25287 30304 36401 43833 52922 64077 77821 94828 115975\nfirst 15 Bell numbers: 1 1 2 5 15 52 203 877 4140 21147 115975 678570 4213597 27644437 190899322\nB(49), the 50th Bell number: 10726137154573358400342215518590002633917247281\n"} diff --git a/tasks/bogosort/bogosort.om b/tasks/bogosort/bogosort.om new file mode 100644 index 0000000..ba7bb02 --- /dev/null +++ b/tasks/bogosort/bogosort.om @@ -0,0 +1,132 @@ +note Rosetta Code task "Sorting algorithms/Bogosort": https://rosettacode.org/wiki/Sorting_algorithms/Bogosort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "3 -1 4 1 5 9 2" +text tshuf "shuffles: " +global vals long 8 +global seed ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note rnd is Knuth's MMIX linear congruential generator, x = x*6364136223846793005 + 1442695040888963407 +note mod 2^64 (ulong arithmetic wraps), starting from seed 0; it returns the top 31 bits, x >> 33 +fn rnd to ulong +x = add mul get seed 0 6364136223846793005 1442695040888963407 +put seed 0 x +ret shr x 33 +end +note shuffle is Fisher-Yates: for i = n-1 .. 1 swap vals[i] with vals[rnd mod (i+1)] +fn shuffle n ulong to void +for x ulong sub n 1 +i = sub sub n x 1 +j = rem rnd add i 1 +t = get vals i +put vals i get vals j +put vals j t +end +end +fn inorder n ulong to bool +for i ulong sub n 1 +if gt get vals i get vals add i 1 +ret false +end +end +ret true +end +note bgsort shuffles until sorted; with this generator and input it takes the count printed, well under +note the bound of 200000 (7! = 5040 orders, so a working generator lands far inside it) +fn bgsort n ulong to ulong +c = ulong 0 +while not inorder n bound 200000 +shuffle n +c = add c 1 +end +ret c +end +fn main to int +n = parse data +show n +c = bgsort n +show n +emit tshuf +emitu c +emit nl +ret 0 +end diff --git a/tasks/bogosort/expected.txt b/tasks/bogosort/expected.txt new file mode 100644 index 0000000..51d4291 --- /dev/null +++ b/tasks/bogosort/expected.txt @@ -0,0 +1,3 @@ +3 -1 4 1 5 9 2 +-1 1 2 3 4 5 9 +shuffles: 2314 diff --git a/tasks/bogosort/oracle.py b/tasks/bogosort/oracle.py new file mode 100644 index 0000000..4244ebb --- /dev/null +++ b/tasks/bogosort/oracle.py @@ -0,0 +1,18 @@ +# Independent reference for "Sorting algorithms/Bogosort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +M = 2 ** 64 +x = 0 +def rnd(): + global x + x = (x * 6364136223846793005 + 1442695040888963407) % M + return x >> 33 +a = [3, -1, 4, 1, 5, 9, 2] +print(*a) +c = 0 +while a != sorted(a): + for i in range(len(a) - 1, 0, -1): + j = rnd() % (i + 1) + a[i], a[j] = a[j], a[i] + c += 1 +print(*a) +print("shuffles:", c) diff --git a/tasks/caesar-cipher/caesar-cipher.om b/tasks/caesar-cipher/caesar-cipher.om new file mode 100644 index 0000000..d4f0722 --- /dev/null +++ b/tasks/caesar-cipher/caesar-cipher.om @@ -0,0 +1,43 @@ +note Rosetta Code task "Caesar cipher": https://rosettacode.org/wiki/Caesar_cipher +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text msg "The quick brown fox Jumped over the lazy Dog" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note shift rotates ASCII letters by k places (0..25) into r; other bytes are copied +fn shift a view uchar * r edit uchar * k uchar to void +for i ulong len a +c = get a i +if and ge c 65 le c 90 +c = add rem add sub c 65 k 26 65 +else +if and ge c 97 le c 122 +c = add rem add sub c 97 k 26 97 +end +end +put r i c +end +end +fn main to int +n = len msg +e = new uchar 256 0 +d = new uchar 256 0 +shift msg part e 0 n 11 +emit part e 0 n +emit nl +note decoding is a shift by 26 - 11 +shift part e 0 n part d 0 n 15 +emit part d 0 n +emit nl +ret 0 +end diff --git a/tasks/caesar-cipher/expected.txt b/tasks/caesar-cipher/expected.txt new file mode 100644 index 0000000..8338dc5 --- /dev/null +++ b/tasks/caesar-cipher/expected.txt @@ -0,0 +1,2 @@ +Esp bftnv mczhy qzi Ufxapo zgpc esp wlkj Ozr +The quick brown fox Jumped over the lazy Dog diff --git a/tasks/caesar-cipher/oracle.py b/tasks/caesar-cipher/oracle.py new file mode 100644 index 0000000..e5892bc --- /dev/null +++ b/tasks/caesar-cipher/oracle.py @@ -0,0 +1,15 @@ +# Independent reference for "Caesar cipher" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def caesar(s, k): + out = [] + for c in s: + if "A" <= c <= "Z": + out.append(chr((ord(c) - 65 + k) % 26 + 65)) + elif "a" <= c <= "z": + out.append(chr((ord(c) - 97 + k) % 26 + 97)) + else: + out.append(c) + return "".join(out) +e = caesar("The quick brown fox Jumped over the lazy Dog", 11) +print(e) +print(caesar(e, -11)) diff --git a/tasks/camel-case-and-snake-case/camel-case-and-snake-case.om b/tasks/camel-case-and-snake-case/camel-case-and-snake-case.om new file mode 100644 index 0000000..5118003 --- /dev/null +++ b/tasks/camel-case-and-snake-case/camel-case-and-snake-case.om @@ -0,0 +1,115 @@ +note Rosetta Code task "Camel case and snake case": https://rosettacode.org/wiki/Camel_case_and_snake_case +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text tsn " -> snake: " +text tcm " -> camel: " +text c0 "snakeCase" +text c1 "snake_case" +text c2 "variable_10_case" +text c3 "variable10Case" +text c4 "hurry-up-joe!" +text c5 "c://my-docs/happy_Flag-Day/12.doc" +text c6 " spaces " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note leading and trailing spaces are kept as they are; inside the text, space and '-' count as +note separators like '_' +fn issep c uchar to bool +ret or or eq c 95 eq c 45 eq c 32 +end +fn lead a view uchar * to ulong +for i ulong len a +if ne get a i 32 +ret i +end +end +ret len a +end +fn trail a view uchar * to ulong +for i ulong len a +if ne get a sub sub len a 1 i 32 +ret sub len a i +end +end +ret 0 +end +note snake: a separator becomes '_', an upper-case letter becomes '_' and its lower case (no '_' first +note when it is at the start or follows a separator) +fn snake a view uchar * to void +l = lead a +t = trail a +emit part a 0 l +for i ulong sub t l +k = add l i +c = get a k +if issep c +emitc 95 +else +if and ge c 65 le c 90 +if gt i 0 +if not issep get a sub k 1 +emitc 95 +end +end +emitc add c 32 +else +emitc c +end +end +end +emit part a t sub len a t +end +note camel: separators are dropped and the letter after one is upper-cased +fn camel a view uchar * to void +l = lead a +t = trail a +emit part a 0 l +up = false +for i ulong sub t l +c = get a add l i +if issep c +up = true +else +if and up and ge c 97 le c 122 +c = sub c 32 +end +emitc c +up = false +end +end +emit part a t sub len a t +end +fn show a view uchar * to void +emit a +emit tsn +snake a +emit tcm +camel a +emit nl +end +fn main to int +show c0 +show c1 +show c2 +show c3 +show c4 +show c5 +show c6 +ret 0 +end diff --git a/tasks/camel-case-and-snake-case/expected.txt b/tasks/camel-case-and-snake-case/expected.txt new file mode 100644 index 0000000..7d6d045 --- /dev/null +++ b/tasks/camel-case-and-snake-case/expected.txt @@ -0,0 +1,7 @@ +snakeCase -> snake: snake_case -> camel: snakeCase +snake_case -> snake: snake_case -> camel: snakeCase +variable_10_case -> snake: variable_10_case -> camel: variable10Case +variable10Case -> snake: variable10_case -> camel: variable10Case +hurry-up-joe! -> snake: hurry_up_joe! -> camel: hurryUpJoe! +c://my-docs/happy_Flag-Day/12.doc -> snake: c://my_docs/happy_flag_day/12.doc -> camel: c://myDocs/happyFlagDay/12.doc + spaces -> snake: spaces -> camel: spaces diff --git a/tasks/camel-case-and-snake-case/oracle.py b/tasks/camel-case-and-snake-case/oracle.py new file mode 100644 index 0000000..43e3021 --- /dev/null +++ b/tasks/camel-case-and-snake-case/oracle.py @@ -0,0 +1,35 @@ +# Independent reference for "Camel case and snake case" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +SEP = "_- " +def split_edges(s): + core = s.strip(" ") + if not core: + return s, "", "" + l = len(s) - len(s.lstrip(" ")) + t = len(s.rstrip(" ")) + return s[:l], s[l:t], s[t:] +def snake(s): + a, core, b = split_edges(s) + out = [] + for i, c in enumerate(core): + if c in SEP: + out.append("_") + elif "A" <= c <= "Z": + if i > 0 and core[i - 1] not in SEP: + out.append("_") + out.append(c.lower()) + else: + out.append(c) + return a + "".join(out) + b +def camel(s): + a, core, b = split_edges(s) + out, up = [], False + for c in core: + if c in SEP: + up = True + else: + out.append(c.upper() if up and "a" <= c <= "z" else c) + up = False + return a + "".join(out) + b +for s in ['snakeCase', 'snake_case', 'variable_10_case', 'variable10Case', 'hurry-up-joe!', 'c://my-docs/happy_Flag-Day/12.doc', ' spaces ']: + print(f"{s} -> snake: {snake(s)} -> camel: {camel(s)}") diff --git a/tasks/catalan-numbers/catalan-numbers.om b/tasks/catalan-numbers/catalan-numbers.om new file mode 100644 index 0000000..a62f2ba --- /dev/null +++ b/tasks/catalan-numbers/catalan-numbers.om @@ -0,0 +1,60 @@ +note Rosetta Code task "Catalan numbers": https://rosettacode.org/wiki/Catalan_numbers +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text cc "C(" +text eqs ") = " +text by " by the recurrence, " +text sum " by the convolution sum\n" +text hdr "n, C(n) by C(n+1) = 2(2n+1)/(n+2) C(n), and again by C(n+1) = sum C(i) C(n-i):\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note cs holds C(0)..C(14) from the convolution; all stay below 2^32, so every product fits +fn main to int +cs = new ulong 15 0 +put cs 0 1 +for n ulong 14 +s = ulong 0 +for i ulong add n 1 +s = add s mul get cs i get cs sub n i +end +put cs add n 1 s +end +emit hdr +c = ulong 1 +for n ulong 15 +emit cc +emitu n +emit eqs +emitu c +emit by +emitu get cs n +emit sum +c = div mul c mul 2 add mul 2 n 1 add n 2 +end +ret 0 +end diff --git a/tasks/catalan-numbers/expected.txt b/tasks/catalan-numbers/expected.txt new file mode 100644 index 0000000..340f497 --- /dev/null +++ b/tasks/catalan-numbers/expected.txt @@ -0,0 +1,16 @@ +n, C(n) by C(n+1) = 2(2n+1)/(n+2) C(n), and again by C(n+1) = sum C(i) C(n-i): +C(0) = 1 by the recurrence, 1 by the convolution sum +C(1) = 1 by the recurrence, 1 by the convolution sum +C(2) = 2 by the recurrence, 2 by the convolution sum +C(3) = 5 by the recurrence, 5 by the convolution sum +C(4) = 14 by the recurrence, 14 by the convolution sum +C(5) = 42 by the recurrence, 42 by the convolution sum +C(6) = 132 by the recurrence, 132 by the convolution sum +C(7) = 429 by the recurrence, 429 by the convolution sum +C(8) = 1430 by the recurrence, 1430 by the convolution sum +C(9) = 4862 by the recurrence, 4862 by the convolution sum +C(10) = 16796 by the recurrence, 16796 by the convolution sum +C(11) = 58786 by the recurrence, 58786 by the convolution sum +C(12) = 208012 by the recurrence, 208012 by the convolution sum +C(13) = 742900 by the recurrence, 742900 by the convolution sum +C(14) = 2674440 by the recurrence, 2674440 by the convolution sum diff --git a/tasks/catalan-numbers/oracle.py b/tasks/catalan-numbers/oracle.py new file mode 100644 index 0000000..5b9739a --- /dev/null +++ b/tasks/catalan-numbers/oracle.py @@ -0,0 +1,7 @@ +# Independent reference for "Catalan numbers" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import math +print("n, C(n) by C(n+1) = 2(2n+1)/(n+2) C(n), and again by C(n+1) = sum C(i) C(n-i):") +for n in range(15): + c = math.comb(2 * n, n) // (n + 1) + print(f"C({n}) = {c} by the recurrence, {c} by the convolution sum") diff --git a/tasks/catalan-numbers/wolfram.txt b/tasks/catalan-numbers/wolfram.txt new file mode 100644 index 0000000..99bfa8d --- /dev/null +++ b/tasks/catalan-numbers/wolfram.txt @@ -0,0 +1,12 @@ +Wolfram cross-check of tasks/catalan-numbers/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 235bbe8b8e270c4390754d3169defd3c1b377a79cdbafb21114c036bd361dfee. +agreement: YES +query: +s = "n, C(n) by C(n+1) = 2(2n+1)/(n+2) C(n), and again by C(n+1) = sum C(i) C(n-i):\n" <> + StringJoin[Table["C(" <> ToString[n] <> ") = " <> ToString[CatalanNumber[n]] <> " by the recurrence, " <> ToString[CatalanNumber[n]] <> " by the convolution sum\n", {n, 0, 14}]]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"235bbe8b8e270c4390754d3169defd3c1b377a79cdbafb21114c036bd361dfee", "n, C(n) by C(n+1) = 2(2n+1)/(n+2) C(n), and again by C(n+1) = sum C(i) C(n-i):\nC(0) = 1 by the recurrence, 1 by the convolution sum\nC(1) = 1 by the recurrence, 1 by the convolution sum\nC(2) = 2 by the recurrence, 2 by the convolution sum\nC(3) = 5 by the recurrence, 5 by the convolution sum\nC(4) = 14 by the recurrence, 14 by the convolution sum\nC(5) = 42 by the recurrence, 42 by the convolution sum\nC(6) = 132 by the recurrence, 132 by the convolution sum\nC(7) = 429 by the recurrence, 429 by the convolution sum\nC(8) = 1430 by the recurrence, 1430 by the convolution sum\nC(9) = 4862 by the recurrence, 4862 by the convolution sum\nC(10) = 16796 by the recurrence, 16796 by the convolution sum\nC(11) = 58786 by the recurrence, 58786 by the convolution sum\nC(12) = 208012 by the recurrence, 208012 by the convolution sum\nC(13) = 742900 by the recurrence, 742900 by the convolution sum\nC(14) = 2674440 by the recurrence, 2674440 by the convolution sum\n"} diff --git a/tasks/character-codes/character-codes.om b/tasks/character-codes/character-codes.om new file mode 100644 index 0000000..0e5b7ab --- /dev/null +++ b/tasks/character-codes/character-codes.om @@ -0,0 +1,50 @@ +note Rosetta Code task "Character codes": https://rosettacode.org/wiki/Character_codes +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text ch "a" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +fn main to int +note a byte is a number: the code of a is get ch 0, and the number 97 printed as a byte is a +emit ch +emit sp +emitu ulong get ch 0 +emit nl +emitu 97 +emit sp +emitc 97 +emit nl +ret 0 +end diff --git a/tasks/character-codes/expected.txt b/tasks/character-codes/expected.txt new file mode 100644 index 0000000..cbfdb9b --- /dev/null +++ b/tasks/character-codes/expected.txt @@ -0,0 +1,2 @@ +a 97 +97 a diff --git a/tasks/character-codes/oracle.py b/tasks/character-codes/oracle.py new file mode 100644 index 0000000..a33c60c --- /dev/null +++ b/tasks/character-codes/oracle.py @@ -0,0 +1,4 @@ +# Independent reference for "Character codes" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +print("a", ord("a")) +print(97, chr(97)) diff --git a/tasks/cocktail-sort-with-shifting-bounds/cocktail-sort-with-shifting-bounds.om b/tasks/cocktail-sort-with-shifting-bounds/cocktail-sort-with-shifting-bounds.om new file mode 100644 index 0000000..827aecf --- /dev/null +++ b/tasks/cocktail-sort-with-shifting-bounds/cocktail-sort-with-shifting-bounds.om @@ -0,0 +1,125 @@ +note Rosetta Code task "Sorting algorithms/Cocktail sort with shifting bounds": https://rosettacode.org/wiki/Sorting_algorithms/Cocktail_sort_with_shifting_bounds +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "21 -4 7 7 0 88 -15 3 42 9 1 -4 60" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note cssort runs forward and backward passes over vals[b..e]; after a forward pass everything past the +note last swap is in place, so e moves to it, and b likewise after a backward pass; each round moves +note e down or b up by at least one, so n rounds (at most 16) suffice +fn cssort n ulong to void +b = ulong 0 +e = sub n 1 +while lt b e bound 16 +ls = b +i = b +while lt i e bound 16 +if gt get vals i get vals add i 1 +t = get vals i +put vals i get vals add i 1 +put vals add i 1 t +ls = i +end +i = add i 1 +end +e = ls +fs = e +i = e +while gt i b bound 16 +if gt get vals sub i 1 get vals i +t = get vals i +put vals i get vals sub i 1 +put vals sub i 1 t +fs = i +end +i = sub i 1 +end +b = fs +end +end +fn main to int +n = parse data +show n +cssort n +show n +ret 0 +end diff --git a/tasks/cocktail-sort-with-shifting-bounds/expected.txt b/tasks/cocktail-sort-with-shifting-bounds/expected.txt new file mode 100644 index 0000000..9308cf0 --- /dev/null +++ b/tasks/cocktail-sort-with-shifting-bounds/expected.txt @@ -0,0 +1,2 @@ +21 -4 7 7 0 88 -15 3 42 9 1 -4 60 +-15 -4 -4 0 1 3 7 7 9 21 42 60 88 diff --git a/tasks/cocktail-sort-with-shifting-bounds/oracle.py b/tasks/cocktail-sort-with-shifting-bounds/oracle.py new file mode 100644 index 0000000..224db94 --- /dev/null +++ b/tasks/cocktail-sort-with-shifting-bounds/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Sorting algorithms/Cocktail sort with shifting bounds" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [21, -4, 7, 7, 0, 88, -15, 3, 42, 9, 1, -4, 60] +print(*a) +print(*sorted(a)) diff --git a/tasks/cocktail-sort/cocktail-sort.om b/tasks/cocktail-sort/cocktail-sort.om new file mode 100644 index 0000000..3316b25 --- /dev/null +++ b/tasks/cocktail-sort/cocktail-sort.om @@ -0,0 +1,125 @@ +note Rosetta Code task "Sorting algorithms/Cocktail sort": https://rosettacode.org/wiki/Sorting_algorithms/Cocktail_sort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "5 1 -4 2 8 0 2 -9 13 7 3" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note cswap swaps vals[i] and vals[i+1] when they are out of order and says whether it did +fn cswap i ulong to bool +if gt get vals i get vals add i 1 +t = get vals i +put vals i get vals add i 1 +put vals add i 1 t +ret true +end +ret false +end +note csort passes forward then backward until a pass swaps nothing; each round puts at least the +note largest remaining element in place, so n rounds (at most 16) suffice +fn csort n ulong to void +while true bound 16 +done = true +for i ulong sub n 1 +if cswap i +done = false +end +end +if done +break +end +for x ulong sub n 1 +if cswap sub sub n 2 x +done = false +end +end +if done +break +end +end +end +fn main to int +n = parse data +show n +csort n +show n +ret 0 +end diff --git a/tasks/cocktail-sort/expected.txt b/tasks/cocktail-sort/expected.txt new file mode 100644 index 0000000..df97d77 --- /dev/null +++ b/tasks/cocktail-sort/expected.txt @@ -0,0 +1,2 @@ +5 1 -4 2 8 0 2 -9 13 7 3 +-9 -4 0 1 2 2 3 5 7 8 13 diff --git a/tasks/cocktail-sort/oracle.py b/tasks/cocktail-sort/oracle.py new file mode 100644 index 0000000..e9d73fd --- /dev/null +++ b/tasks/cocktail-sort/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Sorting algorithms/Cocktail sort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [5, 1, -4, 2, 8, 0, 2, -9, 13, 7, 3] +print(*a) +print(*sorted(a)) diff --git a/tasks/comb-sort/comb-sort.om b/tasks/comb-sort/comb-sort.om new file mode 100644 index 0000000..2d0b73a --- /dev/null +++ b/tasks/comb-sort/comb-sort.om @@ -0,0 +1,113 @@ +note Rosetta Code task "Sorting algorithms/Comb sort": https://rosettacode.org/wiki/Sorting_algorithms/Comb_sort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "63 -2 18 7 0 99 41 -30 5 5 72 11 -8 26" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note cbsort shrinks the gap by 10/13 each pass and finishes with gap-1 passes until nothing swaps; +note gap 1 then behaves as bubble sort, which needs at most n (16) passes, plus at most 10 shrink passes +fn cbsort n ulong to void +g = n +done = false +while or gt g 1 not done bound 26 +g = div mul g 10 13 +if eq g 0 +g = 1 +end +done = true +for x ulong sub n g +if gt get vals x get vals add x g +t = get vals x +put vals x get vals add x g +put vals add x g t +done = false +end +end +end +end +fn main to int +n = parse data +show n +cbsort n +show n +ret 0 +end diff --git a/tasks/comb-sort/expected.txt b/tasks/comb-sort/expected.txt new file mode 100644 index 0000000..f62a4ee --- /dev/null +++ b/tasks/comb-sort/expected.txt @@ -0,0 +1,2 @@ +63 -2 18 7 0 99 41 -30 5 5 72 11 -8 26 +-30 -8 -2 0 5 5 7 11 18 26 41 63 72 99 diff --git a/tasks/comb-sort/oracle.py b/tasks/comb-sort/oracle.py new file mode 100644 index 0000000..12f0f46 --- /dev/null +++ b/tasks/comb-sort/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Sorting algorithms/Comb sort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [63, -2, 18, 7, 0, 99, 41, -30, 5, 5, 72, 11, -8, 26] +print(*a) +print(*sorted(a)) diff --git a/tasks/combinations-with-repetitions/combinations-with-repetitions.om b/tasks/combinations-with-repetitions/combinations-with-repetitions.om new file mode 100644 index 0000000..acf72a0 --- /dev/null +++ b/tasks/combinations-with-repetitions/combinations-with-repetitions.om @@ -0,0 +1,90 @@ +note Rosetta Code task "Combinations with repetitions": https://rosettacode.org/wiki/Combinations_with_repetitions +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text t0 "iced" +text t1 "jam" +text t2 "plain" +text tcount "choices of 3 from 10 kinds: " +global pick ulong 4 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note nextm steps pick[0..k-1], a non-decreasing list of kinds 0..n-1, to the next one in lexicographic +note order: raise the last pick below n-1 and set every pick after it to the same kind +fn nextm k ulong n ulong to bool +i = k +while gt i 0 bound 4 +i = sub i 1 +if lt get pick i sub n 1 +v = add get pick i 1 +for x ulong sub k i +put pick add i x v +end +ret true +end +end +ret false +end +fn kind d ulong to void +if eq d 0 +emit t0 +end +if eq d 1 +emit t1 +end +if eq d 2 +emit t2 +end +end +fn main to int +note the two-doughnut choices from three kinds: C(3+2-1, 2) = 6 +put pick 0 0 +put pick 1 0 +while true bound 6 +kind get pick 0 +emit sp +kind get pick 1 +emit nl +if not nextm 2 3 +break +end +end +note counted, not computed: C(10+3-1, 3) = 220 choices bound the loop +put pick 0 0 +put pick 1 0 +put pick 2 0 +c = ulong 1 +while nextm 3 10 bound 220 +c = add c 1 +end +emit tcount +emitu c +emit nl +ret 0 +end diff --git a/tasks/combinations-with-repetitions/expected.txt b/tasks/combinations-with-repetitions/expected.txt new file mode 100644 index 0000000..f9da61b --- /dev/null +++ b/tasks/combinations-with-repetitions/expected.txt @@ -0,0 +1,7 @@ +iced iced +iced jam +iced plain +jam jam +jam plain +plain plain +choices of 3 from 10 kinds: 220 diff --git a/tasks/combinations-with-repetitions/oracle.py b/tasks/combinations-with-repetitions/oracle.py new file mode 100644 index 0000000..e023f89 --- /dev/null +++ b/tasks/combinations-with-repetitions/oracle.py @@ -0,0 +1,13 @@ +# Independent reference for "Combinations with repetitions" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# Cross-check, WolframLanguageEvaluator: {Binomial[3 + 2 - 1, 2], Binomial[10 + 3 - 1, 3]} -> {6, 220} +from itertools import product +from math import comb +kinds = ["iced", "jam", "plain"] +# all ordered pairs, keeping those already in non-decreasing order +for a, b in product(range(3), repeat=2): + if a <= b: + print(kinds[a], kinds[b]) +n = sum(1 for t in product(range(10), repeat=3) if t[0] <= t[1] <= t[2]) +assert n == comb(10 + 3 - 1, 3) +print("choices of 3 from 10 kinds:", n) diff --git a/tasks/combinations/combinations.om b/tasks/combinations/combinations.om new file mode 100644 index 0000000..ee0a78b --- /dev/null +++ b/tasks/combinations/combinations.om @@ -0,0 +1,77 @@ +note Rosetta Code task "Combinations": https://rosettacode.org/wiki/Combinations +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tcount "combinations: " +global comb ulong 8 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note nextc steps comb[0..k-1] to the next k-subset of 0..n-1 in lexicographic order: raise the last +note element that can still rise and reset the ones after it; false after the last subset +fn nextc k ulong n ulong to bool +i = k +while gt i 0 bound 8 +i = sub i 1 +if lt get comb i add sub n k i +put comb i add get comb i 1 +for x ulong sub sub k i 1 +j = add add i x 1 +put comb j add get comb sub j 1 1 +end +ret true +end +end +ret false +end +fn show k ulong to void +for i ulong k +if gt i 0 +emit sp +end +emitu get comb i +end +emit nl +end +note 5 choose 3 is 10, which bounds the loop +fn main to int +k = ulong 3 +for i ulong k +put comb i i +end +c = ulong 1 +show k +while nextc k 5 bound 10 +show k +c = add c 1 +end +emit tcount +emitu c +emit nl +ret 0 +end diff --git a/tasks/combinations/expected.txt b/tasks/combinations/expected.txt new file mode 100644 index 0000000..842b433 --- /dev/null +++ b/tasks/combinations/expected.txt @@ -0,0 +1,11 @@ +0 1 2 +0 1 3 +0 1 4 +0 2 3 +0 2 4 +0 3 4 +1 2 3 +1 2 4 +1 3 4 +2 3 4 +combinations: 10 diff --git a/tasks/combinations/oracle.py b/tasks/combinations/oracle.py new file mode 100644 index 0000000..1a6651c --- /dev/null +++ b/tasks/combinations/oracle.py @@ -0,0 +1,14 @@ +# Independent reference for "Combinations" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# recursive: either take the smallest remaining element or skip it +def combs(start, n, k): + if k == 0: + yield [] + return + for i in range(start, n - k + 1): + for rest in combs(i + 1, n, k - 1): + yield [i] + rest +cs = list(combs(0, 5, 3)) +for c in cs: + print(*c) +print("combinations:", len(cs)) diff --git a/tasks/comma-quibbling/comma-quibbling.om b/tasks/comma-quibbling/comma-quibbling.om new file mode 100644 index 0000000..94b7ef1 --- /dev/null +++ b/tasks/comma-quibbling/comma-quibbling.om @@ -0,0 +1,65 @@ +note Rosetta Code task "Comma quibbling": https://rosettacode.org/wiki/Comma_quibbling +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text lb "{" +text rb "}" +text sep ", " +text tand " and " +text w1 "ABC" +text w2 "ABC DEF" +text w3 "ABC DEF G H" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note quib takes the words of a (single spaces between them): all but the last two are followed by a +note comma, the last two are joined by "and", inside braces +fn quib a view uchar * to void +st = new ulong 16 0 +ln = new ulong 16 0 +k = ulong 0 +s = ulong 0 +for i ulong add len a 1 +e = eq i len a +if not e +e = eq get a i 32 +end +if and e gt i s +put st k s +put ln k sub i s +k = add k 1 +end +if e +s = add i 1 +end +end +emit lb +for w ulong k +if gt w 0 +if eq w sub k 1 +emit tand +else +emit sep +end +end +emit part a get st w get ln w +end +emit rb +emit nl +end +fn main to int +note the empty list, as a slice of length 0 +quib part w1 0 0 +quib w1 +quib w2 +quib w3 +ret 0 +end diff --git a/tasks/comma-quibbling/expected.txt b/tasks/comma-quibbling/expected.txt new file mode 100644 index 0000000..0ac1ba7 --- /dev/null +++ b/tasks/comma-quibbling/expected.txt @@ -0,0 +1,4 @@ +{} +{ABC} +{ABC and DEF} +{ABC, DEF, G and H} diff --git a/tasks/comma-quibbling/oracle.py b/tasks/comma-quibbling/oracle.py new file mode 100644 index 0000000..4d21a17 --- /dev/null +++ b/tasks/comma-quibbling/oracle.py @@ -0,0 +1,8 @@ +# Independent reference for "Comma quibbling" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def quibble(words): + if len(words) <= 1: + return "{" + "".join(words) + "}" + return "{" + ", ".join(words[:-1]) + " and " + words[-1] + "}" +for s in ['', 'ABC', 'ABC DEF', 'ABC DEF G H']: + print(quibble(s.split())) diff --git a/tasks/conways-game-of-life/conways-game-of-life.om b/tasks/conways-game-of-life/conways-game-of-life.om new file mode 100644 index 0000000..b625d77 --- /dev/null +++ b/tasks/conways-game-of-life/conways-game-of-life.om @@ -0,0 +1,126 @@ +note Rosetta Code task "Conway's Game of Life": https://rosettacode.org/wiki/Conway's_Game_of_Life +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tgen "generation " +text tdot "." +text thash "#" +global cur uchar 64 +global nxt uchar 64 +global dim ulong 2 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note the board is dim[0] rows by dim[1] columns, row by row, with dead cells beyond the edges; a live +note cell with 2 or 3 live neighbours lives on, a dead one with exactly 3 comes alive +fn alive r long c long to ulong +if or or lt r 0 ge r long get dim 0 or lt c 0 ge c long get dim 1 +ret 0 +end +ret ulong get cur add mul ulong r get dim 1 ulong c +end +fn around r long c long to ulong +n = ulong 0 +for a ulong 3 +for b ulong 3 +if or ne a 1 ne b 1 +n = add n alive sub add r long a 1 sub add c long b 1 +end +end +end +ret n +end +fn step to void +w = get dim 1 +for r ulong get dim 0 +for c ulong w +n = around long r long c +v = uchar 0 +if eq n 3 +v = 1 +end +if and eq n 2 eq get cur add mul r w c 1 +v = 1 +end +put nxt add mul r w c v +end +end +for i ulong mul get dim 0 w +put cur i get nxt i +end +end +fn show g ulong to void +emit tgen +emitu g +emit nl +w = get dim 1 +for r ulong get dim 0 +for c ulong w +if eq get cur add mul r w c 1 +emit thash +else +emit tdot +end +end +emit nl +end +end +fn run gens ulong to void +for g ulong add gens 1 +show g +if lt g gens +step +end +end +end +fn main to int +note a blinker on a 3 x 3 board for 3 generations +put dim 0 3 +put dim 1 3 +for i ulong 9 +put cur i 0 +end +put cur 3 1 +put cur 4 1 +put cur 5 1 +run 2 +emit nl +note a glider on an 8 x 8 board; after 4 generations it has moved one cell down and right +put dim 0 8 +put dim 1 8 +for i ulong 64 +put cur i 0 +end +put cur 1 1 +put cur 10 1 +put cur 16 1 +put cur 17 1 +put cur 18 1 +run 4 +ret 0 +end diff --git a/tasks/conways-game-of-life/expected.txt b/tasks/conways-game-of-life/expected.txt new file mode 100644 index 0000000..2cf7bda --- /dev/null +++ b/tasks/conways-game-of-life/expected.txt @@ -0,0 +1,58 @@ +generation 0 +... +### +... +generation 1 +.#. +.#. +.#. +generation 2 +... +### +... + +generation 0 +.#...... +..#..... +###..... +........ +........ +........ +........ +........ +generation 1 +........ +#.#..... +.##..... +.#...... +........ +........ +........ +........ +generation 2 +........ +..#..... +#.#..... +.##..... +........ +........ +........ +........ +generation 3 +........ +.#...... +..##.... +.##..... +........ +........ +........ +........ +generation 4 +........ +..#..... +...#.... +.###.... +........ +........ +........ +........ diff --git a/tasks/conways-game-of-life/oracle.py b/tasks/conways-game-of-life/oracle.py new file mode 100644 index 0000000..c4b5af6 --- /dev/null +++ b/tasks/conways-game-of-life/oracle.py @@ -0,0 +1,18 @@ +# Independent reference for "Conway's Game of Life" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# the live cells as a set of (row, col); neighbour counts come from a Counter over all live cells +from collections import Counter +def step(live, rows, cols): + counts = Counter((r + dr, c + dc) for r, c in live + for dr in (-1, 0, 1) for dc in (-1, 0, 1) if (dr, dc) != (0, 0)) + return {p for p, n in counts.items() + if 0 <= p[0] < rows and 0 <= p[1] < cols and (n == 3 or (n == 2 and p in live))} +def run(live, rows, cols, gens): + for g in range(gens + 1): + print("generation", g) + for r in range(rows): + print("".join("#" if (r, c) in live else "." for c in range(cols))) + live = step(live, rows, cols) +run({(1, 0), (1, 1), (1, 2)}, 3, 3, 2) +print() +run({(0, 1), (1, 2), (2, 0), (2, 1), (2, 2)}, 8, 8, 4) diff --git a/tasks/copy-a-string/copy-a-string.om b/tasks/copy-a-string/copy-a-string.om new file mode 100644 index 0000000..14c968a --- /dev/null +++ b/tasks/copy-a-string/copy-a-string.om @@ -0,0 +1,38 @@ +note Rosetta Code task "Copy a string": https://rosettacode.org/wiki/Copy_a_string +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text src "Hello" +text tsrc "source: " +text tcp "copy: " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note copyto copies s into d at offset off and returns the offset after it (past the end traps 3) +fn copyto d edit uchar * off ulong s view uchar * to ulong +for i ulong len s +put d add off i get s i +end +ret add off len s +end +fn main to int +note a text is read-only; copying it into a local array gives an independent, writable string +c = new uchar 16 0 +n = copyto c 0 src +put c 0 74 +emit tsrc +emit src +emit nl +emit tcp +emit part c 0 n +emit nl +ret 0 +end diff --git a/tasks/copy-a-string/expected.txt b/tasks/copy-a-string/expected.txt new file mode 100644 index 0000000..636f47c --- /dev/null +++ b/tasks/copy-a-string/expected.txt @@ -0,0 +1,2 @@ +source: Hello +copy: Jello diff --git a/tasks/copy-a-string/oracle.py b/tasks/copy-a-string/oracle.py new file mode 100644 index 0000000..d8bccfc --- /dev/null +++ b/tasks/copy-a-string/oracle.py @@ -0,0 +1,7 @@ +# Independent reference for "Copy a string" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +src = "Hello" +dst = list(src) # an independent copy +dst[0] = "J" +print("source: " + src) +print("copy: " + "".join(dst)) diff --git a/tasks/count-how-many-vowels-and-consonants-occur-in-a-string/count-how-many-vowels-and-consonants-occur-in-a-string.om b/tasks/count-how-many-vowels-and-consonants-occur-in-a-string/count-how-many-vowels-and-consonants-occur-in-a-string.om new file mode 100644 index 0000000..9b9a8ab --- /dev/null +++ b/tasks/count-how-many-vowels-and-consonants-occur-in-a-string/count-how-many-vowels-and-consonants-occur-in-a-string.om @@ -0,0 +1,82 @@ +note Rosetta Code task "Count how many vowels and consonants occur in a string": https://rosettacode.org/wiki/Count_how_many_vowels_and_consonants_occur_in_a_string +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text tv "vowels " +text tc ", consonants " +text td "distinct vowels " +text msg "Now is the time for all good men to come to the aid of their country." +text vow "aeiou" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note letters are ASCII a..z either case; vowels a e i o u, the other letters are consonants +fn main to int +seen = new uchar 26 0 +v = ulong 0 +c = ulong 0 +for i ulong len msg +x = get msg i +if and ge x 65 le x 90 +x = add x 32 +end +if and ge x 97 le x 122 +put seen sub x 97 1 +isv = false +for j ulong len vow +if eq x get vow j +isv = true +end +end +if isv +v = add v 1 +else +c = add c 1 +end +end +end +emit tv +emitu v +emit tc +emitu c +emit nl +dv = ulong 0 +dc = ulong 0 +for j ulong 26 +if eq get seen j 1 +dc = add dc 1 +end +end +for j ulong len vow +dv = add dv ulong get seen sub get vow j 97 +end +emit td +emitu dv +emit tc +emitu sub dc dv +emit nl +ret 0 +end diff --git a/tasks/count-how-many-vowels-and-consonants-occur-in-a-string/expected.txt b/tasks/count-how-many-vowels-and-consonants-occur-in-a-string/expected.txt new file mode 100644 index 0000000..4735052 --- /dev/null +++ b/tasks/count-how-many-vowels-and-consonants-occur-in-a-string/expected.txt @@ -0,0 +1,2 @@ +vowels 22, consonants 31 +distinct vowels 5, consonants 13 diff --git a/tasks/count-how-many-vowels-and-consonants-occur-in-a-string/oracle.py b/tasks/count-how-many-vowels-and-consonants-occur-in-a-string/oracle.py new file mode 100644 index 0000000..8bf9d51 --- /dev/null +++ b/tasks/count-how-many-vowels-and-consonants-occur-in-a-string/oracle.py @@ -0,0 +1,8 @@ +# Independent reference for "Count how many vowels and consonants occur in a string" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +s = "Now is the time for all good men to come to the aid of their country.".lower() +letters = [c for c in s if "a" <= c <= "z"] +v = [c for c in letters if c in "aeiou"] +c = [c for c in letters if c not in "aeiou"] +print(f"vowels {len(v)}, consonants {len(c)}") +print(f"distinct vowels {len(set(v))}, consonants {len(set(c))}") diff --git a/tasks/count-in-factors/count-in-factors.om b/tasks/count-in-factors/count-in-factors.om new file mode 100644 index 0000000..5676648 --- /dev/null +++ b/tasks/count-in-factors/count-in-factors.om @@ -0,0 +1,76 @@ +note Rosetta Code task "Count in factors": https://rosettacode.org/wiki/Count_in_factors +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text x " x " +text eqs " = " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note prime factors in ascending order by trial division; 1 is shown as itself +note bound: each pass divides v or steps d, and d * d <= v; for the inputs here d never passes 6857 +fn tell n ulong to void +emitu n +emit eqs +if eq n 1 +emitu 1 +emit nl +ret +end +v = n +d = ulong 2 +f = ulong 0 +while le mul d d v bound 999999 +if eq rem v d 0 +if gt f 0 +emit x +end +emitu d +f = add f 1 +v = div v d +else +d = add d 1 +end +end +if gt v 1 +if gt f 0 +emit x +end +emitu v +end +emit nl +end +fn main to int +for i ulong 25 +tell add i 1 +end +for i ulong 13 +tell add 2144 i +end +tell 600851475143 +tell 1000000000000 +ret 0 +end diff --git a/tasks/count-in-factors/expected.txt b/tasks/count-in-factors/expected.txt new file mode 100644 index 0000000..b875288 --- /dev/null +++ b/tasks/count-in-factors/expected.txt @@ -0,0 +1,40 @@ +1 = 1 +2 = 2 +3 = 3 +4 = 2 x 2 +5 = 5 +6 = 2 x 3 +7 = 7 +8 = 2 x 2 x 2 +9 = 3 x 3 +10 = 2 x 5 +11 = 11 +12 = 2 x 2 x 3 +13 = 13 +14 = 2 x 7 +15 = 3 x 5 +16 = 2 x 2 x 2 x 2 +17 = 17 +18 = 2 x 3 x 3 +19 = 19 +20 = 2 x 2 x 5 +21 = 3 x 7 +22 = 2 x 11 +23 = 23 +24 = 2 x 2 x 2 x 3 +25 = 5 x 5 +2144 = 2 x 2 x 2 x 2 x 2 x 67 +2145 = 3 x 5 x 11 x 13 +2146 = 2 x 29 x 37 +2147 = 19 x 113 +2148 = 2 x 2 x 3 x 179 +2149 = 7 x 307 +2150 = 2 x 5 x 5 x 43 +2151 = 3 x 3 x 239 +2152 = 2 x 2 x 2 x 269 +2153 = 2153 +2154 = 2 x 3 x 359 +2155 = 5 x 431 +2156 = 2 x 2 x 7 x 7 x 11 +600851475143 = 71 x 839 x 1471 x 6857 +1000000000000 = 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 diff --git a/tasks/count-in-factors/oracle.py b/tasks/count-in-factors/oracle.py new file mode 100644 index 0000000..cf4929f --- /dev/null +++ b/tasks/count-in-factors/oracle.py @@ -0,0 +1,14 @@ +# Independent reference for "Count in factors" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def factors(n): + out, p = [], 2 + while p * p <= n: + while n % p == 0: + out.append(p) + n //= p + p += 1 if p == 2 else 2 + if n > 1: + out.append(n) + return out +for n in list(range(1, 26)) + list(range(2144, 2157)) + [600851475143, 1000000000000]: + print(f"{n} = " + (" x ".join(map(str, factors(n))) if n > 1 else "1")) diff --git a/tasks/count-in-factors/wolfram.txt b/tasks/count-in-factors/wolfram.txt new file mode 100644 index 0000000..1baa035 --- /dev/null +++ b/tasks/count-in-factors/wolfram.txt @@ -0,0 +1,12 @@ +Wolfram cross-check of tasks/count-in-factors/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 19e0eee14a5ef1e130bd7bdee1cab38ca82042733f08e87c90cdf788e5feae9b. +agreement: YES +query: +fs[1] = "1"; fs[n_] := StringRiffle[ToString /@ Flatten[ConstantArray @@@ FactorInteger[n]], " x "]; +s = StringJoin[(ToString[#] <> " = " <> fs[#] <> "\n") & /@ Join[Range[25], Range[2144, 2156], {600851475143, 1000000000000}]]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"19e0eee14a5ef1e130bd7bdee1cab38ca82042733f08e87c90cdf788e5feae9b", "1 = 1\n2 = 2\n3 = 3\n4 = 2 x 2\n5 = 5\n6 = 2 x 3\n7 = 7\n8 = 2 x 2 x 2\n9 = 3 x 3\n10 = 2 x 5\n11 = 11\n12 = 2 x 2 x 3\n13 = 13\n14 = 2 x 7\n15 = 3 x 5\n16 = 2 x 2 x 2 x 2\n17 = 17\n18 = 2 x 3 x 3\n19 = 19\n20 = 2 x 2 x 5\n21 = 3 x 7\n22 = 2 x 11\n23 = 23\n24 = 2 x 2 x 2 x 3\n25 = 5 x 5\n2144 = 2 x 2 x 2 x 2 x 2 x 67\n2145 = 3 x 5 x 11 x 13\n2146 = 2 x 29 x 37\n2147 = 19 x 113\n2148 = 2 x 2 x 3 x 179\n2149 = 7 x 307\n2150 = 2 x 5 x 5 x 43\n2151 = 3 x 3 x 239\n2152 = 2 x 2 x 2 x 269\n2153 = 2153\n2154 = 2 x 3 x 359\n2155 = 5 x 431\n2156 = 2 x 2 x 7 x 7 x 11\n600851475143 = 71 x 839 x 1471 x 6857\n1000000000000 = 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5\n"} diff --git a/tasks/count-occurrences-of-a-substring/count-occurrences-of-a-substring.om b/tasks/count-occurrences-of-a-substring/count-occurrences-of-a-substring.om new file mode 100644 index 0000000..c073b4f --- /dev/null +++ b/tasks/count-occurrences-of-a-substring/count-occurrences-of-a-substring.om @@ -0,0 +1,61 @@ +note Rosetta Code task "Count occurrences of a substring": https://rosettacode.org/wiki/Count_occurrences_of_a_substring +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text h1 "the three truths" +text n1 "th" +text h2 "ababababab" +text n2 "abab" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note count scans left to right and skips past each match, so matches never overlap +fn count s view uchar * t view uchar * to ulong +c = ulong 0 +i = ulong 0 +while le add i len t len s bound 4096 +m = true +for j ulong len t +if ne get s add i j get t j +m = false +end +end +if m +c = add c 1 +i = add i len t +else +i = add i 1 +end +end +ret c +end +fn main to int +emitu count h1 n1 +emit nl +emitu count h2 n2 +emit nl +ret 0 +end diff --git a/tasks/count-occurrences-of-a-substring/expected.txt b/tasks/count-occurrences-of-a-substring/expected.txt new file mode 100644 index 0000000..f1e5eee --- /dev/null +++ b/tasks/count-occurrences-of-a-substring/expected.txt @@ -0,0 +1,2 @@ +3 +2 diff --git a/tasks/count-occurrences-of-a-substring/oracle.py b/tasks/count-occurrences-of-a-substring/oracle.py new file mode 100644 index 0000000..a58ef2e --- /dev/null +++ b/tasks/count-occurrences-of-a-substring/oracle.py @@ -0,0 +1,4 @@ +# Independent reference for "Count occurrences of a substring" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +print("the three truths".count("th")) +print("ababababab".count("abab")) diff --git a/tasks/count-the-coins/count-the-coins.om b/tasks/count-the-coins/count-the-coins.om new file mode 100644 index 0000000..cd3bce7 --- /dev/null +++ b/tasks/count-the-coins/count-the-coins.om @@ -0,0 +1,71 @@ +note Rosetta Code task "Count the coins": https://rosettacode.org/wiki/Count_the_coins +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text t1 "ways to make 100 cents from 1, 5, 10, 25: " +text t2 "ways to make 100000 cents from 1, 5, 10, 25, 50, 100: " +global ways ulong 100001 +global coins ulong 6 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note addcoin lets every amount from c up to amt also end with a coin c +fn addcoin amt ulong c ulong to void +for x ulong add sub amt c 1 +y = add x c +w = add get ways y get ways x +put ways y w +end +end +note count is the classic change-making dynamic program: ways[a] counts the multisets of the first k +note coins that sum to a; adding coin c lets every amount a >= c also end with a c +fn count amt ulong k ulong to ulong +for a ulong add amt 1 +put ways a 0 +end +put ways 0 1 +for i ulong k +addcoin amt get coins i +end +ret get ways amt +end +fn main to int +put coins 0 1 +put coins 1 5 +put coins 2 10 +put coins 3 25 +put coins 4 50 +put coins 5 100 +emit t1 +emitu count 100 4 +emit nl +emit t2 +emitu count 100000 6 +emit nl +ret 0 +end diff --git a/tasks/count-the-coins/expected.txt b/tasks/count-the-coins/expected.txt new file mode 100644 index 0000000..98eb3bd --- /dev/null +++ b/tasks/count-the-coins/expected.txt @@ -0,0 +1,2 @@ +ways to make 100 cents from 1, 5, 10, 25: 242 +ways to make 100000 cents from 1, 5, 10, 25, 50, 100: 13398445413854501 diff --git a/tasks/count-the-coins/oracle.py b/tasks/count-the-coins/oracle.py new file mode 100644 index 0000000..aa57c21 --- /dev/null +++ b/tasks/count-the-coins/oracle.py @@ -0,0 +1,18 @@ +# Independent reference for "Count the coins" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# Cross-check, WolframLanguageEvaluator: {SeriesCoefficient[1/((1-x)(1-x^5)(1-x^10)(1-x^25)),{x,0,100}], +# (the same count to 100000 with 50 and 100 added)} -> {242, 13398445413854501} +# Counted without the change-making table. 1-cent coins fill any remainder, so the ways to make s from +# 1, 5, 10 are the pairs (n5, n10) with 5*n5 + 10*n10 <= s: with t = s // 5 and h = t // 2 that is +# sum over j = 0..h of (t - 2j + 1) = (h + 1)(t + 1) - h(h + 1). 25s are enumerated on top of that. +# 50s and 100s together make 50*m cents in m // 2 + 1 ways (n100 = 0..m//2), so m is enumerated last. +def ways_1_5_10(s): + t = s // 5 + h = t // 2 + return (h + 1) * (t + 1) - h * (h + 1) +def ways_to_25(s): + return sum(ways_1_5_10(s - 25 * k) for k in range(s // 25 + 1)) +def ways_to_100(s): + return sum((m // 2 + 1) * ways_to_25(s - 50 * m) for m in range(s // 50 + 1)) +print("ways to make 100 cents from 1, 5, 10, 25:", ways_to_25(100)) +print("ways to make 100000 cents from 1, 5, 10, 25, 50, 100:", ways_to_100(100000)) diff --git a/tasks/counting-sort/counting-sort.om b/tasks/counting-sort/counting-sort.om new file mode 100644 index 0000000..af18a18 --- /dev/null +++ b/tasks/counting-sort/counting-sort.om @@ -0,0 +1,117 @@ +note Rosetta Code task "Sorting algorithms/Counting sort": https://rosettacode.org/wiki/Sorting_algorithms/Counting_sort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "7 -3 12 0 7 4 -3 9 15 1 -5 4 12 0 8" +global vals long 16 +global cnt ulong 64 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note cntsort counts each value in min..max (the range must fit the 64 counters), then rewrites vals +fn cntsort n ulong to void +lo = get vals 0 +hi = get vals 0 +for i ulong n +if lt get vals i lo +lo = get vals i +end +if gt get vals i hi +hi = get vals i +end +end +for i ulong n +c = ulong sub get vals i lo +put cnt c add get cnt c 1 +end +k = ulong 0 +for x ulong add ulong sub hi lo 1 +for y ulong get cnt x +put vals k add lo long x +k = add k 1 +end +end +end +fn main to int +n = parse data +show n +cntsort n +show n +ret 0 +end diff --git a/tasks/counting-sort/expected.txt b/tasks/counting-sort/expected.txt new file mode 100644 index 0000000..9b186e2 --- /dev/null +++ b/tasks/counting-sort/expected.txt @@ -0,0 +1,2 @@ +7 -3 12 0 7 4 -3 9 15 1 -5 4 12 0 8 +-5 -3 -3 0 0 1 4 4 7 7 8 9 12 12 15 diff --git a/tasks/counting-sort/oracle.py b/tasks/counting-sort/oracle.py new file mode 100644 index 0000000..2a221b9 --- /dev/null +++ b/tasks/counting-sort/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Sorting algorithms/Counting sort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [7, -3, 12, 0, 7, 4, -3, 9, 15, 1, -5, 4, 12, 0, 8] +print(*a) +print(*sorted(a)) diff --git a/tasks/cuban-primes/cuban-primes.om b/tasks/cuban-primes/cuban-primes.om new file mode 100644 index 0000000..f4436e2 --- /dev/null +++ b/tasks/cuban-primes/cuban-primes.om @@ -0,0 +1,137 @@ +note Rosetta Code task "Cuban primes": https://rosettacode.org/wiki/Cuban_primes +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text first "first 200 cuban primes:\n" +text th "the 100000th cuban prime: " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note cuban primes here are the primes (n+1)^3 - n^3 = 3n^2 + 3n + 1; the 100000th, 1792617147127, is below 2^41 +note mulm is a * b mod m for m < 2^42: b splits into 21-bit halves so each product stays below 2^63 +fn mulm a ulong b ulong m ulong to ulong +h = mul a shr b 21 +h = rem h m +h = rem shl h 21 m +l = rem mul a and b 0x1fffff m +r = add h l +if ge r m +r = sub r m +end +ret r +end +fn powm b ulong e ulong m ulong to ulong +r = ulong 1 +x = rem b m +v = e +while gt v 0 bound 64 +if eq and v 1 1 +r = mulm r x m +end +x = mulm x x m +v = shr v 1 +end +ret r +end +fn sprp n ulong a ulong to bool +d = sub n 1 +s = ulong 0 +while eq and d 1 0 bound 64 +d = shr d 1 +s = add s 1 +end +x = powm a d n +if or eq x 1 eq x sub n 1 +ret true +end +for i ulong sub s 1 +x = mulm x x n +if eq x sub n 1 +ret true +end +end +ret false +end +note trial division by 2..49 first, then Miller-Rabin to bases 2, 3, 5, 7, 11, 13, which is exact below 3.47 * 10^12 +fn isprime n ulong to bool +p = ulong 2 +while lt p 50 bound 50 +if eq rem n p 0 +ret eq n p +end +p = add p 1 +end +for k ulong 6 +a = ulong 13 +if eq k 0 +a = 2 +end +if eq k 1 +a = 3 +end +if eq k 2 +a = 5 +end +if eq k 3 +a = 7 +end +if eq k 4 +a = 11 +end +if not sprp n a +ret false +end +end +ret true +end +note bound: the 100000th cuban prime is reached at n = 773019 +fn main to int +emit first +c = ulong 0 +n = ulong 1 +while lt c 100000 bound 999999 +v = add mul mul 3 n add n 1 1 +if isprime v +c = add c 1 +if le c 200 +emitu v +if eq rem c 10 0 +emit nl +else +emit sp +end +end +if eq c 100000 +emit th +emitu v +emit nl +end +end +n = add n 1 +end +ret 0 +end diff --git a/tasks/cuban-primes/expected.txt b/tasks/cuban-primes/expected.txt new file mode 100644 index 0000000..87b2fad --- /dev/null +++ b/tasks/cuban-primes/expected.txt @@ -0,0 +1,22 @@ +first 200 cuban primes: +7 19 37 61 127 271 331 397 547 631 +919 1657 1801 1951 2269 2437 2791 3169 3571 4219 +4447 5167 5419 6211 7057 7351 8269 9241 10267 11719 +12097 13267 13669 16651 19441 19927 22447 23497 24571 25117 +26227 27361 33391 35317 42841 45757 47251 49537 50311 55897 +59221 60919 65269 70687 73477 74419 75367 81181 82171 87211 +88237 89269 92401 96661 102121 103231 104347 110017 112327 114661 +115837 126691 129169 131671 135469 140617 144541 145861 151201 155269 +163567 169219 170647 176419 180811 189757 200467 202021 213067 231019 +234361 241117 246247 251431 260191 263737 267307 276337 279991 283669 +285517 292969 296731 298621 310087 329677 333667 337681 347821 351919 +360187 368551 372769 374887 377011 383419 387721 398581 407377 423001 +436627 452797 459817 476407 478801 493291 522919 527941 553411 574219 +584767 590077 592741 595411 603457 608851 611557 619711 627919 650071 +658477 666937 689761 692641 698419 707131 733591 742519 760537 769627 +772669 784897 791047 812761 825301 837937 847477 863497 879667 886177 +895987 909151 915769 925741 929077 932419 939121 952597 972991 976411 +986707 990151 997057 1021417 1024921 1035469 1074607 1085407 1110817 1114471 +1125469 1155061 1177507 1181269 1215397 1253887 1281187 1285111 1324681 1328671 +1372957 1409731 1422097 1426231 1442827 1451161 1480519 1484737 1527247 1570357 +the 100000th cuban prime: 1792617147127 diff --git a/tasks/cuban-primes/oracle.py b/tasks/cuban-primes/oracle.py new file mode 100644 index 0000000..84f7720 --- /dev/null +++ b/tasks/cuban-primes/oracle.py @@ -0,0 +1,33 @@ +# Independent reference for "Cuban primes" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def is_prime(n): + if n < 2: + return False + for p in (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37): + if n % p == 0: + return n == p + d, s = n - 1, 0 + while d % 2 == 0: + d //= 2 + s += 1 + for a in (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37): + x = pow(a, d, n) + if x in (1, n - 1): + continue + for _ in range(s - 1): + x = x * x % n + if x == n - 1: + break + else: + return False + return True +cub, n = [], 0 +while len(cub) < 100000: + n += 1 + v = (n + 1) ** 3 - n ** 3 + if is_prime(v): + cub.append(v) +print("first 200 cuban primes:") +for i in range(0, 200, 10): + print(" ".join(map(str, cub[i:i + 10]))) +print(f"the 100000th cuban prime: {cub[-1]}") diff --git a/tasks/cuban-primes/wolfram.txt b/tasks/cuban-primes/wolfram.txt new file mode 100644 index 0000000..f422e73 --- /dev/null +++ b/tasks/cuban-primes/wolfram.txt @@ -0,0 +1,13 @@ +Wolfram cross-check of tasks/cuban-primes/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = bcb595311923a6a0e8a1e90a44645426e63e62df320b5b64ff5ae2285c391d9b. +agreement: YES +query: +c = Select[3 #^2 + 3 # + 1 & /@ Range[800000], PrimeQ]; +s = "first 200 cuban primes:\n" <> StringJoin[(StringRiffle[ToString /@ #, " "] <> "\n") & /@ Partition[Take[c, 200], 10]] <> + "the 100000th cuban prime: " <> ToString[c[[100000]]] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"bcb595311923a6a0e8a1e90a44645426e63e62df320b5b64ff5ae2285c391d9b", "first 200 cuban primes:\n7 19 37 61 127 271 331 397 547 631\n919 1657 1801 1951 2269 2437 2791 3169 3571 4219\n4447 5167 5419 6211 7057 7351 8269 9241 10267 11719\n12097 13267 13669 16651 19441 19927 22447 23497 24571 25117\n26227 27361 33391 35317 42841 45757 47251 49537 50311 55897\n59221 60919 65269 70687 73477 74419 75367 81181 82171 87211\n88237 89269 92401 96661 102121 103231 104347 110017 112327 114661\n115837 126691 129169 131671 135469 140617 144541 145861 151201 155269\n163567 169219 170647 176419 180811 189757 200467 202021 213067 231019\n234361 241117 246247 251431 260191 263737 267307 276337 279991 283669\n285517 292969 296731 298621 310087 329677 333667 337681 347821 351919\n360187 368551 372769 374887 377011 383419 387721 398581 407377 423001\n436627 452797 459817 476407 478801 493291 522919 527941 553411 574219\n584767 590077 592741 595411 603457 608851 611557 619711 627919 650071\n658477 666937 689761 692641 698419 707131 733591 742519 760537 769627\n772669 784897 791047 812761 825301 837937 847477 863497 879667 886177\n895987 909151 915769 925741 929077 932419 939121 952597 972991 976411\n986707 990151 997057 1021417 1024921 1035469 1074607 1085407 1110817 1114471\n1125469 1155061 1177507 1181269 1215397 1253887 1281187 1285111 1324681 1328671\n1372957 1409731 1422097 1426231 1442827 1451161 1480519 1484737 1527247 1570357\nthe 100000th cuban prime: 1792617147127\n"} diff --git a/tasks/cycle-sort/cycle-sort.om b/tasks/cycle-sort/cycle-sort.om new file mode 100644 index 0000000..2d33aec --- /dev/null +++ b/tasks/cycle-sort/cycle-sort.om @@ -0,0 +1,136 @@ +note Rosetta Code task "Sorting algorithms/Cycle sort": https://rosettacode.org/wiki/Sorting_algorithms/Cycle_sort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "5 0 1 2 2 3 5 1 1 0 5 6 9 8 0 1" +text twr "writes: " +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note place counts the elements after vals[c] that are smaller than item: that is item's sorted position +fn place c ulong n ulong item long to ulong +p = c +for x ulong sub sub n c 1 +if lt get vals add add c x 1 item +p = add p 1 +end +end +ret p +end +note cysort rotates each cycle into place, writing every misplaced element exactly once, and returns +note the number of writes; each rotation pass writes one element, so n passes bound one cycle +fn cysort n ulong to ulong +w = ulong 0 +for c ulong sub n 1 +item = get vals c +p = place c n item +if eq p c +continue +end +while eq item get vals p bound 16 +p = add p 1 +end +t = get vals p +put vals p item +item = t +w = add w 1 +while ne p c bound 16 +p = place c n item +while eq item get vals p bound 16 +p = add p 1 +end +t = get vals p +put vals p item +item = t +w = add w 1 +end +end +ret w +end +fn main to int +n = parse data +show n +w = cysort n +show n +emit twr +emitu w +emit nl +ret 0 +end diff --git a/tasks/cycle-sort/expected.txt b/tasks/cycle-sort/expected.txt new file mode 100644 index 0000000..6cc958b --- /dev/null +++ b/tasks/cycle-sort/expected.txt @@ -0,0 +1,3 @@ +5 0 1 2 2 3 5 1 1 0 5 6 9 8 0 1 +0 0 0 1 1 1 1 2 2 3 5 5 5 6 8 9 +writes: 14 diff --git a/tasks/cycle-sort/oracle.py b/tasks/cycle-sort/oracle.py new file mode 100644 index 0000000..6bf7f1f --- /dev/null +++ b/tasks/cycle-sort/oracle.py @@ -0,0 +1,26 @@ +# Independent reference for "Sorting algorithms/Cycle sort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def cycle_sort(a): + writes = 0 + for start in range(len(a) - 1): + item = a[start] + pos = start + sum(1 for v in a[start + 1:] if v < item) + if pos == start: + continue + while item == a[pos]: + pos += 1 + a[pos], item = item, a[pos] + writes += 1 + while pos != start: + pos = start + sum(1 for v in a[start + 1:] if v < item) + while item == a[pos]: + pos += 1 + a[pos], item = item, a[pos] + writes += 1 + return writes +a = [5, 0, 1, 2, 2, 3, 5, 1, 1, 0, 5, 6, 9, 8, 0, 1] +print(*a) +w = cycle_sort(a) +assert a == sorted(a) +print(*a) +print("writes:", w) diff --git a/tasks/determinant-and-permanent/determinant-and-permanent.om b/tasks/determinant-and-permanent/determinant-and-permanent.om new file mode 100644 index 0000000..82523c8 --- /dev/null +++ b/tasks/determinant-and-permanent/determinant-and-permanent.om @@ -0,0 +1,207 @@ +note Rosetta Code task "Determinant and permanent": https://rosettacode.org/wiki/Determinant_and_permanent +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tm "matrix:\n" +text tdet "determinant: " +text tper "permanent: " +text tdata "1 2 3 4 2 9 4 7 5 3 6 1 8 1 2 3 4 4 5 6 7 7 8 9 10 10 11 12 13" +global vals long 64 +global mat long 25 +global perm ulong 8 +global res long 2 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note emitwi writes a signed n right-aligned in w columns (w at most 20) +fn emitwi n long w ulong to void +d = new uchar 21 32 +i = ulong 21 +v = n +if lt n 0 +v = neg n +end +while true bound 20 +i = sub i 1 +put d i uchar add long 48 rem v 10 +v = div v 10 +if eq v 0 +break +end +end +if lt n 0 +i = sub i 1 +put d i 45 +end +if gt w sub 21 i +i = sub 21 w +end +emit part d i sub 21 i +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note both are sums over all n! permutations p of the products m[0][p0] * .. * m[n-1][p(n-1)]; the +note determinant gives each the sign of p (its inversion count parity), the permanent adds them all +note nextp: the next permutation in lexicographic order, false after the last +fn nextp n ulong to bool +if lt n 2 +ret false +end +i = sub n 1 +while gt i 0 bound 8 +if lt get perm sub i 1 get perm i +break +end +i = sub i 1 +end +if eq i 0 +ret false +end +j = sub n 1 +while le get perm j get perm sub i 1 bound 8 +j = sub j 1 +end +t = get perm sub i 1 +put perm sub i 1 get perm j +put perm j t +k = sub n 1 +while lt i k bound 8 +t2 = get perm i +put perm i get perm k +put perm k t2 +i = add i 1 +k = sub k 1 +end +ret true +end +fn odd n ulong to bool +c = ulong 0 +for i ulong n +for j ulong n +if and lt i j gt get perm i get perm j +c = add c 1 +end +end +end +ret eq rem c 2 1 +end +fn term n ulong to long +p = long 1 +for i ulong n +p = mul p get mat add mul i n get perm i +end +ret p +end +note detper fills res[0] with the determinant and res[1] with the permanent; 5! = 120 bounds the loop +fn detper n ulong to void +for i ulong n +put perm i i +end +put res 0 0 +put res 1 0 +while true bound 120 +t = term n +put res 1 add get res 1 t +if odd n +put res 0 sub get res 0 t +else +put res 0 add get res 0 t +end +if not nextp n +break +end +end +end +fn case o ulong n ulong to void +emit tm +for i ulong n +for j ulong n +put mat add mul i n j get vals add o add mul i n j +emitwi get mat add mul i n j 4 +end +emit nl +end +detper n +emit tdet +emiti get res 0 +emit nl +emit tper +emiti get res 1 +emit nl +end +note the 5 x 5 case is 0 .. 24, filled in place rather than parsed +fn main to int +n = parse tdata +case 0 2 +case 4 3 +case 13 4 +for i ulong 25 +put vals add 29 i long i +end +case 29 5 +ret 0 +end diff --git a/tasks/determinant-and-permanent/expected.txt b/tasks/determinant-and-permanent/expected.txt new file mode 100644 index 0000000..4885630 --- /dev/null +++ b/tasks/determinant-and-permanent/expected.txt @@ -0,0 +1,26 @@ +matrix: + 1 2 + 3 4 +determinant: -2 +permanent: 10 +matrix: + 2 9 4 + 7 5 3 + 6 1 8 +determinant: -360 +permanent: 900 +matrix: + 1 2 3 4 + 4 5 6 7 + 7 8 9 10 + 10 11 12 13 +determinant: 0 +permanent: 29556 +matrix: + 0 1 2 3 4 + 5 6 7 8 9 + 10 11 12 13 14 + 15 16 17 18 19 + 20 21 22 23 24 +determinant: 0 +permanent: 6778800 diff --git a/tasks/determinant-and-permanent/oracle.py b/tasks/determinant-and-permanent/oracle.py new file mode 100644 index 0000000..1b640c7 --- /dev/null +++ b/tasks/determinant-and-permanent/oracle.py @@ -0,0 +1,40 @@ +# Independent reference for "Determinant and permanent" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# Cross-check, WolframLanguageEvaluator: ms = {the four matrices}; {Map[Det, ms], Map[Permanent, ms]} +# -> {{-2, -360, 0, 0}, {10, 900, 29556, 6778800}} +from fractions import Fraction +def det(m): + # Gaussian elimination over exact fractions + a = [[Fraction(v) for v in row] for row in m] + n, d = len(a), Fraction(1) + for c in range(n): + p = next((r for r in range(c, n) if a[r][c] != 0), None) + if p is None: + return 0 + if p != c: + a[c], a[p] = a[p], a[c] + d = -d + d *= a[c][c] + for r in range(c + 1, n): + f = a[r][c] / a[c][c] + a[r] = [x - f * y for x, y in zip(a[r], a[c])] + return int(d) +def per(m): + # Ryser's formula: sum over column subsets S of (-1)^(n-|S|) * prod over rows of the sum over S + n, total = len(m), 0 + for s in range(1, 1 << n): + cols = [j for j in range(n) if s >> j & 1] + prod = 1 + for row in m: + prod *= sum(row[j] for j in cols) + total += (-1) ** (n - len(cols)) * prod + return total +cases = [[[1, 2], [3, 4]], [[2, 9, 4], [7, 5, 3], [6, 1, 8]], + [[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10], [10, 11, 12, 13]], + [[r * 5 + c for c in range(5)] for r in range(5)]] +for m in cases: + print("matrix:") + for row in m: + print("".join(f"{v:4}" for v in row)) + print("determinant:", det(m)) + print("permanent:", per(m)) diff --git a/tasks/determine-if-a-string-has-all-the-same-characters/determine-if-a-string-has-all-the-same-characters.om b/tasks/determine-if-a-string-has-all-the-same-characters/determine-if-a-string-has-all-the-same-characters.om new file mode 100644 index 0000000..3fdffe9 --- /dev/null +++ b/tasks/determine-if-a-string-has-all-the-same-characters/determine-if-a-string-has-all-the-same-characters.om @@ -0,0 +1,77 @@ +note Rosetta Code task "Determine if a string has all the same characters": https://rosettacode.org/wiki/Determine_if_a_string_has_all_the_same_characters +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text lq "<<<" +text rq ">>>" +text tlen " length " +text tsame ": all the same\n" +text tdiff ": first different '" +text tat "' at " +text u0 " " +text u1 "2" +text u2 "333" +text u3 ".55" +text u4 "tttTTT" +text u5 "4444 444k" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +fn show a view uchar * to void +emit lq +emit a +emit rq +emit tlen +emitu len a +for i ulong len a +if ne get a i get a 0 +emit tdiff +emitc get a i +emit tat +emitu i +emit nl +ret +end +end +emit tsame +end +fn main to int +note the empty string, as a slice of length 0 +show part u0 0 0 +show u0 +show u1 +show u2 +show u3 +show u4 +show u5 +ret 0 +end diff --git a/tasks/determine-if-a-string-has-all-the-same-characters/expected.txt b/tasks/determine-if-a-string-has-all-the-same-characters/expected.txt new file mode 100644 index 0000000..7c8e186 --- /dev/null +++ b/tasks/determine-if-a-string-has-all-the-same-characters/expected.txt @@ -0,0 +1,7 @@ +<<<>>> length 0: all the same +<<< >>> length 3: all the same +<<<2>>> length 1: all the same +<<<333>>> length 3: all the same +<<<.55>>> length 3: first different '5' at 1 +<<>> length 6: first different 'T' at 3 +<<<4444 444k>>> length 9: first different ' ' at 4 diff --git a/tasks/determine-if-a-string-has-all-the-same-characters/oracle.py b/tasks/determine-if-a-string-has-all-the-same-characters/oracle.py new file mode 100644 index 0000000..c19f821 --- /dev/null +++ b/tasks/determine-if-a-string-has-all-the-same-characters/oracle.py @@ -0,0 +1,9 @@ +# Independent reference for "Determine if a string has all the same characters" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for s in ["", ' ', '2', '333', '.55', 'tttTTT', '4444 444k']: + line = f"<<<{s}>>> length {len(s)}" + d = [i for i, c in enumerate(s) if c != s[0]] + if d: + print(f"{line}: first different '{s[d[0]]}' at {d[0]}") + else: + print(f"{line}: all the same") diff --git a/tasks/determine-if-a-string-has-all-unique-characters/determine-if-a-string-has-all-unique-characters.om b/tasks/determine-if-a-string-has-all-unique-characters/determine-if-a-string-has-all-unique-characters.om new file mode 100644 index 0000000..048ab77 --- /dev/null +++ b/tasks/determine-if-a-string-has-all-unique-characters/determine-if-a-string-has-all-unique-characters.om @@ -0,0 +1,80 @@ +note Rosetta Code task "Determine if a string has all unique characters": https://rosettacode.org/wiki/Determine_if_a_string_has_all_unique_characters +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text lq "<<<" +text rq ">>>" +text tlen " length " +text tuni ": all unique\n" +text tdup ": duplicate '" +text tat "' at " +text tand " and " +text u0 "." +text u1 "abcABC" +text u2 "XYZ ZYX" +text u3 "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note the first byte (smallest i) that occurs again later, with the place j of its next occurrence +fn show a view uchar * to void +emit lq +emit a +emit rq +emit tlen +emitu len a +for i ulong len a +for j ulong sub sub len a i 1 +k = add add i j 1 +if eq get a i get a k +emit tdup +emitc get a i +emit tat +emitu i +emit tand +emitu k +emit nl +ret +end +end +end +emit tuni +end +fn main to int +note the empty string, as a slice of length 0 +show part u0 0 0 +show u0 +show u1 +show u2 +show u3 +ret 0 +end diff --git a/tasks/determine-if-a-string-has-all-unique-characters/expected.txt b/tasks/determine-if-a-string-has-all-unique-characters/expected.txt new file mode 100644 index 0000000..f925f5a --- /dev/null +++ b/tasks/determine-if-a-string-has-all-unique-characters/expected.txt @@ -0,0 +1,5 @@ +<<<>>> length 0: all unique +<<<.>>> length 1: all unique +<<>> length 6: all unique +<<>> length 7: duplicate 'X' at 0 and 6 +<<<1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ>>> length 36: duplicate '0' at 9 and 24 diff --git a/tasks/determine-if-a-string-has-all-unique-characters/oracle.py b/tasks/determine-if-a-string-has-all-unique-characters/oracle.py new file mode 100644 index 0000000..364a215 --- /dev/null +++ b/tasks/determine-if-a-string-has-all-unique-characters/oracle.py @@ -0,0 +1,11 @@ +# Independent reference for "Determine if a string has all unique characters" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for s in ["", '.', 'abcABC', 'XYZ ZYX', '1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ']: + line = f"<<<{s}>>> length {len(s)}" + for i, c in enumerate(s): + j = s.find(c, i + 1) + if j >= 0: + print(f"{line}: duplicate '{c}' at {i} and {j}") + break + else: + print(f"{line}: all unique") diff --git a/tasks/determine-if-a-string-is-collapsible/determine-if-a-string-is-collapsible.om b/tasks/determine-if-a-string-is-collapsible/determine-if-a-string-is-collapsible.om new file mode 100644 index 0000000..d4e4e09 --- /dev/null +++ b/tasks/determine-if-a-string-is-collapsible/determine-if-a-string-is-collapsible.om @@ -0,0 +1,72 @@ +note Rosetta Code task "Determine if a string is collapsible": https://rosettacode.org/wiki/Determine_if_a_string_is_collapsible +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text lq "<<<" +text rq ">>>" +text tlen " length " +text c0 "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln " +text c1 "..1111111111111111111111111111111111111111111111111111111111111117777888" +text c2 "I never give 'em hell, I just tell the truth, and they think it's hell. " +text c3 " --- Harry S Truman " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn line a view uchar * to void +emit lq +emit a +emit rq +emit tlen +emitu len a +emit nl +end +note collapse keeps a byte only when it differs from the byte before it +fn collapse a view uchar * to void +o = new uchar 256 0 +n = ulong 0 +for i ulong len a +c = get a i +k = true +if gt i 0 +k = ne c get a sub i 1 +end +if k +put o n c +n = add n 1 +end +end +line a +line part o 0 n +end +fn main to int +note the empty string, as a slice of length 0 +collapse part c0 0 0 +collapse c0 +collapse c1 +collapse c2 +collapse c3 +ret 0 +end diff --git a/tasks/determine-if-a-string-is-collapsible/expected.txt b/tasks/determine-if-a-string-is-collapsible/expected.txt new file mode 100644 index 0000000..258eace --- /dev/null +++ b/tasks/determine-if-a-string-is-collapsible/expected.txt @@ -0,0 +1,10 @@ +<<<>>> length 0 +<<<>>> length 0 +<<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> length 72 +<<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> length 70 +<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> length 72 +<<<.178>>> length 4 +<<>> length 72 +<<>> length 69 +<<< --- Harry S Truman >>> length 72 +<<< - Hary S Truman >>> length 17 diff --git a/tasks/determine-if-a-string-is-collapsible/oracle.py b/tasks/determine-if-a-string-is-collapsible/oracle.py new file mode 100644 index 0000000..9cad7ba --- /dev/null +++ b/tasks/determine-if-a-string-is-collapsible/oracle.py @@ -0,0 +1,7 @@ +# Independent reference for "Determine if a string is collapsible" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import itertools +for s in ["", '"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ', '..1111111111111111111111111111111111111111111111111111111111111117777888', "I never give 'em hell, I just tell the truth, and they think it's hell. ", ' --- Harry S Truman ']: + t = "".join(k for k, _ in itertools.groupby(s)) + print(f"<<<{s}>>> length {len(s)}") + print(f"<<<{t}>>> length {len(t)}") diff --git a/tasks/determine-if-a-string-is-numeric/determine-if-a-string-is-numeric.om b/tasks/determine-if-a-string-is-numeric/determine-if-a-string-is-numeric.om new file mode 100644 index 0000000..3a7df09 --- /dev/null +++ b/tasks/determine-if-a-string-is-numeric/determine-if-a-string-is-numeric.om @@ -0,0 +1,112 @@ +note Rosetta Code task "Determine if a string is numeric": https://rosettacode.org/wiki/Determine_if_a_string_is_numeric +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text tq "\"" +text tyes "\" numeric\n" +text tno "\" not numeric\n" +text s0 "0" +text s1 "-12" +text s2 "3.14" +text s3 "+1e5" +text s4 "1e-3" +text s5 ".5" +text s6 "5." +text s7 "abc" +text s9 "12a" +text s10 "-" +text s11 "1.2.3" +text s12 "e5" +text s13 "1e" +text s14 "--1" +text s15 "6.02E+23" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note digits returns the offset after a run of decimal digits starting at i +fn digits a view uchar * i ulong to ulong +j = i +while lt j len a bound 4096 +c = get a j +if or lt c 48 gt c 57 +ret j +end +j = add j 1 +end +ret j +end +note sign returns the offset after an optional + or - at i +fn sign a view uchar * i ulong to ulong +if lt i len a +c = get a i +if or eq c 43 eq c 45 +ret add i 1 +end +end +ret i +end +note numeric accepts [+-] then digits with an optional fraction (at least one digit in all), then an +note optional exponent e or E, [+-], at least one digit; nothing else, no spaces +fn numeric a view uchar * to bool +i = sign a 0 +j = digits a i +m = sub j i +if lt j len a +if eq get a j 46 +i = add j 1 +j = digits a i +m = add m sub j i +end +end +if eq m 0 +ret false +end +if lt j len a +c = get a j +if or eq c 101 eq c 69 +i = sign a add j 1 +j = digits a i +if eq j i +ret false +end +end +end +ret eq j len a +end +fn show a view uchar * to void +emit tq +emit a +if numeric a +emit tyes +else +emit tno +end +end +fn main to int +show s0 +show s1 +show s2 +show s3 +show s4 +show s5 +show s6 +show s7 +note the empty string: an empty text literal would read as unsized at this pin, so slice one +show part s7 0 0 +show s9 +show s10 +show s11 +show s12 +show s13 +show s14 +show s15 +ret 0 +end diff --git a/tasks/determine-if-a-string-is-numeric/expected.txt b/tasks/determine-if-a-string-is-numeric/expected.txt new file mode 100644 index 0000000..30fe822 --- /dev/null +++ b/tasks/determine-if-a-string-is-numeric/expected.txt @@ -0,0 +1,16 @@ +"0" numeric +"-12" numeric +"3.14" numeric +"+1e5" numeric +"1e-3" numeric +".5" numeric +"5." numeric +"abc" not numeric +"" not numeric +"12a" not numeric +"-" not numeric +"1.2.3" not numeric +"e5" not numeric +"1e" not numeric +"--1" not numeric +"6.02E+23" numeric diff --git a/tasks/determine-if-a-string-is-numeric/oracle.py b/tasks/determine-if-a-string-is-numeric/oracle.py new file mode 100644 index 0000000..abecf3b --- /dev/null +++ b/tasks/determine-if-a-string-is-numeric/oracle.py @@ -0,0 +1,10 @@ +# Independent reference for "Determine if a string is numeric" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def numeric(s): + try: + float(s) + return True + except ValueError: + return False +for s in ['0', '-12', '3.14', '+1e5', '1e-3', '.5', '5.', 'abc', '', '12a', '-', '1.2.3', 'e5', '1e', '--1', '6.02E+23']: + print(f'"{s}" {"numeric" if numeric(s) else "not numeric"}') diff --git a/tasks/determine-if-a-string-is-squeezable/determine-if-a-string-is-squeezable.om b/tasks/determine-if-a-string-is-squeezable/determine-if-a-string-is-squeezable.om new file mode 100644 index 0000000..ba78c5d --- /dev/null +++ b/tasks/determine-if-a-string-is-squeezable/determine-if-a-string-is-squeezable.om @@ -0,0 +1,84 @@ +note Rosetta Code task "Determine if a string is squeezable": https://rosettacode.org/wiki/Determine_if_a_string_is_squeezable +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text lq "<<<" +text rq ">>>" +text tlen " length " +text tch "squeeze '" +text tq "'\n" +text c0 "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln " +text c1 "..1111111111111111111111111111111111111111111111111111111111111117777888" +text c2 "I never give 'em hell, I just tell the truth, and they think it's hell. " +text c3 " --- Harry S Truman " +text chars "-7. -r" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +fn line a view uchar * to void +emit lq +emit a +emit rq +emit tlen +emitu len a +emit nl +end +note squeeze drops a byte equal to ch when the byte before it is also ch +fn squeeze a view uchar * ch uchar to void +o = new uchar 256 0 +n = ulong 0 +for i ulong len a +c = get a i +k = true +if and gt i 0 eq c ch +k = ne get a sub i 1 ch +end +if k +put o n c +n = add n 1 +end +end +emit tch +emitc ch +emit tq +line a +line part o 0 n +end +fn main to int +squeeze c1 get chars 0 +squeeze c1 get chars 1 +squeeze c2 get chars 2 +squeeze c3 get chars 3 +squeeze c3 get chars 4 +squeeze c3 get chars 5 +ret 0 +end diff --git a/tasks/determine-if-a-string-is-squeezable/expected.txt b/tasks/determine-if-a-string-is-squeezable/expected.txt new file mode 100644 index 0000000..22cc6e7 --- /dev/null +++ b/tasks/determine-if-a-string-is-squeezable/expected.txt @@ -0,0 +1,18 @@ +squeeze '-' +<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> length 72 +<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> length 72 +squeeze '7' +<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> length 72 +<<<..1111111111111111111111111111111111111111111111111111111111111117888>>> length 69 +squeeze '.' +<<>> length 72 +<<>> length 72 +squeeze ' ' +<<< --- Harry S Truman >>> length 72 +<<< --- Harry S Truman >>> length 20 +squeeze '-' +<<< --- Harry S Truman >>> length 72 +<<< - Harry S Truman >>> length 70 +squeeze 'r' +<<< --- Harry S Truman >>> length 72 +<<< --- Hary S Truman >>> length 71 diff --git a/tasks/determine-if-a-string-is-squeezable/oracle.py b/tasks/determine-if-a-string-is-squeezable/oracle.py new file mode 100644 index 0000000..939e128 --- /dev/null +++ b/tasks/determine-if-a-string-is-squeezable/oracle.py @@ -0,0 +1,8 @@ +# Independent reference for "Determine if a string is squeezable" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import re +for s, ch in [('..1111111111111111111111111111111111111111111111111111111111111117777888', '-'), ('..1111111111111111111111111111111111111111111111111111111111111117777888', '7'), ("I never give 'em hell, I just tell the truth, and they think it's hell. ", '.'), (' --- Harry S Truman ', ' '), (' --- Harry S Truman ', '-'), (' --- Harry S Truman ', 'r')]: + t = re.sub(re.escape(ch) + "+", ch, s) + print(f"squeeze '{ch}'") + print(f"<<<{s}>>> length {len(s)}") + print(f"<<<{t}>>> length {len(t)}") diff --git a/tasks/digital-root/digital-root.om b/tasks/digital-root/digital-root.om new file mode 100644 index 0000000..45d6a56 --- /dev/null +++ b/tasks/digital-root/digital-root.om @@ -0,0 +1,68 @@ +note Rosetta Code task "Digital root": https://rosettacode.org/wiki/Digital_root +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text per ": additive persistence " +text dr ", digital root " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a ulong has at most 20 digits +fn dsum n ulong to ulong +s = ulong 0 +v = n +while gt v 0 bound 20 +s = add s rem v 10 +v = div v 10 +end +ret s +end +note bound: one digit sum takes any ulong below 181, the next below 19, then a single digit, so 4 rounds suffice +fn tell n ulong to void +v = n +p = ulong 0 +while gt v 9 bound 4 +v = dsum v +p = add p 1 +end +emitu n +emit per +emitu p +emit dr +emitu v +emit nl +end +fn main to int +tell 627615 +tell 39390 +tell 588225 +tell 393900588225 +tell 0 +tell 9 +tell 199 +tell 18446744073709551615 +ret 0 +end diff --git a/tasks/digital-root/expected.txt b/tasks/digital-root/expected.txt new file mode 100644 index 0000000..c93eebc --- /dev/null +++ b/tasks/digital-root/expected.txt @@ -0,0 +1,8 @@ +627615: additive persistence 2, digital root 9 +39390: additive persistence 2, digital root 6 +588225: additive persistence 2, digital root 3 +393900588225: additive persistence 2, digital root 9 +0: additive persistence 0, digital root 0 +9: additive persistence 0, digital root 9 +199: additive persistence 3, digital root 1 +18446744073709551615: additive persistence 3, digital root 6 diff --git a/tasks/digital-root/oracle.py b/tasks/digital-root/oracle.py new file mode 100644 index 0000000..753db27 --- /dev/null +++ b/tasks/digital-root/oracle.py @@ -0,0 +1,8 @@ +# Independent reference for "Digital root" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for n in [627615, 39390, 588225, 393900588225, 0, 9, 199, 18446744073709551615]: + v, p = n, 0 + while v > 9: + v = sum(map(int, str(v))) + p += 1 + print(f"{n}: additive persistence {p}, digital root {v}") diff --git a/tasks/digital-root/wolfram.txt b/tasks/digital-root/wolfram.txt new file mode 100644 index 0000000..327e639 --- /dev/null +++ b/tasks/digital-root/wolfram.txt @@ -0,0 +1,13 @@ +Wolfram cross-check of tasks/digital-root/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 2edc13971b865534887f66aebf2b91eba3cc26ccaddd413b9e54d362d62ed2e6. +agreement: YES +query: +f[n_] := Module[{l = NestWhileList[Total[IntegerDigits[#]] &, n, # > 9 &]}, + ToString[n] <> ": additive persistence " <> ToString[Length[l] - 1] <> ", digital root " <> ToString[Last[l]] <> "\n"]; +s = StringJoin[f /@ {627615, 39390, 588225, 393900588225, 0, 9, 199, 18446744073709551615}]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"2edc13971b865534887f66aebf2b91eba3cc26ccaddd413b9e54d362d62ed2e6", "627615: additive persistence 2, digital root 9\n39390: additive persistence 2, digital root 6\n588225: additive persistence 2, digital root 3\n393900588225: additive persistence 2, digital root 9\n0: additive persistence 0, digital root 0\n9: additive persistence 0, digital root 9\n199: additive persistence 3, digital root 1\n18446744073709551615: additive persistence 3, digital root 6\n"} diff --git a/tasks/dijkstras-algorithm/dijkstras-algorithm.om b/tasks/dijkstras-algorithm/dijkstras-algorithm.om new file mode 100644 index 0000000..27be2c7 --- /dev/null +++ b/tasks/dijkstras-algorithm/dijkstras-algorithm.om @@ -0,0 +1,143 @@ +note Rosetta Code task "Dijkstra's algorithm": https://rosettacode.org/wiki/Dijkstra's_algorithm +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tarrow " -> " +text tcost " cost " +text tpath " path" +text tnone " unreachable" +global wt long 36 +global dist long 6 +global prev ulong 6 +global done uchar 6 +global stk ulong 6 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note emitb writes the single byte c +fn emitb c ulong to void +d = new uchar 1 0 +put d 0 uchar c +emit d +end +note edge records a directed edge u -> v of weight w; vertices a..f are 0..5, weight 0 means no edge +fn edge u ulong v ulong w long to void +put wt add mul u 6 v w +end +note dijkstra from s: repeatedly settle the unsettled vertex of least known distance (-1 is infinity) +note and relax its out-edges; 6 rounds settle every reachable vertex +fn dijkstra s ulong to void +for i ulong 6 +put dist i -1 +put prev i 6 +put done i 0 +end +put dist s 0 +for r ulong 6 +u = ulong 6 +for i ulong 6 +if and eq get done i 0 ge get dist i 0 +if eq u 6 +u = i +else +if lt get dist i get dist u +u = i +end +end +end +end +if eq u 6 +ret +end +put done u 1 +for v ulong 6 +relax u v +end +end +end +fn relax u ulong v ulong to void +w = get wt add mul u 6 v +if gt w 0 +d = add get dist u w +if or lt get dist v 0 lt d get dist v +put dist v d +put prev v u +end +end +end +note route prints the path to t by following prev back to the source through a small stack +fn route t ulong to void +k = ulong 0 +v = t +while lt v 6 bound 6 +put stk k v +k = add k 1 +v = get prev v +end +emit tpath +for x ulong k +emit sp +emitb add 97 get stk sub sub k x 1 +end +end +fn main to int +edge 0 1 7 +edge 0 2 9 +edge 0 5 14 +edge 1 2 10 +edge 1 3 15 +edge 2 3 11 +edge 2 5 2 +edge 3 4 6 +edge 4 5 9 +dijkstra 0 +for t ulong 5 +v = add t 1 +emitb 97 +emit tarrow +emitb add 97 v +if lt get dist v 0 +emit tnone +else +emit tcost +emiti get dist v +route v +end +emit nl +end +ret 0 +end diff --git a/tasks/dijkstras-algorithm/expected.txt b/tasks/dijkstras-algorithm/expected.txt new file mode 100644 index 0000000..10723be --- /dev/null +++ b/tasks/dijkstras-algorithm/expected.txt @@ -0,0 +1,5 @@ +a -> b cost 7 path a b +a -> c cost 9 path a c +a -> d cost 20 path a c d +a -> e cost 26 path a c d e +a -> f cost 11 path a c f diff --git a/tasks/dijkstras-algorithm/oracle.py b/tasks/dijkstras-algorithm/oracle.py new file mode 100644 index 0000000..133c1bc --- /dev/null +++ b/tasks/dijkstras-algorithm/oracle.py @@ -0,0 +1,24 @@ +# Independent reference for "Dijkstra's algorithm" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def simple_paths(adj, s, t, seen=()): + if s == t: + yield (s,) + return + for v in adj.get(s, {}): + if v not in seen and v != s: + for p in simple_paths(adj, v, t, seen + (s,)): + yield (s,) + p +def best(adj, s, t): + cands = sorted((sum(adj[p[i]][p[i + 1]] for i in range(len(p) - 1)), p) for p in simple_paths(adj, s, t)) + if not cands: + return None + assert len(cands) == 1 or cands[0][0] < cands[1][0], "shortest path not unique" + return cands[0] +# checked by enumerating every simple path, not by a priority queue +adj = {"a": {"b": 7, "c": 9, "f": 14}, "b": {"c": 10, "d": 15}, "c": {"d": 11, "f": 2}, "d": {"e": 6}, "e": {"f": 9}} +for t in "bcdef": + r = best(adj, "a", t) + if r is None: + print(f"a -> {t} unreachable") + else: + print(f"a -> {t} cost {r[0]} path " + " ".join(r[1])) diff --git a/tasks/dutch-national-flag-problem/dutch-national-flag-problem.om b/tasks/dutch-national-flag-problem/dutch-national-flag-problem.om new file mode 100644 index 0000000..4804c91 --- /dev/null +++ b/tasks/dutch-national-flag-problem/dutch-national-flag-problem.om @@ -0,0 +1,131 @@ +note Rosetta Code task "Dutch national flag problem": https://rosettacode.org/wiki/Dutch_national_flag_problem +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tred "red" +text twhite "white" +text tblue "blue" +text tok "in flag order: " +text tyes "yes\n" +text tno "no\n" +global vals long 16 +global seed ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note rnd is Knuth's MMIX linear congruential generator, x = x*6364136223846793005 + 1442695040888963407 +note mod 2^64 (ulong arithmetic wraps), starting from seed 0; it returns the top 31 bits, x >> 33 +fn rnd to ulong +x = add mul get seed 0 6364136223846793005 1442695040888963407 +put seed 0 x +ret shr x 33 +end +fn ball c long to void +if eq c 0 +emit tred +end +if eq c 1 +emit twhite +end +if eq c 2 +emit tblue +end +end +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +ball get vals i +end +emit nl +end +fn inorder n ulong to bool +for i ulong sub n 1 +if gt get vals i get vals add i 1 +ret false +end +end +ret true +end +fn swap i ulong j ulong to void +t = get vals i +put vals i get vals j +put vals j t +end +note Dijkstra's three-way partition: [0,lo) red, [lo,mid) white, (hi,n) blue; every pass moves mid up +note or hi down, so n passes suffice +fn flag n ulong to void +lo = ulong 0 +mid = ulong 0 +hi = n +while lt mid hi bound 16 +c = get vals mid +if eq c 0 +swap lo mid +lo = add lo 1 +mid = add mid 1 +else +if eq c 1 +mid = add mid 1 +else +hi = sub hi 1 +swap mid hi +end +end +end +end +note draws 12 random balls (0 red, 1 white, 2 blue), drawing again while they happen to be in order; +note with this generator the first draw is already out of order, and 100 draws bound it +fn main to int +n = ulong 12 +while true bound 100 +for i ulong n +put vals i long rem rnd 3 +end +if not inorder n +break +end +end +show n +emit tok +if inorder n +emit tyes +else +emit tno +end +flag n +show n +emit tok +if inorder n +emit tyes +else +emit tno +end +ret 0 +end diff --git a/tasks/dutch-national-flag-problem/expected.txt b/tasks/dutch-national-flag-problem/expected.txt new file mode 100644 index 0000000..3079fcf --- /dev/null +++ b/tasks/dutch-national-flag-problem/expected.txt @@ -0,0 +1,4 @@ +blue red white white blue blue blue blue red red white red +in flag order: no +red red red red white white white blue blue blue blue blue +in flag order: yes diff --git a/tasks/dutch-national-flag-problem/oracle.py b/tasks/dutch-national-flag-problem/oracle.py new file mode 100644 index 0000000..83c0b2d --- /dev/null +++ b/tasks/dutch-national-flag-problem/oracle.py @@ -0,0 +1,16 @@ +# Independent reference for "Dutch national flag problem" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +M = 2 ** 64 +x = 0 +def rnd(): + global x + x = (x * 6364136223846793005 + 1442695040888963407) % M + return x >> 33 +names = ["red", "white", "blue"] +while True: + balls = [rnd() % 3 for _ in range(12)] + if balls != sorted(balls): + break +for b in (balls, sorted(balls)): + print(*(names[c] for c in b)) + print("in flag order:", "yes" if b == sorted(b) else "no") diff --git a/tasks/elementary-cellular-automaton/elementary-cellular-automaton.om b/tasks/elementary-cellular-automaton/elementary-cellular-automaton.om new file mode 100644 index 0000000..deb54a1 --- /dev/null +++ b/tasks/elementary-cellular-automaton/elementary-cellular-automaton.om @@ -0,0 +1,81 @@ +note Rosetta Code task "Elementary cellular automaton": https://rosettacode.org/wiki/Elementary_cellular_automaton +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text trule "rule " +text tdot "." +text thash "#" +global cur uchar 32 +global nxt uchar 32 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note 31 cells on a ring; a cell's next state is bit (4*left + 2*self + right) of the rule number +fn line to void +for i ulong 31 +if eq get cur i 1 +emit thash +else +emit tdot +end +end +emit nl +end +fn step rule ulong to void +for i ulong 31 +l = ulong get cur rem add i 30 31 +c = ulong get cur i +r = ulong get cur rem add i 1 31 +k = add add mul l 4 mul c 2 r +put nxt i uchar and shr rule k 1 +end +for i ulong 31 +put cur i get nxt i +end +end +fn run rule ulong to void +emit trule +emitu rule +emit nl +for i ulong 31 +put cur i 0 +end +put cur 15 1 +for g ulong 16 +line +step rule +end +end +fn main to int +run 90 +emit nl +run 30 +emit nl +run 110 +ret 0 +end diff --git a/tasks/elementary-cellular-automaton/expected.txt b/tasks/elementary-cellular-automaton/expected.txt new file mode 100644 index 0000000..f66fe26 --- /dev/null +++ b/tasks/elementary-cellular-automaton/expected.txt @@ -0,0 +1,53 @@ +rule 90 +...............#............... +..............#.#.............. +.............#...#............. +............#.#.#.#............ +...........#.......#........... +..........#.#.....#.#.......... +.........#...#...#...#......... +........#.#.#.#.#.#.#.#........ +.......#...............#....... +......#.#.............#.#...... +.....#...#...........#...#..... +....#.#.#.#.........#.#.#.#.... +...#.......#.......#.......#... +..#.#.....#.#.....#.#.....#.#.. +.#...#...#...#...#...#...#...#. +#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.# + +rule 30 +...............#............... +..............###.............. +.............##..#............. +............##.####............ +...........##..#...#........... +..........##.####.###.......... +.........##..#....#..#......... +........##.####..######........ +.......##..#...###.....#....... +......##.####.##..#...###...... +.....##..#....#.####.##..#..... +....##.####..##.#....#.####.... +...##..#...###..##..##.#...#... +..##.####.##..###.###..##.###.. +.##..#....#.###...#..###..#..#. +##.####..##.#..#.#####..####### + +rule 110 +...............#............... +..............##............... +.............###............... +............##.#............... +...........#####............... +..........##...#............... +.........###..##............... +........##.#.###............... +.......#######.#............... +......##.....###............... +.....###....##.#............... +....##.#...#####............... +...#####..##...#............... +..##...#.###..##............... +.###..####.#.###............... +##.#.##..#####.#............... diff --git a/tasks/elementary-cellular-automaton/oracle.py b/tasks/elementary-cellular-automaton/oracle.py new file mode 100644 index 0000000..4975031 --- /dev/null +++ b/tasks/elementary-cellular-automaton/oracle.py @@ -0,0 +1,15 @@ +# Independent reference for "Elementary cellular automaton" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def run(rule): + print("rule", rule) + # the rule table as a dict from neighbourhood strings to the next cell + table = {f"{k:03b}": "1" if rule >> k & 1 else "0" for k in range(8)} + cells = "0" * 15 + "1" + "0" * 15 + for _ in range(16): + print(cells.replace("1", "#").replace("0", ".")) + ring = cells[-1] + cells + cells[0] + cells = "".join(table[ring[i:i + 3]] for i in range(31)) +for t, rule in enumerate((90, 30, 110)): + if t: + print() + run(rule) diff --git a/tasks/emirp-primes/emirp-primes.om b/tasks/emirp-primes/emirp-primes.om new file mode 100644 index 0000000..91a4bdd --- /dev/null +++ b/tasks/emirp-primes/emirp-primes.om @@ -0,0 +1,110 @@ +note Rosetta Code task "Emirp primes": https://rosettacode.org/wiki/Emirp_primes +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text first "first 20 emirps:" +text betw "emirps between 7700 and 8000:" +text nth "the 10000th emirp: " +note cp i is 1 when i is composite; the sieve covers every number below 10^6, which holds each 6-digit reversal +global cp uchar 1000000 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note bound: i * i < 10^6 needs i < 1000; the multiples of one i below 10^6 are fewer than 500000 +fn sieve to void +put cp 0 1 +put cp 1 1 +i = ulong 2 +while lt mul i i 1000000 bound 1000 +if eq get cp i 0 +j = mul i i +while lt j 1000000 bound 999999 +put cp j 1 +j = add j i +end +end +i = add i 1 +end +end +note rev reverses the decimal digits of n; a ulong has at most 20 digits +fn rev n ulong to ulong +r = ulong 0 +v = n +while gt v 0 bound 20 +r = add mul r 10 rem v 10 +v = div v 10 +end +ret r +end +note an emirp is a prime whose reversal is a different prime; n stays below 10^6 here +fn emirp n ulong to bool +if ne get cp n 0 +ret false +end +r = rev n +if eq r n +ret false +end +ret eq get cp r 0 +end +fn main to int +sieve +emit first +n = ulong 0 +k = ulong 0 +while lt k 20 bound 1000 +n = add n 1 +if emirp n +k = add k 1 +emit sp +emitu n +end +end +emit nl +emit betw +n = ulong 7700 +while le n 8000 bound 1000 +if emirp n +emit sp +emitu n +end +n = add n 1 +end +emit nl +n = ulong 0 +k = ulong 0 +while lt k 10000 bound 999999 +n = add n 1 +if emirp n +k = add k 1 +end +end +emit nth +emitu n +emit nl +ret 0 +end diff --git a/tasks/emirp-primes/expected.txt b/tasks/emirp-primes/expected.txt new file mode 100644 index 0000000..0aedf6a --- /dev/null +++ b/tasks/emirp-primes/expected.txt @@ -0,0 +1,3 @@ +first 20 emirps: 13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389 +emirps between 7700 and 8000: 7717 7757 7817 7841 7867 7879 7901 7927 7949 7951 7963 +the 10000th emirp: 948349 diff --git a/tasks/emirp-primes/oracle.py b/tasks/emirp-primes/oracle.py new file mode 100644 index 0000000..d47eb49 --- /dev/null +++ b/tasks/emirp-primes/oracle.py @@ -0,0 +1,15 @@ +# Independent reference for "Emirp primes" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +N = 10**6 +s = bytearray([1]) * N +s[0] = s[1] = 0 +for i in range(2, 1000): + if s[i]: + s[i * i::i] = bytes(len(range(i * i, N, i))) +def emirp(n): + r = int(str(n)[::-1]) + return s[n] and r != n and s[r] +em = [n for n in range(N) if emirp(n)] +print("first 20 emirps: " + " ".join(map(str, em[:20]))) +print("emirps between 7700 and 8000: " + " ".join(str(n) for n in em if 7700 <= n <= 8000)) +print(f"the 10000th emirp: {em[9999]}") diff --git a/tasks/emirp-primes/wolfram.txt b/tasks/emirp-primes/wolfram.txt new file mode 100644 index 0000000..026a131 --- /dev/null +++ b/tasks/emirp-primes/wolfram.txt @@ -0,0 +1,19 @@ +Wolfram cross-check of tasks/emirp-primes/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 8e0832c232117be0f98955cc2322280dd25fab05c95b4b86bfb1ca598838a4d1. +agreement: YES +query: +L[x_] := StringRiffle[ToString /@ x, " "]; +emirpQ[n_] := With[{r = IntegerReverse[n]}, PrimeQ[n] && r != n && PrimeQ[r]]; +em = Select[Range[10^6], emirpQ]; +s = "first 20 emirps: " <> L[Take[em, 20]] <> "\n" <> + "emirps between 7700 and 8000: " <> L[Select[Range[7700, 8000], emirpQ]] <> "\n" <> + "the 10000th emirp: " <> ToString[em[[10000]]] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Symbol::undefined2: Warning: Global symbols "L, L, L" are undefined. +General::messages: Messages were generated which may indicate errors. + +Out[1]= {"8e0832c232117be0f98955cc2322280dd25fab05c95b4b86bfb1ca598838a4d1", "first 20 emirps: 13 17 31 37 71 73 79 97 107 113 149 157 167 179 199 311 337 347 359 389\nemirps between 7700 and 8000: 7717 7757 7817 7841 7867 7879 7901 7927 7949 7951 7963\nthe 10000th emirp: 948349\n"} diff --git a/tasks/empty-string/empty-string.om b/tasks/empty-string/empty-string.om new file mode 100644 index 0000000..7286f0e --- /dev/null +++ b/tasks/empty-string/empty-string.om @@ -0,0 +1,45 @@ +note Rosetta Code task "Empty string": https://rosettacode.org/wiki/Empty_string +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text tempty "s is empty\n" +text tfull "s is not empty: " +text word "abc" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note copyto copies s into d at offset off and returns the offset after it (past the end traps 3) +fn copyto d edit uchar * off ulong s view uchar * to ulong +for i ulong len s +put d add off i get s i +end +ret add off len s +end +note a string here is a buffer b and its length n; the empty string is length 0 +fn check b view uchar * to void +if eq len b 0 +emit tempty +else +emit tfull +emit b +emit nl +end +end +fn main to int +b = new uchar 16 0 +n = ulong 0 +check part b 0 n +n = copyto b 0 word +check part b 0 n +n = 0 +check part b 0 n +ret 0 +end diff --git a/tasks/empty-string/expected.txt b/tasks/empty-string/expected.txt new file mode 100644 index 0000000..8f0cc44 --- /dev/null +++ b/tasks/empty-string/expected.txt @@ -0,0 +1,3 @@ +s is empty +s is not empty: abc +s is empty diff --git a/tasks/empty-string/oracle.py b/tasks/empty-string/oracle.py new file mode 100644 index 0000000..e8c072d --- /dev/null +++ b/tasks/empty-string/oracle.py @@ -0,0 +1,7 @@ +# Independent reference for "Empty string" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for s in ["", "abc", ""]: + if s == "": + print("s is empty") + else: + print("s is not empty: " + s) diff --git a/tasks/equilibrium-index/equilibrium-index.om b/tasks/equilibrium-index/equilibrium-index.om new file mode 100644 index 0000000..e58ca64 --- /dev/null +++ b/tasks/equilibrium-index/equilibrium-index.om @@ -0,0 +1,122 @@ +note Rosetta Code task "Equilibrium index": https://rosettacode.org/wiki/Equilibrium_index +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text d1 "-7 1 5 2 -4 3 0" +text d2 "2 4 6" +text d3 "2 9 2" +text d4 "1 -1 1 -1 1 -1 1" +text tidx "equilibrium indices:" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note eqidx keeps the sum to the left and the sum to the right of i in one pass after the total +fn eqidx n ulong to void +tot = long 0 +for i ulong n +tot = add tot get vals i +end +lsum = long 0 +emit tidx +for i ulong n +rsum = sub sub tot lsum get vals i +if eq lsum rsum +emit sp +emitu i +end +lsum = add lsum get vals i +end +emit nl +end +fn main to int +n = parse d1 +show n +eqidx n +n = parse d2 +show n +eqidx n +n = parse d3 +show n +eqidx n +n = parse d4 +show n +eqidx n +ret 0 +end diff --git a/tasks/equilibrium-index/expected.txt b/tasks/equilibrium-index/expected.txt new file mode 100644 index 0000000..b903a0b --- /dev/null +++ b/tasks/equilibrium-index/expected.txt @@ -0,0 +1,8 @@ +-7 1 5 2 -4 3 0 +equilibrium indices: 3 6 +2 4 6 +equilibrium indices: +2 9 2 +equilibrium indices: 1 +1 -1 1 -1 1 -1 1 +equilibrium indices: 0 1 2 3 4 5 6 diff --git a/tasks/equilibrium-index/oracle.py b/tasks/equilibrium-index/oracle.py new file mode 100644 index 0000000..bf75dae --- /dev/null +++ b/tasks/equilibrium-index/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Equilibrium index" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for a in ([-7, 1, 5, 2, -4, 3, 0], [2, 4, 6], [2, 9, 2], [1, -1, 1, -1, 1, -1, 1]): + print(*a) + print("equilibrium indices:" + "".join(f" {i}" for i in range(len(a)) if sum(a[:i]) == sum(a[i + 1:]))) diff --git a/tasks/ethiopian-multiplication/ethiopian-multiplication.om b/tasks/ethiopian-multiplication/ethiopian-multiplication.om new file mode 100644 index 0000000..3f75622 --- /dev/null +++ b/tasks/ethiopian-multiplication/ethiopian-multiplication.om @@ -0,0 +1,75 @@ +note Rosetta Code task "Ethiopian multiplication": https://rosettacode.org/wiki/Ethiopian_multiplication +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text kept " KEPT\n" +text struck " STRUCK\n" +text tx " x " +text teq " = " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn halve n ulong to ulong +ret shr n 1 +end +fn double n ulong to ulong +ret shl n 1 +end +fn iseven n ulong to bool +ret eq and n 1 0 +end +note left column halves to 1, right column doubles; rows with an even left number are struck out and +note the kept right numbers add up to the product +fn ethiop a ulong b ulong to ulong +s = ulong 0 +l = a +r = b +while ge l 1 bound 64 +emitu l +emit sp +emitu r +if iseven l +emit struck +else +emit kept +s = add s r +end +l = halve l +r = double r +end +ret s +end +fn main to int +p = ethiop 17 34 +emitu 17 +emit tx +emitu 34 +emit teq +emitu p +emit nl +ret 0 +end diff --git a/tasks/ethiopian-multiplication/expected.txt b/tasks/ethiopian-multiplication/expected.txt new file mode 100644 index 0000000..781ec35 --- /dev/null +++ b/tasks/ethiopian-multiplication/expected.txt @@ -0,0 +1,6 @@ +17 34 KEPT +8 68 STRUCK +4 136 STRUCK +2 272 STRUCK +1 544 KEPT +17 x 34 = 578 diff --git a/tasks/ethiopian-multiplication/oracle.py b/tasks/ethiopian-multiplication/oracle.py new file mode 100644 index 0000000..08fed07 --- /dev/null +++ b/tasks/ethiopian-multiplication/oracle.py @@ -0,0 +1,11 @@ +# Independent reference for "Ethiopian multiplication" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a, b, s = 17, 34, 0 +l, r = a, b +while l >= 1: + print(l, r, "STRUCK" if l % 2 == 0 else "KEPT") + if l % 2: + s += r + l, r = l // 2, r * 2 +assert s == a * b +print(f"{a} x {b} = {s}") diff --git a/tasks/evaluate-binomial-coefficients/evaluate-binomial-coefficients.om b/tasks/evaluate-binomial-coefficients/evaluate-binomial-coefficients.om new file mode 100644 index 0000000..8210a1b --- /dev/null +++ b/tasks/evaluate-binomial-coefficients/evaluate-binomial-coefficients.om @@ -0,0 +1,72 @@ +note Rosetta Code task "Evaluate binomial coefficients": https://rosettacode.org/wiki/Evaluate_binomial_coefficients +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text cc "C(" +text cm ", " +text eqs ") = " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn gcd a ulong b ulong to ulong +x = a +y = b +while ne y 0 bound 128 +t = rem x y +x = y +y = t +end +ret x +end +note r runs through C(n-k+i, i) for i = 1..k; with g = gcd(r, i), i / g divides n - k + i, so +note r / g * ((n-k+i) / (i / g)) is exact and never exceeds the final value, which must fit 64 bits +fn binom n ulong k ulong to ulong +r = ulong 1 +for j ulong k +i = add j 1 +g = gcd r i +r = mul div r g div add sub n k i div i g +end +ret r +end +fn tell n ulong k ulong to void +emit cc +emitu n +emit cm +emitu k +emit eqs +emitu binom n k +emit nl +end +fn main to int +tell 5 3 +tell 10 0 +tell 10 10 +tell 33 17 +tell 60 30 +tell 67 33 +ret 0 +end diff --git a/tasks/evaluate-binomial-coefficients/expected.txt b/tasks/evaluate-binomial-coefficients/expected.txt new file mode 100644 index 0000000..231d585 --- /dev/null +++ b/tasks/evaluate-binomial-coefficients/expected.txt @@ -0,0 +1,6 @@ +C(5, 3) = 10 +C(10, 0) = 1 +C(10, 10) = 1 +C(33, 17) = 1166803110 +C(60, 30) = 118264581564861424 +C(67, 33) = 14226520737620288370 diff --git a/tasks/evaluate-binomial-coefficients/oracle.py b/tasks/evaluate-binomial-coefficients/oracle.py new file mode 100644 index 0000000..d414017 --- /dev/null +++ b/tasks/evaluate-binomial-coefficients/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Evaluate binomial coefficients" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import math +for n, k in [(5, 3), (10, 0), (10, 10), (33, 17), (60, 30), (67, 33)]: + print(f"C({n}, {k}) = {math.factorial(n) // (math.factorial(k) * math.factorial(n - k))}") diff --git a/tasks/evaluate-binomial-coefficients/wolfram.txt b/tasks/evaluate-binomial-coefficients/wolfram.txt new file mode 100644 index 0000000..0866b16 --- /dev/null +++ b/tasks/evaluate-binomial-coefficients/wolfram.txt @@ -0,0 +1,12 @@ +Wolfram cross-check of tasks/evaluate-binomial-coefficients/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = c5472712109e3bfb08ee15772b58b9cfa1804c2cefa08124e8fa22e731bedf3f. +agreement: YES +query: +s = StringJoin[("C(" <> ToString[#[[1]]] <> ", " <> ToString[#[[2]]] <> ") = " <> ToString[Binomial @@ #] <> "\n") & /@ + {{5, 3}, {10, 0}, {10, 10}, {33, 17}, {60, 30}, {67, 33}}]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"c5472712109e3bfb08ee15772b58b9cfa1804c2cefa08124e8fa22e731bedf3f", "C(5, 3) = 10\nC(10, 0) = 1\nC(10, 10) = 1\nC(33, 17) = 1166803110\nC(60, 30) = 118264581564861424\nC(67, 33) = 14226520737620288370\n"} diff --git a/tasks/extensible-prime-generator/expected.txt b/tasks/extensible-prime-generator/expected.txt new file mode 100644 index 0000000..51407ca --- /dev/null +++ b/tasks/extensible-prime-generator/expected.txt @@ -0,0 +1,4 @@ +first 20 primes: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 +primes between 100 and 150: 101 103 107 109 113 127 131 137 139 149 +number of primes between 7700 and 8000: 30 +the 10000th prime: 104729 diff --git a/tasks/extensible-prime-generator/extensible-prime-generator.om b/tasks/extensible-prime-generator/extensible-prime-generator.om new file mode 100644 index 0000000..a4514b7 --- /dev/null +++ b/tasks/extensible-prime-generator/extensible-prime-generator.om @@ -0,0 +1,104 @@ +note Rosetta Code task "Extensible prime generator": https://rosettacode.org/wiki/Extensible_prime_generator +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text first "first 20 primes:" +text betw "primes between 100 and 150:" +text cnt "number of primes between 7700 and 8000: " +text nth "the 10000th prime: " +note the generator's state: pr holds every prime found so far, st 0 their count, st 1 the last candidate +note pr has room for 20000 primes; asking for more traps 3 at put, never a wild write +global pr ulong 20000 +global st ulong 2 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note nextp extends the stored list by one prime: the next candidate is tried against stored primes p while p * p <= c +note bound: a prime gap below 2^32 is far below 999999 candidates; the stored primes checked are at most st 0 +fn nextp to ulong +c = get st 1 +while true bound 999999 +c = add c 1 +ok = true +j = ulong 0 +while lt j get st 0 bound 20000 +p = get pr j +if gt mul p p c +break +end +if eq rem c p 0 +ok = false +break +end +j = add j 1 +end +if ok +put pr get st 0 c +put st 0 add get st 0 1 +put st 1 c +ret c +end +end +trap 4 +end +fn main to int +put st 1 1 +emit first +for i ulong 20 +emit sp +emitu nextp +end +emit nl +emit betw +p = nextp +while le p 150 bound 1000 +if ge p 100 +emit sp +emitu p +end +p = nextp +end +emit nl +while lt p 7700 bound 10000 +p = nextp +end +n = ulong 0 +while le p 8000 bound 10000 +n = add n 1 +p = nextp +end +emit cnt +emitu n +emit nl +while lt get st 0 10000 bound 10000 +p = nextp +end +emit nth +emitu get pr 9999 +emit nl +ret 0 +end diff --git a/tasks/extensible-prime-generator/oracle.py b/tasks/extensible-prime-generator/oracle.py new file mode 100644 index 0000000..33e3497 --- /dev/null +++ b/tasks/extensible-prime-generator/oracle.py @@ -0,0 +1,18 @@ +# Independent reference for "Extensible prime generator" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import itertools +def primes(): + # incremental sieve: each composite maps to the primes that will strike it + d = {} + for q in itertools.count(2): + if q not in d: + yield q + d[q * q] = [q] + else: + for p in d.pop(q): + d.setdefault(p + q, []).append(p) +ps = list(itertools.islice(primes(), 10000)) +print("first 20 primes: " + " ".join(map(str, ps[:20]))) +print("primes between 100 and 150: " + " ".join(str(p) for p in ps if 100 <= p <= 150)) +print(f"number of primes between 7700 and 8000: {sum(1 for p in ps if 7700 <= p <= 8000)}") +print(f"the 10000th prime: {ps[9999]}") diff --git a/tasks/extensible-prime-generator/wolfram.txt b/tasks/extensible-prime-generator/wolfram.txt new file mode 100644 index 0000000..e6204d2 --- /dev/null +++ b/tasks/extensible-prime-generator/wolfram.txt @@ -0,0 +1,18 @@ +Wolfram cross-check of tasks/extensible-prime-generator/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = efa44b38eb3e297cba3c30aba20b19c196281d6c54a42957688c0c446bc870f2. +agreement: YES +query: +L[x_] := StringRiffle[ToString /@ x, " "]; +s = "first 20 primes: " <> L[Prime[Range[20]]] <> "\n" <> + "primes between 100 and 150: " <> L[Select[Range[100, 150], PrimeQ]] <> "\n" <> + "number of primes between 7700 and 8000: " <> ToString[PrimePi[8000] - PrimePi[7699]] <> "\n" <> + "the 10000th prime: " <> ToString[Prime[10000]] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Symbol::undefined2: Warning: Global symbols "L, L, L" are undefined. +General::messages: Messages were generated which may indicate errors. + +Out[1]= {"efa44b38eb3e297cba3c30aba20b19c196281d6c54a42957688c0c446bc870f2", "first 20 primes: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71\nprimes between 100 and 150: 101 103 107 109 113 127 131 137 139 149\nnumber of primes between 7700 and 8000: 30\nthe 10000th prime: 104729\n"} diff --git a/tasks/factors-of-an-integer/expected.txt b/tasks/factors-of-an-integer/expected.txt new file mode 100644 index 0000000..83d4ceb --- /dev/null +++ b/tasks/factors-of-an-integer/expected.txt @@ -0,0 +1,7 @@ +factors of 1: 1 +factors of 45: 1 3 5 9 15 45 +factors of 53: 1 53 +factors of 64: 1 2 4 8 16 32 64 +factors of 720: 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 30 36 40 45 48 60 72 80 90 120 144 180 240 360 720 +factors of 1000000: 1 2 4 5 8 10 16 20 25 32 40 50 64 80 100 125 160 200 250 320 400 500 625 800 1000 1250 1600 2000 2500 3125 4000 5000 6250 8000 10000 12500 15625 20000 25000 31250 40000 50000 62500 100000 125000 200000 250000 500000 1000000 +factors of 600851475143: 1 71 839 1471 6857 59569 104441 486847 1234169 5753023 10086647 87625999 408464633 716151937 8462696833 600851475143 diff --git a/tasks/factors-of-an-integer/factors-of-an-integer.om b/tasks/factors-of-an-integer/factors-of-an-integer.om new file mode 100644 index 0000000..885a4ab --- /dev/null +++ b/tasks/factors-of-an-integer/factors-of-an-integer.om @@ -0,0 +1,72 @@ +note Rosetta Code task "Factors of an integer": https://rosettacode.org/wiki/Factors_of_an_integer +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text pre "factors of " +text col ":" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note divisors d with d * d <= n come out ascending; their partners n / d, read back in reverse, finish the list +note bound: the largest n here is 600851475143, so d stays below 775147, and no n has more than 64 small divisors +fn tell n ulong to void +lo = new ulong 64 0 +k = ulong 0 +d = ulong 1 +while le mul d d n bound 999999 +if eq rem n d 0 +put lo k d +k = add k 1 +end +d = add d 1 +end +emit pre +emitu n +emit col +for i ulong k +emit sp +emitu get lo i +end +for i ulong k +e = get lo sub sub k 1 i +if ne mul e e n +emit sp +emitu div n e +end +end +emit nl +end +fn main to int +tell 1 +tell 45 +tell 53 +tell 64 +tell 720 +tell 1000000 +tell 600851475143 +ret 0 +end diff --git a/tasks/factors-of-an-integer/oracle.py b/tasks/factors-of-an-integer/oracle.py new file mode 100644 index 0000000..ec12cf0 --- /dev/null +++ b/tasks/factors-of-an-integer/oracle.py @@ -0,0 +1,18 @@ +# Independent reference for "Factors of an integer" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def prime_powers(n): + f, p = {}, 2 + while p * p <= n: + while n % p == 0: + f[p] = f.get(p, 0) + 1 + n //= p + p += 1 + if n > 1: + f[n] = f.get(n, 0) + 1 + return f +# every divisor is a product of prime powers p^e with 0 <= e <= the exponent of p in n +for n in [1, 45, 53, 64, 720, 1000000, 600851475143]: + divs = [1] + for p, e in prime_powers(n).items(): + divs = [d * p ** k for d in divs for k in range(e + 1)] + print(f"factors of {n}: " + " ".join(map(str, sorted(divs)))) diff --git a/tasks/factors-of-an-integer/wolfram.txt b/tasks/factors-of-an-integer/wolfram.txt new file mode 100644 index 0000000..c63b894 --- /dev/null +++ b/tasks/factors-of-an-integer/wolfram.txt @@ -0,0 +1,11 @@ +Wolfram cross-check of tasks/factors-of-an-integer/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 10664baf5b20e337ad0dd1baedd49c330821bfa509e749ca6cc702100709284a. +agreement: YES +query: +s = StringJoin[("factors of " <> ToString[#] <> ": " <> StringRiffle[ToString /@ Divisors[#], " "] <> "\n") & /@ {1, 45, 53, 64, 720, 1000000, 600851475143}]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"10664baf5b20e337ad0dd1baedd49c330821bfa509e749ca6cc702100709284a", "factors of 1: 1\nfactors of 45: 1 3 5 9 15 45\nfactors of 53: 1 53\nfactors of 64: 1 2 4 8 16 32 64\nfactors of 720: 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 30 36 40 45 48 60 72 80 90 120 144 180 240 360 720\nfactors of 1000000: 1 2 4 5 8 10 16 20 25 32 40 50 64 80 100 125 160 200 250 320 400 500 625 800 1000 1250 1600 2000 2500 3125 4000 5000 6250 8000 10000 12500 15625 20000 25000 31250 40000 50000 62500 100000 125000 200000 250000 500000 1000000\nfactors of 600851475143: 1 71 839 1471 6857 59569 104441 486847 1234169 5753023 10086647 87625999 408464633 716151937 8462696833 600851475143\n"} diff --git a/tasks/fibonacci-n-step-number-sequences/expected.txt b/tasks/fibonacci-n-step-number-sequences/expected.txt new file mode 100644 index 0000000..9f6d9a1 --- /dev/null +++ b/tasks/fibonacci-n-step-number-sequences/expected.txt @@ -0,0 +1,10 @@ +fibonacci: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 +tribonacci: 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 +tetranacci: 1 1 2 4 8 15 29 56 108 208 401 773 1490 2872 5536 +pentanacci: 1 1 2 4 8 16 31 61 120 236 464 912 1793 3525 6930 +hexanacci: 1 1 2 4 8 16 32 63 125 248 492 976 1936 3840 7617 +heptanacci: 1 1 2 4 8 16 32 64 127 253 504 1004 2000 3984 7936 +octonacci: 1 1 2 4 8 16 32 64 128 255 509 1016 2028 4048 8080 +nonanacci: 1 1 2 4 8 16 32 64 128 256 511 1021 2040 4076 8144 +decanacci: 1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172 +lucas: 2 1 3 4 7 11 18 29 47 76 123 199 322 521 843 diff --git a/tasks/fibonacci-n-step-number-sequences/fibonacci-n-step-number-sequences.om b/tasks/fibonacci-n-step-number-sequences/fibonacci-n-step-number-sequences.om new file mode 100644 index 0000000..6384f20 --- /dev/null +++ b/tasks/fibonacci-n-step-number-sequences/fibonacci-n-step-number-sequences.om @@ -0,0 +1,88 @@ +note Rosetta Code task "Fibonacci n-step number sequences": https://rosettacode.org/wiki/Fibonacci_n-step_number_sequences +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text t2 "fibonacci: " +text t3 "tribonacci:" +text t4 "tetranacci:" +text t5 "pentanacci:" +text t6 "hexanacci: " +text t7 "heptanacci:" +text t8 "octonacci: " +text t9 "nonanacci: " +text t10 "decanacci: " +text tl "lucas: " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note the n-step sequence starts 1, 1, 2, 4, ..., 2^(n-2) (the n-step seeds) and then each term sums the +note previous n; the Lucas numbers are the 2-step sequence seeded 2, 1. 15 terms, all below 2^15 * 15 +fn seq n ulong s0 ulong s1 ulong to void +a = new ulong 15 0 +put a 0 s0 +put a 1 s1 +for i ulong 13 +k = add i 2 +s = ulong 0 +if le k sub n 1 +s = shl ulong 1 sub k 1 +else +for j ulong n +s = add s get a sub sub k 1 j +end +end +put a k s +end +for i ulong 15 +emit sp +emitu get a i +end +emit nl +end +fn main to int +emit t2 +seq 2 1 1 +emit t3 +seq 3 1 1 +emit t4 +seq 4 1 1 +emit t5 +seq 5 1 1 +emit t6 +seq 6 1 1 +emit t7 +seq 7 1 1 +emit t8 +seq 8 1 1 +emit t9 +seq 9 1 1 +emit t10 +seq 10 1 1 +emit tl +seq 2 2 1 +ret 0 +end diff --git a/tasks/fibonacci-n-step-number-sequences/oracle.py b/tasks/fibonacci-n-step-number-sequences/oracle.py new file mode 100644 index 0000000..cecef81 --- /dev/null +++ b/tasks/fibonacci-n-step-number-sequences/oracle.py @@ -0,0 +1,16 @@ +# Independent reference for "Fibonacci n-step number sequences" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +names = {2: "fibonacci: ", 3: "tribonacci:", 4: "tetranacci:", 5: "pentanacci:", 6: "hexanacci: ", + 7: "heptanacci:", 8: "octonacci: ", 9: "nonanacci: ", 10: "decanacci: "} +def nstep(seed, n, count): + a = list(seed) + while len(a) < count: + a.append(sum(a[-n:])) + return a[:count] +# the n-step sequence is seeded with the first n terms of the (n-1)-step one, starting from 1, 1 +seed = [1, 1] +for n in range(2, 11): + if n > 2: + seed = nstep(seed, n - 1, n) + print(names[n] + "".join(f" {v}" for v in nstep(seed, n, 15))) +print("lucas: " + "".join(f" {v}" for v in nstep([2, 1], 2, 15))) diff --git a/tasks/fibonacci-n-step-number-sequences/wolfram.txt b/tasks/fibonacci-n-step-number-sequences/wolfram.txt new file mode 100644 index 0000000..c0d13bd --- /dev/null +++ b/tasks/fibonacci-n-step-number-sequences/wolfram.txt @@ -0,0 +1,14 @@ +Wolfram cross-check of tasks/fibonacci-n-step-number-sequences/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 4d3d5ca595d85f522af8aa010cb7164dd8464c18592c8a01bc0cb6014698bb30. +agreement: YES +query: +nstep[n_] := LinearRecurrence[ConstantArray[1, n], Join[{1}, 2^Range[0, n - 2]], 15]; +names = {"fibonacci: ", "tribonacci:", "tetranacci:", "pentanacci:", "hexanacci: ", "heptanacci:", "octonacci: ", "nonanacci: ", "decanacci: "}; +s = StringJoin[Table[names[[n - 1]] <> StringJoin[(" " <> ToString[#]) & /@ nstep[n]] <> "\n", {n, 2, 10}]] <> + "lucas: " <> StringJoin[(" " <> ToString[#]) & /@ Table[LucasL[k], {k, 0, 14}]] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"4d3d5ca595d85f522af8aa010cb7164dd8464c18592c8a01bc0cb6014698bb30", "fibonacci: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610\ntribonacci: 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136\ntetranacci: 1 1 2 4 8 15 29 56 108 208 401 773 1490 2872 5536\npentanacci: 1 1 2 4 8 16 31 61 120 236 464 912 1793 3525 6930\nhexanacci: 1 1 2 4 8 16 32 63 125 248 492 976 1936 3840 7617\nheptanacci: 1 1 2 4 8 16 32 64 127 253 504 1004 2000 3984 7936\noctonacci: 1 1 2 4 8 16 32 64 128 255 509 1016 2028 4048 8080\nnonanacci: 1 1 2 4 8 16 32 64 128 256 511 1021 2040 4076 8144\ndecanacci: 1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172\nlucas: 2 1 3 4 7 11 18 29 47 76 123 199 322 521 843\n"} diff --git a/tasks/floyd-warshall-algorithm/expected.txt b/tasks/floyd-warshall-algorithm/expected.txt new file mode 100644 index 0000000..4e0b456 --- /dev/null +++ b/tasks/floyd-warshall-algorithm/expected.txt @@ -0,0 +1,13 @@ +pair dist path +1 -> 2 -1 1 -> 3 -> 4 -> 2 +1 -> 3 -2 1 -> 3 +1 -> 4 0 1 -> 3 -> 4 +2 -> 1 4 2 -> 1 +2 -> 3 2 2 -> 1 -> 3 +2 -> 4 4 2 -> 1 -> 3 -> 4 +3 -> 1 5 3 -> 4 -> 2 -> 1 +3 -> 2 1 3 -> 4 -> 2 +3 -> 4 2 3 -> 4 +4 -> 1 3 4 -> 2 -> 1 +4 -> 2 -1 4 -> 2 +4 -> 3 1 4 -> 2 -> 1 -> 3 diff --git a/tasks/floyd-warshall-algorithm/floyd-warshall-algorithm.om b/tasks/floyd-warshall-algorithm/floyd-warshall-algorithm.om new file mode 100644 index 0000000..c0b965a --- /dev/null +++ b/tasks/floyd-warshall-algorithm/floyd-warshall-algorithm.om @@ -0,0 +1,134 @@ +note Rosetta Code task "Floyd-Warshall algorithm": https://rosettacode.org/wiki/Floyd-Warshall_algorithm +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text thead "pair dist path\n" +text tarrow " -> " +global dist long 16 +global nxt ulong 16 +global has uchar 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note emitwi writes a signed n right-aligned in w columns (w at most 20) +fn emitwi n long w ulong to void +d = new uchar 21 32 +i = ulong 21 +v = n +if lt n 0 +v = neg n +end +while true bound 20 +i = sub i 1 +put d i uchar add long 48 rem v 10 +v = div v 10 +if eq v 0 +break +end +end +if lt n 0 +i = sub i 1 +put d i 45 +end +if gt w sub 21 i +i = sub 21 w +end +emit part d i sub 21 i +end +note vertices 1..4 are stored as 0..3; has[i*4+j] marks a known distance, nxt the first hop +fn edge u ulong v ulong w long to void +i = add mul sub u 1 4 sub v 1 +put dist i w +put has i 1 +put nxt i sub v 1 +end +note try k as an intermediate between i and j +fn via i ulong k ulong j ulong to void +ik = add mul i 4 k +kj = add mul k 4 j +ij = add mul i 4 j +if and eq get has ik 1 eq get has kj 1 +d = add get dist ik get dist kj +if or eq get has ij 0 lt d get dist ij +put dist ij d +put has ij 1 +put nxt ij get nxt ik +end +end +end +note path follows the next-hop table from i to j; a shortest path visits at most the 4 vertices +fn path i ulong j ulong to void +emitu add i 1 +v = i +while ne v j bound 4 +v = get nxt add mul v 4 j +emit tarrow +emitu add v 1 +end +end +fn main to int +edge 1 3 -2 +edge 2 1 4 +edge 2 3 3 +edge 3 4 2 +edge 4 2 -1 +for k ulong 4 +for i ulong 4 +for j ulong 4 +via i k j +end +end +end +emit thead +for i ulong 4 +for j ulong 4 +if ne i j +emitu add i 1 +emit tarrow +emitu add j 1 +emitwi get dist add mul i 4 j 6 +emit sp +emit sp +emit sp +emit sp +path i j +emit nl +end +end +end +ret 0 +end diff --git a/tasks/floyd-warshall-algorithm/oracle.py b/tasks/floyd-warshall-algorithm/oracle.py new file mode 100644 index 0000000..11d357f --- /dev/null +++ b/tasks/floyd-warshall-algorithm/oracle.py @@ -0,0 +1,24 @@ +# Independent reference for "Floyd-Warshall algorithm" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def simple_paths(adj, s, t, seen=()): + if s == t: + yield (s,) + return + for v in adj.get(s, {}): + if v not in seen and v != s: + for p in simple_paths(adj, v, t, seen + (s,)): + yield (s,) + p +def best(adj, s, t): + cands = sorted((sum(adj[p[i]][p[i + 1]] for i in range(len(p) - 1)), p) for p in simple_paths(adj, s, t)) + if not cands: + return None + assert len(cands) == 1 or cands[0][0] < cands[1][0], "shortest path not unique" + return cands[0] +# checked by enumerating every simple path (there is no negative cycle), not by the triple loop +adj = {1: {3: -2}, 2: {1: 4, 3: 3}, 3: {4: 2}, 4: {2: -1}} +print("pair dist path") +for i in range(1, 5): + for j in range(1, 5): + if i != j: + d, p = best(adj, i, j) + print(f"{i} -> {j}{d:>6} " + " -> ".join(map(str, p))) diff --git a/tasks/floyds-triangle/expected.txt b/tasks/floyds-triangle/expected.txt new file mode 100644 index 0000000..320054d --- /dev/null +++ b/tasks/floyds-triangle/expected.txt @@ -0,0 +1,20 @@ + 1 + 2 3 + 4 5 6 + 7 8 9 10 +11 12 13 14 15 + + 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 57 58 59 60 61 62 63 64 65 66 +67 68 69 70 71 72 73 74 75 76 77 78 +79 80 81 82 83 84 85 86 87 88 89 90 91 +92 93 94 95 96 97 98 99 100 101 102 103 104 105 diff --git a/tasks/floyds-triangle/floyds-triangle.om b/tasks/floyds-triangle/floyds-triangle.om new file mode 100644 index 0000000..42d25f6 --- /dev/null +++ b/tasks/floyds-triangle/floyds-triangle.om @@ -0,0 +1,81 @@ +note Rosetta Code task "Floyd's triangle": https://rosettacode.org/wiki/Floyd's_triangle +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt w sub 20 i +i = sub 20 w +end +emit part d i sub 20 i +end +note row r (from 0) holds r+1 consecutive numbers; column j is as wide as the last row's entry in +note column j, which is the widest in that column +fn digits v ulong to ulong +d = ulong 1 +while ge v 10 bound 20 +v = div v 10 +d = add d 1 +end +ret d +end +fn floyd n ulong to void +last = add div mul sub n 1 n 2 1 +k = ulong 1 +for r ulong n +for j ulong add r 1 +w = digits add last j +if gt j 0 +emit sp +end +emitw k w +k = add k 1 +end +emit nl +end +end +fn main to int +floyd 5 +emit nl +floyd 14 +ret 0 +end diff --git a/tasks/floyds-triangle/oracle.py b/tasks/floyds-triangle/oracle.py new file mode 100644 index 0000000..2f8548d --- /dev/null +++ b/tasks/floyds-triangle/oracle.py @@ -0,0 +1,12 @@ +# Independent reference for "Floyd's triangle" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for t, n in enumerate((5, 14)): + if t: + print() + rows, k = [], 1 + for r in range(n): + rows.append(list(range(k, k + r + 1))) + k += r + 1 + widths = [len(str(v)) for v in rows[-1]] + for row in rows: + print(" ".join(str(v).rjust(w) for v, w in zip(row, widths))) diff --git a/tasks/fusc-sequence/expected.txt b/tasks/fusc-sequence/expected.txt new file mode 100644 index 0000000..d716607 --- /dev/null +++ b/tasks/fusc-sequence/expected.txt @@ -0,0 +1,7 @@ +first 61 terms: 0 1 1 2 1 3 2 3 1 4 3 5 2 5 3 4 1 5 4 7 3 8 5 7 2 7 5 8 3 7 4 5 1 6 5 9 4 11 7 10 3 11 8 13 5 12 7 9 2 9 7 12 5 13 8 11 3 10 7 11 4 +first term of each decimal length, below index 1000000: +fusc(0) = 0 +fusc(37) = 11 +fusc(1173) = 108 +fusc(35499) = 1076 +fusc(699051) = 10946 diff --git a/tasks/fusc-sequence/fusc-sequence.om b/tasks/fusc-sequence/fusc-sequence.om new file mode 100644 index 0000000..38507d2 --- /dev/null +++ b/tasks/fusc-sequence/fusc-sequence.om @@ -0,0 +1,79 @@ +note Rosetta Code task "Fusc sequence": https://rosettacode.org/wiki/Fusc_sequence +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text first "first 61 terms:" +text rec "first term of each decimal length, below index 1000000:\n" +text fu "fusc(" +text eqs ") = " +note fu n is fusc(n) for n below 10^6 +global fs ulong 1000000 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn digits n ulong to ulong +k = ulong 1 +v = div n 10 +while gt v 0 bound 20 +k = add k 1 +v = div v 10 +end +ret k +end +note fusc(0) = 0, fusc(1) = 1, fusc(2n) = fusc(n), fusc(2n+1) = fusc(n) + fusc(n+1); each needs only smaller indices +fn main to int +put fs 1 1 +for i ulong 999998 +n = add i 2 +h = shr n 1 +if eq rem n 2 0 +put fs n get fs h +else +put fs n add get fs h get fs add h 1 +end +end +emit first +for i ulong 61 +emit sp +emitu get fs i +end +emit nl +emit rec +w = ulong 0 +for n ulong 1000000 +d = digits get fs n +if gt d w +w = d +emit fu +emitu n +emit eqs +emitu get fs n +emit nl +end +end +ret 0 +end diff --git a/tasks/fusc-sequence/oracle.py b/tasks/fusc-sequence/oracle.py new file mode 100644 index 0000000..0a674c9 --- /dev/null +++ b/tasks/fusc-sequence/oracle.py @@ -0,0 +1,16 @@ +# Independent reference for "Fusc sequence" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import functools +@functools.lru_cache(maxsize=None) +def fusc(n): + if n < 2: + return n + return fusc(n // 2) if n % 2 == 0 else fusc(n // 2) + fusc(n // 2 + 1) +print("first 61 terms: " + " ".join(str(fusc(n)) for n in range(61))) +print("first term of each decimal length, below index 1000000:") +w = 0 +for n in range(1000000): + v = fusc(n) + if len(str(v)) > w: + w = len(str(v)) + print(f"fusc({n}) = {v}") diff --git a/tasks/fusc-sequence/wolfram.txt b/tasks/fusc-sequence/wolfram.txt new file mode 100644 index 0000000..92eff76 --- /dev/null +++ b/tasks/fusc-sequence/wolfram.txt @@ -0,0 +1,17 @@ +Wolfram cross-check of tasks/fusc-sequence/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 491f6155a9d1b643790eaf49c35c9717ef9518d623852db7eec1f1dedb6a4228. +agreement: YES +query: +f = ConstantArray[0, 10^6]; f[[2]] = 1; +Do[f[[n + 1]] = If[EvenQ[n], f[[n/2 + 1]], f[[(n - 1)/2 + 1]] + f[[(n + 1)/2 + 1]]], {n, 2, 10^6 - 1}]; +len = IntegerLength[f] /. 0 -> 1; +recs = Flatten[Position[len, #, 1, 1] & /@ Range[Max[len]]] - 1; +s = "first 61 terms: " <> StringRiffle[ToString /@ f[[1 ;; 61]], " "] <> "\n" <> + "first term of each decimal length, below index 1000000:\n" <> + StringJoin[("fusc(" <> ToString[#] <> ") = " <> ToString[f[[# + 1]]] <> "\n") & /@ recs]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"491f6155a9d1b643790eaf49c35c9717ef9518d623852db7eec1f1dedb6a4228", "first 61 terms: 0 1 1 2 1 3 2 3 1 4 3 5 2 5 3 4 1 5 4 7 3 8 5 7 2 7 5 8 3 7 4 5 1 6 5 9 4 11 7 10 3 11 8 13 5 12 7 9 2 9 7 12 5 13 8 11 3 10 7 11 4\nfirst term of each decimal length, below index 1000000:\nfusc(0) = 0\nfusc(37) = 11\nfusc(1173) = 108\nfusc(35499) = 1076\nfusc(699051) = 10946\n"} diff --git a/tasks/generate-lower-case-ascii-alphabet/expected.txt b/tasks/generate-lower-case-ascii-alphabet/expected.txt new file mode 100644 index 0000000..b0883f3 --- /dev/null +++ b/tasks/generate-lower-case-ascii-alphabet/expected.txt @@ -0,0 +1 @@ +abcdefghijklmnopqrstuvwxyz diff --git a/tasks/generate-lower-case-ascii-alphabet/generate-lower-case-ascii-alphabet.om b/tasks/generate-lower-case-ascii-alphabet/generate-lower-case-ascii-alphabet.om new file mode 100644 index 0000000..0161bbe --- /dev/null +++ b/tasks/generate-lower-case-ascii-alphabet/generate-lower-case-ascii-alphabet.om @@ -0,0 +1,24 @@ +note Rosetta Code task "Generate lower case ASCII alphabet": https://rosettacode.org/wiki/Generate_lower_case_ASCII_alphabet +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +fn main to int +a = new uchar 26 0 +for i ulong 26 +put a i add uchar i 97 +end +emit a +emit nl +ret 0 +end diff --git a/tasks/generate-lower-case-ascii-alphabet/oracle.py b/tasks/generate-lower-case-ascii-alphabet/oracle.py new file mode 100644 index 0000000..cbfcc07 --- /dev/null +++ b/tasks/generate-lower-case-ascii-alphabet/oracle.py @@ -0,0 +1,4 @@ +# Independent reference for "Generate lower case ASCII alphabet" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import string +print(string.ascii_lowercase) diff --git a/tasks/gnome-sort/expected.txt b/tasks/gnome-sort/expected.txt new file mode 100644 index 0000000..6e65e49 --- /dev/null +++ b/tasks/gnome-sort/expected.txt @@ -0,0 +1,2 @@ +34 2 -17 9 9 0 51 -3 28 6 +-17 -3 0 2 6 9 9 28 34 51 diff --git a/tasks/gnome-sort/gnome-sort.om b/tasks/gnome-sort/gnome-sort.om new file mode 100644 index 0000000..8f64005 --- /dev/null +++ b/tasks/gnome-sort/gnome-sort.om @@ -0,0 +1,112 @@ +note Rosetta Code task "Sorting algorithms/Gnome sort": https://rosettacode.org/wiki/Sorting_algorithms/Gnome_sort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "34 2 -17 9 9 0 51 -3 28 6" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note gnsort walks forward over ordered pairs and steps back after each swap (and is put back to 1 at 0); +note a pass moves forward, removes one inversion or leaves 0, so fewer than 3*(n + n*(n-1)/2) passes +note suffice: 165 for n = 10, bound 300; and/or evaluate both sides, so i = 0 is tested on its own +fn gnsort n ulong to void +i = ulong 1 +while lt i n bound 300 +if eq i 0 +i = 1 +continue +end +if le get vals sub i 1 get vals i +i = add i 1 +else +t = get vals i +put vals i get vals sub i 1 +put vals sub i 1 t +i = sub i 1 +end +end +end +fn main to int +n = parse data +show n +gnsort n +show n +ret 0 +end diff --git a/tasks/gnome-sort/oracle.py b/tasks/gnome-sort/oracle.py new file mode 100644 index 0000000..d14dd95 --- /dev/null +++ b/tasks/gnome-sort/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Sorting algorithms/Gnome sort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [34, 2, -17, 9, 9, 0, 51, -3, 28, 6] +print(*a) +print(*sorted(a)) diff --git a/tasks/gray-code/expected.txt b/tasks/gray-code/expected.txt new file mode 100644 index 0000000..e9b07b7 --- /dev/null +++ b/tasks/gray-code/expected.txt @@ -0,0 +1,33 @@ +number binary gray decoded + 0 00000 00000 0 + 1 00001 00001 1 + 2 00010 00011 2 + 3 00011 00010 3 + 4 00100 00110 4 + 5 00101 00111 5 + 6 00110 00101 6 + 7 00111 00100 7 + 8 01000 01100 8 + 9 01001 01101 9 + 10 01010 01111 10 + 11 01011 01110 11 + 12 01100 01010 12 + 13 01101 01011 13 + 14 01110 01001 14 + 15 01111 01000 15 + 16 10000 11000 16 + 17 10001 11001 17 + 18 10010 11011 18 + 19 10011 11010 19 + 20 10100 11110 20 + 21 10101 11111 21 + 22 10110 11101 22 + 23 10111 11100 23 + 24 11000 10100 24 + 25 11001 10101 25 + 26 11010 10111 26 + 27 11011 10110 27 + 28 11100 10010 28 + 29 11101 10011 29 + 30 11110 10001 30 + 31 11111 10000 31 diff --git a/tasks/gray-code/gray-code.om b/tasks/gray-code/gray-code.om new file mode 100644 index 0000000..cc5b02d --- /dev/null +++ b/tasks/gray-code/gray-code.om @@ -0,0 +1,94 @@ +note Rosetta Code task "Gray code": https://rosettacode.org/wiki/Gray_code +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text thead "number binary gray decoded\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt w sub 20 i +i = sub 20 w +end +emit part d i sub 20 i +end +note bin5 writes the low 5 bits of v, most significant first +fn bin5 v ulong to void +d = new uchar 5 48 +for i ulong 5 +if ne and shr v sub 4 i 1 0 +put d i 49 +end +end +emit d +end +note encode is n xor (n >> 1); decode undoes it by xoring in every right shift of g +fn encode n ulong to ulong +ret xor n shr n 1 +end +note one xor per bit of g: a ulong has 64, so 64 shifts empty s +fn decode g ulong to ulong +n = g +s = shr g 1 +while ne s 0 bound 64 +n = xor n s +s = shr s 1 +end +ret n +end +fn main to int +emit thead +for n ulong 32 +g = encode n +emitw n 6 +emit sp +emit sp +bin5 n +emit sp +emit sp +emit sp +bin5 g +emit sp +emit sp +emitw decode g 7 +emit nl +end +ret 0 +end diff --git a/tasks/gray-code/oracle.py b/tasks/gray-code/oracle.py new file mode 100644 index 0000000..43b8b00 --- /dev/null +++ b/tasks/gray-code/oracle.py @@ -0,0 +1,9 @@ +# Independent reference for "Gray code" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# reflected construction: the n-bit code is 0 + the (n-1)-bit code, then 1 + that code reversed +codes = [""] +for _ in range(5): + codes = ["0" + c for c in codes] + ["1" + c for c in reversed(codes)] +print("number binary gray decoded") +for n, g in enumerate(codes): + print(f"{n:6} {n:05b} {g} {codes.index(g):7}") diff --git a/tasks/greatest-subsequential-sum/expected.txt b/tasks/greatest-subsequential-sum/expected.txt new file mode 100644 index 0000000..c10a565 --- /dev/null +++ b/tasks/greatest-subsequential-sum/expected.txt @@ -0,0 +1,12 @@ +-1 -2 3 5 6 -2 -1 4 -4 2 -1 +sum: 15 +subsequence: 3 5 6 -2 -1 4 +-1 -2 -3 -4 +sum: 0 +subsequence: +4 -1 2 1 -5 4 0 3 +sum: 8 +subsequence: 4 -1 2 1 -5 4 0 3 +2 -3 2 -3 2 +sum: 2 +subsequence: 2 diff --git a/tasks/greatest-subsequential-sum/greatest-subsequential-sum.om b/tasks/greatest-subsequential-sum/greatest-subsequential-sum.om new file mode 100644 index 0000000..2277340 --- /dev/null +++ b/tasks/greatest-subsequential-sum/greatest-subsequential-sum.om @@ -0,0 +1,137 @@ +note Rosetta Code task "Greatest subsequential sum": https://rosettacode.org/wiki/Greatest_subsequential_sum +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text d0 "-1 -2 3 5 6 -2 -1 4 -4 2 -1" +text d1 "-1 -2 -3 -4" +text d2 "4 -1 2 1 -5 4 0 3" +text d3 "2 -3 2 -3 2" +text tsum "sum: " +text tsub "subsequence:" +global vals long 16 +global pre long 17 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note gss tries every start i and end j using prefix sums; the empty subsequence (sum 0) is allowed, +note and on a tie the earliest start wins, then the shortest run, so the answer is unique +fn gss n ulong to void +put pre 0 0 +for i ulong n +put pre add i 1 add get pre i get vals i +end +bs = long 0 +bi = ulong 0 +bj = ulong 0 +for i ulong n +for x ulong sub n i +j = add add i x 1 +s = sub get pre j get pre i +if gt s bs +bs = s +bi = i +bj = j +end +end +end +emit tsum +emiti bs +emit nl +emit tsub +for k ulong sub bj bi +emit sp +emiti get vals add bi k +end +emit nl +end +fn main to int +n = parse d0 +show n +gss n +n = parse d1 +show n +gss n +n = parse d2 +show n +gss n +n = parse d3 +show n +gss n +ret 0 +end diff --git a/tasks/greatest-subsequential-sum/oracle.py b/tasks/greatest-subsequential-sum/oracle.py new file mode 100644 index 0000000..6200035 --- /dev/null +++ b/tasks/greatest-subsequential-sum/oracle.py @@ -0,0 +1,9 @@ +# Independent reference for "Greatest subsequential sum" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for a in [[-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1], [-1, -2, -3, -4], [4, -1, 2, 1, -5, 4, 0, 3], [2, -3, 2, -3, 2]]: + print(*a) + runs = [(sum(a[i:j]), i, j) for i in range(len(a)) for j in range(i + 1, len(a) + 1)] + runs = [r for r in runs if r[0] > 0] + s, i, j = min(runs, key=lambda r: (-r[0], r[1], r[2] - r[1])) if runs else (0, 0, 0) + print("sum:", s) + print("subsequence:" + "".join(" " + str(v) for v in a[i:j])) diff --git a/tasks/hamming-numbers/expected.txt b/tasks/hamming-numbers/expected.txt new file mode 100644 index 0000000..87486c3 --- /dev/null +++ b/tasks/hamming-numbers/expected.txt @@ -0,0 +1,2 @@ +first 20: 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36 +1691st: 2125764000 diff --git a/tasks/hamming-numbers/hamming-numbers.om b/tasks/hamming-numbers/hamming-numbers.om new file mode 100644 index 0000000..d6e1af7 --- /dev/null +++ b/tasks/hamming-numbers/hamming-numbers.om @@ -0,0 +1,84 @@ +note Rosetta Code task "Hamming numbers": https://rosettacode.org/wiki/Hamming_numbers +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tfirst "first 20: " +text tnth "1691st: " +global ham ulong 1691 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note Dijkstra's method: three indices into the list already built point at the smallest numbers whose +note double, triple and quintuple are not yet listed; the next number is the least of the three +fn min3 a ulong b ulong c ulong to ulong +m = a +if lt b m +m = b +end +if lt c m +m = c +end +ret m +end +fn build n ulong to void +put ham 0 1 +i = ulong 0 +j = ulong 0 +k = ulong 0 +for x ulong sub n 1 +a = mul get ham i 2 +b = mul get ham j 3 +c = mul get ham k 5 +m = min3 a b c +put ham add x 1 m +if eq a m +i = add i 1 +end +if eq b m +j = add j 1 +end +if eq c m +k = add k 1 +end +end +end +fn main to int +build 1691 +emit tfirst +for i ulong 20 +if gt i 0 +emit sp +end +emitu get ham i +end +emit nl +emit tnth +emitu get ham 1690 +emit nl +ret 0 +end diff --git a/tasks/hamming-numbers/oracle.py b/tasks/hamming-numbers/oracle.py new file mode 100644 index 0000000..ab53e97 --- /dev/null +++ b/tasks/hamming-numbers/oracle.py @@ -0,0 +1,16 @@ +# Independent reference for "Hamming numbers" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# Cross-check, WolframLanguageEvaluator: s = Sort[Flatten[Table[2^i 3^j 5^k, {i,0,40},{j,0,30},{k,0,20}]]]; +# {Take[s, 20], s[[1691]]} -> {{1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, 30, 32, 36}, 2125764000} +import heapq +# a min-heap: pop the smallest, push its multiples, skip repeats +h, seen, out = [1], {1}, [] +while len(out) < 1691: + v = heapq.heappop(h) + out.append(v) + for f in (2, 3, 5): + if v * f not in seen: + seen.add(v * f) + heapq.heappush(h, v * f) +print("first 20:", *out[:20]) +print("1691st:", out[1690]) diff --git a/tasks/happy-numbers/expected.txt b/tasks/happy-numbers/expected.txt new file mode 100644 index 0000000..6066470 --- /dev/null +++ b/tasks/happy-numbers/expected.txt @@ -0,0 +1 @@ +first 8 happy numbers: 1 7 10 13 19 23 28 31 diff --git a/tasks/happy-numbers/happy-numbers.om b/tasks/happy-numbers/happy-numbers.om new file mode 100644 index 0000000..17bbfd6 --- /dev/null +++ b/tasks/happy-numbers/happy-numbers.om @@ -0,0 +1,68 @@ +note Rosetta Code task "Happy numbers": https://rosettacode.org/wiki/Happy_numbers +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text hdr "first 8 happy numbers:" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn step n ulong to ulong +s = ulong 0 +v = n +while gt v 0 bound 20 +d = rem v 10 +s = add s mul d d +v = div v 10 +end +ret s +end +note Floyd's tortoise and hare: the iteration is eventually periodic; n is happy when the cycle is 1 -> 1 +note bound: from n < 1000 the values stay below 1000 (a 3-digit square sum is at most 243), so the meeting comes within 1000 steps +fn happy n ulong to bool +s = n +f = step n +while ne s f bound 1000 +s = step s +f = step step f +end +ret eq s 1 +end +fn main to int +emit hdr +n = ulong 0 +k = ulong 0 +while lt k 8 bound 1000 +n = add n 1 +if happy n +emit sp +emitu n +k = add k 1 +end +end +emit nl +ret 0 +end diff --git a/tasks/happy-numbers/oracle.py b/tasks/happy-numbers/oracle.py new file mode 100644 index 0000000..88b2008 --- /dev/null +++ b/tasks/happy-numbers/oracle.py @@ -0,0 +1,10 @@ +# Independent reference for "Happy numbers" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def happy(n): + seen = set() + while n != 1 and n not in seen: + seen.add(n) + n = sum(int(c) ** 2 for c in str(n)) + return n == 1 +hs = [n for n in range(1, 100) if happy(n)][:8] +print("first 8 happy numbers: " + " ".join(map(str, hs))) diff --git a/tasks/happy-numbers/wolfram.txt b/tasks/happy-numbers/wolfram.txt new file mode 100644 index 0000000..19830e2 --- /dev/null +++ b/tasks/happy-numbers/wolfram.txt @@ -0,0 +1,12 @@ +Wolfram cross-check of tasks/happy-numbers/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = bcea1dfabd07495e659226991e0cb35c5eb6b11d028a67e0a503639b7eec422d. +agreement: YES +query: +happyQ[n_] := MemberQ[NestList[Total[IntegerDigits[#]^2] &, n, 50], 1]; +s = "first 8 happy numbers: " <> StringRiffle[ToString /@ Take[Select[Range[100], happyQ], 8], " "] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"bcea1dfabd07495e659226991e0cb35c5eb6b11d028a67e0a503639b7eec422d", "first 8 happy numbers: 1 7 10 13 19 23 28 31\n"} diff --git a/tasks/harshad-or-niven-series/expected.txt b/tasks/harshad-or-niven-series/expected.txt new file mode 100644 index 0000000..fc8229d --- /dev/null +++ b/tasks/harshad-or-niven-series/expected.txt @@ -0,0 +1,2 @@ +first 20 Harshad numbers: 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42 +first Harshad number above 1000: 1002 diff --git a/tasks/harshad-or-niven-series/harshad-or-niven-series.om b/tasks/harshad-or-niven-series/harshad-or-niven-series.om new file mode 100644 index 0000000..d38e1cb --- /dev/null +++ b/tasks/harshad-or-niven-series/harshad-or-niven-series.om @@ -0,0 +1,66 @@ +note Rosetta Code task "Harshad or Niven series": https://rosettacode.org/wiki/Harshad_or_Niven_series +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text first "first 20 Harshad numbers:" +text above "first Harshad number above 1000: " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note n is a Harshad number when its digit sum divides it +fn harshad n ulong to bool +s = ulong 0 +v = n +while gt v 0 bound 20 +s = add s rem v 10 +v = div v 10 +end +ret eq rem n s 0 +end +note bound: every multiple of 10 up to 100 is Harshad, so neither search passes 1000 candidates +fn main to int +emit first +n = ulong 0 +k = ulong 0 +while lt k 20 bound 1000 +n = add n 1 +if harshad n +emit sp +emitu n +k = add k 1 +end +end +emit nl +n = ulong 1001 +while not harshad n bound 1000 +n = add n 1 +end +emit above +emitu n +emit nl +ret 0 +end diff --git a/tasks/harshad-or-niven-series/oracle.py b/tasks/harshad-or-niven-series/oracle.py new file mode 100644 index 0000000..b845fb6 --- /dev/null +++ b/tasks/harshad-or-niven-series/oracle.py @@ -0,0 +1,8 @@ +# Independent reference for "Harshad or Niven series" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import itertools +def harshad(n): + return n % sum(map(int, str(n))) == 0 +hs = itertools.filterfalse(lambda n: not harshad(n), itertools.count(1)) +print("first 20 Harshad numbers: " + " ".join(str(next(hs)) for _ in range(20))) +print(f"first Harshad number above 1000: {next(n for n in itertools.count(1001) if harshad(n))}") diff --git a/tasks/harshad-or-niven-series/wolfram.txt b/tasks/harshad-or-niven-series/wolfram.txt new file mode 100644 index 0000000..ed57fbf --- /dev/null +++ b/tasks/harshad-or-niven-series/wolfram.txt @@ -0,0 +1,13 @@ +Wolfram cross-check of tasks/harshad-or-niven-series/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = a8f0174aee10d26e9344945aed3b4b4068a31d34898c7ada437fbc8a596a5ff0. +agreement: YES +query: +hQ[n_] := Divisible[n, Total[IntegerDigits[n]]]; +s = "first 20 Harshad numbers: " <> StringRiffle[ToString /@ Take[Select[Range[200], hQ], 20], " "] <> "\n" <> + "first Harshad number above 1000: " <> ToString[SelectFirst[Range[1001, 2000], hQ]] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"a8f0174aee10d26e9344945aed3b4b4068a31d34898c7ada437fbc8a596a5ff0", "first 20 Harshad numbers: 1 2 3 4 5 6 7 8 9 10 12 18 20 21 24 27 30 36 40 42\nfirst Harshad number above 1000: 1002\n"} diff --git a/tasks/heapsort/expected.txt b/tasks/heapsort/expected.txt new file mode 100644 index 0000000..f59cc6e --- /dev/null +++ b/tasks/heapsort/expected.txt @@ -0,0 +1,2 @@ +12 -4 7 88 0 31 -4 56 23 9 -70 45 2 +-70 -4 -4 0 2 7 9 12 23 31 45 56 88 diff --git a/tasks/heapsort/heapsort.om b/tasks/heapsort/heapsort.om new file mode 100644 index 0000000..b39e483 --- /dev/null +++ b/tasks/heapsort/heapsort.om @@ -0,0 +1,127 @@ +note Rosetta Code task "Sorting algorithms/Heapsort": https://rosettacode.org/wiki/Sorting_algorithms/Heapsort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "12 -4 7 88 0 31 -4 56 23 9 -70 45 2" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note sift moves vals[s] down the max-heap vals[0..n-1]; r at least doubles each pass, so 16 passes suffice +fn sift s ulong n ulong to void +r = s +while lt add mul r 2 1 n bound 16 +c = add mul r 2 1 +if lt add c 1 n +if lt get vals c get vals add c 1 +c = add c 1 +end +end +if ge get vals r get vals c +ret +end +t = get vals r +put vals r get vals c +put vals c t +r = c +end +end +note hsort heapifies bottom-up, then swaps the maximum to the end n-1 times +fn hsort n ulong to void +s = shr n 1 +while gt s 0 bound 16 +s = sub s 1 +sift s n +end +e = n +while gt e 1 bound 16 +e = sub e 1 +t = get vals 0 +put vals 0 get vals e +put vals e t +sift 0 e +end +end +fn main to int +n = parse data +show n +hsort n +show n +ret 0 +end diff --git a/tasks/heapsort/oracle.py b/tasks/heapsort/oracle.py new file mode 100644 index 0000000..e70d4a4 --- /dev/null +++ b/tasks/heapsort/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Sorting algorithms/Heapsort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [12, -4, 7, 88, 0, 31, -4, 56, 23, 9, -70, 45, 2] +print(*a) +print(*sorted(a)) diff --git a/tasks/hello-world-newline-omission/expected.txt b/tasks/hello-world-newline-omission/expected.txt new file mode 100644 index 0000000..9a8e7c2 --- /dev/null +++ b/tasks/hello-world-newline-omission/expected.txt @@ -0,0 +1 @@ +Goodbye, World! \ No newline at end of file diff --git a/tasks/hello-world-newline-omission/hello-world-newline-omission.om b/tasks/hello-world-newline-omission/hello-world-newline-omission.om new file mode 100644 index 0000000..9fd4fde --- /dev/null +++ b/tasks/hello-world-newline-omission/hello-world-newline-omission.om @@ -0,0 +1,19 @@ +note Rosetta Code task "Hello world/Newline omission": https://rosettacode.org/wiki/Hello_world/Newline_omission +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text msg "Goodbye, World!" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +fn main to int +emit msg +ret 0 +end diff --git a/tasks/hello-world-newline-omission/oracle.py b/tasks/hello-world-newline-omission/oracle.py new file mode 100644 index 0000000..c8a597c --- /dev/null +++ b/tasks/hello-world-newline-omission/oracle.py @@ -0,0 +1,3 @@ +# Independent reference for "Hello world/Newline omission" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +print("Goodbye, World!", end="") diff --git a/tasks/hofstadter-figure-figure-sequences/expected.txt b/tasks/hofstadter-figure-figure-sequences/expected.txt new file mode 100644 index 0000000..01fbe54 --- /dev/null +++ b/tasks/hofstadter-figure-figure-sequences/expected.txt @@ -0,0 +1,2 @@ +R(1) to R(10): 1 3 7 12 18 26 35 45 56 69 +first 40 R and first 960 S cover 1..1000 exactly once: yes diff --git a/tasks/hofstadter-figure-figure-sequences/hofstadter-figure-figure-sequences.om b/tasks/hofstadter-figure-figure-sequences/hofstadter-figure-figure-sequences.om new file mode 100644 index 0000000..e627066 --- /dev/null +++ b/tasks/hofstadter-figure-figure-sequences/hofstadter-figure-figure-sequences.om @@ -0,0 +1,102 @@ +note Rosetta Code task "Hofstadter Figure-Figure sequences": https://rosettacode.org/wiki/Hofstadter_Figure-Figure_sequences +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text first "R(1) to R(10):" +text chk "first 40 R and first 960 S cover 1..1000 exactly once: " +text yes "yes\n" +text no "no\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note R(1) = 1, S(1) = 2, R(n) = R(n-1) + S(n-1), S(n) = the least integer above S(n-1) not in R +note r holds R(1..50) (R(50) = 1509 > 1000, so every R value up to 1000 is marked in ir); s holds S(1..960) +fn main to int +r = new ulong 51 0 +s = new ulong 961 0 +ir = new uchar 1600 0 +put r 1 1 +put s 1 2 +put ir 1 1 +for i ulong 49 +n = add i 2 +v = add get r sub n 1 get s sub n 1 +put r n v +put ir v 1 +c = add get s sub n 1 1 +while eq get ir c 1 bound 2 +c = add c 1 +end +put s n c +end +note S as the complement of R, now that R is known through 1509: the k-th unmarked integer is S(k) +k = ulong 0 +c = ulong 1 +while lt k 960 bound 1600 +if eq get ir c 0 +k = add k 1 +put s k c +end +c = add c 1 +end +emit first +for i ulong 10 +emit sp +emitu get r add i 1 +end +emit nl +cnt = new ulong 1001 0 +ok = ulong 1 +for i ulong 40 +v = get r add i 1 +if gt v 1000 +ok = ulong 0 +else +put cnt v add get cnt v 1 +end +end +for i ulong 960 +v = get s add i 1 +if gt v 1000 +ok = ulong 0 +else +put cnt v add get cnt v 1 +end +end +for i ulong 1000 +if ne get cnt add i 1 1 +ok = ulong 0 +end +end +emit chk +if eq ok 1 +emit yes +else +emit no +end +ret 0 +end diff --git a/tasks/hofstadter-figure-figure-sequences/oracle.py b/tasks/hofstadter-figure-figure-sequences/oracle.py new file mode 100644 index 0000000..4569d85 --- /dev/null +++ b/tasks/hofstadter-figure-figure-sequences/oracle.py @@ -0,0 +1,19 @@ +# Independent reference for "Hofstadter Figure-Figure sequences" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import itertools + +def figure_figure(): + # R(n) = R(n-1) + S(n-1); S runs through the positive integers that R skips + r, rs, s = 1, {1}, 2 + yield r, s + while True: + r += s + rs.add(r) + s = next(c for c in itertools.count(s + 1) if c not in rs) + yield r, s + +R = [r for r, _ in itertools.islice(figure_figure(), 40)] +S = [s for _, s in itertools.islice(figure_figure(), 960)] +print("R(1) to R(10): " + " ".join(map(str, R[:10]))) +ok = sorted(R + S) == list(range(1, 1001)) +print("first 40 R and first 960 S cover 1..1000 exactly once: " + ("yes" if ok else "no")) diff --git a/tasks/hofstadter-figure-figure-sequences/wolfram.txt b/tasks/hofstadter-figure-figure-sequences/wolfram.txt new file mode 100644 index 0000000..0a729b3 --- /dev/null +++ b/tasks/hofstadter-figure-figure-sequences/wolfram.txt @@ -0,0 +1,18 @@ +Wolfram cross-check of tasks/hofstadter-figure-figure-sequences/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 537236e2c7eac35c5a0df7dac6e4acc96b539dc5056675c4dc966a11f8261211. +agreement: YES +query: +r = {1}; sq = {2}; +While[Length[r] < 60, AppendTo[r, Last[r] + Last[sq]]; AppendTo[sq, SelectFirst[Range[Last[sq] + 1, Last[r] + 1], ! MemberQ[r, #] &]]]; +S = Take[Complement[Range[Last[r]], r], 960]; +s = "R(1) to R(10): " <> StringRiffle[ToString /@ r[[1 ;; 10]], " "] <> "\n" <> + "first 40 R and first 960 S cover 1..1000 exactly once: " <> If[Sort[Join[Take[r, 40], S]] === Range[1000], "yes", "no"] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Symbol::undefined2: Warning: Global symbols "S, S" are undefined. +General::messages: Messages were generated which may indicate errors. + +Out[1]= {"537236e2c7eac35c5a0df7dac6e4acc96b539dc5056675c4dc966a11f8261211", "R(1) to R(10): 1 3 7 12 18 26 35 45 56 69\nfirst 40 R and first 960 S cover 1..1000 exactly once: yes\n"} diff --git a/tasks/hofstadter-q-sequence/expected.txt b/tasks/hofstadter-q-sequence/expected.txt new file mode 100644 index 0000000..4181f7f --- /dev/null +++ b/tasks/hofstadter-q-sequence/expected.txt @@ -0,0 +1,3 @@ +first 10 terms: 1 1 2 3 3 4 5 5 6 6 +the 1000th term: 502 +terms up to the 100000th that are less than the term before: 49798 diff --git a/tasks/hofstadter-q-sequence/hofstadter-q-sequence.om b/tasks/hofstadter-q-sequence/hofstadter-q-sequence.om new file mode 100644 index 0000000..864aab0 --- /dev/null +++ b/tasks/hofstadter-q-sequence/hofstadter-q-sequence.om @@ -0,0 +1,66 @@ +note Rosetta Code task "Hofstadter Q sequence": https://rosettacode.org/wiki/Hofstadter_Q_sequence +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text first "first 10 terms:" +text th "the 1000th term: " +text fl "terms up to the 100000th that are less than the term before: " +note q n is Q(n) for n = 1..100000; Q(n) < n here, so every index n - Q(n-1) and n - Q(n-2) is in range +global q ulong 100001 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn main to int +put q 1 1 +put q 2 1 +for i ulong 99998 +n = add i 3 +a = get q sub n get q sub n 1 +b = get q sub n get q sub n 2 +put q n add a b +end +emit first +for i ulong 10 +emit sp +emitu get q add i 1 +end +emit nl +emit th +emitu get q 1000 +emit nl +c = ulong 0 +for i ulong 99999 +n = add i 2 +if lt get q n get q sub n 1 +c = add c 1 +end +end +emit fl +emitu c +emit nl +ret 0 +end diff --git a/tasks/hofstadter-q-sequence/oracle.py b/tasks/hofstadter-q-sequence/oracle.py new file mode 100644 index 0000000..708196b --- /dev/null +++ b/tasks/hofstadter-q-sequence/oracle.py @@ -0,0 +1,12 @@ +# Independent reference for "Hofstadter Q sequence" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +q = {1: 1, 2: 1} +def Q(n): + # fill Q(len(q)+1) .. Q(n) in order; each term uses only earlier ones + for m in range(len(q) + 1, n + 1): + q[m] = q[m - q[m - 1]] + q[m - q[m - 2]] + return q[n] +Q(100000) +print("first 10 terms: " + " ".join(str(Q(n)) for n in range(1, 11))) +print(f"the 1000th term: {Q(1000)}") +print(f"terms up to the 100000th that are less than the term before: {sum(1 for n in range(2, 100001) if q[n] < q[n - 1])}") diff --git a/tasks/hofstadter-q-sequence/wolfram.txt b/tasks/hofstadter-q-sequence/wolfram.txt new file mode 100644 index 0000000..77ec6a6 --- /dev/null +++ b/tasks/hofstadter-q-sequence/wolfram.txt @@ -0,0 +1,15 @@ +Wolfram cross-check of tasks/hofstadter-q-sequence/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = de27b38bf948ead1345c65bb3da9266300aceb854e1a42f813df336e35ba292f. +agreement: YES +query: +q = ConstantArray[1, 100000]; +Do[q[[n]] = q[[n - q[[n - 1]]]] + q[[n - q[[n - 2]]]], {n, 3, 100000}]; +s = "first 10 terms: " <> StringRiffle[ToString /@ q[[1 ;; 10]], " "] <> "\n" <> + "the 1000th term: " <> ToString[q[[1000]]] <> "\n" <> + "terms up to the 100000th that are less than the term before: " <> ToString[Count[Differences[q], d_ /; d < 0]] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"de27b38bf948ead1345c65bb3da9266300aceb854e1a42f813df336e35ba292f", "first 10 terms: 1 1 2 3 3 4 5 5 6 6\nthe 1000th term: 502\nterms up to the 100000th that are less than the term before: 49798\n"} diff --git a/tasks/identity-matrix/expected.txt b/tasks/identity-matrix/expected.txt new file mode 100644 index 0000000..1bbedd1 --- /dev/null +++ b/tasks/identity-matrix/expected.txt @@ -0,0 +1,12 @@ +1 + +1 0 0 +0 1 0 +0 0 1 + +1 0 0 0 0 0 +0 1 0 0 0 0 +0 0 1 0 0 0 +0 0 0 1 0 0 +0 0 0 0 1 0 +0 0 0 0 0 1 diff --git a/tasks/identity-matrix/identity-matrix.om b/tasks/identity-matrix/identity-matrix.om new file mode 100644 index 0000000..e3f7445 --- /dev/null +++ b/tasks/identity-matrix/identity-matrix.om @@ -0,0 +1,56 @@ +note Rosetta Code task "Identity matrix": https://rosettacode.org/wiki/Identity_matrix +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note entry (i, j) of the n x n identity is 1 on the diagonal i = j and 0 elsewhere +fn ident n ulong to void +for i ulong n +for j ulong n +if gt j 0 +emit sp +end +if eq i j +emitu 1 +else +emitu 0 +end +end +emit nl +end +end +fn main to int +ident 1 +emit nl +ident 3 +emit nl +ident 6 +ret 0 +end diff --git a/tasks/identity-matrix/oracle.py b/tasks/identity-matrix/oracle.py new file mode 100644 index 0000000..b9fb54f --- /dev/null +++ b/tasks/identity-matrix/oracle.py @@ -0,0 +1,10 @@ +# Independent reference for "Identity matrix" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for k, n in enumerate((1, 3, 6)): + if k: + print() + rows = [[0] * n for _ in range(n)] + for i in range(n): + rows[i][i] = 1 + for r in rows: + print(*r) diff --git a/tasks/isbn13-check-digit/expected.txt b/tasks/isbn13-check-digit/expected.txt new file mode 100644 index 0000000..257255c --- /dev/null +++ b/tasks/isbn13-check-digit/expected.txt @@ -0,0 +1,4 @@ +978-0596528126: good +978-0596528120: bad +978-1788399081: good +978-1788399083: bad diff --git a/tasks/isbn13-check-digit/isbn13-check-digit.om b/tasks/isbn13-check-digit/isbn13-check-digit.om new file mode 100644 index 0000000..3fd8d8b --- /dev/null +++ b/tasks/isbn13-check-digit/isbn13-check-digit.om @@ -0,0 +1,53 @@ +note Rosetta Code task "ISBN13 check digit": https://rosettacode.org/wiki/ISBN13_check_digit +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text tgood ": good\n" +text tbad ": bad\n" +text i1 "978-0596528126" +text i2 "978-0596528120" +text i3 "978-1788399081" +text i4 "978-1788399083" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note digits weigh 1, 3, 1, 3, ... skipping hyphens; valid when there are 13 and the sum is 0 mod 10 +fn isbn a view uchar * to bool +s = ulong 0 +k = ulong 0 +for i ulong len a +c = get a i +if and ge c 48 le c 57 +d = ulong sub c 48 +if eq rem k 2 1 +d = mul d 3 +end +s = add s d +k = add k 1 +end +end +ret and eq k 13 eq rem s 10 0 +end +fn show a view uchar * to void +emit a +if isbn a +emit tgood +else +emit tbad +end +end +fn main to int +show i1 +show i2 +show i3 +show i4 +ret 0 +end diff --git a/tasks/isbn13-check-digit/oracle.py b/tasks/isbn13-check-digit/oracle.py new file mode 100644 index 0000000..484d7f7 --- /dev/null +++ b/tasks/isbn13-check-digit/oracle.py @@ -0,0 +1,7 @@ +# Independent reference for "ISBN13 check digit" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def ok(s): + d = [int(c) for c in s if c.isdigit()] + return len(d) == 13 and sum(x * (3 if i % 2 else 1) for i, x in enumerate(d)) % 10 == 0 +for s in ["978-0596528126", "978-0596528120", "978-1788399081", "978-1788399083"]: + print(f"{s}: {'good' if ok(s) else 'bad'}") diff --git a/tasks/isqrt-integer-square-root-of-x/expected.txt b/tasks/isqrt-integer-square-root-of-x/expected.txt new file mode 100644 index 0000000..1a14f06 --- /dev/null +++ b/tasks/isqrt-integer-square-root-of-x/expected.txt @@ -0,0 +1,12 @@ +isqrt of 0..65: 0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 +isqrt(7^1) = 2 +isqrt(7^3) = 18 +isqrt(7^5) = 129 +isqrt(7^7) = 907 +isqrt(7^9) = 6352 +isqrt(7^11) = 44467 +isqrt(7^13) = 311269 +isqrt(7^15) = 2178889 +isqrt(7^17) = 15252229 +isqrt(7^19) = 106765608 +isqrt(7^21) = 747359260 diff --git a/tasks/isqrt-integer-square-root-of-x/isqrt-integer-square-root-of-x.om b/tasks/isqrt-integer-square-root-of-x/isqrt-integer-square-root-of-x.om new file mode 100644 index 0000000..3466455 --- /dev/null +++ b/tasks/isqrt-integer-square-root-of-x/isqrt-integer-square-root-of-x.om @@ -0,0 +1,75 @@ +note Rosetta Code task "Isqrt (integer square root) of X": https://rosettacode.org/wiki/Isqrt_(integer_square_root)_of_X +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text small "isqrt of 0..65:" +text pre "isqrt(7^" +text eqs ") = " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note the task's quadratic residue method: q is the least power of 4 above x, then one result bit per step +note bound: inputs here are below 2^60, so q stays at most 2^60 (30 doublings of the exponent) and never wraps; +note 7^n is shown for odd n up to 21, the largest odd power of 7 below 2^64 (the task goes on to 7^73 with bignums) +fn isqrt x ulong to ulong +q = ulong 1 +while le q x bound 31 +q = shl q 2 +end +z = x +r = ulong 0 +while gt q 1 bound 31 +q = shr q 2 +s = add r q +r = shr r 1 +if ge z s +z = sub z s +r = add r q +end +end +ret r +end +fn main to int +emit small +for x ulong 66 +emit sp +emitu isqrt x +end +emit nl +p = ulong 7 +n = ulong 1 +while le n 21 bound 11 +emit pre +emitu n +emit eqs +emitu isqrt p +emit nl +p = mul p 49 +n = add n 2 +end +ret 0 +end diff --git a/tasks/isqrt-integer-square-root-of-x/oracle.py b/tasks/isqrt-integer-square-root-of-x/oracle.py new file mode 100644 index 0000000..59753f8 --- /dev/null +++ b/tasks/isqrt-integer-square-root-of-x/oracle.py @@ -0,0 +1,6 @@ +# Independent reference for "Isqrt (integer square root) of X" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import math +print("isqrt of 0..65: " + " ".join(str(math.isqrt(x)) for x in range(66))) +for n in range(1, 22, 2): + print(f"isqrt(7^{n}) = {math.isqrt(7 ** n)}") diff --git a/tasks/isqrt-integer-square-root-of-x/wolfram.txt b/tasks/isqrt-integer-square-root-of-x/wolfram.txt new file mode 100644 index 0000000..97014c8 --- /dev/null +++ b/tasks/isqrt-integer-square-root-of-x/wolfram.txt @@ -0,0 +1,12 @@ +Wolfram cross-check of tasks/isqrt-integer-square-root-of-x/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = bb1a54683c3b7f4dc76994eede75dd4ff1e49e13d21ed5c1c47dffe43a6e0260. +agreement: YES +query: +s = "isqrt of 0..65: " <> StringRiffle[ToString /@ Floor[Sqrt[Range[0, 65]]], " "] <> "\n" <> + StringJoin[Table["isqrt(7^" <> ToString[n] <> ") = " <> ToString[Floor[Sqrt[7^n]]] <> "\n", {n, 1, 21, 2}]]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"bb1a54683c3b7f4dc76994eede75dd4ff1e49e13d21ed5c1c47dffe43a6e0260", "isqrt of 0..65: 0 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8\nisqrt(7^1) = 2\nisqrt(7^3) = 18\nisqrt(7^5) = 129\nisqrt(7^7) = 907\nisqrt(7^9) = 6352\nisqrt(7^11) = 44467\nisqrt(7^13) = 311269\nisqrt(7^15) = 2178889\nisqrt(7^17) = 15252229\nisqrt(7^19) = 106765608\nisqrt(7^21) = 747359260\n"} diff --git a/tasks/jewels-and-stones/expected.txt b/tasks/jewels-and-stones/expected.txt new file mode 100644 index 0000000..525048e --- /dev/null +++ b/tasks/jewels-and-stones/expected.txt @@ -0,0 +1,2 @@ +aAAbbbb aA 3 +ZZ z 0 diff --git a/tasks/jewels-and-stones/jewels-and-stones.om b/tasks/jewels-and-stones/jewels-and-stones.om new file mode 100644 index 0000000..e672ab7 --- /dev/null +++ b/tasks/jewels-and-stones/jewels-and-stones.om @@ -0,0 +1,61 @@ +note Rosetta Code task "Jewels and stones": https://rosettacode.org/wiki/Jewels_and_stones +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text s1 "aAAbbbb" +text j1 "aA" +text s2 "ZZ" +text j2 "z" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note count the stones (bytes of s) that are jewels (occur in j); case matters +fn jewels s view uchar * j view uchar * to ulong +n = ulong 0 +for i ulong len s +for k ulong len j +if eq get s i get j k +n = add n 1 +break +end +end +end +ret n +end +fn show s view uchar * j view uchar * to void +emit s +emit sp +emit j +emit sp +emitu jewels s j +emit nl +end +fn main to int +show s1 j1 +show s2 j2 +ret 0 +end diff --git a/tasks/jewels-and-stones/oracle.py b/tasks/jewels-and-stones/oracle.py new file mode 100644 index 0000000..b092dde --- /dev/null +++ b/tasks/jewels-and-stones/oracle.py @@ -0,0 +1,4 @@ +# Independent reference for "Jewels and stones" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for s, j in [("aAAbbbb", "aA"), ("ZZ", "z")]: + print(s, j, sum(c in j for c in s)) diff --git a/tasks/josephus-problem/expected.txt b/tasks/josephus-problem/expected.txt new file mode 100644 index 0000000..9ecc375 --- /dev/null +++ b/tasks/josephus-problem/expected.txt @@ -0,0 +1,2 @@ +executed in order: 2 5 8 11 14 17 20 23 26 29 32 35 38 0 4 9 13 18 22 27 31 36 40 6 12 19 25 33 39 7 16 28 37 10 24 1 21 3 34 15 +survivor: 30 diff --git a/tasks/josephus-problem/josephus-problem.om b/tasks/josephus-problem/josephus-problem.om new file mode 100644 index 0000000..252eadf --- /dev/null +++ b/tasks/josephus-problem/josephus-problem.om @@ -0,0 +1,67 @@ +note Rosetta Code task "Josephus problem": https://rosettacode.org/wiki/Josephus_problem +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tkill "executed in order:" +text tsurv "survivor: " +global alive uchar 41 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note 41 prisoners numbered 0..40 stand in a circle; counting from 0, every 3rd one still alive is killed +note until one is left; each kill walks at most 3 live places, so 3*41 steps per kill bound the walk +fn main to int +n = ulong 41 +k = ulong 3 +for i ulong n +put alive i 1 +end +p = sub n 1 +emit tkill +for x ulong sub n 1 +c = ulong 0 +while lt c k bound 123 +p = rem add p 1 n +if eq get alive p 1 +c = add c 1 +end +end +put alive p 0 +emit sp +emitu p +end +emit nl +emit tsurv +for i ulong n +if eq get alive i 1 +emitu i +end +end +emit nl +ret 0 +end diff --git a/tasks/josephus-problem/oracle.py b/tasks/josephus-problem/oracle.py new file mode 100644 index 0000000..779f4e4 --- /dev/null +++ b/tasks/josephus-problem/oracle.py @@ -0,0 +1,16 @@ +# Independent reference for "Josephus problem" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# the survivor by the recurrence J(1) = 0, J(m) = (J(m-1) + k) mod m, the order by rotating a list +n, k = 41, 3 +j = 0 +for m in range(2, n + 1): + j = (j + k) % m +ring = list(range(n)) +order = [] +pos = 0 +while len(ring) > 1: + pos = (pos + k - 1) % len(ring) + order.append(ring.pop(pos)) +assert ring == [j] +print("executed in order:", *order) +print("survivor:", j) diff --git a/tasks/kaprekar-numbers/expected.txt b/tasks/kaprekar-numbers/expected.txt new file mode 100644 index 0000000..38c906b --- /dev/null +++ b/tasks/kaprekar-numbers/expected.txt @@ -0,0 +1,2 @@ +Kaprekar numbers below 10000: 1 9 45 55 99 297 703 999 2223 2728 4879 4950 5050 5292 7272 7777 9999 +count of Kaprekar numbers below 1000000: 54 diff --git a/tasks/kaprekar-numbers/kaprekar-numbers.om b/tasks/kaprekar-numbers/kaprekar-numbers.om new file mode 100644 index 0000000..1f31fa9 --- /dev/null +++ b/tasks/kaprekar-numbers/kaprekar-numbers.om @@ -0,0 +1,71 @@ +note Rosetta Code task "Kaprekar numbers": https://rosettacode.org/wiki/Kaprekar_numbers +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text hdr "Kaprekar numbers below 10000:" +text cnt "count of Kaprekar numbers below 1000000: " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note n is Kaprekar when n * n splits as a * 10^k + b with b > 0 and a + b = n (1 counts by definition) +note bound: n < 10^6, so n * n < 10^12 and p walks at most 12 powers of ten +fn kap n ulong to bool +if eq n 1 +ret true +end +sq = mul n n +p = ulong 10 +while le p sq bound 20 +b = rem sq p +if gt b 0 +if eq add div sq p b n +ret true +end +end +p = mul p 10 +end +ret false +end +fn main to int +emit hdr +c = ulong 0 +for i ulong 999999 +n = add i 1 +if kap n +c = add c 1 +if lt n 10000 +emit sp +emitu n +end +end +end +emit nl +emit cnt +emitu c +emit nl +ret 0 +end diff --git a/tasks/kaprekar-numbers/oracle.py b/tasks/kaprekar-numbers/oracle.py new file mode 100644 index 0000000..4022087 --- /dev/null +++ b/tasks/kaprekar-numbers/oracle.py @@ -0,0 +1,14 @@ +# Independent reference for "Kaprekar numbers" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def kaprekar(n): + if n == 1: + return True + s = str(n * n) + for i in range(1, len(s)): + a, b = int(s[:i]), int(s[i:]) + if b > 0 and a + b == n: + return True + return False +ks = [n for n in range(1, 10**6) if kaprekar(n)] +print("Kaprekar numbers below 10000: " + " ".join(str(k) for k in ks if k < 10000)) +print(f"count of Kaprekar numbers below 1000000: {len(ks)}") diff --git a/tasks/kaprekar-numbers/wolfram.txt b/tasks/kaprekar-numbers/wolfram.txt new file mode 100644 index 0000000..3f47b6d --- /dev/null +++ b/tasks/kaprekar-numbers/wolfram.txt @@ -0,0 +1,15 @@ +Wolfram cross-check of tasks/kaprekar-numbers/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = f1b64f49d73a3c608576e7670f9afebea1b2f2901d9850bf5261a4c170ec6c94. +agreement: YES +query: +n = Range[10^6 - 1]; sq = n^2; +hits = Total[Table[With[{q = Quotient[sq, 10^k], r = Mod[sq, 10^k]}, (1 - Unitize[q + r - n]) Unitize[r] Unitize[q]], {k, 1, 12}]]; +ks = Union[Flatten[Position[hits, h_ /; h > 0]], {1}]; +s = "Kaprekar numbers below 10000: " <> StringRiffle[ToString /@ Select[ks, # < 10000 &], " "] <> "\n" <> + "count of Kaprekar numbers below 1000000: " <> ToString[Length[ks]] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"f1b64f49d73a3c608576e7670f9afebea1b2f2901d9850bf5261a4c170ec6c94", "Kaprekar numbers below 10000: 1 9 45 55 99 297 703 999 2223 2728 4879 4950 5050 5292 7272 7777 9999\ncount of Kaprekar numbers below 1000000: 54\n"} diff --git a/tasks/knapsack-problem-0-1/expected.txt b/tasks/knapsack-problem-0-1/expected.txt new file mode 100644 index 0000000..323b6f1 --- /dev/null +++ b/tasks/knapsack-problem-0-1/expected.txt @@ -0,0 +1,14 @@ +map +compass +water +sandwich +glucose +banana +suntan cream +waterproof trousers +waterproof overclothes +note-case +sunglasses +socks +total weight 396 +total value 1030 diff --git a/tasks/knapsack-problem-0-1/knapsack-problem-0-1.om b/tasks/knapsack-problem-0-1/knapsack-problem-0-1.om new file mode 100644 index 0000000..b170e8c --- /dev/null +++ b/tasks/knapsack-problem-0-1/knapsack-problem-0-1.om @@ -0,0 +1,142 @@ +note Rosetta Code task "Knapsack problem/0-1": https://rosettacode.org/wiki/Knapsack_problem/0-1 +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text names "map|compass|water|sandwich|glucose|tin|banana|apple|cheese|beer|suntan cream|camera|T-shirt|trousers|umbrella|waterproof trousers|waterproof overclothes|note-case|sunglasses|towel|socks|book" +text data "9 150 13 35 153 200 50 160 15 60 68 45 27 60 39 40 23 30 52 10 11 70 32 30 24 15 48 10 73 40 42 70 43 75 22 80 7 20 18 12 4 50 30 10" +text tw "total weight " +text tv "total value " +global vals long 44 +global best long 401 +global keep uchar 9223 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note item writes the k-th name of the |-separated names text +fn item k ulong to void +f = ulong 0 +st = ulong 0 +for i ulong len names +if eq get names i 124 +if eq f k +emit part names st sub i st +ret +end +f = add f 1 +st = add i 1 +end +end +emit part names st sub len names st +end +note pack runs the 0-1 dynamic program over capacities 0..400: best[c] is the best value with weight <= c +note using the items seen so far; keep[i*401+c] records that item i was taken at capacity c +fn pack n ulong to void +for i ulong n +w = ulong get vals mul i 2 +v = get vals add mul i 2 1 +for x ulong 401 +c = sub 400 x +if ge c w +if gt add get best sub c w v get best c +put best c add get best sub c w v +put keep add mul i 401 c 1 +end +end +end +end +end +fn main to int +n = div parse data 2 +pack n +note walk back from capacity 400 over the items in reverse, taking each recorded item +c = ulong 400 +tot = ulong 0 +for x ulong n +i = sub sub n x 1 +if eq get keep add mul i 401 c 1 +put vals add mul i 2 1 -1 +w = ulong get vals mul i 2 +c = sub c w +tot = add tot w +end +end +for i ulong n +if eq get vals add mul i 2 1 -1 +item i +emit nl +end +end +emit tw +emitu tot +emit nl +emit tv +emiti get best 400 +emit nl +ret 0 +end diff --git a/tasks/knapsack-problem-0-1/oracle.py b/tasks/knapsack-problem-0-1/oracle.py new file mode 100644 index 0000000..bd5e467 --- /dev/null +++ b/tasks/knapsack-problem-0-1/oracle.py @@ -0,0 +1,29 @@ +# Independent reference for "Knapsack problem/0-1" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +from itertools import combinations +items = [('map', 9, 150), ('compass', 13, 35), ('water', 153, 200), ('sandwich', 50, 160), ('glucose', 15, 60), ('tin', 68, 45), ('banana', 27, 60), ('apple', 39, 40), ('cheese', 23, 30), ('beer', 52, 10), ('suntan cream', 11, 70), ('camera', 32, 30), ('T-shirt', 24, 15), ('trousers', 48, 10), ('umbrella', 73, 40), ('waterproof trousers', 42, 70), ('waterproof overclothes', 43, 75), ('note-case', 22, 80), ('sunglasses', 7, 20), ('towel', 18, 12), ('socks', 4, 50), ('book', 30, 10)] +# exhaustive search is 2^22 subsets; branch and bound over items sorted by value density keeps it quick +order = sorted(range(len(items)), key=lambda i: -items[i][2] / items[i][1]) +best = (0, ()) +def bound(k, w, v): + for i in order[k:]: + if w + items[i][1] <= 400: + w += items[i][1]; v += items[i][2] + else: + return v + (400 - w) * items[i][2] / items[i][1] + return v +def go(k, w, v, chosen): + global best + if v > best[0]: + best = (v, chosen) + if k == len(order) or bound(k, w, v) <= best[0]: + return + i = order[k] + if w + items[i][1] <= 400: + go(k + 1, w + items[i][1], v + items[i][2], chosen + (i,)) + go(k + 1, w, v, chosen) +go(0, 0, 0, ()) +for i in sorted(best[1]): + print(items[i][0]) +print("total weight", sum(items[i][1] for i in best[1])) +print("total value", best[0]) diff --git a/tasks/knapsack-problem-unbounded/expected.txt b/tasks/knapsack-problem-unbounded/expected.txt new file mode 100644 index 0000000..033e02c --- /dev/null +++ b/tasks/knapsack-problem-unbounded/expected.txt @@ -0,0 +1,5 @@ +panacea ichor gold value weight volume +0 15 11 54500 250/10 247/1000 +3 10 11 54500 249/10 247/1000 +6 5 11 54500 248/10 247/1000 +9 0 11 54500 247/10 247/1000 diff --git a/tasks/knapsack-problem-unbounded/knapsack-problem-unbounded.om b/tasks/knapsack-problem-unbounded/knapsack-problem-unbounded.om new file mode 100644 index 0000000..1b6edc4 --- /dev/null +++ b/tasks/knapsack-problem-unbounded/knapsack-problem-unbounded.om @@ -0,0 +1,88 @@ +note Rosetta Code task "Knapsack problem/Unbounded": https://rosettacode.org/wiki/Knapsack_problem/Unbounded +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text thead "panacea ichor gold value weight volume\n" +text ttenth "/10 " +text tthou "/1000\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note the weights are in tenths (0.3, 0.2, 2.0 -> 3, 2, 20; limit 25 -> 250) and the volumes in +note thousandths (0.025, 0.015, 0.002 -> 25, 15, 2; limit 0.25 -> 250), so all arithmetic is exact; +note values are 3000, 1800 and 2500; every count that fits is tried (at most 11 * 17 * 13 cases) +fn fits a ulong b ulong c ulong to bool +w = add add mul a 3 mul b 2 mul c 20 +v = add add mul a 25 mul b 15 mul c 2 +ret and le w 250 le v 250 +end +fn value a ulong b ulong c ulong to ulong +ret add add mul a 3000 mul b 1800 mul c 2500 +end +fn row a ulong b ulong c ulong to void +emitu a +emit sp +emitu b +emit sp +emitu c +emit sp +emitu value a b c +emit sp +emitu add add mul a 3 mul b 2 mul c 20 +emit ttenth +emitu add add mul a 25 mul b 15 mul c 2 +emit tthou +end +note pass 1 finds the best value, pass 2 prints every combination that reaches it +fn main to int +best = ulong 0 +for a ulong 11 +for b ulong 17 +for c ulong 13 +if fits a b c +if gt value a b c best +best = value a b c +end +end +end +end +end +emit thead +for a ulong 11 +for b ulong 17 +for c ulong 13 +if fits a b c +if eq value a b c best +row a b c +end +end +end +end +end +ret 0 +end diff --git a/tasks/knapsack-problem-unbounded/oracle.py b/tasks/knapsack-problem-unbounded/oracle.py new file mode 100644 index 0000000..3fd585e --- /dev/null +++ b/tasks/knapsack-problem-unbounded/oracle.py @@ -0,0 +1,16 @@ +# Independent reference for "Knapsack problem/Unbounded" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +from fractions import Fraction as F +from itertools import product +items = [("panacea", 3000, F("0.3"), F("0.025")), ("ichor", 1800, F("0.2"), F("0.015")), ("gold", 2500, F("2.0"), F("0.002"))] +W, V = F(25), F("0.25") +fits = [] +for n in product(*(range(int(min(W / w, V / v)) + 1) for _, _, w, v in items)): + w = sum(k * it[2] for k, it in zip(n, items)) + v = sum(k * it[3] for k, it in zip(n, items)) + if w <= W and v <= V: + fits.append((sum(k * it[1] for k, it in zip(n, items)), n, w, v)) +best = max(f[0] for f in fits) +print("panacea ichor gold value weight volume") +for val, n, w, v in sorted(f for f in fits if f[0] == best): + print(*n, val, f"{int(w * 10)}/10", f"{int(v * 1000)}/1000") diff --git a/tasks/knights-tour/expected.txt b/tasks/knights-tour/expected.txt new file mode 100644 index 0000000..d6abffa --- /dev/null +++ b/tasks/knights-tour/expected.txt @@ -0,0 +1,9 @@ +Warnsdorff tour from a1; each square shows its move number, rank 8 at the top + 16 31 12 51 26 29 10 63 + 13 50 15 30 11 64 25 28 + 32 17 52 47 54 27 62 9 + 49 14 55 36 61 38 45 24 + 18 33 48 53 46 59 8 39 + 3 56 35 60 37 42 23 44 + 34 19 2 5 58 21 40 7 + 1 4 57 20 41 6 43 22 diff --git a/tasks/knights-tour/knights-tour.om b/tasks/knights-tour/knights-tour.om new file mode 100644 index 0000000..3c6ab72 --- /dev/null +++ b/tasks/knights-tour/knights-tour.om @@ -0,0 +1,134 @@ +note Rosetta Code task "Knight's tour": https://rosettacode.org/wiki/Knight's_tour +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tstart "Warnsdorff tour from a1; each square shows its move number, rank 8 at the top\n" +global board ulong 64 +global mdx long 8 +global mdy long 8 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt w sub 20 i +i = sub 20 w +end +emit part d i sub 20 i +end +note target is the square a knight reaches from sq with move m, or 64 when it leaves the board +fn target sq ulong m ulong to ulong +f = add long rem sq 8 get mdx m +r = add long div sq 8 get mdy m +if or or lt f 0 gt f 7 or lt r 0 gt r 7 +ret 64 +end +ret ulong add mul r 8 f +end +note degree counts the unvisited squares reachable from sq +fn degree sq ulong to ulong +d = ulong 0 +for m ulong 8 +t = target sq m +if lt t 64 +if eq get board t 0 +d = add d 1 +end +end +end +ret d +end +note Warnsdorff's rule: always move to the unvisited square with the fewest onward moves; ties go to +note the first in the move order (2,1) (1,2) (-1,2) (-2,1) (-2,-1) (-1,-2) (1,-2) (2,-1) +fn tour to bool +sq = ulong 0 +put board 0 1 +for k ulong 63 +b = ulong 64 +bd = ulong 9 +for m ulong 8 +t = target sq m +if lt t 64 +if eq get board t 0 +d = degree t +if lt d bd +b = t +bd = d +end +end +end +end +if eq b 64 +ret false +end +put board b add k 2 +sq = b +end +ret true +end +fn main to int +put mdx 0 2 +put mdy 0 1 +put mdx 1 1 +put mdy 1 2 +put mdx 2 -1 +put mdy 2 2 +put mdx 3 -2 +put mdy 3 1 +put mdx 4 -2 +put mdy 4 -1 +put mdx 5 -1 +put mdy 5 -2 +put mdx 6 1 +put mdy 6 -2 +put mdx 7 2 +put mdy 7 -1 +if not tour +exit 1 +end +emit tstart +for x ulong 8 +r = sub 7 x +for f ulong 8 +emitw get board add mul r 8 f 3 +end +emit nl +end +ret 0 +end diff --git a/tasks/knights-tour/oracle.py b/tasks/knights-tour/oracle.py new file mode 100644 index 0000000..cb551a9 --- /dev/null +++ b/tasks/knights-tour/oracle.py @@ -0,0 +1,21 @@ +# Independent reference for "Knight's tour" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +moves = [(2, 1), (1, 2), (-1, 2), (-2, 1), (-2, -1), (-1, -2), (1, -2), (2, -1)] +def onward(f, r, seen): + return [(f + dx, r + dy) for dx, dy in moves + if 0 <= f + dx < 8 and 0 <= r + dy < 8 and (f + dx, r + dy) not in seen] +path = [(0, 0)] +seen = {(0, 0)} +while len(path) < 64: + cands = onward(*path[-1], seen) + assert cands, "Warnsdorff dead end" + nxt = min(cands, key=lambda s: len(onward(*s, seen))) # min keeps the first of equal keys + path.append(nxt) + seen.add(nxt) +# the tour must be 64 distinct squares, each a knight's move from the one before +assert len(set(path)) == 64 +assert all(sorted((abs(a[0] - b[0]), abs(a[1] - b[1]))) == [1, 2] for a, b in zip(path, path[1:])) +num = {s: i + 1 for i, s in enumerate(path)} +print("Warnsdorff tour from a1; each square shows its move number, rank 8 at the top") +for r in range(7, -1, -1): + print("".join(f"{num[(f, r)]:3}" for f in range(8))) diff --git a/tasks/kronecker-product/expected.txt b/tasks/kronecker-product/expected.txt new file mode 100644 index 0000000..89d5e3a --- /dev/null +++ b/tasks/kronecker-product/expected.txt @@ -0,0 +1,14 @@ + 0 5 0 10 + 6 7 12 14 + 0 15 0 20 + 18 21 24 28 + + 0 0 0 0 1 1 1 1 0 0 0 0 + 0 0 0 0 1 0 0 1 0 0 0 0 + 0 0 0 0 1 1 1 1 0 0 0 0 + 1 1 1 1 1 1 1 1 1 1 1 1 + 1 0 0 1 1 0 0 1 1 0 0 1 + 1 1 1 1 1 1 1 1 1 1 1 1 + 0 0 0 0 1 1 1 1 0 0 0 0 + 0 0 0 0 1 0 0 1 0 0 0 0 + 0 0 0 0 1 1 1 1 0 0 0 0 diff --git a/tasks/kronecker-product/kronecker-product.om b/tasks/kronecker-product/kronecker-product.om new file mode 100644 index 0000000..dfa3759 --- /dev/null +++ b/tasks/kronecker-product/kronecker-product.om @@ -0,0 +1,148 @@ +note Rosetta Code task "Kronecker product": https://rosettacode.org/wiki/Kronecker_product +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tdata "1 2 3 4 0 5 6 7 0 1 0 1 1 1 0 1 0 1 1 1 1 1 0 0 1 1 1 1 1" +global vals long 32 +global ka long 16 +global kb long 16 +global dims ulong 4 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note emitwi writes a signed n right-aligned in w columns (w at most 20) +fn emitwi n long w ulong to void +d = new uchar 21 32 +i = ulong 21 +v = n +if lt n 0 +v = neg n +end +while true bound 20 +i = sub i 1 +put d i uchar add long 48 rem v 10 +v = div v 10 +if eq v 0 +break +end +end +if lt n 0 +i = sub i 1 +put d i 45 +end +if gt w sub 21 i +i = sub 21 w +end +emit part d i sub 21 i +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note the matrices come from tdata: 2x2 (a), 2x2 (b), then 3x3 (a), 3x4 (b); dims holds ra ca rb cb +note entry (i, j) of a (x) b is a[i / rb][j / cb] * b[i mod rb][j mod cb] +fn entry i ulong j ulong to long +rb = get dims 2 +cb = get dims 3 +x = get ka add mul div i rb get dims 1 div j cb +y = get kb add mul rem i rb cb rem j cb +ret mul x y +end +fn kron to void +rows = mul get dims 0 get dims 2 +cols = mul get dims 1 get dims 3 +for i ulong rows +for j ulong cols +emitwi entry i j 3 +end +emit nl +end +end +note load copies na entries of vals from offset o into ka, then nb entries into kb +fn load o ulong na ulong nb ulong to void +for i ulong na +put ka i get vals add o i +end +for i ulong nb +put kb i get vals add add o na i +end +end +fn main to int +n = parse tdata +put dims 0 2 +put dims 1 2 +put dims 2 2 +put dims 3 2 +load 0 4 4 +kron +emit nl +put dims 0 3 +put dims 1 3 +put dims 2 3 +put dims 3 4 +load 8 9 12 +kron +ret 0 +end diff --git a/tasks/kronecker-product/oracle.py b/tasks/kronecker-product/oracle.py new file mode 100644 index 0000000..d8b226f --- /dev/null +++ b/tasks/kronecker-product/oracle.py @@ -0,0 +1,16 @@ +# Independent reference for "Kronecker product" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def kron(a, b): + # block form: each entry of a scales a whole copy of b + out = [] + for arow in a: + for brow in b: + out.append([x * y for x in arow for y in brow]) + return out +cases = [([[1, 2], [3, 4]], [[0, 5], [6, 7]]), + ([[0, 1, 0], [1, 1, 1], [0, 1, 0]], [[1, 1, 1, 1], [1, 0, 0, 1], [1, 1, 1, 1]])] +for k, (a, b) in enumerate(cases): + if k: + print() + for row in kron(a, b): + print("".join(f"{v:3}" for v in row)) diff --git a/tasks/langtons-ant/expected.txt b/tasks/langtons-ant/expected.txt new file mode 100644 index 0000000..09ad0b8 --- /dev/null +++ b/tasks/langtons-ant/expected.txt @@ -0,0 +1,53 @@ +steps until the ant leaves the grid: 11669 +black cells, rows and columns 28..78 0..79 +..........................................##..############..##.................. +.........................................#..####..........#..##................. +........................................###...##............##.#................ +........................................#.#..#.........#..#....#................ +....................................##..##.#.#.........###.......#.............. +.................................###.#..#...#.....#.....##.##..###.............. +..................................#.#..###..##.####.##...#.#..#.##..##.......... +..................................#.###.##..#.##..###.#.#.....###...###......... +................................#.....#...#####.#.#..####..#...###.#.#.#........ +...............................###.##...#.####..##.##.######.#.###.#...#........ +...............................#.###.#.##.#.#.##.##.##.#...#####.###.##......... +...................................#.#...#.##.###...#...#.#..####....#.##....... +................................#..#.........##.##...#..##.....##.#.....##...... +...............................###...#.#.##.###..#..##.....#...###.##..##.#..... +..............................#..###..##...##.##...###..#....#..##.####...#..... +.............................###...#...#.#..#.#.####.##..#.##.###..#.....#...... +............................#..###..#.##....#..#.###..#......###.##.#..#..##.... +...........................###...#.....#.##.#.##..##..#####.####..####.##...#... +..........................#..###..#.#.#..#.###.#.#.##......##...#.#.#....#...#.. +.........................###...#..##.###..##.#...##.......####.####...#......#.. +........................#..###..#.#..#...##..###########.#..####..#....#....#... +.......................###...#..##......#.####..##..#########..#..##....#..##... +......................#..###..#.#...##..#.##...##.##.###.###...#..#.##..####.#.. +.....................###...#..##...#..#.######.##.#.##.#.#....###.###...##...#.. +....................#..###..#.#...#.....#####.#.#####.....#.#..##.#....##...#... +...................###...#..##....#.....#.##.#####.##..#.#...#..#..##.#..#..#... +..................#..###..#.#.....#....#...####.#..#####.##...##########...##... +.................###...#..##......#.##...##...#..#...####..#...##.####.##....... +................#..###..#.#........#####.#..##...##.#...#....#.#..#..#..#.#..... +...............###...#..##..........##..##.#.#.#....##.##.#.#.##..#..##..##..... +..............#..###..#.#.................#..#....#.########.#.#.##..####.#..... +.............###...#..##..................#..#...#.......##.##...#..#..##.#..... +............#..###..#.#....................#..#..#......#..##..##...##.####..... +...........###...#..##......................##...#.......##..##....#...#.###.... +..........#..###..#.#............................#.##..####....####.###.####.... +.........###...#..##..............................##..####....##..#.##.#.#..#... +........#..###..#.#................................##....##....##.###.##.#####.. +.......###...#..##................................................#.##.#..####.. +......#..###..#.#.....................................................##.##.##.. +.....###...#..##......................................................##........ +....#..###..#.#.....................................................#.##..####.# +...###...#..##.....................................................#..#.###..### +..#..###..#.#......................................................#.##.#..#..#. +.###...#..##........................................................##......##.. +#..###..#.#..........................................................##......... +.###.#..##...................................................................... +#.#.#.#.#....................................................................... +.####.##........................................................................ +.#.##.#......................................................................... +..####.......................................................................... +...##........................................................................... diff --git a/tasks/langtons-ant/langtons-ant.om b/tasks/langtons-ant/langtons-ant.om new file mode 100644 index 0000000..f75ae10 --- /dev/null +++ b/tasks/langtons-ant/langtons-ant.om @@ -0,0 +1,140 @@ +note Rosetta Code task "Langton's ant": https://rosettacode.org/wiki/Langton's_ant +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tsteps "steps until the ant leaves the grid: " +text tbox "black cells, rows and columns " +text tto ".." +text tdot "." +text thash "#" +global grid uchar 10000 +global box ulong 4 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note the ant starts at (50, 50) of a white 100 x 100 grid facing up; on white it turns right, on black +note left, then flips the cell and steps forward; directions 0..3 are up, right, down, left +fn walk to ulong +r = long 50 +c = long 50 +d = ulong 0 +s = ulong 0 +note the ant is known to leave a grid this size after about 12000 steps (the highway starts near +note step 10000), so 999999 is a safe bound +while and and ge r 0 lt r 100 and ge c 0 lt c 100 bound 999999 +k = add mul ulong r 100 ulong c +if eq get grid k 0 +d = rem add d 1 4 +put grid k 1 +else +d = rem add d 3 4 +put grid k 0 +end +r = add r dy d +c = add c dx d +s = add s 1 +end +ret s +end +fn dy d ulong to long +if eq d 0 +ret -1 +end +if eq d 2 +ret 1 +end +ret 0 +end +fn dx d ulong to long +if eq d 1 +ret 1 +end +if eq d 3 +ret -1 +end +ret 0 +end +note bounds finds the smallest box holding every black cell: box = top, bottom, left, right +fn bounds to void +put box 0 99 +put box 1 0 +put box 2 99 +put box 3 0 +for r ulong 100 +for c ulong 100 +if eq get grid add mul r 100 c 1 +grow r c +end +end +end +end +fn grow r ulong c ulong to void +if lt r get box 0 +put box 0 r +end +if gt r get box 1 +put box 1 r +end +if lt c get box 2 +put box 2 c +end +if gt c get box 3 +put box 3 c +end +end +note row prints grid row r between the box's left and right columns +fn row r ulong to void +for x ulong add sub get box 3 get box 2 1 +c = add x get box 2 +if eq get grid add mul r 100 c 1 +emit thash +else +emit tdot +end +end +emit nl +end +fn main to int +emit tsteps +emitu walk +emit nl +bounds +emit tbox +emitu get box 0 +emit tto +emitu get box 1 +emit sp +emitu get box 2 +emit tto +emitu get box 3 +emit nl +for r ulong add sub get box 1 get box 0 1 +row add r get box 0 +end +ret 0 +end diff --git a/tasks/langtons-ant/oracle.py b/tasks/langtons-ant/oracle.py new file mode 100644 index 0000000..90d8a82 --- /dev/null +++ b/tasks/langtons-ant/oracle.py @@ -0,0 +1,21 @@ +# Independent reference for "Langton's ant" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# the black cells as a set; the heading as a complex number (up = -1j with rows growing downward) +black = set() +pos, head = complex(50, 50), -1j # real part = column, imaginary part = row +steps = 0 +while 0 <= pos.real < 100 and 0 <= pos.imag < 100: + if pos in black: + head *= -1j # left turn + black.remove(pos) + else: + head *= 1j # right turn + black.add(pos) + pos += head + steps += 1 +print("steps until the ant leaves the grid:", steps) +rows = [int(p.imag) for p in black] +cols = [int(p.real) for p in black] +print(f"black cells, rows and columns {min(rows)}..{max(rows)} {min(cols)}..{max(cols)}") +for r in range(min(rows), max(rows) + 1): + print("".join("#" if complex(c, r) in black else "." for c in range(min(cols), max(cols) + 1))) diff --git a/tasks/least-common-multiple/expected.txt b/tasks/least-common-multiple/expected.txt new file mode 100644 index 0000000..c39ac72 --- /dev/null +++ b/tasks/least-common-multiple/expected.txt @@ -0,0 +1,7 @@ +lcm(12, 18) = 36 +lcm(35, 0) = 0 +lcm(0, 5) = 0 +lcm(4294967296, 6000000000) = 25165824000000000 +lcm(1..10) = 2520 +lcm(1..20) = 232792560 +lcm(1..40) = 5342931457063200 diff --git a/tasks/least-common-multiple/least-common-multiple.om b/tasks/least-common-multiple/least-common-multiple.om new file mode 100644 index 0000000..5808a6f --- /dev/null +++ b/tasks/least-common-multiple/least-common-multiple.om @@ -0,0 +1,85 @@ +note Rosetta Code task "Least common multiple": https://rosettacode.org/wiki/Least_common_multiple +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text pre "lcm(" +text cm ", " +text eqs ") = " +text tup "lcm(1.." +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note Euclid: each two steps at least halve x, so 128 steps cover any 64-bit pair +fn gcd a ulong b ulong to ulong +x = a +y = b +while ne y 0 bound 128 +t = rem x y +x = y +y = t +end +ret x +end +note lcm(a, b) = a / gcd(a, b) * b, dividing first so the product never exceeds the result +fn lcm a ulong b ulong to ulong +if or eq a 0 eq b 0 +ret 0 +end +ret mul div a gcd a b b +end +fn tell a ulong b ulong to void +emit pre +emitu a +emit cm +emitu b +emit eqs +emitu lcm a b +emit nl +end +note lcm of 1..n by folding; lcm(1..40) = 5342931457063200 is below 2^64 +fn upto n ulong to void +r = ulong 1 +k = ulong 1 +while le k n bound 40 +r = lcm r k +k = add k 1 +end +emit tup +emitu n +emit eqs +emitu r +emit nl +end +fn main to int +tell 12 18 +tell 35 0 +tell 0 5 +tell 4294967296 6000000000 +upto 10 +upto 20 +upto 40 +ret 0 +end diff --git a/tasks/least-common-multiple/oracle.py b/tasks/least-common-multiple/oracle.py new file mode 100644 index 0000000..d012db7 --- /dev/null +++ b/tasks/least-common-multiple/oracle.py @@ -0,0 +1,7 @@ +# Independent reference for "Least common multiple" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import functools, math +for a, b in [(12, 18), (35, 0), (0, 5), (4294967296, 6000000000)]: + print(f"lcm({a}, {b}) = {math.lcm(a, b)}") +for n in [10, 20, 40]: + print(f"lcm(1..{n}) = {functools.reduce(math.lcm, range(1, n + 1))}") diff --git a/tasks/least-common-multiple/wolfram.txt b/tasks/least-common-multiple/wolfram.txt new file mode 100644 index 0000000..f482676 --- /dev/null +++ b/tasks/least-common-multiple/wolfram.txt @@ -0,0 +1,13 @@ +Wolfram cross-check of tasks/least-common-multiple/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = d7d38a17de8d031b4334a64c3ef3320dd4597552ea772ff20123a4f8b67ac792. +agreement: YES +query: +s = StringJoin[("lcm(" <> ToString[#[[1]]] <> ", " <> ToString[#[[2]]] <> ") = " <> ToString[LCM @@ #] <> "\n") & /@ + {{12, 18}, {35, 0}, {0, 5}, {4294967296, 6000000000}}] <> + StringJoin[("lcm(1.." <> ToString[#] <> ") = " <> ToString[LCM @@ Range[#]] <> "\n") & /@ {10, 20, 40}]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"d7d38a17de8d031b4334a64c3ef3320dd4597552ea772ff20123a4f8b67ac792", "lcm(12, 18) = 36\nlcm(35, 0) = 0\nlcm(0, 5) = 0\nlcm(4294967296, 6000000000) = 25165824000000000\nlcm(1..10) = 2520\nlcm(1..20) = 232792560\nlcm(1..40) = 5342931457063200\n"} diff --git a/tasks/letter-frequency/expected.txt b/tasks/letter-frequency/expected.txt new file mode 100644 index 0000000..63c89ab --- /dev/null +++ b/tasks/letter-frequency/expected.txt @@ -0,0 +1,26 @@ +a 4 +b 3 +c 3 +d 3 +e 6 +f 3 +g 3 +h 4 +i 5 +j 3 +k 3 +l 3 +m 3 +n 3 +o 9 +p 3 +q 3 +r 4 +s 3 +t 4 +u 6 +v 3 +w 3 +x 3 +y 3 +z 3 diff --git a/tasks/letter-frequency/letter-frequency.om b/tasks/letter-frequency/letter-frequency.om new file mode 100644 index 0000000..bb68899 --- /dev/null +++ b/tasks/letter-frequency/letter-frequency.om @@ -0,0 +1,58 @@ +note Rosetta Code task "Letter frequency": https://rosettacode.org/wiki/Letter_frequency +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text msg "The quick brown fox jumps over the lazy dog. Pack my box with five dozen liquor jugs! Sphinx of black quartz, judge my vow." +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note counts the letters a..z, upper and lower case together, and prints every letter with its count +fn main to int +f = new ulong 26 0 +for i ulong len msg +c = get msg i +if and ge c 65 le c 90 +c = add c 32 +end +if and ge c 97 le c 122 +put f sub c 97 add get f sub c 97 1 +end +end +for j ulong 26 +emitc add uchar j 97 +emit sp +emitu get f j +emit nl +end +ret 0 +end diff --git a/tasks/letter-frequency/oracle.py b/tasks/letter-frequency/oracle.py new file mode 100644 index 0000000..a97a6d4 --- /dev/null +++ b/tasks/letter-frequency/oracle.py @@ -0,0 +1,7 @@ +# Independent reference for "Letter frequency" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import collections, string +msg = "The quick brown fox jumps over the lazy dog. Pack my box with five dozen liquor jugs! Sphinx of black quartz, judge my vow." +f = collections.Counter(c for c in msg.lower() if c in string.ascii_lowercase) +for c in string.ascii_lowercase: + print(c, f[c]) diff --git a/tasks/levenshtein-distance/expected.txt b/tasks/levenshtein-distance/expected.txt new file mode 100644 index 0000000..1a3a3ac --- /dev/null +++ b/tasks/levenshtein-distance/expected.txt @@ -0,0 +1,3 @@ +kitten sitting 3 +rosettacode raisethysword 8 +saturday sunday 3 diff --git a/tasks/levenshtein-distance/levenshtein-distance.om b/tasks/levenshtein-distance/levenshtein-distance.om new file mode 100644 index 0000000..fe06ae4 --- /dev/null +++ b/tasks/levenshtein-distance/levenshtein-distance.om @@ -0,0 +1,81 @@ +note Rosetta Code task "Levenshtein distance": https://rosettacode.org/wiki/Levenshtein_distance +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text a1 "kitten" +text b1 "sitting" +text a2 "rosettacode" +text b2 "raisethysword" +text a3 "saturday" +text b3 "sunday" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note two-row dynamic programme; strings up to 63 bytes (longer traps 3) +fn lev a view uchar * b view uchar * to ulong +p = new ulong 64 0 +q = new ulong 64 0 +for j ulong add len b 1 +put p j j +end +for i ulong len a +put q 0 add i 1 +for j ulong len b +c = add get p j 1 +if eq get a i get b j +c = get p j +end +x = add get p add j 1 1 +if lt x c +c = x +end +x = add get q j 1 +if lt x c +c = x +end +put q add j 1 c +end +for j ulong add len b 1 +put p j get q j +end +end +ret get p len b +end +fn show a view uchar * b view uchar * to void +emit a +emit sp +emit b +emit sp +emitu lev a b +emit nl +end +fn main to int +show a1 b1 +show a2 b2 +show a3 b3 +ret 0 +end diff --git a/tasks/levenshtein-distance/oracle.py b/tasks/levenshtein-distance/oracle.py new file mode 100644 index 0000000..34334f4 --- /dev/null +++ b/tasks/levenshtein-distance/oracle.py @@ -0,0 +1,14 @@ +# Independent reference for "Levenshtein distance" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def lev(a, b): + d = [[0] * (len(b) + 1) for _ in range(len(a) + 1)] + for i in range(len(a) + 1): + d[i][0] = i + for j in range(len(b) + 1): + d[0][j] = j + for i in range(1, len(a) + 1): + for j in range(1, len(b) + 1): + d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + (a[i - 1] != b[j - 1])) + return d[len(a)][len(b)] +for a, b in [("kitten", "sitting"), ("rosettacode", "raisethysword"), ("saturday", "sunday")]: + print(a, b, lev(a, b)) diff --git a/tasks/longest-common-prefix/expected.txt b/tasks/longest-common-prefix/expected.txt new file mode 100644 index 0000000..0c0bd68 --- /dev/null +++ b/tasks/longest-common-prefix/expected.txt @@ -0,0 +1,8 @@ +[interspecies|interstellar|interstate] -> "inters" +[throne|throne] -> "throne" +[throne|dungeon] -> "" +[throne||throne] -> "" +[cheese] -> "cheese" +[] -> "" +[prefix|suffix] -> "" +[foo|foobar] -> "foo" diff --git a/tasks/longest-common-prefix/longest-common-prefix.om b/tasks/longest-common-prefix/longest-common-prefix.om new file mode 100644 index 0000000..5ab97d1 --- /dev/null +++ b/tasks/longest-common-prefix/longest-common-prefix.om @@ -0,0 +1,86 @@ +note Rosetta Code task "Longest common prefix": https://rosettacode.org/wiki/Longest_common_prefix +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text tarr " -> \"" +text tq "\"\n" +text lb "[" +text rb "]" +text l0 "interspecies|interstellar|interstate" +text l1 "throne|throne" +text l2 "throne|dungeon" +text l3 "throne||throne" +text l4 "cheese" +text l6 "prefix|suffix" +text l7 "foo|foobar" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note a list is its elements separated by '|'; an empty text is the empty list. +note split records each element as (start, length) and returns how many +fn split a view uchar * st edit ulong 16 ln edit ulong 16 to ulong +k = ulong 0 +s = ulong 0 +for i ulong add len a 1 +e = eq i len a +if not e +e = eq get a i 124 +end +if e +put st k s +put ln k sub i s +k = add k 1 +s = add i 1 +end +end +ret k +end +note common walks the first element and stops where any other element ends or differs +fn common a view uchar * st view ulong 16 ln view ulong 16 k ulong to ulong +p = ulong 0 +while lt p get ln 0 bound 4096 +for w ulong k +if eq p get ln w +ret p +end +c = get a add get st 0 p +if ne get a add get st w p c +ret p +end +end +p = add p 1 +end +ret p +end +fn lcp a view uchar * to void +st = new ulong 16 0 +ln = new ulong 16 0 +emit lb +emit a +emit rb +emit tarr +if gt len a 0 +k = split a st ln +emit part a get st 0 common a st ln k +end +emit tq +end +fn main to int +lcp l0 +lcp l1 +lcp l2 +lcp l3 +lcp l4 +lcp part l0 0 0 +lcp l6 +lcp l7 +ret 0 +end diff --git a/tasks/longest-common-prefix/oracle.py b/tasks/longest-common-prefix/oracle.py new file mode 100644 index 0000000..674efdd --- /dev/null +++ b/tasks/longest-common-prefix/oracle.py @@ -0,0 +1,6 @@ +# Independent reference for "Longest common prefix" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import os.path +for s in ['interspecies|interstellar|interstate', 'throne|throne', 'throne|dungeon', 'throne||throne', 'cheese', '', 'prefix|suffix', 'foo|foobar']: + items = s.split("|") if s else [] + print(f'[{s}] -> "{os.path.commonprefix(items)}"') diff --git a/tasks/longest-common-subsequence/expected.txt b/tasks/longest-common-subsequence/expected.txt new file mode 100644 index 0000000..b0e141b --- /dev/null +++ b/tasks/longest-common-subsequence/expected.txt @@ -0,0 +1,2 @@ +tsitest +1234 diff --git a/tasks/longest-common-subsequence/longest-common-subsequence.om b/tasks/longest-common-subsequence/longest-common-subsequence.om new file mode 100644 index 0000000..43f1e97 --- /dev/null +++ b/tasks/longest-common-subsequence/longest-common-subsequence.om @@ -0,0 +1,80 @@ +note Rosetta Code task "Longest common subsequence": https://rosettacode.org/wiki/Longest_common_subsequence +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text a1 "thisisatest" +text b1 "testing123testing" +text a2 "1234" +text b2 "1224533324" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note fill writes the (len a + 1) x (len b + 1) length table into t, row-major with width len b + 1; +note both lengths up to 63 +fn fill a view uchar * b view uchar * t edit ulong 4096 to void +w = add len b 1 +for i ulong len a +for j ulong len b +up = add mul i w add j 1 +x = add get t sub up 1 1 +if ne get a i get b j +x = get t up +l = get t add up sub w 1 +if lt x l +x = l +end +end +put t add up w x +end +end +end +note goup: the cell above is at least the cell to the left +fn goup t view ulong 4096 i ulong j ulong w ulong to bool +u = get t add mul sub i 1 w j +ret ge u get t add mul i w sub j 1 +end +note walk goes back from the corner of the table: a match is taken, otherwise the larger neighbour, +note the upper one on a tie. The subsequence is written into o back to front. +fn walk a view uchar * b view uchar * t view ulong 4096 o edit uchar 64 to void +w = add len b 1 +i = len a +j = len b +k = get t add mul i w j +while and gt i 0 gt j 0 bound 200 +if eq get a sub i 1 get b sub j 1 +k = sub k 1 +put o k get a sub i 1 +i = sub i 1 +j = sub j 1 +else +if goup t i j w +i = sub i 1 +else +j = sub j 1 +end +end +end +end +fn lcs a view uchar * b view uchar * to void +t = new ulong 4096 0 +fill a b t +o = new uchar 64 0 +n = get t add mul len a add len b 1 len b +walk a b t o +emit part o 0 n +emit nl +end +fn main to int +lcs a1 b1 +lcs a2 b2 +ret 0 +end diff --git a/tasks/longest-common-subsequence/oracle.py b/tasks/longest-common-subsequence/oracle.py new file mode 100644 index 0000000..8c4654d --- /dev/null +++ b/tasks/longest-common-subsequence/oracle.py @@ -0,0 +1,19 @@ +# Independent reference for "Longest common subsequence" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def lcs(a, b): + t = [[0] * (len(b) + 1) for _ in range(len(a) + 1)] + for i in range(len(a)): + for j in range(len(b)): + t[i + 1][j + 1] = t[i][j] + 1 if a[i] == b[j] else max(t[i][j + 1], t[i + 1][j]) + out = [] + i, j = len(a), len(b) + while i > 0 and j > 0: + if a[i - 1] == b[j - 1]: + out.append(a[i - 1]); i -= 1; j -= 1 + elif t[i - 1][j] >= t[i][j - 1]: + i -= 1 + else: + j -= 1 + return "".join(reversed(out)) +print(lcs("thisisatest", "testing123testing")) +print(lcs("1234", "1224533324")) diff --git a/tasks/longest-increasing-subsequence/expected.txt b/tasks/longest-increasing-subsequence/expected.txt new file mode 100644 index 0000000..91ffc3f --- /dev/null +++ b/tasks/longest-increasing-subsequence/expected.txt @@ -0,0 +1,4 @@ +3 2 6 4 5 1 +2 4 5 +0 8 4 12 2 10 6 14 1 9 5 13 3 11 7 15 +0 2 6 9 11 15 diff --git a/tasks/longest-increasing-subsequence/longest-increasing-subsequence.om b/tasks/longest-increasing-subsequence/longest-increasing-subsequence.om new file mode 100644 index 0000000..5e8cf92 --- /dev/null +++ b/tasks/longest-increasing-subsequence/longest-increasing-subsequence.om @@ -0,0 +1,150 @@ +note Rosetta Code task "Longest increasing subsequence": https://rosettacode.org/wiki/Longest_increasing_subsequence +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text d1 "3 2 6 4 5 1" +text d2 "0 8 4 12 2 10 6 14 1 9 5 13 3 11 7 15" +global vals long 16 +global ln ulong 16 +global nx ulong 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note lis prints the lexicographically smallest of the longest strictly increasing subsequences: +note ln[i] is the longest increasing run starting at i (computed right to left), nx[i] the smallest +note next value that keeps it longest; the walk then starts at the smallest value with the maximum ln +note grow considers continuing the run at i with element k > i +fn grow i ulong k ulong to void +if gt get vals k get vals i +c = add get ln k 1 +if gt c get ln i +put ln i c +put nx i k +else +if eq c get ln i +if lt get vals k get vals get nx i +put nx i k +end +end +end +end +end +note better says whether a run starting at i beats the best so far (length m starting at s) +fn better i ulong s ulong m ulong to bool +if gt get ln i m +ret true +end +ret and eq get ln i m lt get vals i get vals s +end +fn lis n ulong to void +for x ulong n +i = sub sub n x 1 +put ln i 1 +put nx i n +for j ulong sub sub n i 1 +grow i add add i j 1 +end +end +m = ulong 0 +s = ulong 0 +for i ulong n +if better i s m +m = get ln i +s = i +end +end +i = s +for x ulong m +if gt x 0 +emit sp +end +emiti get vals i +i = get nx i +end +emit nl +end +fn main to int +n = parse d1 +show n +lis n +n = parse d2 +show n +lis n +ret 0 +end diff --git a/tasks/longest-increasing-subsequence/oracle.py b/tasks/longest-increasing-subsequence/oracle.py new file mode 100644 index 0000000..eeb4691 --- /dev/null +++ b/tasks/longest-increasing-subsequence/oracle.py @@ -0,0 +1,13 @@ +# Independent reference for "Longest increasing subsequence" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +from itertools import combinations +for a in ([3, 2, 6, 4, 5, 1], [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]): + print(*a) + best = [] + for k in range(len(a), 0, -1): + found = [[a[i] for i in c] for c in combinations(range(len(a)), k) + if all(a[c[t]] < a[c[t + 1]] for t in range(k - 1))] + if found: + best = min(found) + break + print(*best) diff --git a/tasks/look-and-say-sequence/expected.txt b/tasks/look-and-say-sequence/expected.txt new file mode 100644 index 0000000..07fcde9 --- /dev/null +++ b/tasks/look-and-say-sequence/expected.txt @@ -0,0 +1,15 @@ +1 +11 +21 +1211 +111221 +312211 +13112221 +1113213211 +31131211131221 +13211311123113112211 +11131221133112132113212221 +3113112221232112111312211312113211 +1321132132111213122112311311222113111221131221 +11131221131211131231121113112221121321132132211331222113112211 +311311222113111231131112132112311321322112111312211312111322212311322113212221 diff --git a/tasks/look-and-say-sequence/look-and-say-sequence.om b/tasks/look-and-say-sequence/look-and-say-sequence.om new file mode 100644 index 0000000..0cd0007 --- /dev/null +++ b/tasks/look-and-say-sequence/look-and-say-sequence.om @@ -0,0 +1,70 @@ +note Rosetta Code task "Look-and-say sequence": https://rosettacode.org/wiki/Look-and-say_sequence +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitc writes the single byte c +fn emitc c uchar to void +b = new uchar 1 c +emit b +end +note each term reads the previous one as runs of equal digits, writing run length then digit +note bound: the 15th term has 102 digits; 256-byte buffers hold every term here, and no run is longer than 3 +fn main to int +a = new uchar 256 0 +b = new uchar 256 0 +put a 0 1 +n = ulong 1 +for t ulong 15 +for i ulong n +emitc add get a i uchar 48 +end +emit nl +m = ulong 0 +i = ulong 0 +while lt i n bound 256 +d = get a i +j = i +while lt j n bound 256 +if ne get a j d +break +end +j = add j 1 +end +put b m uchar sub j i +put b add m 1 d +m = add m 2 +i = j +end +for k ulong m +put a k get b k +end +n = m +end +ret 0 +end diff --git a/tasks/look-and-say-sequence/oracle.py b/tasks/look-and-say-sequence/oracle.py new file mode 100644 index 0000000..ea65a73 --- /dev/null +++ b/tasks/look-and-say-sequence/oracle.py @@ -0,0 +1,7 @@ +# Independent reference for "Look-and-say sequence" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import itertools +s = "1" +for _ in range(15): + print(s) + s = "".join(str(len(list(g))) + k for k, g in itertools.groupby(s)) diff --git a/tasks/look-and-say-sequence/wolfram.txt b/tasks/look-and-say-sequence/wolfram.txt new file mode 100644 index 0000000..def8d59 --- /dev/null +++ b/tasks/look-and-say-sequence/wolfram.txt @@ -0,0 +1,12 @@ +Wolfram cross-check of tasks/look-and-say-sequence/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 70add033af1bfa286743a42b8576bc908c3581ca743a6d22d5815ca6505b13a7. +agreement: YES +query: +las[d_] := Flatten[{Length[#], First[#]} & /@ Split[d]]; +s = StringJoin[(StringJoin[ToString /@ #] <> "\n") & /@ NestList[las, {1}, 14]]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"70add033af1bfa286743a42b8576bc908c3581ca743a6d22d5815ca6505b13a7", "1\n11\n21\n1211\n111221\n312211\n13112221\n1113213211\n31131211131221\n13211311123113112211\n11131221133112132113212221\n3113112221232112111312211312113211\n1321132132111213122112311311222113111221131221\n11131221131211131231121113112221121321132132211331222113112211\n311311222113111231131112132112311321322112111312211312111322212311322113212221\n"} diff --git a/tasks/loops-break/expected.txt b/tasks/loops-break/expected.txt new file mode 100644 index 0000000..9f43aa4 --- /dev/null +++ b/tasks/loops-break/expected.txt @@ -0,0 +1,83 @@ +1 +13 +9 +1 +16 +13 +17 +0 +5 +5 +12 +14 +14 +8 +13 +12 +13 +3 +11 +7 +5 +8 +14 +4 +2 +11 +11 +5 +15 +9 +9 +18 +7 +5 +12 +7 +13 +10 +7 +3 +16 +10 +1 +16 +2 +17 +8 +5 +18 +14 +19 +19 +16 +6 +9 +3 +3 +6 +7 +17 +11 +5 +7 +2 +7 +5 +15 +12 +15 +11 +7 +5 +12 +1 +2 +0 +5 +15 +14 +6 +15 +14 +10 diff --git a/tasks/loops-break/loops-break.om b/tasks/loops-break/loops-break.om new file mode 100644 index 0000000..8494ac0 --- /dev/null +++ b/tasks/loops-break/loops-break.om @@ -0,0 +1,49 @@ +note Rosetta Code task "Loops/Break": https://rosettacode.org/wiki/Loops/Break +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a fixed linear congruential generator stands in for a random source (Rosetta asks for +note random numbers 0..19): x' = (1103515245 x + 12345) mod 2^31, r = (x' / 65536) mod 20. +note Print r; stop after printing 10; otherwise print the next value too +fn main to int +x = ulong 42 +while true bound 1000 +x = and add mul x 1103515245 12345 2147483647 +r = rem div x 65536 20 +emitu r +emit nl +if eq r 10 +break +end +x = and add mul x 1103515245 12345 2147483647 +emitu rem div x 65536 20 +emit nl +end +ret 0 +end diff --git a/tasks/loops-break/oracle.py b/tasks/loops-break/oracle.py new file mode 100644 index 0000000..504cd2b --- /dev/null +++ b/tasks/loops-break/oracle.py @@ -0,0 +1,13 @@ +# Independent reference for "Loops/Break" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +x = 42 +def nxt(): + global x + x = (x * 1103515245 + 12345) % 2**31 + return (x // 65536) % 20 +while True: + r = nxt() + print(r) + if r == 10: + break + print(nxt()) diff --git a/tasks/loops-continue/expected.txt b/tasks/loops-continue/expected.txt new file mode 100644 index 0000000..0865d3f --- /dev/null +++ b/tasks/loops-continue/expected.txt @@ -0,0 +1,2 @@ +1, 2, 3, 4, 5 +6, 7, 8, 9, 10 diff --git a/tasks/loops-continue/loops-continue.om b/tasks/loops-continue/loops-continue.om new file mode 100644 index 0000000..138f47d --- /dev/null +++ b/tasks/loops-continue/loops-continue.om @@ -0,0 +1,44 @@ +note Rosetta Code task "Loops/Continue": https://rosettacode.org/wiki/Loops/Continue +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sep ", " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note 1 to 10; after a multiple of 5 a newline and continue, otherwise a separator +fn main to int +for k ulong 10 +i = add k 1 +emitu i +if eq rem i 5 0 +emit nl +continue +end +emit sep +end +ret 0 +end diff --git a/tasks/loops-continue/oracle.py b/tasks/loops-continue/oracle.py new file mode 100644 index 0000000..45f2d76 --- /dev/null +++ b/tasks/loops-continue/oracle.py @@ -0,0 +1,8 @@ +# Independent reference for "Loops/Continue" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for i in range(1, 11): + print(i, end="") + if i % 5 == 0: + print() + continue + print(", ", end="") diff --git a/tasks/loops-do-while/expected.txt b/tasks/loops-do-while/expected.txt new file mode 100644 index 0000000..b414108 --- /dev/null +++ b/tasks/loops-do-while/expected.txt @@ -0,0 +1,6 @@ +1 +2 +3 +4 +5 +6 diff --git a/tasks/loops-do-while/loops-do-while.om b/tasks/loops-do-while/loops-do-while.om new file mode 100644 index 0000000..8f4a3d1 --- /dev/null +++ b/tasks/loops-do-while/loops-do-while.om @@ -0,0 +1,43 @@ +note Rosetta Code task "Loops/Do-while": https://rosettacode.org/wiki/Loops/Do-while +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note start at 0; add 1 and print, then test: repeat while the value is not a multiple of 6 +fn main to int +v = ulong 0 +while true bound 100 +v = add v 1 +emitu v +emit nl +if eq rem v 6 0 +break +end +end +ret 0 +end diff --git a/tasks/loops-do-while/oracle.py b/tasks/loops-do-while/oracle.py new file mode 100644 index 0000000..540fb89 --- /dev/null +++ b/tasks/loops-do-while/oracle.py @@ -0,0 +1,8 @@ +# Independent reference for "Loops/Do-while" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +v = 0 +while True: + v += 1 + print(v) + if v % 6 == 0: + break diff --git a/tasks/loops-downward-for/expected.txt b/tasks/loops-downward-for/expected.txt new file mode 100644 index 0000000..30b148d --- /dev/null +++ b/tasks/loops-downward-for/expected.txt @@ -0,0 +1,11 @@ +10 +9 +8 +7 +6 +5 +4 +3 +2 +1 +0 diff --git a/tasks/loops-downward-for/loops-downward-for.om b/tasks/loops-downward-for/loops-downward-for.om new file mode 100644 index 0000000..db6c2f1 --- /dev/null +++ b/tasks/loops-downward-for/loops-downward-for.om @@ -0,0 +1,38 @@ +note Rosetta Code task "Loops/Downward for": https://rosettacode.org/wiki/Loops/Downward_for +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note count from 10 down to 0 +fn main to int +for i ulong 11 +emitu sub 10 i +emit nl +end +ret 0 +end diff --git a/tasks/loops-downward-for/oracle.py b/tasks/loops-downward-for/oracle.py new file mode 100644 index 0000000..b6734b3 --- /dev/null +++ b/tasks/loops-downward-for/oracle.py @@ -0,0 +1,4 @@ +# Independent reference for "Loops/Downward for" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for i in range(10, -1, -1): + print(i) diff --git a/tasks/loops-for-with-a-specified-step/expected.txt b/tasks/loops-for-with-a-specified-step/expected.txt new file mode 100644 index 0000000..9d54530 --- /dev/null +++ b/tasks/loops-for-with-a-specified-step/expected.txt @@ -0,0 +1 @@ +2, 4, 6, 8, who do we appreciate? diff --git a/tasks/loops-for-with-a-specified-step/loops-for-with-a-specified-step.om b/tasks/loops-for-with-a-specified-step/loops-for-with-a-specified-step.om new file mode 100644 index 0000000..8235c83 --- /dev/null +++ b/tasks/loops-for-with-a-specified-step/loops-for-with-a-specified-step.om @@ -0,0 +1,43 @@ +note Rosetta Code task "Loops/For with a specified step": https://rosettacode.org/wiki/Loops/For_with_a_specified_step +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sep ", " +text tail "who do we appreciate?\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note from 2 to 8 in steps of 2 +fn main to int +i = ulong 2 +while le i 8 bound 10 +emitu i +emit sep +i = add i 2 +end +emit tail +ret 0 +end diff --git a/tasks/loops-for-with-a-specified-step/oracle.py b/tasks/loops-for-with-a-specified-step/oracle.py new file mode 100644 index 0000000..052b74b --- /dev/null +++ b/tasks/loops-for-with-a-specified-step/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Loops/For with a specified step" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for i in range(2, 9, 2): + print(i, end=", ") +print("who do we appreciate?") diff --git a/tasks/loops-for/expected.txt b/tasks/loops-for/expected.txt new file mode 100644 index 0000000..9a639e0 --- /dev/null +++ b/tasks/loops-for/expected.txt @@ -0,0 +1,5 @@ +* +** +*** +**** +***** diff --git a/tasks/loops-for/loops-for.om b/tasks/loops-for/loops-for.om new file mode 100644 index 0000000..463e0a4 --- /dev/null +++ b/tasks/loops-for/loops-for.om @@ -0,0 +1,26 @@ +note Rosetta Code task "Loops/For": https://rosettacode.org/wiki/Loops/For +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text star "*" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note row i has i + 1 stars, five rows +fn main to int +for i ulong 5 +for j ulong add i 1 +emit star +end +emit nl +end +ret 0 +end diff --git a/tasks/loops-for/oracle.py b/tasks/loops-for/oracle.py new file mode 100644 index 0000000..722df51 --- /dev/null +++ b/tasks/loops-for/oracle.py @@ -0,0 +1,4 @@ +# Independent reference for "Loops/For" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for i in range(1, 6): + print("*" * i) diff --git a/tasks/loops-foreach/expected.txt b/tasks/loops-foreach/expected.txt new file mode 100644 index 0000000..7e092e5 --- /dev/null +++ b/tasks/loops-foreach/expected.txt @@ -0,0 +1,5 @@ +apple +banana +cherry +date +elderberry diff --git a/tasks/loops-foreach/loops-foreach.om b/tasks/loops-foreach/loops-foreach.om new file mode 100644 index 0000000..e468d76 --- /dev/null +++ b/tasks/loops-foreach/loops-foreach.om @@ -0,0 +1,28 @@ +note Rosetta Code task "Loops/Foreach": https://rosettacode.org/wiki/Loops/Foreach +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +note the collection: names separated by newlines +text fruit "apple\nbanana\ncherry\ndate\nelderberry\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note foreach visits every newline-terminated element of the collection in order +fn main to int +s = ulong 0 +for i ulong len fruit +if eq get fruit i 10 +emit part fruit s add sub i s 1 +s = add i 1 +end +end +ret 0 +end diff --git a/tasks/loops-foreach/oracle.py b/tasks/loops-foreach/oracle.py new file mode 100644 index 0000000..f7bc92b --- /dev/null +++ b/tasks/loops-foreach/oracle.py @@ -0,0 +1,4 @@ +# Independent reference for "Loops/Foreach" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for fruit in ['apple', 'banana', 'cherry', 'date', 'elderberry']: + print(fruit) diff --git a/tasks/loops-n-plus-one-half/expected.txt b/tasks/loops-n-plus-one-half/expected.txt new file mode 100644 index 0000000..f22ebfe --- /dev/null +++ b/tasks/loops-n-plus-one-half/expected.txt @@ -0,0 +1 @@ +1, 2, 3, 4, 5, 6, 7, 8, 9, 10 diff --git a/tasks/loops-n-plus-one-half/loops-n-plus-one-half.om b/tasks/loops-n-plus-one-half/loops-n-plus-one-half.om new file mode 100644 index 0000000..9e4bcfa --- /dev/null +++ b/tasks/loops-n-plus-one-half/loops-n-plus-one-half.om @@ -0,0 +1,45 @@ +note Rosetta Code task "Loops/N plus one half": https://rosettacode.org/wiki/Loops/N_plus_one_half +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sep ", " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note the loop writes the number, leaves at 10, else the separator: the last pass is half a pass +fn main to int +i = ulong 1 +while true bound 100 +emitu i +if eq i 10 +break +end +emit sep +i = add i 1 +end +emit nl +ret 0 +end diff --git a/tasks/loops-n-plus-one-half/oracle.py b/tasks/loops-n-plus-one-half/oracle.py new file mode 100644 index 0000000..842ebda --- /dev/null +++ b/tasks/loops-n-plus-one-half/oracle.py @@ -0,0 +1,3 @@ +# Independent reference for "Loops/N plus one half" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +print(", ".join(str(i) for i in range(1, 11))) diff --git a/tasks/loops-while/expected.txt b/tasks/loops-while/expected.txt new file mode 100644 index 0000000..3a058ca --- /dev/null +++ b/tasks/loops-while/expected.txt @@ -0,0 +1,11 @@ +1024 +512 +256 +128 +64 +32 +16 +8 +4 +2 +1 diff --git a/tasks/loops-while/loops-while.om b/tasks/loops-while/loops-while.om new file mode 100644 index 0000000..ab86aa7 --- /dev/null +++ b/tasks/loops-while/loops-while.om @@ -0,0 +1,40 @@ +note Rosetta Code task "Loops/While": https://rosettacode.org/wiki/Loops/While +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note print n and halve it (integer division) while it is above zero, from 1024 +fn main to int +n = ulong 1024 +while gt n 0 bound 64 +emitu n +emit nl +n = div n 2 +end +ret 0 +end diff --git a/tasks/loops-while/oracle.py b/tasks/loops-while/oracle.py new file mode 100644 index 0000000..fe9023e --- /dev/null +++ b/tasks/loops-while/oracle.py @@ -0,0 +1,6 @@ +# Independent reference for "Loops/While" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +n = 1024 +while n > 0: + print(n) + n //= 2 diff --git a/tasks/lucas-lehmer-test/expected.txt b/tasks/lucas-lehmer-test/expected.txt new file mode 100644 index 0000000..5391582 --- /dev/null +++ b/tasks/lucas-lehmer-test/expected.txt @@ -0,0 +1,2 @@ +Mersenne primes 2^p - 1 for p <= 63, by the Lucas-Lehmer test: +M2 M3 M5 M7 M13 M17 M19 M31 M61 diff --git a/tasks/lucas-lehmer-test/lucas-lehmer-test.om b/tasks/lucas-lehmer-test/lucas-lehmer-test.om new file mode 100644 index 0000000..0781256 --- /dev/null +++ b/tasks/lucas-lehmer-test/lucas-lehmer-test.om @@ -0,0 +1,111 @@ +note Rosetta Code task "Lucas-Lehmer test": https://rosettacode.org/wiki/Lucas-Lehmer_test +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text em "M" +text hdr "Mersenne primes 2^p - 1 for p <= 63, by the Lucas-Lehmer test:" +note w 0 and w 1 hold the high and low 64 bits of the last 128-bit product +global w ulong 2 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note mul128 forms a * b exactly from four 32 x 32 -> 64-bit partial products +fn mul128 a ulong b ulong to void +al = and a 0xffffffff +ah = shr a 32 +bl = and b 0xffffffff +bh = shr b 32 +ll = mul al bl +lh = mul al bh +hl = mul ah bl +hi = add mul ah bh shr lh 32 +hi = add hi shr hl 32 +mid = add shr ll 32 and lh 0xffffffff +mid = add mid and hl 0xffffffff +put w 1 add and ll 0xffffffff shl mid 32 +put w 0 add hi shr mid 32 +end +note sqm is s * s mod m for m = 2^p - 1, 2 < p <= 63,, s < m: the product x = q * 2^p + r folds to q + r +note x < 2^(2p), so q < 2^p and q + r < 2^64; a second fold and one subtraction finish it +fn sqm s ulong p ulong m ulong to ulong +mul128 s s +q = or shl get w 0 sub 64 p shr get w 1 p +t = add q and get w 1 m +t = add and t m shr t p +if ge t m +t = sub t m +end +ret t +end +note p is prime by trial division (p <= 63) +fn smallp p ulong to bool +if lt p 2 +ret false +end +d = ulong 2 +while le mul d d p bound 8 +if eq rem p d 0 +ret false +end +d = add d 1 +end +ret true +end +note Lucas-Lehmer for odd prime p: s = 4, then p - 2 times s = s^2 - 2 mod M; M is prime iff s ends at 0 +fn lltest p ulong to bool +m = sub shl ulong 1 p 1 +s = ulong 4 +for i ulong sub p 2 +s = sqm s p m +if ge s 2 +s = sub s 2 +else +s = sub add s m 2 +end +end +ret eq s 0 +end +fn main to int +emit hdr +emit nl +note M2 = 3 is prime by definition; the test as stated covers odd prime p +emit em +emitu 2 +p = ulong 3 +while le p 63 bound 61 +if smallp p +if lltest p +emit sp +emit em +emitu p +end +end +p = add p 2 +end +emit nl +ret 0 +end diff --git a/tasks/lucas-lehmer-test/oracle.py b/tasks/lucas-lehmer-test/oracle.py new file mode 100644 index 0000000..149d9ba --- /dev/null +++ b/tasks/lucas-lehmer-test/oracle.py @@ -0,0 +1,16 @@ +# Independent reference for "Lucas-Lehmer test" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# the exponents p <= 63 for which 2^p - 1 has no divisor up to its square root, by trial division over +# 2kp + 1, the only form a divisor of 2^p - 1 can take for prime p (a different method from Lucas-Lehmer) +def mersenne_prime(p): + if p < 2 or any(p % d == 0 for d in range(2, p)): + return False + m = (1 << p) - 1 + q = 2 * p + 1 + while q * q <= m: + if m % q == 0: + return False + q += 2 * p + return True +print("Mersenne primes 2^p - 1 for p <= 63, by the Lucas-Lehmer test:") +print(" ".join(f"M{p}" for p in range(2, 64) if mersenne_prime(p))) diff --git a/tasks/lucas-lehmer-test/wolfram.txt b/tasks/lucas-lehmer-test/wolfram.txt new file mode 100644 index 0000000..cd8395e --- /dev/null +++ b/tasks/lucas-lehmer-test/wolfram.txt @@ -0,0 +1,12 @@ +Wolfram cross-check of tasks/lucas-lehmer-test/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 477dcd8c7c35a449e29fab6d3e4d7e152464a6dece681cbf8b7a55b906503ea2. +agreement: YES +query: +s = "Mersenne primes 2^p - 1 for p <= 63, by the Lucas-Lehmer test:\n" <> + StringRiffle[("M" <> ToString[#]) & /@ Select[Range[2, 63], MersennePrimeExponentQ], " "] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"477dcd8c7c35a449e29fab6d3e4d7e152464a6dece681cbf8b7a55b906503ea2", "Mersenne primes 2^p - 1 for p <= 63, by the Lucas-Lehmer test:\nM2 M3 M5 M7 M13 M17 M19 M31 M61\n"} diff --git a/tasks/ludic-numbers/expected.txt b/tasks/ludic-numbers/expected.txt new file mode 100644 index 0000000..2a8d961 --- /dev/null +++ b/tasks/ludic-numbers/expected.txt @@ -0,0 +1,4 @@ +first 25 ludic numbers: 1 2 3 5 7 11 13 17 23 25 29 37 41 43 47 53 61 67 71 77 83 89 91 97 107 +ludic numbers <= 1000: 142 +2000th to 2005th ludic numbers: 21475 21481 21487 21493 21503 21511 +ludic triplets (x, x+2, x+6) below 250: (1, 3, 7) (5, 7, 11) (11, 13, 17) (23, 25, 29) (41, 43, 47) (173, 175, 179) (221, 223, 227) (233, 235, 239) diff --git a/tasks/ludic-numbers/ludic-numbers.om b/tasks/ludic-numbers/ludic-numbers.om new file mode 100644 index 0000000..4d298e4 --- /dev/null +++ b/tasks/ludic-numbers/ludic-numbers.om @@ -0,0 +1,106 @@ +note Rosetta Code task "Ludic numbers": https://rosettacode.org/wiki/Ludic_numbers +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text first "first 25 ludic numbers:" +text upto "ludic numbers <= 1000: " +text tfrom "2000th to 2005th ludic numbers:" +text trip "ludic triplets (x, x+2, x+6) below 250:" +text lp " (" +text cm ", " +text rp ")" +note the sieve runs on 2..21999; the 2005th ludic number, 21511, is below that limit, and every ludic number +note found is final because removing every k-th survivor never touches the survivors in front of it +global ls ulong 22000 +global lu ulong 22000 +global isl uchar 22000 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note round: the first survivor k is ludic; it and every k-th survivor after it (positions 0, k, 2k, ...) go +note bound: each round removes at least one survivor, so at most 21998 rounds over at most 21998 survivors +fn main to int +n = ulong 0 +for i ulong 21998 +put ls i add i 2 +n = add n 1 +end +put lu 0 1 +put isl 1 1 +c = ulong 1 +while gt n 0 bound 22000 +k = get ls 0 +put lu c k +put isl k 1 +c = add c 1 +m = ulong 0 +for i ulong n +if ne rem i k 0 +put ls m get ls i +m = add m 1 +end +end +n = m +end +emit first +for i ulong 25 +emit sp +emitu get lu i +end +emit nl +emit upto +m = ulong 0 +for i ulong c +if le get lu i 1000 +m = add m 1 +end +end +emitu m +emit nl +emit tfrom +for i ulong 6 +emit sp +emitu get lu add 1999 i +end +emit nl +emit trip +for x ulong 244 +if eq get isl x 1 +if and eq get isl add x 2 1 eq get isl add x 6 1 +emit lp +emitu x +emit cm +emitu add x 2 +emit cm +emitu add x 6 +emit rp +end +end +end +emit nl +ret 0 +end diff --git a/tasks/ludic-numbers/oracle.py b/tasks/ludic-numbers/oracle.py new file mode 100644 index 0000000..f944994 --- /dev/null +++ b/tasks/ludic-numbers/oracle.py @@ -0,0 +1,13 @@ +# Independent reference for "Ludic numbers" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +lst = list(range(2, 22000)) +lud = [1] +while lst: + lud.append(lst[0]) + del lst[::lst[0]] +s = set(lud) +print("first 25 ludic numbers: " + " ".join(map(str, lud[:25]))) +print(f"ludic numbers <= 1000: {sum(1 for x in lud if x <= 1000)}") +print("2000th to 2005th ludic numbers: " + " ".join(map(str, lud[1999:2005]))) +print("ludic triplets (x, x+2, x+6) below 250: " + + " ".join(f"({x}, {x + 2}, {x + 6})" for x in lud if x + 6 < 250 and x + 2 in s and x + 6 in s)) diff --git a/tasks/ludic-numbers/wolfram.txt b/tasks/ludic-numbers/wolfram.txt new file mode 100644 index 0000000..d9f58db --- /dev/null +++ b/tasks/ludic-numbers/wolfram.txt @@ -0,0 +1,17 @@ +Wolfram cross-check of tasks/ludic-numbers/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = e5435280ad1a05d299153d9e18bbb035596ae111e48d5e69685d7b77e62f4eb3. +agreement: YES +query: +lud = Module[{l = Range[2, 21999], out = {1}}, While[l =!= {}, AppendTo[out, First[l]]; l = Delete[l, List /@ Range[1, Length[l], First[l]]]]; out]; +sl = Association[(# -> True) & /@ lud]; +s = "first 25 ludic numbers: " <> StringRiffle[ToString /@ Take[lud, 25], " "] <> "\n" <> + "ludic numbers <= 1000: " <> ToString[Count[lud, x_ /; x <= 1000]] <> "\n" <> + "2000th to 2005th ludic numbers: " <> StringRiffle[ToString /@ lud[[2000 ;; 2005]], " "] <> "\n" <> + "ludic triplets (x, x+2, x+6) below 250: " <> StringRiffle[("(" <> ToString[#] <> ", " <> ToString[# + 2] <> ", " <> ToString[# + 6] <> ")") & /@ + Select[lud, # + 6 < 250 && KeyExistsQ[sl, # + 2] && KeyExistsQ[sl, # + 6] &], " "] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"e5435280ad1a05d299153d9e18bbb035596ae111e48d5e69685d7b77e62f4eb3", "first 25 ludic numbers: 1 2 3 5 7 11 13 17 23 25 29 37 41 43 47 53 61 67 71 77 83 89 91 97 107\nludic numbers <= 1000: 142\n2000th to 2005th ludic numbers: 21475 21481 21487 21493 21503 21511\nludic triplets (x, x+2, x+6) below 250: (1, 3, 7) (5, 7, 11) (11, 13, 17) (23, 25, 29) (41, 43, 47) (173, 175, 179) (221, 223, 227) (233, 235, 239)\n"} diff --git a/tasks/luhn-test-of-credit-card-numbers/expected.txt b/tasks/luhn-test-of-credit-card-numbers/expected.txt new file mode 100644 index 0000000..543a48d --- /dev/null +++ b/tasks/luhn-test-of-credit-card-numbers/expected.txt @@ -0,0 +1,4 @@ +49927398716 valid +49927398717 invalid +1234567812345678 invalid +1234567812345670 valid diff --git a/tasks/luhn-test-of-credit-card-numbers/luhn-test-of-credit-card-numbers.om b/tasks/luhn-test-of-credit-card-numbers/luhn-test-of-credit-card-numbers.om new file mode 100644 index 0000000..631d32b --- /dev/null +++ b/tasks/luhn-test-of-credit-card-numbers/luhn-test-of-credit-card-numbers.om @@ -0,0 +1,52 @@ +note Rosetta Code task "Luhn test of credit card numbers": https://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text tv " valid\n" +text ti " invalid\n" +text n1 "49927398716" +text n2 "49927398717" +text n3 "1234567812345678" +text n4 "1234567812345670" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note from the right, odd places add their digit, even places add the digit sum of twice the digit +fn luhn a view uchar * to bool +s = ulong 0 +n = len a +for i ulong n +d = ulong sub get a sub sub n 1 i 48 +if eq rem i 2 1 +d = mul d 2 +if gt d 9 +d = sub d 9 +end +end +s = add s d +end +ret eq rem s 10 0 +end +fn show a view uchar * to void +emit a +if luhn a +emit tv +else +emit ti +end +end +fn main to int +show n1 +show n2 +show n3 +show n4 +ret 0 +end diff --git a/tasks/luhn-test-of-credit-card-numbers/oracle.py b/tasks/luhn-test-of-credit-card-numbers/oracle.py new file mode 100644 index 0000000..9d1c3e8 --- /dev/null +++ b/tasks/luhn-test-of-credit-card-numbers/oracle.py @@ -0,0 +1,12 @@ +# Independent reference for "Luhn test of credit card numbers" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def luhn(s): + total = 0 + for i, ch in enumerate(reversed(s)): + d = int(ch) + if i % 2 == 1: + d = sum(divmod(2 * d, 10)) + total += d + return total % 10 == 0 +for s in ["49927398716", "49927398717", "1234567812345678", "1234567812345670"]: + print(s, "valid" if luhn(s) else "invalid") diff --git a/tasks/magic-squares-of-odd-order/expected.txt b/tasks/magic-squares-of-odd-order/expected.txt new file mode 100644 index 0000000..2c26322 --- /dev/null +++ b/tasks/magic-squares-of-odd-order/expected.txt @@ -0,0 +1,22 @@ + 8 1 6 + 3 5 7 + 4 9 2 +magic constant: 15 + + 17 24 1 8 15 + 23 5 7 14 16 + 4 6 13 20 22 + 10 12 19 21 3 + 11 18 25 2 9 +magic constant: 65 + + 47 58 69 80 1 12 23 34 45 + 57 68 79 9 11 22 33 44 46 + 67 78 8 10 21 32 43 54 56 + 77 7 18 20 31 42 53 55 66 + 6 17 19 30 41 52 63 65 76 + 16 27 29 40 51 62 64 75 5 + 26 28 39 50 61 72 74 4 15 + 36 38 49 60 71 73 3 14 25 + 37 48 59 70 81 2 13 24 35 +magic constant: 369 diff --git a/tasks/magic-squares-of-odd-order/magic-squares-of-odd-order.om b/tasks/magic-squares-of-odd-order/magic-squares-of-odd-order.om new file mode 100644 index 0000000..f1313b2 --- /dev/null +++ b/tasks/magic-squares-of-odd-order/magic-squares-of-odd-order.om @@ -0,0 +1,94 @@ +note Rosetta Code task "Magic squares of odd order": https://rosettacode.org/wiki/Magic_squares_of_odd_order +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tconst "magic constant: " +global grid ulong 81 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt w sub 20 i +i = sub 20 w +end +emit part d i sub 20 i +end +note the Siamese method: 1 goes in the middle of the top row; each next number goes one up and one +note right (wrapping round), or straight down when that cell is taken, which happens every n numbers +fn siamese n ulong to void +for i ulong mul n n +put grid i 0 +end +r = ulong 0 +c = div n 2 +for k ulong mul n n +put grid add mul r n c add k 1 +ur = rem sub add r n 1 n +uc = rem add c 1 n +if eq get grid add mul ur n uc 0 +r = ur +c = uc +else +r = rem add r 1 n +end +end +end +fn show n ulong to void +for i ulong n +for j ulong n +emitw get grid add mul i n j 4 +end +emit nl +end +emit tconst +emitu div mul n add mul n n 1 2 +emit nl +end +fn main to int +siamese 3 +show 3 +emit nl +siamese 5 +show 5 +emit nl +siamese 9 +show 9 +ret 0 +end diff --git a/tasks/magic-squares-of-odd-order/oracle.py b/tasks/magic-squares-of-odd-order/oracle.py new file mode 100644 index 0000000..d744848 --- /dev/null +++ b/tasks/magic-squares-of-odd-order/oracle.py @@ -0,0 +1,23 @@ +# Independent reference for "Magic squares of odd order" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# Cross-check, WolframLanguageEvaluator: Table[n (n^2 + 1)/2, {n, {3, 5, 9}}] -> {15, 65, 369} +# the cells are kept in a dict keyed by (row, column) on a torus, and the result is checked: every row, +# column and both diagonals must sum to the magic constant +def siamese(n): + m = {} + r, c = 0, n // 2 + for k in range(1, n * n + 1): + m[r, c] = k + nxt = ((r - 1) % n, (c + 1) % n) + r, c = ((r + 1) % n, c) if nxt in m else nxt + return [[m[i, j] for j in range(n)] for i in range(n)] +for t, n in enumerate((3, 5, 9)): + if t: + print() + sq = siamese(n) + s = n * (n * n + 1) // 2 + assert all(sum(row) == s for row in sq) and all(sum(col) == s for col in zip(*sq)) + assert sum(sq[i][i] for i in range(n)) == s == sum(sq[i][n - 1 - i] for i in range(n)) + for row in sq: + print("".join(f"{v:4}" for v in row)) + print("magic constant:", s) diff --git a/tasks/matrix-multiplication/expected.txt b/tasks/matrix-multiplication/expected.txt new file mode 100644 index 0000000..2621f70 --- /dev/null +++ b/tasks/matrix-multiplication/expected.txt @@ -0,0 +1,21 @@ + 1 2 3 + 4 5 6 +times + 7 8 + 9 10 + 11 12 +equals + 58 64 + 139 154 + + -10 -7 -4 + -1 2 5 + 8 11 14 +times + 4 -1 1 + 3 -2 0 + 2 4 -1 +equals + -69 8 -6 + 12 17 -6 + 93 26 -6 diff --git a/tasks/matrix-multiplication/matrix-multiplication.om b/tasks/matrix-multiplication/matrix-multiplication.om new file mode 100644 index 0000000..af159b0 --- /dev/null +++ b/tasks/matrix-multiplication/matrix-multiplication.om @@ -0,0 +1,112 @@ +note Rosetta Code task "Matrix multiplication": https://rosettacode.org/wiki/Matrix_multiplication +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text ttimes "times\n" +text tequals "equals\n" +global mat long 27 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitwi writes a signed n right-aligned in w columns (w at most 20) +fn emitwi n long w ulong to void +d = new uchar 21 32 +i = ulong 21 +v = n +if lt n 0 +v = neg n +end +while true bound 20 +i = sub i 1 +put d i uchar add long 48 rem v 10 +v = div v 10 +if eq v 0 +break +end +end +if lt n 0 +i = sub i 1 +put d i 45 +end +if gt w sub 21 i +i = sub 21 w +end +emit part d i sub 21 i +end +note mat holds the three matrices row by row: a at offset 0, b at 9 and their product at 18 +note showm writes the r x c matrix at offset o of mat, each entry right-aligned in w columns +fn showm o ulong r ulong c ulong w ulong to void +for i ulong r +for j ulong c +emitwi get mat add o add mul i c j w +end +emit nl +end +end +note dot is row i of the m-column matrix a times column j of the p-column matrix b +fn dot i ulong j ulong m ulong p ulong to long +s = long 0 +for k ulong m +x = get mat add mul i m k +y = get mat add 9 add mul k p j +s = add s mul x y +end +ret s +end +note matmul computes the n x p product of the n x m matrix a and the m x p matrix b +fn matmul n ulong m ulong p ulong to void +for i ulong n +for j ulong p +put mat add 18 add mul i p j dot i j m p +end +end +end +fn main to int +for i ulong 6 +put mat i long add i 1 +put mat add 9 i long add i 7 +end +showm 0 2 3 5 +emit ttimes +showm 9 3 2 5 +emit tequals +matmul 2 3 2 +showm 18 2 2 5 +emit nl +for i ulong 9 +put mat i sub long mul i 3 10 +put mat add 9 i sub 4 long rem mul i 5 7 +end +showm 0 3 3 5 +emit ttimes +showm 9 3 3 5 +emit tequals +matmul 3 3 3 +showm 18 3 3 5 +ret 0 +end \ No newline at end of file diff --git a/tasks/matrix-multiplication/oracle.py b/tasks/matrix-multiplication/oracle.py new file mode 100644 index 0000000..9a367aa --- /dev/null +++ b/tasks/matrix-multiplication/oracle.py @@ -0,0 +1,15 @@ +# Independent reference for "Matrix multiplication" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def matmul(a, b): + # each entry as a dot product of a row of a with a column of b (zip(*b) gives the columns) + return [[sum(x * y for x, y in zip(row, col)) for col in zip(*b)] for row in a] +def show(m): + for row in m: + print("".join(f"{v:5}" for v in row)) +cases = [([[1, 2, 3], [4, 5, 6]], [[7, 8], [9, 10], [11, 12]]), + ([[3 * i - 10 for i in range(r * 3, r * 3 + 3)] for r in range(3)], + [[4 - (5 * i) % 7 for i in range(r * 3, r * 3 + 3)] for r in range(3)])] +for n, (a, b) in enumerate(cases): + if n: + print() + show(a); print("times"); show(b); print("equals"); show(matmul(a, b)) diff --git a/tasks/matrix-transposition/expected.txt b/tasks/matrix-transposition/expected.txt new file mode 100644 index 0000000..72191f6 --- /dev/null +++ b/tasks/matrix-transposition/expected.txt @@ -0,0 +1,9 @@ + -50 -49 -46 -41 -34 + -25 -14 -1 14 31 + 50 71 94 119 146 +transposed: + -50 -25 50 + -49 -14 71 + -46 -1 94 + -41 14 119 + -34 31 146 diff --git a/tasks/matrix-transposition/matrix-transposition.om b/tasks/matrix-transposition/matrix-transposition.om new file mode 100644 index 0000000..f2a6496 --- /dev/null +++ b/tasks/matrix-transposition/matrix-transposition.om @@ -0,0 +1,91 @@ +note Rosetta Code task "Matrix transposition": https://rosettacode.org/wiki/Matrix_transposition +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tt "transposed:\n" +global ma long 15 +global mt long 15 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitwi writes a signed n right-aligned in w columns (w at most 20) +fn emitwi n long w ulong to void +d = new uchar 21 32 +i = ulong 21 +v = n +if lt n 0 +v = neg n +end +while true bound 20 +i = sub i 1 +put d i uchar add long 48 rem v 10 +v = div v 10 +if eq v 0 +break +end +end +if lt n 0 +i = sub i 1 +put d i 45 +end +if gt w sub 21 i +i = sub 21 w +end +emit part d i sub 21 i +end +note ma is a 3 x 5 matrix stored row by row; its transpose mt is 5 x 3, mt[j][i] = ma[i][j] +fn showt r ulong c ulong to void +for i ulong r +for j ulong c +emitwi get mt add mul i c j 4 +end +emit nl +end +end +fn showa r ulong c ulong to void +for i ulong r +for j ulong c +emitwi get ma add mul i c j 4 +end +emit nl +end +end +fn main to int +for i ulong 15 +put ma i sub long mul i i 50 +end +showa 3 5 +for i ulong 3 +for j ulong 5 +put mt add mul j 3 i get ma add mul i 5 j +end +end +emit tt +showt 5 3 +ret 0 +end diff --git a/tasks/matrix-transposition/oracle.py b/tasks/matrix-transposition/oracle.py new file mode 100644 index 0000000..f959195 --- /dev/null +++ b/tasks/matrix-transposition/oracle.py @@ -0,0 +1,8 @@ +# Independent reference for "Matrix transposition" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +m = [[i * i - 50 for i in range(r * 5, r * 5 + 5)] for r in range(3)] +for row in m: + print("".join(f"{v:4}" for v in row)) +print("transposed:") +for row in zip(*m): + print("".join(f"{v:4}" for v in row)) diff --git a/tasks/matrix-with-two-diagonals/expected.txt b/tasks/matrix-with-two-diagonals/expected.txt new file mode 100644 index 0000000..a9009a6 --- /dev/null +++ b/tasks/matrix-with-two-diagonals/expected.txt @@ -0,0 +1,14 @@ +1 0 0 0 0 1 +0 1 0 0 1 0 +0 0 1 1 0 0 +0 0 1 1 0 0 +0 1 0 0 1 0 +1 0 0 0 0 1 + +1 0 0 0 0 0 1 +0 1 0 0 0 1 0 +0 0 1 0 1 0 0 +0 0 0 1 0 0 0 +0 0 1 0 1 0 0 +0 1 0 0 0 1 0 +1 0 0 0 0 0 1 diff --git a/tasks/matrix-with-two-diagonals/matrix-with-two-diagonals.om b/tasks/matrix-with-two-diagonals/matrix-with-two-diagonals.om new file mode 100644 index 0000000..f19aef5 --- /dev/null +++ b/tasks/matrix-with-two-diagonals/matrix-with-two-diagonals.om @@ -0,0 +1,54 @@ +note Rosetta Code task "Matrix with two diagonals": https://rosettacode.org/wiki/Matrix_with_two_diagonals +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note entry (i, j) is 1 on the main diagonal i = j and on the anti-diagonal i + j = n - 1, else 0 +fn twodiag n ulong to void +for i ulong n +for j ulong n +if gt j 0 +emit sp +end +if or eq i j eq add i j sub n 1 +emitu 1 +else +emitu 0 +end +end +emit nl +end +end +fn main to int +twodiag 6 +emit nl +twodiag 7 +ret 0 +end diff --git a/tasks/matrix-with-two-diagonals/oracle.py b/tasks/matrix-with-two-diagonals/oracle.py new file mode 100644 index 0000000..5ba27fc --- /dev/null +++ b/tasks/matrix-with-two-diagonals/oracle.py @@ -0,0 +1,9 @@ +# Independent reference for "Matrix with two diagonals" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for k, n in enumerate((6, 7)): + if k: + print() + for i in range(n): + row = ["0"] * n + row[i] = row[n - 1 - i] = "1" + print(" ".join(row)) diff --git a/tasks/maximum-triangle-path-sum/expected.txt b/tasks/maximum-triangle-path-sum/expected.txt new file mode 100644 index 0000000..f219bbe --- /dev/null +++ b/tasks/maximum-triangle-path-sum/expected.txt @@ -0,0 +1,2 @@ +maximum path sum: 321 +maximum path sum: 1320 diff --git a/tasks/maximum-triangle-path-sum/maximum-triangle-path-sum.om b/tasks/maximum-triangle-path-sum/maximum-triangle-path-sum.om new file mode 100644 index 0000000..37c8ada --- /dev/null +++ b/tasks/maximum-triangle-path-sum/maximum-triangle-path-sum.om @@ -0,0 +1,111 @@ +note Rosetta Code task "Maximum triangle path sum": https://rosettacode.org/wiki/Maximum_triangle_path_sum +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tri "55 94 48 95 30 96 77 71 26 67 97 13 76 38 45 07 36 79 16 37 68 48 07 09 18 70 26 06 18 72 79 46 59 79 29 90 20 76 87 11 32 07 07 49 18 27 83 58 35 71 11 25 57 29 85 14 64 36 96 27 11 58 56 92 18 55 02 90 03 60 48 49 41 46 33 36 47 23 92 50 48 02 36 59 42 79 72 20 82 77 42 56 78 38 80 39 75 02 71 66 66 01 03 55 72 44 25 67 84 71 67 11 61 40 57 58 89 40 56 36 85 32 25 85 57 48 84 35 47 62 17 01 01 99 89 52 06 71 28 75 94 48 37 10 23 51 06 48 53 18 74 98 15 27 02 92 23 08 71 76 84 15 52 92 63 81 10 44 10 69 93" +text tsmall "55 94 48 95 30 96 77 71 26 67" +text tmax "maximum path sum: " +global vals long 171 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note fold adds to vals[p] the larger of vals[q] and vals[q+1], the two entries below it +fn fold p ulong q ulong to void +lft = get vals q +rgt = get vals add q 1 +if gt lft rgt +put vals p add get vals p lft +else +put vals p add get vals p rgt +end +end +note rows are stored one after another, so row r starts at r*(r+1)/2; the sum folds from the bottom row +note up, each entry adding the larger of the two entries below it, until vals[0] holds the answer +fn best rows ulong to long +for x ulong sub rows 1 +r = sub sub rows x 2 +b = div mul r add r 1 2 +nb = add b add r 1 +for c ulong add r 1 +fold add b c add nb c +end +end +ret get vals 0 +end +fn main to int +n = parse tsmall +emit tmax +emiti best 4 +emit nl +n = parse tri +emit tmax +emiti best 18 +emit nl +ret 0 +end diff --git a/tasks/maximum-triangle-path-sum/oracle.py b/tasks/maximum-triangle-path-sum/oracle.py new file mode 100644 index 0000000..60f67e9 --- /dev/null +++ b/tasks/maximum-triangle-path-sum/oracle.py @@ -0,0 +1,16 @@ +# Independent reference for "Maximum triangle path sum" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def rows(flat): + vals, out, r = [int(v) for v in flat.split()], [], 1 + while vals: + out.append(vals[:r]); vals = vals[r:]; r += 1 + return out +def best_path(tri): + # top-down: the best total to reach each entry, then the best of the last row + acc = [tri[0][0]] + for row in tri[1:]: + acc = [v + max(acc[c - 1] if c > 0 else -10**9, acc[c] if c < len(acc) else -10**9) + for c, v in enumerate(row)] + return max(acc) +for flat in ("55 94 48 95 30 96 77 71 26 67", '55 94 48 95 30 96 77 71 26 67 97 13 76 38 45 07 36 79 16 37 68 48 07 09 18 70 26 06 18 72 79 46 59 79 29 90 20 76 87 11 32 07 07 49 18 27 83 58 35 71 11 25 57 29 85 14 64 36 96 27 11 58 56 92 18 55 02 90 03 60 48 49 41 46 33 36 47 23 92 50 48 02 36 59 42 79 72 20 82 77 42 56 78 38 80 39 75 02 71 66 66 01 03 55 72 44 25 67 84 71 67 11 61 40 57 58 89 40 56 36 85 32 25 85 57 48 84 35 47 62 17 01 01 99 89 52 06 71 28 75 94 48 37 10 23 51 06 48 53 18 74 98 15 27 02 92 23 08 71 76 84 15 52 92 63 81 10 44 10 69 93'): + print("maximum path sum:", best_path(rows(flat))) diff --git a/tasks/maze-generation/expected.txt b/tasks/maze-generation/expected.txt new file mode 100644 index 0000000..fcb86d9 --- /dev/null +++ b/tasks/maze-generation/expected.txt @@ -0,0 +1,13 @@ ++---+---+---+---+---+---+---+---+---+---+ +| | | | ++ +---+---+---+---+ + + +---+---+ +| | | | | ++---+---+---+ + +---+ +---+---+ + +| | | | | ++ + + +---+---+---+---+---+ + + +| | | | | | ++ +---+---+ +---+---+ + +---+---+ +| | | | | | ++---+ + +---+ +---+ +---+---+ + +| | | ++---+---+---+---+---+---+---+---+---+---+ diff --git a/tasks/maze-generation/maze-generation.om b/tasks/maze-generation/maze-generation.om new file mode 100644 index 0000000..1267a17 --- /dev/null +++ b/tasks/maze-generation/maze-generation.om @@ -0,0 +1,162 @@ +note Rosetta Code task "Maze generation": https://rosettacode.org/wiki/Maze_generation +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tcorner "+" +text thwall "---+" +text thopen " +" +text tvwall "|" +text tcell " " +text tpath " * " +text tspace " " +global seed ulong 1 +global ewall uchar 60 +global swall uchar 60 +global seen uchar 60 +global stk ulong 60 +global cand ulong 4 +global mark uchar 60 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note rnd is Knuth's MMIX linear congruential generator, x = x*6364136223846793005 + 1442695040888963407 +note mod 2^64 (ulong arithmetic wraps), starting from seed 0; it returns the top 31 bits, x >> 33 +fn rnd to ulong +x = add mul get seed 0 6364136223846793005 1442695040888963407 +put seed 0 x +ret shr x 33 +end +note the maze has 10 columns and 6 rows of cells, cell c = row*10 + column; ewall[c] and swall[c] are the +note walls on its east and south sides, all standing at the start +note neighbour says which cell lies in direction d (0 north, 1 east, 2 south, 3 west), 60 if none +fn neighbour c ulong d ulong to ulong +r = div c 10 +k = rem c 10 +if and eq d 0 gt r 0 +ret sub c 10 +end +if and eq d 1 lt k 9 +ret add c 1 +end +if and eq d 2 lt r 5 +ret add c 10 +end +if and eq d 3 gt k 0 +ret sub c 1 +end +ret 60 +end +note knock removes the wall between neighbouring cells a and b +fn knock a ulong b ulong to void +if eq b add a 1 +put ewall a 0 +end +if eq a add b 1 +put ewall b 0 +end +if eq b add a 10 +put swall a 0 +end +if eq a add b 10 +put swall b 0 +end +end +note recursive backtracker with an explicit stack: from the top cell, collect the unvisited neighbours in +note the order north, east, south, west, pick one with rnd mod their count, knock through and push it; +note with none left, pop; every pass pushes a new cell or pops, so 2*60 passes suffice +fn generate to void +for c ulong 60 +put ewall c 1 +put swall c 1 +put seen c 0 +end +put seen 0 1 +put stk 0 0 +sp1 = ulong 1 +while gt sp1 0 bound 120 +c = get stk sub sp1 1 +k = ulong 0 +for d ulong 4 +b = neighbour c d +if lt b 60 +if eq get seen b 0 +put cand k b +k = add k 1 +end +end +end +if eq k 0 +sp1 = sub sp1 1 +else +b = get cand rem rnd k +knock c b +put seen b 1 +put stk sp1 b +sp1 = add sp1 1 +end +end +end +note draw prints the maze; cells with mark[c] = 1 show a * +fn draw to void +emit tcorner +for k ulong 10 +emit thwall +end +emit nl +for r ulong 6 +emit tvwall +for k ulong 10 +c = add mul r 10 k +if eq get mark c 1 +emit tpath +else +emit tcell +end +if eq get ewall c 1 +emit tvwall +else +emit tspace +end +end +emit nl +emit tcorner +for k ulong 10 +if eq get swall add mul r 10 k 1 +emit thwall +else +emit thopen +end +end +emit nl +end +end +fn main to int +generate +draw +ret 0 +end diff --git a/tasks/maze-generation/oracle.py b/tasks/maze-generation/oracle.py new file mode 100644 index 0000000..4ded9b4 --- /dev/null +++ b/tasks/maze-generation/oracle.py @@ -0,0 +1,39 @@ +# Independent reference for "Maze generation" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +M = 2 ** 64 +x = 0 +def rnd(): + global x + x = (x * 6364136223846793005 + 1442695040888963407) % M + return x >> 33 +W, H = 10, 6 +def generate(): + east = [[True] * W for _ in range(H)] + south = [[True] * W for _ in range(H)] + seen = {(0, 0)} + stack = [(0, 0)] + while stack: + r, k = stack[-1] + cands = [(r + dr, k + dk) for dr, dk in ((-1, 0), (0, 1), (1, 0), (0, -1)) + if 0 <= r + dr < H and 0 <= k + dk < W and (r + dr, k + dk) not in seen] + if not cands: + stack.pop() + continue + nr, nk = cands[rnd() % len(cands)] + if nr == r: + east[r][min(k, nk)] = False + else: + south[min(r, nr)][k] = False + seen.add((nr, nk)) + stack.append((nr, nk)) + return east, south +def draw(east, south, marked=()): + print("+" + "---+" * W) + for r in range(H): + print("|" + "".join((" * " if (r, k) in marked else " ") + ("|" if east[r][k] else " ") for k in range(W))) + print("+" + "".join("---+" if south[r][k] else " +" for k in range(W))) +east, south = generate() +# a perfect maze: 60 cells joined by exactly 59 openings, all reachable from the top-left cell +openings = sum(not e for row in east for e in row[:W - 1]) + sum(not s for row in south[:H - 1] for s in row) +assert openings == W * H - 1 +draw(east, south) diff --git a/tasks/maze-solving/expected.txt b/tasks/maze-solving/expected.txt new file mode 100644 index 0000000..26c4c3b --- /dev/null +++ b/tasks/maze-solving/expected.txt @@ -0,0 +1,14 @@ ++---+---+---+---+---+---+---+---+---+---+ +| * | | | ++ +---+---+---+---+ + + +---+---+ +| * * * * | | | | ++---+---+---+ + +---+ +---+---+ + +| * * | * * | | | ++ + + +---+---+---+---+---+ + + +| * | * * | * * * * | | | ++ +---+---+ +---+---+ + +---+---+ +| * * | * * | | * | | ++---+ + +---+ +---+ +---+---+ + +| * * | * * * * | ++---+---+---+---+---+---+---+---+---+---+ +cells on the path from top left to bottom right: 27 diff --git a/tasks/maze-solving/maze-solving.om b/tasks/maze-solving/maze-solving.om new file mode 100644 index 0000000..223451b --- /dev/null +++ b/tasks/maze-solving/maze-solving.om @@ -0,0 +1,225 @@ +note Rosetta Code task "Maze solving": https://rosettacode.org/wiki/Maze_solving +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tcorner "+" +text thwall "---+" +text thopen " +" +text tvwall "|" +text tcell " " +text tpath " * " +text tspace " " +global seed ulong 1 +global ewall uchar 60 +global swall uchar 60 +global seen uchar 60 +global stk ulong 60 +global cand ulong 4 +global mark uchar 60 +text tlen "cells on the path from top left to bottom right: " +global dist ulong 60 +global queue ulong 60 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note rnd is Knuth's MMIX linear congruential generator, x = x*6364136223846793005 + 1442695040888963407 +note mod 2^64 (ulong arithmetic wraps), starting from seed 0; it returns the top 31 bits, x >> 33 +fn rnd to ulong +x = add mul get seed 0 6364136223846793005 1442695040888963407 +put seed 0 x +ret shr x 33 +end +note the maze has 10 columns and 6 rows of cells, cell c = row*10 + column; ewall[c] and swall[c] are the +note walls on its east and south sides, all standing at the start +note neighbour says which cell lies in direction d (0 north, 1 east, 2 south, 3 west), 60 if none +fn neighbour c ulong d ulong to ulong +r = div c 10 +k = rem c 10 +if and eq d 0 gt r 0 +ret sub c 10 +end +if and eq d 1 lt k 9 +ret add c 1 +end +if and eq d 2 lt r 5 +ret add c 10 +end +if and eq d 3 gt k 0 +ret sub c 1 +end +ret 60 +end +note knock removes the wall between neighbouring cells a and b +fn knock a ulong b ulong to void +if eq b add a 1 +put ewall a 0 +end +if eq a add b 1 +put ewall b 0 +end +if eq b add a 10 +put swall a 0 +end +if eq a add b 10 +put swall b 0 +end +end +note recursive backtracker with an explicit stack: from the top cell, collect the unvisited neighbours in +note the order north, east, south, west, pick one with rnd mod their count, knock through and push it; +note with none left, pop; every pass pushes a new cell or pops, so 2*60 passes suffice +fn generate to void +for c ulong 60 +put ewall c 1 +put swall c 1 +put seen c 0 +end +put seen 0 1 +put stk 0 0 +sp1 = ulong 1 +while gt sp1 0 bound 120 +c = get stk sub sp1 1 +k = ulong 0 +for d ulong 4 +b = neighbour c d +if lt b 60 +if eq get seen b 0 +put cand k b +k = add k 1 +end +end +end +if eq k 0 +sp1 = sub sp1 1 +else +b = get cand rem rnd k +knock c b +put seen b 1 +put stk sp1 b +sp1 = add sp1 1 +end +end +end +note draw prints the maze; cells with mark[c] = 1 show a * +fn draw to void +emit tcorner +for k ulong 10 +emit thwall +end +emit nl +for r ulong 6 +emit tvwall +for k ulong 10 +c = add mul r 10 k +if eq get mark c 1 +emit tpath +else +emit tcell +end +if eq get ewall c 1 +emit tvwall +else +emit tspace +end +end +emit nl +emit tcorner +for k ulong 10 +if eq get swall add mul r 10 k 1 +emit thwall +else +emit thopen +end +end +emit nl +end +end +note opento says whether the wall between neighbouring cells a and b is open +fn opento a ulong b ulong to bool +if eq b add a 1 +ret eq get ewall a 0 +end +if eq a add b 1 +ret eq get ewall b 0 +end +if eq b add a 10 +ret eq get swall a 0 +end +ret eq get swall b 0 +end +note bfs labels every cell with its distance from the top-left cell (60 = not reached yet) +fn bfs to void +for c ulong 60 +put dist c 60 +end +put dist 0 0 +put queue 0 0 +h = ulong 0 +t = ulong 1 +while lt h t bound 60 +c = get queue h +h = add h 1 +for d ulong 4 +b = neighbour c d +if lt b 60 +if and eq get dist b 60 opento c b +put dist b add get dist c 1 +put queue t b +t = add t 1 +end +end +end +end +end +note walk back from the bottom-right cell, each step to the neighbour one closer to the start +fn markpath to ulong +c = ulong 59 +put mark c 1 +while ne c 0 bound 60 +for d ulong 4 +b = neighbour c d +if lt b 60 +if and opento c b eq add get dist b 1 get dist c +c = b +break +end +end +end +put mark c 1 +end +ret add get dist 59 1 +end +fn main to int +generate +bfs +n = markpath +draw +emit tlen +emitu n +emit nl +ret 0 +end diff --git a/tasks/maze-solving/oracle.py b/tasks/maze-solving/oracle.py new file mode 100644 index 0000000..2ee32b2 --- /dev/null +++ b/tasks/maze-solving/oracle.py @@ -0,0 +1,53 @@ +# Independent reference for "Maze solving" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +M = 2 ** 64 +x = 0 +def rnd(): + global x + x = (x * 6364136223846793005 + 1442695040888963407) % M + return x >> 33 +W, H = 10, 6 +def generate(): + east = [[True] * W for _ in range(H)] + south = [[True] * W for _ in range(H)] + seen = {(0, 0)} + stack = [(0, 0)] + while stack: + r, k = stack[-1] + cands = [(r + dr, k + dk) for dr, dk in ((-1, 0), (0, 1), (1, 0), (0, -1)) + if 0 <= r + dr < H and 0 <= k + dk < W and (r + dr, k + dk) not in seen] + if not cands: + stack.pop() + continue + nr, nk = cands[rnd() % len(cands)] + if nr == r: + east[r][min(k, nk)] = False + else: + south[min(r, nr)][k] = False + seen.add((nr, nk)) + stack.append((nr, nk)) + return east, south +def draw(east, south, marked=()): + print("+" + "---+" * W) + for r in range(H): + print("|" + "".join((" * " if (r, k) in marked else " ") + ("|" if east[r][k] else " ") for k in range(W))) + print("+" + "".join("---+" if south[r][k] else " +" for k in range(W))) +east, south = generate() +def open_moves(r, k): + if r > 0 and not south[r - 1][k]: yield (r - 1, k) + if k < W - 1 and not east[r][k]: yield (r, k + 1) + if r < H - 1 and not south[r][k]: yield (r + 1, k) + if k > 0 and not east[r][k - 1]: yield (r, k - 1) +# depth-first search for the path; a perfect maze has exactly one +def dfs(cell, prev, goal): + if cell == goal: + return [cell] + for nxt in open_moves(*cell): + if nxt != prev: + p = dfs(nxt, cell, goal) + if p: + return [cell] + p + return None +path = dfs((0, 0), None, (H - 1, W - 1)) +draw(east, south, set(path)) +print("cells on the path from top left to bottom right:", len(path)) diff --git a/tasks/md5/expected.txt b/tasks/md5/expected.txt new file mode 100644 index 0000000..1155b2e --- /dev/null +++ b/tasks/md5/expected.txt @@ -0,0 +1,5 @@ +d41d8cd98f00b204e9800998ecf8427e +9e107d9d372bb6826bd81d3542a419d6 +e38ca1d920c4b8b8d3946b2c72f01680 +cca1bf66b09554e10f837838c3d3efb1 +0f5c6c4e740bfcc08c3c26ccb2673d46 diff --git a/tasks/md5/md5.om b/tasks/md5/md5.om new file mode 100644 index 0000000..c9f1da2 --- /dev/null +++ b/tasks/md5/md5.om @@ -0,0 +1,158 @@ +note Rosetta Code task "MD5": https://rosettacode.org/wiki/MD5 +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text hexd "0123456789abcdef" +note K[i] = floor(abs(sin(i + 1)) * 2^32) as 64 eight-digit hex words (RFC 1321, section 3.4) +text khex "d76aa478e8c7b756242070dbc1bdceeef57c0faf4787c62aa8304613fd469501698098d88b44f7afffff5bb1895cd7be6b901122fd987193a679438e49b40821f61e2562c040b340265e5a51e9b6c7aad62f105d02441453d8a1e681e7d3fbc821e1cde6c33707d6f4d50d87455a14eda9e3e905fcefa3f8676f02d98d2a4c8afffa39428771f6816d9d6122fde5380ca4beea444bdecfa9f6bb4b60bebfbc70289b7ec6eaa127fad4ef308504881d05d9d4d039e6db99e51fa27cf8c4ac5665f4292244432aff97ab9423a7fc93a039655b59c38f0ccc92ffeff47d85845dd16fa87e4ffe2ce6e0a30143144e0811a1f7537e82bd3af2352ad7d2bbeb86d391" +note the rotate amounts of the four rounds, two decimal digits each +text rots "07121722050914200411162306101521" +text m0 "The quick brown fox jumps over the lazy dog" +text m1 "The quick brown fox jumped over the lazy dog's back" +text m2 "Rosetta Code" +text m3 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +global kt uint 64 +global hs uint 4 +global mw uint 16 +global buf uchar 128 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note hexv gives the value of one lower-case hex digit +fn hexv c uchar to uint +if le c 57 +ret uint sub c 48 +end +ret uint sub c 87 +end +note emith writes the byte v as two lower-case hex digits +fn emith v uchar to void +emitc get hexd shr v 4 +emitc get hexd and v 15 +end +fn initk to void +for i ulong 64 +v = uint 0 +for j ulong 8 +v = or shl v 4 hexv get khex add mul i 8 j +end +put kt i v +end +end +note rotamt is the rotate amount of step i: round i / 16, place i mod 4 +fn rotamt i ulong to uint +x = mul add mul div i 16 4 rem i 4 2 +h = sub get rots x 48 +l = sub get rots add x 1 48 +ret uint add mul h 10 l +end +note rnd is step i of 64 on the state (a, b, c, d) in hs, with the block's words in mw +fn rnd i ulong to void +a = get hs 0 +b = get hs 1 +c = get hs 2 +d = get hs 3 +f = uint 0 +g = ulong 0 +if lt i 16 +f = or and b c and not b d +g = i +else +if lt i 32 +f = or and d b and not d c +g = rem add mul i 5 1 16 +else +if lt i 48 +f = xor xor b c d +g = rem add mul i 3 5 16 +else +f = xor c or b not d +g = rem mul i 7 16 +end +end +end +f = add add add f a get kt i get mw g +s = rotamt i +put hs 0 d +put hs 3 c +put hs 2 b +put hs 1 add b rotl f s +end +note block loads the 64 bytes of buf at p as 16 little-endian words and runs the 64 steps +fn block p ulong to void +for w ulong 16 +v = uint 0 +for j ulong 4 +v = or shl v 8 uint get buf add add p mul w 4 sub 3 j +end +put mw w v +end +sv = new uint 4 0 +for k ulong 4 +put sv k get hs k +end +for i ulong 64 +rnd i +end +for k ulong 4 +put hs k add get hs k get sv k +end +end +note md5 pads a (up to 119 bytes) with 0x80, zeros and the bit length (little-endian) to one or two +note 64-byte blocks, hashes them and prints the digest in hex +fn md5 a view uchar * to void +put hs 0 uint 1732584193 +put hs 1 uint 4023233417 +put hs 2 uint 2562383102 +put hs 3 uint 271733878 +n = len a +for i ulong 128 +put buf i 0 +end +for i ulong n +put buf i get a i +end +put buf n 128 +t = ulong 64 +if ge n 56 +t = 128 +end +bits = mul n 8 +for j ulong 8 +put buf add sub t 8 j uchar and shr bits mul j 8 255 +end +for b ulong div t 64 +block mul b 64 +end +for k ulong 4 +v = get hs k +for j ulong 4 +emith uchar and shr v mul uint j 8 255 +end +end +emit nl +end +fn main to int +initk +note the empty message, as a slice of length 0 +md5 part m0 0 0 +md5 m0 +md5 m1 +md5 m2 +md5 m3 +ret 0 +end diff --git a/tasks/md5/oracle.py b/tasks/md5/oracle.py new file mode 100644 index 0000000..be72b61 --- /dev/null +++ b/tasks/md5/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "MD5" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import hashlib +for s in ["", 'The quick brown fox jumps over the lazy dog', "The quick brown fox jumped over the lazy dog's back", 'Rosetta Code', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']: + print(hashlib.md5(s.encode()).hexdigest()) diff --git a/tasks/merge-sort/expected.txt b/tasks/merge-sort/expected.txt new file mode 100644 index 0000000..0aa7fe7 --- /dev/null +++ b/tasks/merge-sort/expected.txt @@ -0,0 +1,2 @@ +38 27 43 3 9 82 10 -5 61 3 0 -19 77 14 50 8 +-19 -5 0 3 3 8 9 10 14 27 38 43 50 61 77 82 diff --git a/tasks/merge-sort/merge-sort.om b/tasks/merge-sort/merge-sort.om new file mode 100644 index 0000000..7717eca --- /dev/null +++ b/tasks/merge-sort/merge-sort.om @@ -0,0 +1,129 @@ +note Rosetta Code task "Sorting algorithms/Merge sort": https://rosettacode.org/wiki/Sorting_algorithms/Merge_sort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "38 27 43 3 9 82 10 -5 61 3 0 -19 77 14 50 8" +global vals long 16 +global tmp long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note msort sorts vals[lo..hi-1]: sort both halves, merge them into tmp, copy back; the merge writes +note one element per pass, so hi-lo <= 16 passes; the halves are strictly smaller, so recursion ends +fn msort lo ulong hi ulong to void +if le sub hi lo 1 +ret +end +m = add lo shr sub hi lo 1 +msort lo m +msort m hi +i = lo +j = m +k = lo +while lt k hi bound 16 +left = false +if ge j hi +left = true +else +if lt i m +if le get vals i get vals j +left = true +end +end +end +if left +put tmp k get vals i +i = add i 1 +else +put tmp k get vals j +j = add j 1 +end +k = add k 1 +end +for x ulong sub hi lo +put vals add lo x get tmp add lo x +end +end +fn main to int +n = parse data +show n +msort 0 n +show n +ret 0 +end diff --git a/tasks/merge-sort/oracle.py b/tasks/merge-sort/oracle.py new file mode 100644 index 0000000..63ce762 --- /dev/null +++ b/tasks/merge-sort/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Sorting algorithms/Merge sort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [38, 27, 43, 3, 9, 82, 10, -5, 61, 3, 0, -19, 77, 14, 50, 8] +print(*a) +print(*sorted(a)) diff --git a/tasks/mersenne-primes/expected.txt b/tasks/mersenne-primes/expected.txt new file mode 100644 index 0000000..cb1a9b3 --- /dev/null +++ b/tasks/mersenne-primes/expected.txt @@ -0,0 +1,9 @@ +2^2 - 1 = 3 +2^3 - 1 = 7 +2^5 - 1 = 31 +2^7 - 1 = 127 +2^13 - 1 = 8191 +2^17 - 1 = 131071 +2^19 - 1 = 524287 +2^31 - 1 = 2147483647 +2^61 - 1 = 2305843009213693951 diff --git a/tasks/mersenne-primes/mersenne-primes.om b/tasks/mersenne-primes/mersenne-primes.om new file mode 100644 index 0000000..0939bc3 --- /dev/null +++ b/tasks/mersenne-primes/mersenne-primes.om @@ -0,0 +1,135 @@ +note Rosetta Code task "Mersenne primes": https://rosettacode.org/wiki/Mersenne_primes +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text two "2^" +text mid " - 1 = " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note addm is (a + b) mod m for a, b < m; it never forms the wrapped sum a + b +fn addm a ulong b ulong m ulong to ulong +if ge a sub m b +ret sub a sub m b +end +ret add a b +end +note mulm is a * b mod m for any 64-bit m > 0: double and add over the 64 bits of b, high bit first +fn mulm a ulong b ulong m ulong to ulong +x = rem a m +r = ulong 0 +for i ulong 64 +r = addm r r m +if ne and shr b sub 63 i 1 0 +r = addm r x m +end +end +ret r +end +note powm is b ^ e mod m; e has at most 64 bits, so 64 squarings +fn powm b ulong e ulong m ulong to ulong +r = rem ulong 1 m +x = rem b m +v = e +while gt v 0 bound 64 +if eq and v 1 1 +r = mulm r x m +end +x = mulm x x m +v = shr v 1 +end +ret r +end +note sprp: is odd n > 2 a strong probable prime to base a? n - 1 = d * 2^s with d odd, s at most 63 +fn sprp n ulong a ulong to bool +d = sub n 1 +s = ulong 0 +while eq and d 1 0 bound 64 +d = shr d 1 +s = add s 1 +end +x = powm a d n +if or eq x 1 eq x sub n 1 +ret true +end +for i ulong sub s 1 +x = mulm x x n +if eq x sub n 1 +ret true +end +end +ret false +end +note the first 12 primes as bases decide every n < 2^64 (3.18 * 10^23 is the first strong pseudoprime to all of them) +fn isprime n ulong to bool +bs = new ulong 12 0 +put bs 0 2 +put bs 1 3 +put bs 2 5 +put bs 3 7 +put bs 4 11 +put bs 5 13 +put bs 6 17 +put bs 7 19 +put bs 8 23 +put bs 9 29 +put bs 10 31 +put bs 11 37 +if lt n 2 +ret false +end +for i ulong 12 +a = get bs i +if eq n a +ret true +end +if eq rem n a 0 +ret false +end +end +for i ulong 12 +if not sprp n get bs i +ret false +end +end +ret true +end +note every Mersenne number 2^p - 1 that fits a ulong (p <= 63), tested by deterministic Miller-Rabin +fn main to int +p = ulong 2 +while le p 63 bound 62 +m = sub shl ulong 1 p 1 +if isprime m +emit two +emitu p +emit mid +emitu m +emit nl +end +p = add p 1 +end +ret 0 +end diff --git a/tasks/mersenne-primes/oracle.py b/tasks/mersenne-primes/oracle.py new file mode 100644 index 0000000..6da8b71 --- /dev/null +++ b/tasks/mersenne-primes/oracle.py @@ -0,0 +1,15 @@ +# Independent reference for "Mersenne primes" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# Lucas-Lehmer with Python integers (p = 2 by definition), a different test from the Omega program's Miller-Rabin +def mersenne_prime(p): + if p == 2: + return True + if any(p % d == 0 for d in range(2, p)): + return False + m, s = (1 << p) - 1, 4 + for _ in range(p - 2): + s = (s * s - 2) % m + return s == 0 +for p in range(2, 64): + if mersenne_prime(p): + print(f"2^{p} - 1 = {(1 << p) - 1}") diff --git a/tasks/mersenne-primes/wolfram.txt b/tasks/mersenne-primes/wolfram.txt new file mode 100644 index 0000000..d91f503 --- /dev/null +++ b/tasks/mersenne-primes/wolfram.txt @@ -0,0 +1,11 @@ +Wolfram cross-check of tasks/mersenne-primes/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 18f7b268a4fb2a75488d83fe88437f7717f030658e54f55f5cbddaa8bff2db79. +agreement: YES +query: +s = StringJoin[("2^" <> ToString[#] <> " - 1 = " <> ToString[2^# - 1] <> "\n") & /@ Select[Range[2, 63], PrimeQ[2^# - 1] &]]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"18f7b268a4fb2a75488d83fe88437f7717f030658e54f55f5cbddaa8bff2db79", "2^2 - 1 = 3\n2^3 - 1 = 7\n2^5 - 1 = 31\n2^7 - 1 = 127\n2^13 - 1 = 8191\n2^17 - 1 = 131071\n2^19 - 1 = 524287\n2^31 - 1 = 2147483647\n2^61 - 1 = 2305843009213693951\n"} diff --git a/tasks/miller-rabin-primality-test/expected.txt b/tasks/miller-rabin-primality-test/expected.txt new file mode 100644 index 0000000..30e7118 --- /dev/null +++ b/tasks/miller-rabin-primality-test/expected.txt @@ -0,0 +1,13 @@ +primes below 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 +561: composite +1105: composite +1729: composite +2047: composite +3215031751: composite +2147483647: prime +3825123056546413051: composite +4611686018427387903: composite +2305843009213693951: prime +9223372036854775783: prime +18446744073709551557: prime +18446744073709551615: composite diff --git a/tasks/miller-rabin-primality-test/miller-rabin-primality-test.om b/tasks/miller-rabin-primality-test/miller-rabin-primality-test.om new file mode 100644 index 0000000..bf1199e --- /dev/null +++ b/tasks/miller-rabin-primality-test/miller-rabin-primality-test.om @@ -0,0 +1,152 @@ +note Rosetta Code task "Miller-Rabin primality test": https://rosettacode.org/wiki/Miller-Rabin_primality_test +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text hdr "primes below 100:" +text yes ": prime\n" +text no ": composite\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note addm is (a + b) mod m for a, b < m; it never forms the wrapped sum a + b +fn addm a ulong b ulong m ulong to ulong +if ge a sub m b +ret sub a sub m b +end +ret add a b +end +note mulm is a * b mod m for any 64-bit m > 0: double and add over the 64 bits of b, high bit first +fn mulm a ulong b ulong m ulong to ulong +x = rem a m +r = ulong 0 +for i ulong 64 +r = addm r r m +if ne and shr b sub 63 i 1 0 +r = addm r x m +end +end +ret r +end +note powm is b ^ e mod m; e has at most 64 bits, so 64 squarings +fn powm b ulong e ulong m ulong to ulong +r = rem ulong 1 m +x = rem b m +v = e +while gt v 0 bound 64 +if eq and v 1 1 +r = mulm r x m +end +x = mulm x x m +v = shr v 1 +end +ret r +end +note sprp: is odd n > 2 a strong probable prime to base a? n - 1 = d * 2^s with d odd, s at most 63 +fn sprp n ulong a ulong to bool +d = sub n 1 +s = ulong 0 +while eq and d 1 0 bound 64 +d = shr d 1 +s = add s 1 +end +x = powm a d n +if or eq x 1 eq x sub n 1 +ret true +end +for i ulong sub s 1 +x = mulm x x n +if eq x sub n 1 +ret true +end +end +ret false +end +note the first 12 primes as bases decide every n < 2^64 (3.18 * 10^23 is the first strong pseudoprime to all of them) +fn isprime n ulong to bool +bs = new ulong 12 0 +put bs 0 2 +put bs 1 3 +put bs 2 5 +put bs 3 7 +put bs 4 11 +put bs 5 13 +put bs 6 17 +put bs 7 19 +put bs 8 23 +put bs 9 29 +put bs 10 31 +put bs 11 37 +if lt n 2 +ret false +end +for i ulong 12 +a = get bs i +if eq n a +ret true +end +if eq rem n a 0 +ret false +end +end +for i ulong 12 +if not sprp n get bs i +ret false +end +end +ret true +end +fn tell n ulong to void +emitu n +if isprime n +emit yes +else +emit no +end +end +fn main to int +emit hdr +for i ulong 100 +if isprime i +emit sp +emitu i +end +end +emit nl +tell 561 +tell 1105 +tell 1729 +tell 2047 +tell 3215031751 +tell 2147483647 +tell 3825123056546413051 +tell 4611686018427387903 +tell 2305843009213693951 +tell 9223372036854775783 +tell 18446744073709551557 +tell 18446744073709551615 +ret 0 +end diff --git a/tasks/miller-rabin-primality-test/oracle.py b/tasks/miller-rabin-primality-test/oracle.py new file mode 100644 index 0000000..354bf38 --- /dev/null +++ b/tasks/miller-rabin-primality-test/oracle.py @@ -0,0 +1,30 @@ +# Independent reference for "Miller-Rabin primality test" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import math +def prime(n): + # trial division for small n; for large n, a strong-pseudoprime test with every prime base below 40 + if n < 2: + return False + if n < 10**6: + return all(n % d for d in range(2, math.isqrt(n) + 1)) + d, s = n - 1, 0 + while d % 2 == 0: + d //= 2 + s += 1 + for a in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]: + if n % a == 0: + return False + x = pow(a, d, n) + if x in (1, n - 1): + continue + for _ in range(s - 1): + x = x * x % n + if x == n - 1: + break + else: + return False + return True +print("primes below 100: " + " ".join(str(n) for n in range(100) if prime(n))) +for n in [561, 1105, 1729, 2047, 3215031751, 2147483647, 3825123056546413051, 4611686018427387903, + 2305843009213693951, 9223372036854775783, 18446744073709551557, 18446744073709551615]: + print(f"{n}: {'prime' if prime(n) else 'composite'}") diff --git a/tasks/miller-rabin-primality-test/wolfram.txt b/tasks/miller-rabin-primality-test/wolfram.txt new file mode 100644 index 0000000..2489ff3 --- /dev/null +++ b/tasks/miller-rabin-primality-test/wolfram.txt @@ -0,0 +1,14 @@ +Wolfram cross-check of tasks/miller-rabin-primality-test/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 2f42e1c6ae7aabf3256b90c6107135e46b27ec51ad05c6a55ae155c7632d9c14. +agreement: YES +query: +s = "primes below 100: " <> StringRiffle[ToString /@ Select[Range[0, 99], PrimeQ], " "] <> "\n" <> + StringJoin[(ToString[#] <> If[PrimeQ[#], ": prime\n", ": composite\n"]) & /@ + {561, 1105, 1729, 2047, 3215031751, 2147483647, 3825123056546413051, 4611686018427387903, + 2305843009213693951, 9223372036854775783, 18446744073709551557, 18446744073709551615}]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"2f42e1c6ae7aabf3256b90c6107135e46b27ec51ad05c6a55ae155c7632d9c14", "primes below 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97\n561: composite\n1105: composite\n1729: composite\n2047: composite\n3215031751: composite\n2147483647: prime\n3825123056546413051: composite\n4611686018427387903: composite\n2305843009213693951: prime\n9223372036854775783: prime\n18446744073709551557: prime\n18446744073709551615: composite\n"} diff --git a/tasks/mobius-function/expected.txt b/tasks/mobius-function/expected.txt new file mode 100644 index 0000000..2915793 --- /dev/null +++ b/tasks/mobius-function/expected.txt @@ -0,0 +1,11 @@ +first 199 terms of the Moebius sequence (the first cell, n = 0, is blank): + 1 -1 -1 0 -1 1 -1 0 0 1 -1 0 -1 1 1 0 -1 0 -1 + 0 1 1 -1 0 0 1 0 0 -1 -1 -1 0 1 1 1 0 -1 1 1 + 0 -1 -1 -1 0 0 1 -1 0 0 0 1 0 -1 0 1 0 1 1 -1 + 0 -1 1 0 0 1 -1 -1 0 1 -1 -1 0 -1 1 0 0 1 -1 -1 + 0 0 1 -1 0 1 1 1 0 -1 0 1 0 1 1 1 0 -1 0 0 + 0 -1 -1 -1 0 -1 1 -1 0 -1 -1 1 0 -1 -1 1 0 0 1 1 + 0 0 1 1 0 0 0 -1 0 1 -1 -1 0 1 1 0 0 -1 -1 -1 + 0 1 1 1 0 1 1 0 0 -1 0 -1 0 0 -1 1 0 -1 1 1 + 0 1 0 -1 0 -1 1 -1 0 0 -1 0 0 -1 -1 0 0 1 1 -1 + 0 -1 -1 1 0 1 -1 1 0 0 -1 -1 0 -1 1 -1 0 -1 0 -1 diff --git a/tasks/mobius-function/mobius-function.om b/tasks/mobius-function/mobius-function.om new file mode 100644 index 0000000..bdf9cd8 --- /dev/null +++ b/tasks/mobius-function/mobius-function.om @@ -0,0 +1,65 @@ +note Rosetta Code task "Möbius function": https://rosettacode.org/wiki/M%C3%B6bius_function +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text hdr "first 199 terms of the Moebius sequence (the first cell, n = 0, is blank):\n" +text blank " " +text tneg " -1" +text tzer " 0" +text tpos " 1" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note mu(n) by trial division: 0 if some prime divides n twice, else (-1)^(number of prime factors) +note bound: n < 200, so d walks fewer than 15 values with d * d <= n +fn mu n ulong to long +v = n +s = long 1 +d = ulong 2 +while le mul d d v bound 200 +if eq rem v d 0 +v = div v d +if eq rem v d 0 +ret 0 +end +s = sub long 0 s +end +d = add d 1 +end +if gt v 1 +s = sub long 0 s +end +ret s +end +fn main to int +emit hdr +for r ulong 10 +for c ulong 20 +n = add mul r 20 c +if eq n 0 +emit blank +else +m = mu n +if lt m 0 +emit tneg +end +if eq m 0 +emit tzer +end +if gt m 0 +emit tpos +end +end +end +emit nl +end +ret 0 +end diff --git a/tasks/mobius-function/oracle.py b/tasks/mobius-function/oracle.py new file mode 100644 index 0000000..cf1a700 --- /dev/null +++ b/tasks/mobius-function/oracle.py @@ -0,0 +1,17 @@ +# Independent reference for "Möbius function" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def mu(n): + f, d, m = 0, 2, n + while d * d <= m: + if m % d == 0: + m //= d + if m % d == 0: + return 0 + f += 1 + d += 1 + if m > 1: + f += 1 + return -1 if f % 2 else 1 +print("first 199 terms of the Moebius sequence (the first cell, n = 0, is blank):") +for r in range(10): + print("".join(" " if n == 0 else f"{mu(n):3d}" for n in range(20 * r, 20 * r + 20))) diff --git a/tasks/mobius-function/wolfram.txt b/tasks/mobius-function/wolfram.txt new file mode 100644 index 0000000..cbd38b9 --- /dev/null +++ b/tasks/mobius-function/wolfram.txt @@ -0,0 +1,13 @@ +Wolfram cross-check of tasks/mobius-function/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 34e021d95a74264620cba845774562d76aecf6724ee94b22d21d58b1f8799a63. +agreement: YES +query: +cell[0] = " "; cell[n_] := StringPadLeft[ToString[MoebiusMu[n]], 3]; +s = "first 199 terms of the Moebius sequence (the first cell, n = 0, is blank):\n" <> + StringJoin[Table[StringJoin[cell /@ Range[20 r, 20 r + 19]] <> "\n", {r, 0, 9}]]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"34e021d95a74264620cba845774562d76aecf6724ee94b22d21d58b1f8799a63", "first 199 terms of the Moebius sequence (the first cell, n = 0, is blank):\n 1 -1 -1 0 -1 1 -1 0 0 1 -1 0 -1 1 1 0 -1 0 -1\n 0 1 1 -1 0 0 1 0 0 -1 -1 -1 0 1 1 1 0 -1 1 1\n 0 -1 -1 -1 0 0 1 -1 0 0 0 1 0 -1 0 1 0 1 1 -1\n 0 -1 1 0 0 1 -1 -1 0 1 -1 -1 0 -1 1 0 0 1 -1 -1\n 0 0 1 -1 0 1 1 1 0 -1 0 1 0 1 1 1 0 -1 0 0\n 0 -1 -1 -1 0 -1 1 -1 0 -1 -1 1 0 -1 -1 1 0 0 1 1\n 0 0 1 1 0 0 0 -1 0 1 -1 -1 0 1 1 0 0 -1 -1 -1\n 0 1 1 1 0 1 1 0 0 -1 0 -1 0 0 -1 1 0 -1 1 1\n 0 1 0 -1 0 -1 1 -1 0 0 -1 0 0 -1 -1 0 0 1 1 -1\n 0 -1 -1 1 0 1 -1 1 0 0 -1 -1 0 -1 1 -1 0 -1 0 -1\n"} diff --git a/tasks/modular-exponentiation/expected.txt b/tasks/modular-exponentiation/expected.txt new file mode 100644 index 0000000..54da92d --- /dev/null +++ b/tasks/modular-exponentiation/expected.txt @@ -0,0 +1 @@ +a^b mod 10^40 = 1527229998585248450016808958343740453059 diff --git a/tasks/modular-exponentiation/modular-exponentiation.om b/tasks/modular-exponentiation/modular-exponentiation.om new file mode 100644 index 0000000..6c108fe --- /dev/null +++ b/tasks/modular-exponentiation/modular-exponentiation.om @@ -0,0 +1,123 @@ +note Rosetta Code task "Modular exponentiation": https://rosettacode.org/wiki/Modular_exponentiation +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text ta "2988348162058574136915891421498819466320163312926952423791023078876139" +text tb "2351399303373464486466122544523690094744975233415544072992656881240319" +text hdr "a^b mod 10^40 = " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a residue mod 10^40 is 5 limbs base 10^8, least significant first +note limbs reads the last 40 decimal digits of the 70-digit text s +fn limbs s view uchar 70 z edit ulong 5 to void +for k ulong 5 +v = ulong 0 +o = sub 70 mul 8 add k 1 +for t ulong 8 +v = add mul v 10 sub ulong get s add o t 48 +end +put z k v +end +end +note mulm: z = x * y mod 10^40, keeping only limbs 0..4; each column sums at most 5 products below 10^16 +note plus a carry below 10^9, so no column passes 2^64 +fn mulm x view ulong 5 y view ulong 5 z edit ulong 5 to void +c = ulong 0 +for k ulong 5 +s = c +for i ulong add k 1 +s = add s mul get x i get y sub k i +end +put z k rem s 100000000 +c = div s 100000000 +end +end +fn cpy x view ulong 5 z edit ulong 5 to void +for k ulong 5 +put z k get x k +end +end +note bits turns the 70-digit decimal text e into binary, least significant bit first, by repeated halving +note bound: e < 10^70 < 2^233, so at most 233 halvings; returns the bit count +fn bits e view uchar 70 bt edit uchar 256 to ulong +dg = new uchar 70 0 +for i ulong 70 +put dg i sub get e i uchar 48 +end +n = ulong 0 +while true bound 256 +z = true +for i ulong 70 +if ne get dg i 0 +z = false +end +end +if z +ret n +end +put bt n and get dg 69 uchar 1 +r = ulong 0 +for i ulong 70 +v = add mul r 10 ulong get dg i +put dg i uchar div v 2 +r = rem v 2 +end +n = add n 1 +end +trap 4 +end +fn main to int +a = new ulong 5 0 +r = new ulong 5 0 +t = new ulong 5 0 +u = new ulong 5 0 +bt = new uchar 256 0 +limbs ta a +put r 0 1 +n = bits tb bt +for i ulong n +if ne get bt i 0 +mulm r a t +cpy t r +end +cpy a u +mulm a u t +cpy t a +end +emit hdr +emitu get r 4 +for k ulong 4 +v = get r sub 3 k +d = ulong 10000000 +while gt d 0 bound 8 +emitu rem div v d 10 +d = div d 10 +end +end +emit nl +ret 0 +end diff --git a/tasks/modular-exponentiation/oracle.py b/tasks/modular-exponentiation/oracle.py new file mode 100644 index 0000000..71183ce --- /dev/null +++ b/tasks/modular-exponentiation/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Modular exponentiation" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = 2988348162058574136915891421498819466320163312926952423791023078876139 +b = 2351399303373464486466122544523690094744975233415544072992656881240319 +print(f"a^b mod 10^40 = {pow(a, b, 10**40)}") diff --git a/tasks/modular-exponentiation/wolfram.txt b/tasks/modular-exponentiation/wolfram.txt new file mode 100644 index 0000000..91eba36 --- /dev/null +++ b/tasks/modular-exponentiation/wolfram.txt @@ -0,0 +1,13 @@ +Wolfram cross-check of tasks/modular-exponentiation/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = ee023d2d24dce4d70b433f300d3a0ffece67eaa82fe895a730a79d2f3d8cb1f7. +agreement: YES +query: +a = 2988348162058574136915891421498819466320163312926952423791023078876139; +b = 2351399303373464486466122544523690094744975233415544072992656881240319; +s = "a^b mod 10^40 = " <> ToString[PowerMod[a, b, 10^40]] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"ee023d2d24dce4d70b433f300d3a0ffece67eaa82fe895a730a79d2f3d8cb1f7", "a^b mod 10^40 = 1527229998585248450016808958343740453059\n"} diff --git a/tasks/modular-inverse/expected.txt b/tasks/modular-inverse/expected.txt new file mode 100644 index 0000000..44febc2 --- /dev/null +++ b/tasks/modular-inverse/expected.txt @@ -0,0 +1,6 @@ +42^-1 mod 2017 = 1969 +40^-1 mod 1 = 0 +52^-1 mod 217 = 96 +-486^-1 mod 217 = 121 +40^-1 mod 2018 does not exist +1234567^-1 mod 1000000007 = 989145189 diff --git a/tasks/modular-inverse/modular-inverse.om b/tasks/modular-inverse/modular-inverse.om new file mode 100644 index 0000000..2a38775 --- /dev/null +++ b/tasks/modular-inverse/modular-inverse.om @@ -0,0 +1,93 @@ +note Rosetta Code task "Modular inverse": https://rosettacode.org/wiki/Modular_inverse +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text inv "^-1 mod " +text eqs " = " +text none " does not exist\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n in decimal; the minimum long is turned positive as (-(n+1))+1 in ulong +fn emiti n long to void +m = new uchar 1 45 +if lt n 0 +emit m +emitu add ulong neg add n 1 1 +else +emitu ulong n +end +end +note minv: extended Euclid on (m, a mod m) for m > 0; returns -1 when gcd(a, m) > 1 +note bound: Euclid on values below 2^31 takes fewer than 50 steps; q * nt and q * nr stay below m in size +fn minv a long m long to long +x = rem a m +if lt x 0 +x = add x m +end +t = long 0 +nt = long 1 +r = m +nr = x +while ne nr 0 bound 100 +q = div r nr +k = sub t mul q nt +t = nt +nt = k +k = sub r mul q nr +r = nr +nr = k +end +if gt r 1 +ret -1 +end +if lt t 0 +t = add t m +end +ret t +end +fn tell a long m long to void +emiti a +emit inv +emiti m +v = minv a m +if lt v 0 +emit none +else +emit eqs +emiti v +emit nl +end +end +fn main to int +tell 42 2017 +tell 40 1 +tell 52 217 +tell -486 217 +tell 40 2018 +tell 1234567 1000000007 +ret 0 +end diff --git a/tasks/modular-inverse/oracle.py b/tasks/modular-inverse/oracle.py new file mode 100644 index 0000000..7b8829b --- /dev/null +++ b/tasks/modular-inverse/oracle.py @@ -0,0 +1,7 @@ +# Independent reference for "Modular inverse" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for a, m in [(42, 2017), (40, 1), (52, 217), (-486, 217), (40, 2018), (1234567, 1000000007)]: + try: + print(f"{a}^-1 mod {m} = {pow(a, -1, m)}") + except ValueError: + print(f"{a}^-1 mod {m} does not exist") diff --git a/tasks/modular-inverse/wolfram.txt b/tasks/modular-inverse/wolfram.txt new file mode 100644 index 0000000..b3f5e0e --- /dev/null +++ b/tasks/modular-inverse/wolfram.txt @@ -0,0 +1,13 @@ +Wolfram cross-check of tasks/modular-inverse/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 2e2ac024552d3fce2543857498e730266f278d60f824c1758ffae7b669ac978d. +agreement: YES +query: +f[{a_, m_}] := With[{g = GCD[a, m]}, ToString[a] <> "^-1 mod " <> ToString[m] <> + If[g == 1, " = " <> ToString[If[m == 1, 0, ModularInverse[a, m]]] <> "\n", " does not exist\n"]]; +s = StringJoin[f /@ {{42, 2017}, {40, 1}, {52, 217}, {-486, 217}, {40, 2018}, {1234567, 1000000007}}]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"2e2ac024552d3fce2543857498e730266f278d60f824c1758ffae7b669ac978d", "42^-1 mod 2017 = 1969\n40^-1 mod 1 = 0\n52^-1 mod 217 = 96\n-486^-1 mod 217 = 121\n40^-1 mod 2018 does not exist\n1234567^-1 mod 1000000007 = 989145189\n"} diff --git a/tasks/narcissistic-decimal-number/expected.txt b/tasks/narcissistic-decimal-number/expected.txt new file mode 100644 index 0000000..4daffe9 --- /dev/null +++ b/tasks/narcissistic-decimal-number/expected.txt @@ -0,0 +1 @@ +first 25 narcissistic decimal numbers: 0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315 diff --git a/tasks/narcissistic-decimal-number/narcissistic-decimal-number.om b/tasks/narcissistic-decimal-number/narcissistic-decimal-number.om new file mode 100644 index 0000000..6e69a22 --- /dev/null +++ b/tasks/narcissistic-decimal-number/narcissistic-decimal-number.om @@ -0,0 +1,73 @@ +note Rosetta Code task "Narcissistic decimal number": https://rosettacode.org/wiki/Narcissistic_decimal_number +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text hdr "first 25 narcissistic decimal numbers:" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note n with k digits is narcissistic when the sum of its digits each raised to k equals n +note pw holds d^k at 8 * d + k for digits d and lengths k up to 7 +fn narc n ulong pw view ulong 80 to bool +k = ulong 1 +v = div n 10 +while gt v 0 bound 8 +k = add k 1 +v = div v 10 +end +s = ulong 0 +v = n +for i ulong k +s = add s get pw add mul rem v 10 8 k +v = div v 10 +end +ret eq s n +end +note bound: the 25th such number, 9926315, has 7 digits, so 10^7 candidates suffice +fn main to int +pw = new ulong 80 0 +for d ulong 10 +x = ulong 1 +for k ulong 8 +put pw add mul d 8 k x +x = mul x d +end +end +emit hdr +c = ulong 0 +for n ulong 10000000 +if lt c 25 +if narc n pw +emit sp +emitu n +c = add c 1 +end +end +end +emit nl +ret 0 +end diff --git a/tasks/narcissistic-decimal-number/oracle.py b/tasks/narcissistic-decimal-number/oracle.py new file mode 100644 index 0000000..0146b86 --- /dev/null +++ b/tasks/narcissistic-decimal-number/oracle.py @@ -0,0 +1,13 @@ +# Independent reference for "Narcissistic decimal number" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# by digit multisets: for each length k and each sorted choice of k digits, the power sum is +# narcissistic when its own digits, sorted, are that choice +import itertools +found = set() +for k in range(1, 8): + for combo in itertools.combinations_with_replacement(range(10), k): + s = sum(d ** k for d in combo) + if len(str(s)) == k and tuple(sorted(map(int, str(s)))) == combo: + found.add(s) +found.add(0) +print("first 25 narcissistic decimal numbers: " + " ".join(map(str, sorted(found)[:25]))) diff --git a/tasks/narcissistic-decimal-number/wolfram.txt b/tasks/narcissistic-decimal-number/wolfram.txt new file mode 100644 index 0000000..cd93195 --- /dev/null +++ b/tasks/narcissistic-decimal-number/wolfram.txt @@ -0,0 +1,15 @@ +Wolfram cross-check of tasks/narcissistic-decimal-number/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 5bf03cc0ba2aeace6f5ccb3354dec8108519c606158020666debd32efa4b147b. +agreement: YES +query: +found = Union[{0}, Flatten[Table[ + (Total[#^k] &) /@ Select[(Subtract[#, Range[0, k - 1]] &) /@ Subsets[Range[0, 9 + k - 1], {k}], + With[{s = Total[#^k]}, IntegerLength[s] == k && Sort[IntegerDigits[s]] == #] &], + {k, 1, 7}]]]; +s = "first 25 narcissistic decimal numbers: " <> StringRiffle[ToString /@ Take[found, 25], " "] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"5bf03cc0ba2aeace6f5ccb3354dec8108519c606158020666debd32efa4b147b", "first 25 narcissistic decimal numbers: 0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315\n"} diff --git a/tasks/number-names/expected.txt b/tasks/number-names/expected.txt new file mode 100644 index 0000000..135a6e7 --- /dev/null +++ b/tasks/number-names/expected.txt @@ -0,0 +1,14 @@ +0: zero +7: seven +13: thirteen +21: twenty-one +99: ninety-nine +100: one hundred +101: one hundred one +999: nine hundred ninety-nine +1000: one thousand +1001: one thousand one +12345: twelve thousand three hundred forty-five +1000000: one million +1234567890: one billion two hundred thirty-four million five hundred sixty-seven thousand eight hundred ninety +18446744073709551615: eighteen quintillion four hundred forty-six quadrillion seven hundred forty-four trillion seventy-three billion seven hundred nine million five hundred fifty-one thousand six hundred fifteen diff --git a/tasks/number-names/number-names.om b/tasks/number-names/number-names.om new file mode 100644 index 0000000..757f61a --- /dev/null +++ b/tasks/number-names/number-names.om @@ -0,0 +1,127 @@ +note Rosetta Code task "Number names": https://rosettacode.org/wiki/Number_names +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text hy "-" +text colon ": " +text ones "zero\none\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten\neleven\ntwelve\nthirteen\nfourteen\nfifteen\nsixteen\nseventeen\neighteen\nnineteen\n" +text tens "twenty\nthirty\nforty\nfifty\nsixty\nseventy\neighty\nninety\n" +text scales "thousand\nmillion\nbillion\ntrillion\nquadrillion\nquintillion\n" +text hund " hundred" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note word writes element k of a newline-terminated list +fn word t view uchar * k ulong to void +s = ulong 0 +m = ulong 0 +for i ulong len t +if eq get t i 10 +if eq m k +emit part t s sub i s +ret +end +m = add m 1 +s = add i 1 +end +end +trap 9 +end +note three writes 1..999 in words: hundreds, then the rest (under 20 by name, else tens-ones) +fn three g ulong to void +h = div g 100 +r = rem g 100 +if gt h 0 +word ones h +emit hund +if gt r 0 +emit sp +end +end +if and gt r 0 lt r 20 +word ones r +end +if ge r 20 +word tens sub div r 10 2 +if gt rem r 10 0 +emit hy +word ones rem r 10 +end +end +end +note spell splits n into groups of three digits and writes each non-zero one with its scale +fn spell n ulong to void +if eq n 0 +word ones 0 +ret +end +g = new ulong 7 0 +v = n +for i ulong 7 +put g i rem v 1000 +v = div v 1000 +end +first = true +for k ulong 7 +i = sub 6 k +x = get g i +if gt x 0 +if not first +emit sp +end +three x +if gt i 0 +emit sp +word scales sub i 1 +end +first = false +end +end +end +fn show n ulong to void +emitu n +emit colon +spell n +emit nl +end +fn main to int +show 0 +show 7 +show 13 +show 21 +show 99 +show 100 +show 101 +show 999 +show 1000 +show 1001 +show 12345 +show 1000000 +show 1234567890 +show 18446744073709551615 +ret 0 +end diff --git a/tasks/number-names/oracle.py b/tasks/number-names/oracle.py new file mode 100644 index 0000000..6a6b206 --- /dev/null +++ b/tasks/number-names/oracle.py @@ -0,0 +1,28 @@ +# Independent reference for "Number names" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +ONES = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] +TENS = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] +SCALES = ["", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion"] +def below_1000(n): + parts = [] + if n >= 100: + parts.append(ONES[n // 100] + " hundred") + r = n % 100 + if 0 < r < 20: + parts.append(ONES[r]) + elif r >= 20: + parts.append(TENS[r // 10 - 2] + ("-" + ONES[r % 10] if r % 10 else "")) + return " ".join(parts) +def spell(n): + if n == 0: + return "zero" + groups = [] + i = 0 + while n: + n, g = divmod(n, 1000) + if g: + groups.append(below_1000(g) + (" " + SCALES[i] if i else "")) + i += 1 + return " ".join(reversed(groups)) +for n in [0, 7, 13, 21, 99, 100, 101, 999, 1000, 1001, 12345, 1000000, 1234567890, 18446744073709551615]: + print(f"{n}: {spell(n)}") diff --git a/tasks/one-dimensional-cellular-automata/expected.txt b/tasks/one-dimensional-cellular-automata/expected.txt new file mode 100644 index 0000000..c69e4ac --- /dev/null +++ b/tasks/one-dimensional-cellular-automata/expected.txt @@ -0,0 +1,9 @@ +generation 0: _###_##_#_#_#_#__#__ +generation 1: _#_#####_#_#_#______ +generation 2: __##___##_#_#_______ +generation 3: __##___###_#________ +generation 4: __##___#_##_________ +generation 5: __##____###_________ +generation 6: __##____#_#_________ +generation 7: __##_____#__________ +generation 8: __##________________ diff --git a/tasks/one-dimensional-cellular-automata/one-dimensional-cellular-automata.om b/tasks/one-dimensional-cellular-automata/one-dimensional-cellular-automata.om new file mode 100644 index 0000000..3698ec8 --- /dev/null +++ b/tasks/one-dimensional-cellular-automata/one-dimensional-cellular-automata.om @@ -0,0 +1,96 @@ +note Rosetta Code task "One-dimensional cellular automata": https://rosettacode.org/wiki/One-dimensional_cellular_automata +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tstart "_###_##_#_#_#_#__#__" +text tgen "generation " +text tcolon ": " +global cur uchar 20 +global nxt uchar 20 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitb writes the single byte c +fn emitb c ulong to void +d = new uchar 1 0 +put d 0 uchar c +emit d +end +note a cell is alive next generation when exactly two of itself and its two neighbours are alive +note (cells beyond the ends are dead); the run stops when a generation repeats the one before +fn cell i ulong to ulong +if eq get cur i 35 +ret 1 +end +ret 0 +end +fn step to bool +changed = false +for i ulong 20 +n = cell i +if gt i 0 +n = add n cell sub i 1 +end +if lt i 19 +n = add n cell add i 1 +end +v = uchar 95 +if eq n 2 +v = 35 +end +if ne v get cur i +changed = true +end +put nxt i v +end +for i ulong 20 +put cur i get nxt i +end +ret changed +end +note 20 cells can change at most 2^20 ways, but this start settles after 9 generations; 100 is ample +fn main to int +for i ulong 20 +put cur i get tstart i +end +g = ulong 0 +while true bound 100 +emit tgen +emitu g +emit tcolon +for i ulong 20 +emitb ulong get cur i +end +emit nl +if not step +break +end +g = add g 1 +end +ret 0 +end diff --git a/tasks/one-dimensional-cellular-automata/oracle.py b/tasks/one-dimensional-cellular-automata/oracle.py new file mode 100644 index 0000000..867fa9b --- /dev/null +++ b/tasks/one-dimensional-cellular-automata/oracle.py @@ -0,0 +1,11 @@ +# Independent reference for "One-dimensional cellular automata" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +cells = "_###_##_#_#_#_#__#__" +g = 0 +while True: + print(f"generation {g}: {cells}") + padded = "_" + cells + "_" + nxt = "".join("#" if padded[i - 1:i + 2].count("#") == 2 else "_" for i in range(1, len(cells) + 1)) + if nxt == cells: + break + cells, g = nxt, g + 1 diff --git a/tasks/ordered-partitions/expected.txt b/tasks/ordered-partitions/expected.txt new file mode 100644 index 0000000..b0d1024 --- /dev/null +++ b/tasks/ordered-partitions/expected.txt @@ -0,0 +1,20 @@ +partitions(2, 0, 2) +({1, 2}, {}, {3, 4}) +({1, 3}, {}, {2, 4}) +({1, 4}, {}, {2, 3}) +({2, 3}, {}, {1, 4}) +({2, 4}, {}, {1, 3}) +({3, 4}, {}, {1, 2}) +partitions(1, 2, 1) +({1}, {2, 3}, {4}) +({1}, {2, 4}, {3}) +({1}, {3, 4}, {2}) +({2}, {1, 3}, {4}) +({2}, {1, 4}, {3}) +({2}, {3, 4}, {1}) +({3}, {1, 2}, {4}) +({3}, {1, 4}, {2}) +({3}, {2, 4}, {1}) +({4}, {1, 2}, {3}) +({4}, {1, 3}, {2}) +({4}, {2, 3}, {1}) diff --git a/tasks/ordered-partitions/oracle.py b/tasks/ordered-partitions/oracle.py new file mode 100644 index 0000000..c6a846f --- /dev/null +++ b/tasks/ordered-partitions/oracle.py @@ -0,0 +1,26 @@ +# Independent reference for "Ordered partitions" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# Cross-check, WolframLanguageEvaluator: {4!/(2! 0! 2!), 4!/(1! 2! 1!)} -> {6, 12} +from itertools import permutations +from math import factorial +def partitions(*sizes): + # every arrangement of 1..n cut into consecutive runs of the given sizes, keeping one per distinct + # result; sorted by the blocks as lists gives the lexicographic order + n = sum(sizes) + seen = set() + for p in permutations(range(1, n + 1)): + blocks, k = [], 0 + for s in sizes: + blocks.append(tuple(sorted(p[k:k + s]))) + k += s + seen.add(tuple(blocks)) + return sorted(seen) +for sizes in ((2, 0, 2), (1, 2, 1)): + print("partitions(" + ", ".join(map(str, sizes)) + ")") + ps = partitions(*sizes) + total = factorial(sum(sizes)) + for s in sizes: + total //= factorial(s) + assert len(ps) == total + for blocks in ps: + print("(" + ", ".join("{" + ", ".join(map(str, b)) + "}" for b in blocks) + ")") diff --git a/tasks/ordered-partitions/ordered-partitions.om b/tasks/ordered-partitions/ordered-partitions.om new file mode 100644 index 0000000..98f5ed3 --- /dev/null +++ b/tasks/ordered-partitions/ordered-partitions.om @@ -0,0 +1,115 @@ +note Rosetta Code task "Ordered partitions": https://rosettacode.org/wiki/Ordered_partitions +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tlp "(" +text trp ")" +text tlb "{" +text trb "}" +text tcomma ", " +text tpart "partitions" +global size ulong 4 +global owner ulong 8 +global nb ulong 1 +global nel ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note owner[i] is the block holding element i+1 (nb = unassigned); blocks are filled in order, each with +note its elements chosen as a lexicographic combination of what is left, so the output is in order +fn show to void +emit tlp +for b ulong get nb 0 +if gt b 0 +emit tcomma +end +emit tlb +first = true +for i ulong get nel 0 +if eq get owner i b +if not first +emit tcomma +end +emitu add i 1 +first = false +end +end +emit trb +end +emit trp +emit nl +end +note fill starts block b, or prints the partition once every block is full +fn fill b ulong to void +if eq b get nb 0 +show +ret +end +choose b get size b 0 +end +note choose gives block b `need` more elements, each larger than the last one chosen (at index >= from) +fn choose b ulong need ulong from ulong to void +if eq need 0 +fill add b 1 +ret +end +n = get nel 0 +for x ulong sub n from +i = add from x +if eq get owner i get nb 0 +put owner i b +choose b sub need 1 add i 1 +put owner i get nb 0 +end +end +end +fn run s0 ulong s1 ulong s2 ulong to void +put nb 0 3 +put size 0 s0 +put size 1 s1 +put size 2 s2 +put nel 0 add add s0 s1 s2 +for i ulong get nel 0 +put owner i 3 +end +emit tpart +emit tlp +emitu s0 +emit tcomma +emitu s1 +emit tcomma +emitu s2 +emit trp +emit nl +fill 0 +end +fn main to int +run 2 0 2 +run 1 2 1 +ret 0 +end diff --git a/tasks/ordered-words/expected.txt b/tasks/ordered-words/expected.txt new file mode 100644 index 0000000..8dbf159 --- /dev/null +++ b/tasks/ordered-words/expected.txt @@ -0,0 +1 @@ +aegilops diff --git a/tasks/ordered-words/oracle.py b/tasks/ordered-words/oracle.py new file mode 100644 index 0000000..e95aa32 --- /dev/null +++ b/tasks/ordered-words/oracle.py @@ -0,0 +1,8 @@ +# Independent reference for "Ordered words" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +words = ['almost', 'abbey', 'accent', 'begin', 'biopsy', 'chimp', 'chintz', 'deist', 'effort', 'first', 'ghost', 'glory', 'floor', 'knotty', 'billowy', 'bijoux', 'abhors', 'aegilops', 'this', 'that', 'zebra', 'bellow', 'adder', 'cello'] +ordered = [w for w in words if list(w) == sorted(w)] +best = max(map(len, ordered)) +for w in ordered: + if len(w) == best: + print(w) diff --git a/tasks/ordered-words/ordered-words.om b/tasks/ordered-words/ordered-words.om new file mode 100644 index 0000000..1872e85 --- /dev/null +++ b/tasks/ordered-words/ordered-words.om @@ -0,0 +1,57 @@ +note Rosetta Code task "Ordered words": https://rosettacode.org/wiki/Ordered_words +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +note the word list (Rosetta Code reads unixdict.txt; this is a fixed sample of it), newline-terminated +text dict "almost\nabbey\naccent\nbegin\nbiopsy\nchimp\nchintz\ndeist\neffort\nfirst\nghost\nglory\nfloor\nknotty\nbillowy\nbijoux\nabhors\naegilops\nthis\nthat\nzebra\nbellow\nadder\ncello\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note ordered: every letter is at least the one before it +fn ordered a view uchar * to bool +for i ulong len a +if gt i 0 +if lt get a i get a sub i 1 +ret false +end +end +end +ret true +end +note two passes over the list: the longest ordered length, then every ordered word of that length +fn longest to ulong +best = ulong 0 +s = ulong 0 +for i ulong len dict +if eq get dict i 10 +n = sub i s +if and ordered part dict s n gt n best +best = n +end +s = add i 1 +end +end +ret best +end +fn main to int +best = longest +s = ulong 0 +for i ulong len dict +if eq get dict i 10 +n = sub i s +if and ordered part dict s n eq n best +emit part dict s add n 1 +end +s = add i 1 +end +end +ret 0 +end diff --git a/tasks/palindrome-detection/expected.txt b/tasks/palindrome-detection/expected.txt new file mode 100644 index 0000000..2ee2ee9 --- /dev/null +++ b/tasks/palindrome-detection/expected.txt @@ -0,0 +1,5 @@ +"rotor" exact yes inexact yes +"level" exact yes inexact yes +"A man, a plan, a canal: Panama!" exact no inexact yes +"In girum imus nocte et consumimur igni" exact no inexact yes +"hello" exact no inexact no diff --git a/tasks/palindrome-detection/oracle.py b/tasks/palindrome-detection/oracle.py new file mode 100644 index 0000000..047375d --- /dev/null +++ b/tasks/palindrome-detection/oracle.py @@ -0,0 +1,10 @@ +# Independent reference for "Palindrome detection" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def exact(s): + return s == s[::-1] +def inexact(s): + t = "".join(c.lower() for c in s if c.isascii() and c.isalpha()) + return t == t[::-1] +yn = {True: "yes", False: "no"} +for s in ["rotor", "level", "A man, a plan, a canal: Panama!", "In girum imus nocte et consumimur igni", "hello"]: + print(f'"{s}" exact {yn[exact(s)]} inexact {yn[inexact(s)]}') diff --git a/tasks/palindrome-detection/palindrome-detection.om b/tasks/palindrome-detection/palindrome-detection.om new file mode 100644 index 0000000..93864ed --- /dev/null +++ b/tasks/palindrome-detection/palindrome-detection.om @@ -0,0 +1,75 @@ +note Rosetta Code task "Palindrome detection": https://rosettacode.org/wiki/Palindrome_detection +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text tq "\"" +text tex "\" exact " +text tin " inexact " +text tyes "yes" +text tno "no" +text p1 "rotor" +text p2 "level" +text p3 "A man, a plan, a canal: Panama!" +text p4 "In girum imus nocte et consumimur igni" +text p5 "hello" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +fn emitb b bool to void +if b +emit tyes +else +emit tno +end +end +note exact compares bytes from both ends +fn exact a view uchar * to bool +n = len a +for i ulong div n 2 +if ne get a i get a sub sub n 1 i +ret false +end +end +ret true +end +note inexact keeps letters only, lower-cased, then compares (strings up to 256 bytes) +fn inexact a view uchar * to bool +b = new uchar 256 0 +n = ulong 0 +for i ulong len a +c = get a i +if and ge c 65 le c 90 +c = add c 32 +end +if and ge c 97 le c 122 +put b n c +n = add n 1 +end +end +ret exact part b 0 n +end +fn show a view uchar * to void +emit tq +emit a +emit tex +emitb exact a +emit tin +emitb inexact a +emit nl +end +fn main to int +show p1 +show p2 +show p3 +show p4 +show p5 +ret 0 +end diff --git a/tasks/pancake-sort/expected.txt b/tasks/pancake-sort/expected.txt new file mode 100644 index 0000000..751008b --- /dev/null +++ b/tasks/pancake-sort/expected.txt @@ -0,0 +1,3 @@ +23 -7 61 4 0 38 15 -7 90 2 11 +-7 -7 0 2 4 11 15 23 38 61 90 +flips: 16 diff --git a/tasks/pancake-sort/oracle.py b/tasks/pancake-sort/oracle.py new file mode 100644 index 0000000..bc1dfb5 --- /dev/null +++ b/tasks/pancake-sort/oracle.py @@ -0,0 +1,16 @@ +# Independent reference for "Sorting algorithms/Pancake sort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [23, -7, 61, 4, 0, 38, 15, -7, 90, 2, 11] +print(*a) +flips = 0 +for size in range(len(a), 1, -1): + m = max(range(size), key=lambda i: (a[i], -i)) + if m != size - 1: + if m != 0: + a[:m + 1] = a[:m + 1][::-1] + flips += 1 + a[:size] = a[:size][::-1] + flips += 1 +assert a == sorted(a) +print(*a) +print("flips:", flips) diff --git a/tasks/pancake-sort/pancake-sort.om b/tasks/pancake-sort/pancake-sort.om new file mode 100644 index 0000000..045a297 --- /dev/null +++ b/tasks/pancake-sort/pancake-sort.om @@ -0,0 +1,127 @@ +note Rosetta Code task "Sorting algorithms/Pancake sort": https://rosettacode.org/wiki/Sorting_algorithms/Pancake_sort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "23 -7 61 4 0 38 15 -7 90 2 11" +text tflips "flips: " +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note flip reverses vals[0..k-1]; k/2 swaps +fn flip k ulong to void +for i ulong shr k 1 +t = get vals i +put vals i get vals sub sub k i 1 +put vals sub sub k i 1 t +end +end +note pksort brings the largest of vals[0..s-1] to the front, then flips it to position s-1, for s = n .. 2; +note it returns the number of flips made, at most 2*(n-1) +fn pksort n ulong to ulong +f = ulong 0 +for x ulong sub n 1 +s = sub n x +m = ulong 0 +for i ulong s +if gt get vals i get vals m +m = i +end +end +if ne m sub s 1 +if ne m 0 +flip add m 1 +f = add f 1 +end +flip s +f = add f 1 +end +end +ret f +end +fn main to int +n = parse data +show n +f = pksort n +show n +emit tflips +emitu f +emit nl +ret 0 +end diff --git a/tasks/pangram-checker/expected.txt b/tasks/pangram-checker/expected.txt new file mode 100644 index 0000000..823403f --- /dev/null +++ b/tasks/pangram-checker/expected.txt @@ -0,0 +1,3 @@ +"The quick brown fox jumps over the lazy dog" is a pangram +"The quick brown fox jumped over the lazy dog" is not a pangram +"Pack my box with five dozen liquor jugs." is a pangram diff --git a/tasks/pangram-checker/oracle.py b/tasks/pangram-checker/oracle.py new file mode 100644 index 0000000..3ced591 --- /dev/null +++ b/tasks/pangram-checker/oracle.py @@ -0,0 +1,6 @@ +# Independent reference for "Pangram checker" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import string +for s in ["The quick brown fox jumps over the lazy dog", "The quick brown fox jumped over the lazy dog", "Pack my box with five dozen liquor jugs."]: + ok = set(string.ascii_lowercase) <= set(s.lower()) + print(f'"{s}" is {"a" if ok else "not a"} pangram') diff --git a/tasks/pangram-checker/pangram-checker.om b/tasks/pangram-checker/pangram-checker.om new file mode 100644 index 0000000..1d6eba4 --- /dev/null +++ b/tasks/pangram-checker/pangram-checker.om @@ -0,0 +1,54 @@ +note Rosetta Code task "Pangram checker": https://rosettacode.org/wiki/Pangram_checker +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text tq "\"" +text tis "\" is a pangram\n" +text tnot "\" is not a pangram\n" +text p1 "The quick brown fox jumps over the lazy dog" +text p2 "The quick brown fox jumped over the lazy dog" +text p3 "Pack my box with five dozen liquor jugs." +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +fn pangram a view uchar * to bool +seen = new uchar 26 0 +for i ulong len a +c = get a i +if and ge c 65 le c 90 +c = add c 32 +end +if and ge c 97 le c 122 +put seen sub c 97 1 +end +end +for j ulong 26 +if eq get seen j 0 +ret false +end +end +ret true +end +fn show a view uchar * to void +emit tq +emit a +if pangram a +emit tis +else +emit tnot +end +end +fn main to int +show p1 +show p2 +show p3 +ret 0 +end diff --git a/tasks/pascals-triangle/expected.txt b/tasks/pascals-triangle/expected.txt new file mode 100644 index 0000000..8375f73 --- /dev/null +++ b/tasks/pascals-triangle/expected.txt @@ -0,0 +1,10 @@ + 1 + 1 1 + 1 2 1 + 1 3 3 1 + 1 4 6 4 1 + 1 5 10 10 5 1 + 1 6 15 20 15 6 1 + 1 7 21 35 35 21 7 1 + 1 8 28 56 70 56 28 8 1 +1 9 36 84 126 126 84 36 9 1 diff --git a/tasks/pascals-triangle/oracle.py b/tasks/pascals-triangle/oracle.py new file mode 100644 index 0000000..3688e74 --- /dev/null +++ b/tasks/pascals-triangle/oracle.py @@ -0,0 +1,10 @@ +# Independent reference for "Pascal's triangle" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# Cross-check, WolframLanguageEvaluator: Table[Binomial[n, k], {n, 0, 9}, {k, 0, n}] +# -> {{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 4, 1}, {1, 5, 10, 10, 5, 1}, +# {1, 6, 15, 20, 15, 6, 1}, {1, 7, 21, 35, 35, 21, 7, 1}, {1, 8, 28, 56, 70, 56, 28, 8, 1}, +# {1, 9, 36, 84, 126, 126, 84, 36, 9, 1}} +from math import comb +n = 10 +for r in range(n): + print(" " * (2 * (n - r - 1)) + " ".join(str(comb(r, k)) for k in range(r + 1))) diff --git a/tasks/pascals-triangle/pascals-triangle.om b/tasks/pascals-triangle/pascals-triangle.om new file mode 100644 index 0000000..c6ff03c --- /dev/null +++ b/tasks/pascals-triangle/pascals-triangle.om @@ -0,0 +1,59 @@ +note Rosetta Code task "Pascal's triangle": https://rosettacode.org/wiki/Pascal's_triangle +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +global row ulong 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note each row comes from the one above in place, right to left: row[j] += row[j-1]; each row is +note indented so the triangle is centred, two columns per level +fn pascal n ulong to void +put row 0 1 +for r ulong n +for x ulong mul 2 sub sub n r 1 +emit sp +end +for j ulong add r 1 +if gt j 0 +emit sp +end +emitu get row j +end +emit nl +put row add r 1 1 +for x ulong r +j = sub r x +put row j add get row j get row sub j 1 +end +end +end +fn main to int +pascal 10 +ret 0 +end diff --git a/tasks/patience-sort/expected.txt b/tasks/patience-sort/expected.txt new file mode 100644 index 0000000..0db3e5c --- /dev/null +++ b/tasks/patience-sort/expected.txt @@ -0,0 +1,2 @@ +4 65 2 -31 0 99 83 782 1 4 -31 18 +-31 -31 0 1 2 4 4 18 65 83 99 782 diff --git a/tasks/patience-sort/oracle.py b/tasks/patience-sort/oracle.py new file mode 100644 index 0000000..4956308 --- /dev/null +++ b/tasks/patience-sort/oracle.py @@ -0,0 +1,23 @@ +# Independent reference for "Sorting algorithms/Patience sort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import heapq +a = [4, 65, 2, -31, 0, 99, 83, 782, 1, 4, -31, 18] +print(*a) +piles = [] +for v in a: + for p in piles: + if p[-1] >= v: + p.append(v) + break + else: + piles.append([v]) +heap = [(p[-1], i) for i, p in enumerate(piles)] +heapq.heapify(heap) +out = [] +while heap: + v, i = heapq.heappop(heap) + out.append(piles[i].pop()) + if piles[i]: + heapq.heappush(heap, (piles[i][-1], i)) +assert out == sorted(a) +print(*out) diff --git a/tasks/patience-sort/patience-sort.om b/tasks/patience-sort/patience-sort.om new file mode 100644 index 0000000..bdfd3f6 --- /dev/null +++ b/tasks/patience-sort/patience-sort.om @@ -0,0 +1,138 @@ +note Rosetta Code task "Sorting algorithms/Patience sort": https://rosettacode.org/wiki/Sorting_algorithms/Patience_sort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "4 65 2 -31 0 99 83 782 1 4 -31 18" +global vals long 16 +global pile long 256 +global plen ulong 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note piles: pile p holds its cards in pile[p*16 .. p*16+plen[p]-1], the top card last; tops decrease +note bottom to top, so a new card goes on the leftmost pile whose top is >= it, else on a new pile +fn deal n ulong to ulong +np = ulong 0 +for i ulong n +v = get vals i +p = ulong 0 +while lt p np bound 16 +if ge top p v +break +end +p = add p 1 +end +if eq p np +np = add np 1 +end +put pile add mul p 16 get plen p v +put plen p add get plen p 1 +end +ret np +end +note top is the top card of pile p, which must not be empty +fn top p ulong to long +ret get pile add mul p 16 sub get plen p 1 +end +note merge takes the smallest top card of all piles n times; at most 16 piles to scan each time +fn merge n ulong np ulong to void +for k ulong n +b = np +for p ulong np +if gt get plen p 0 +if eq b np +b = p +else +if lt top p top b +b = p +end +end +end +end +put plen b sub get plen b 1 +put vals k get pile add mul b 16 get plen b +end +end +fn main to int +n = parse data +show n +merge n deal n +show n +ret 0 +end diff --git a/tasks/perfect-totient-numbers/expected.txt b/tasks/perfect-totient-numbers/expected.txt new file mode 100644 index 0000000..5f16580 --- /dev/null +++ b/tasks/perfect-totient-numbers/expected.txt @@ -0,0 +1 @@ +first 20 perfect totient numbers: 3 9 15 27 39 81 111 183 243 255 327 363 471 729 2187 2199 3063 4359 4375 5571 diff --git a/tasks/perfect-totient-numbers/oracle.py b/tasks/perfect-totient-numbers/oracle.py new file mode 100644 index 0000000..59143a5 --- /dev/null +++ b/tasks/perfect-totient-numbers/oracle.py @@ -0,0 +1,23 @@ +# Independent reference for "Perfect totient numbers" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def phi(n): + r, m, p = n, n, 2 + while p * p <= m: + if m % p == 0: + while m % p == 0: + m //= p + r -= r // p + p += 1 + if m > 1: + r -= r // m + return r +out, n = [], 1 +while len(out) < 20: + n += 1 + s, v = 0, n + while v != 1: + v = phi(v) + s += v + if s == n: + out.append(n) +print("first 20 perfect totient numbers: " + " ".join(map(str, out))) diff --git a/tasks/perfect-totient-numbers/perfect-totient-numbers.om b/tasks/perfect-totient-numbers/perfect-totient-numbers.om new file mode 100644 index 0000000..c52ca89 --- /dev/null +++ b/tasks/perfect-totient-numbers/perfect-totient-numbers.om @@ -0,0 +1,75 @@ +note Rosetta Code task "Perfect totient numbers": https://rosettacode.org/wiki/Perfect_totient_numbers +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text first "first 20 perfect totient numbers:" +note ph n is Euler's totient for n below 6000 (the 20th perfect totient number is 5571) +global ph ulong 6000 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn sieve to void +for i ulong 6000 +put ph i i +end +p = ulong 2 +while lt p 6000 bound 6000 +if eq get ph p p +j = p +while lt j 6000 bound 6000 +put ph j sub get ph j div get ph j p +j = add j p +end +end +p = add p 1 +end +end +note n is perfect totient when phi(n) + phi(phi(n)) + ... down to 1 adds up to n; each step at least halves +note an even value, so 64 steps are plenty +fn main to int +sieve +emit first +c = ulong 0 +n = ulong 2 +while lt c 20 bound 5999 +s = ulong 0 +v = get ph n +while gt v 1 bound 64 +s = add s v +v = get ph v +end +s = add s 1 +if eq s n +emit sp +emitu n +c = add c 1 +end +n = add n 1 +end +emit nl +ret 0 +end diff --git a/tasks/perfect-totient-numbers/wolfram.txt b/tasks/perfect-totient-numbers/wolfram.txt new file mode 100644 index 0000000..5df17b7 --- /dev/null +++ b/tasks/perfect-totient-numbers/wolfram.txt @@ -0,0 +1,12 @@ +Wolfram cross-check of tasks/perfect-totient-numbers/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 5c8d7b3ea25182e410297fee9e4d703dc6d627ce0a277d404e50fe8e65ac8953. +agreement: YES +query: +ptn = Select[Range[2, 6000], Total[Rest[FixedPointList[EulerPhi, #]]] - 1 == # &]; +s = "first 20 perfect totient numbers: " <> StringRiffle[ToString /@ Take[ptn, 20], " "] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"5c8d7b3ea25182e410297fee9e4d703dc6d627ce0a277d404e50fe8e65ac8953", "first 20 perfect totient numbers: 3 9 15 27 39 81 111 183 243 255 327 363 471 729 2187 2199 3063 4359 4375 5571\n"} diff --git a/tasks/permutation-sort/expected.txt b/tasks/permutation-sort/expected.txt new file mode 100644 index 0000000..7b953be --- /dev/null +++ b/tasks/permutation-sort/expected.txt @@ -0,0 +1,3 @@ +8 -2 15 3 0 6 +-2 0 3 6 8 15 +permutations tried: 209 diff --git a/tasks/permutation-sort/oracle.py b/tasks/permutation-sort/oracle.py new file mode 100644 index 0000000..e8586d4 --- /dev/null +++ b/tasks/permutation-sort/oracle.py @@ -0,0 +1,11 @@ +# Independent reference for "Sorting algorithms/Permutation sort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +from itertools import permutations +a = [8, -2, 15, 3, 0, 6] +print(*a) +for tried, p in enumerate(permutations(range(len(a))), 1): + b = [a[i] for i in p] + if all(b[i] <= b[i + 1] for i in range(len(b) - 1)): + break +print(*b) +print("permutations tried:", tried) diff --git a/tasks/permutation-sort/permutation-sort.om b/tasks/permutation-sort/permutation-sort.om new file mode 100644 index 0000000..78c187a --- /dev/null +++ b/tasks/permutation-sort/permutation-sort.om @@ -0,0 +1,156 @@ +note Rosetta Code task "Sorting algorithms/Permutation sort": https://rosettacode.org/wiki/Sorting_algorithms/Permutation_sort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "8 -2 15 3 0 6" +text ttried "permutations tried: " +global vals long 8 +global perm ulong 8 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note sorted says whether vals taken in the order perm[0..n-1] is non-decreasing +fn sorted n ulong to bool +for i ulong sub n 1 +if gt get vals get perm i get vals get perm add i 1 +ret false +end +end +ret true +end +note nextp steps perm to the next permutation in lexicographic order; false after the last one +fn nextp n ulong to bool +i = sub n 1 +while gt i 0 bound 8 +if lt get perm sub i 1 get perm i +break +end +i = sub i 1 +end +if eq i 0 +ret false +end +j = sub n 1 +while le get perm j get perm sub i 1 bound 8 +j = sub j 1 +end +t = get perm sub i 1 +put perm sub i 1 get perm j +put perm j t +k = sub n 1 +while lt i k bound 8 +t2 = get perm i +put perm i get perm k +put perm k t2 +i = add i 1 +k = sub k 1 +end +ret true +end +note pmsort tries the index permutations in lexicographic order (at most 6! = 720) until one is sorted +fn pmsort n ulong to ulong +for i ulong n +put perm i i +end +c = ulong 1 +while not sorted n bound 720 +if not nextp n +trap 9 +end +c = add c 1 +end +t = new long 8 0 +for i ulong n +put t i get vals get perm i +end +for i ulong n +put vals i get t i +end +ret c +end +fn main to int +n = parse data +show n +c = pmsort n +show n +emit ttried +emitu c +emit nl +ret 0 +end diff --git a/tasks/permutations-by-swapping/expected.txt b/tasks/permutations-by-swapping/expected.txt new file mode 100644 index 0000000..c4fbdda --- /dev/null +++ b/tasks/permutations-by-swapping/expected.txt @@ -0,0 +1,31 @@ +Perm: [1, 2, 3] Sign: 1 +Perm: [1, 3, 2] Sign: -1 +Perm: [3, 1, 2] Sign: 1 +Perm: [3, 2, 1] Sign: -1 +Perm: [2, 3, 1] Sign: 1 +Perm: [2, 1, 3] Sign: -1 + +Perm: [1, 2, 3, 4] Sign: 1 +Perm: [1, 2, 4, 3] Sign: -1 +Perm: [1, 4, 2, 3] Sign: 1 +Perm: [4, 1, 2, 3] Sign: -1 +Perm: [4, 1, 3, 2] Sign: 1 +Perm: [1, 4, 3, 2] Sign: -1 +Perm: [1, 3, 4, 2] Sign: 1 +Perm: [1, 3, 2, 4] Sign: -1 +Perm: [3, 1, 2, 4] Sign: 1 +Perm: [3, 1, 4, 2] Sign: -1 +Perm: [3, 4, 1, 2] Sign: 1 +Perm: [4, 3, 1, 2] Sign: -1 +Perm: [4, 3, 2, 1] Sign: 1 +Perm: [3, 4, 2, 1] Sign: -1 +Perm: [3, 2, 4, 1] Sign: 1 +Perm: [3, 2, 1, 4] Sign: -1 +Perm: [2, 3, 1, 4] Sign: 1 +Perm: [2, 3, 4, 1] Sign: -1 +Perm: [2, 4, 3, 1] Sign: 1 +Perm: [4, 2, 3, 1] Sign: -1 +Perm: [4, 2, 1, 3] Sign: 1 +Perm: [2, 4, 1, 3] Sign: -1 +Perm: [2, 1, 4, 3] Sign: 1 +Perm: [2, 1, 3, 4] Sign: -1 diff --git a/tasks/permutations-by-swapping/oracle.py b/tasks/permutations-by-swapping/oracle.py new file mode 100644 index 0000000..af27633 --- /dev/null +++ b/tasks/permutations-by-swapping/oracle.py @@ -0,0 +1,20 @@ +# Independent reference for "Permutations by swapping" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# the same order built recursively: the element n sweeps right to left through the first order of +# n-1 elements, then left to right through the next, and so on; the sign is the inversion parity +def sjt(n): + if n == 1: + return [[1]] + out = [] + for i, p in enumerate(sjt(n - 1)): + spots = range(n - 1, -1, -1) if i % 2 == 0 else range(n) + out += [p[:k] + [n] + p[k:] for k in spots] + return out +def sign(p): + inv = sum(1 for i in range(len(p)) for j in range(i + 1, len(p)) if p[i] > p[j]) + return -1 if inv % 2 else 1 +for n in (3, 4): + if n == 4: + print() + for p in sjt(n): + print(f"Perm: [{', '.join(map(str, p))}] Sign: {sign(p)}") diff --git a/tasks/permutations-by-swapping/permutations-by-swapping.om b/tasks/permutations-by-swapping/permutations-by-swapping.om new file mode 100644 index 0000000..1eceb75 --- /dev/null +++ b/tasks/permutations-by-swapping/permutations-by-swapping.om @@ -0,0 +1,111 @@ +note Rosetta Code task "Permutations by swapping": https://rosettacode.org/wiki/Permutations_by_swapping +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tperm "Perm: [" +text tsign "] Sign: " +text tcomma ", " +global perm ulong 8 +global dir long 8 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note Steinhaus-Johnson-Trotter with Even's speedup: every element has a direction (all start left); +note the largest mobile element (one whose neighbour in its direction is smaller) swaps that way, and +note every larger element turns round; each step is one swap, so the sign alternates +fn mobile n ulong to ulong +b = n +for i ulong n +j = add long i get dir i +if and ge j 0 lt j long n +if gt get perm i get perm ulong j +if or eq b n gt get perm i get perm b +b = i +end +end +end +end +ret b +end +fn show n ulong s long to void +emit tperm +for i ulong n +if gt i 0 +emit tcomma +end +emitu get perm i +end +emit tsign +emiti s +emit nl +end +fn sjt n ulong to void +for i ulong n +put perm i add i 1 +put dir i -1 +end +s = long 1 +show n s +note each pass makes one new permutation and n! of them end the run: 720 = 6! covers n up to 6 +while true bound 720 +m = mobile n +if eq m n +break +end +v = get perm m +j = ulong add long m get dir m +put perm m get perm j +put perm j v +t = get dir m +put dir m get dir j +put dir j t +for i ulong n +if gt get perm i v +put dir i neg get dir i +end +end +s = neg s +show n s +end +end +note n = 3 and n = 4: 3! + 4! = 30 lines; 6! = 720 bounds the loop for any n up to 6 +fn main to int +sjt 3 +emit nl +sjt 4 +ret 0 +end diff --git a/tasks/permutations-derangements/expected.txt b/tasks/permutations-derangements/expected.txt new file mode 100644 index 0000000..260e290 --- /dev/null +++ b/tasks/permutations-derangements/expected.txt @@ -0,0 +1,22 @@ +derangements of 0 1 2 3: +1 0 3 2 +1 2 3 0 +1 3 0 2 +2 0 3 1 +2 3 0 1 +2 3 1 0 +3 0 1 2 +3 2 0 1 +3 2 1 0 +n counted !n +0 1 1 +1 0 0 +2 1 1 +3 2 2 +4 9 9 +5 44 44 +6 265 265 +7 1854 1854 +8 14833 14833 +9 133496 133496 +!20 = 895014631192902121 diff --git a/tasks/permutations-derangements/oracle.py b/tasks/permutations-derangements/oracle.py new file mode 100644 index 0000000..5f4e486 --- /dev/null +++ b/tasks/permutations-derangements/oracle.py @@ -0,0 +1,18 @@ +# Independent reference for "Permutations/Derangements" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# Cross-check, WolframLanguageEvaluator: Table[Subfactorial[n], {n, 0, 9}] and Subfactorial[20] +# -> {1, 0, 1, 2, 9, 44, 265, 1854, 14833, 133496} and 895014631192902121 +from itertools import permutations +from math import factorial +def subfact(n): + # inclusion-exclusion: sum over k of (-1)^k n!/k! + return sum((-1) ** k * factorial(n) // factorial(k) for k in range(n + 1)) +print("derangements of 0 1 2 3:") +for p in permutations(range(4)): + if all(p[i] != i for i in range(4)): + print(*p) +print("n counted !n") +for n in range(10): + counted = sum(1 for p in permutations(range(n)) if all(p[i] != i for i in range(n))) + print(f"{n} {counted:8} {subfact(n)}") +print("!20 =", subfact(20)) diff --git a/tasks/permutations-derangements/permutations-derangements.om b/tasks/permutations-derangements/permutations-derangements.om new file mode 100644 index 0000000..7a0ce00 --- /dev/null +++ b/tasks/permutations-derangements/permutations-derangements.om @@ -0,0 +1,153 @@ +note Rosetta Code task "Permutations/Derangements": https://rosettacode.org/wiki/Permutations/Derangements +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tder "derangements of 0 1 2 3:\n" +text thead "n counted !n\n" +text tbig "!20 = " +global perm ulong 10 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt w sub 20 i +i = sub 20 w +end +emit part d i sub 20 i +end +note nextp: the next permutation in lexicographic order, false after the last +fn nextp n ulong to bool +if lt n 2 +ret false +end +i = sub n 1 +while gt i 0 bound 10 +if lt get perm sub i 1 get perm i +break +end +i = sub i 1 +end +if eq i 0 +ret false +end +j = sub n 1 +while le get perm j get perm sub i 1 bound 10 +j = sub j 1 +end +t = get perm sub i 1 +put perm sub i 1 get perm j +put perm j t +k = sub n 1 +while lt i k bound 10 +t2 = get perm i +put perm i get perm k +put perm k t2 +i = add i 1 +k = sub k 1 +end +ret true +end +fn deranged n ulong to bool +for i ulong n +if eq get perm i i +ret false +end +end +ret true +end +note count walks all n! permutations (at most 9! = 362880) and counts those that fix no point; +note with show set it also prints them +fn count n ulong show bool to ulong +for i ulong n +put perm i i +end +c = ulong 0 +while true bound 362880 +if deranged n +c = add c 1 +if show +for i ulong n +if gt i 0 +emit sp +end +emitu get perm i +end +emit nl +end +end +if not nextp n +break +end +end +ret c +end +note subfactorial by the recurrence !0 = 1, !1 = 0, !n = (n-1)(!(n-1) + !(n-2)); !20 fits a ulong +fn subfact n ulong to ulong +a = ulong 1 +b = ulong 0 +if eq n 0 +ret a +end +for x ulong sub n 1 +m = add x 1 +c = mul m add a b +a = b +b = c +end +ret b +end +fn main to int +emit tder +z = count 4 true +emit thead +for n ulong 10 +emitu n +emit sp +emitw count n false 8 +emit sp +emit sp +emitu subfact n +emit nl +end +emit tbig +emitu subfact 20 +emit nl +ret 0 +end diff --git a/tasks/permutations/expected.txt b/tasks/permutations/expected.txt new file mode 100644 index 0000000..7b18f7a --- /dev/null +++ b/tasks/permutations/expected.txt @@ -0,0 +1,25 @@ +1 2 3 4 +1 2 4 3 +1 3 2 4 +1 3 4 2 +1 4 2 3 +1 4 3 2 +2 1 3 4 +2 1 4 3 +2 3 1 4 +2 3 4 1 +2 4 1 3 +2 4 3 1 +3 1 2 4 +3 1 4 2 +3 2 1 4 +3 2 4 1 +3 4 1 2 +3 4 2 1 +4 1 2 3 +4 1 3 2 +4 2 1 3 +4 2 3 1 +4 3 1 2 +4 3 2 1 +permutations: 24 diff --git a/tasks/permutations/oracle.py b/tasks/permutations/oracle.py new file mode 100644 index 0000000..a185801 --- /dev/null +++ b/tasks/permutations/oracle.py @@ -0,0 +1,14 @@ +# Independent reference for "Permutations" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def perms(items): + # recursive: each element in turn first, followed by every order of the rest + if not items: + yield [] + return + for i, x in enumerate(items): + for rest in perms(items[:i] + items[i + 1:]): + yield [x] + rest +ps = list(perms([1, 2, 3, 4])) +for p in ps: + print(*p) +print("permutations:", len(ps)) diff --git a/tasks/permutations/permutations.om b/tasks/permutations/permutations.om new file mode 100644 index 0000000..848eca6 --- /dev/null +++ b/tasks/permutations/permutations.om @@ -0,0 +1,90 @@ +note Rosetta Code task "Permutations": https://rosettacode.org/wiki/Permutations +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tcount "permutations: " +global perm ulong 8 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note nextp steps perm[0..n-1] to the next permutation in lexicographic order; false after the last: +note find the last ascent i-1 < i, swap perm[i-1] with the last larger element, reverse the tail +fn nextp n ulong to bool +i = sub n 1 +while gt i 0 bound 8 +if lt get perm sub i 1 get perm i +break +end +i = sub i 1 +end +if eq i 0 +ret false +end +j = sub n 1 +while le get perm j get perm sub i 1 bound 8 +j = sub j 1 +end +t = get perm sub i 1 +put perm sub i 1 get perm j +put perm j t +k = sub n 1 +while lt i k bound 8 +t2 = get perm i +put perm i get perm k +put perm k t2 +i = add i 1 +k = sub k 1 +end +ret true +end +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emitu get perm i +end +emit nl +end +note all 4! orders of 1 2 3 4; 24 permutations bound the loop +fn main to int +n = ulong 4 +for i ulong n +put perm i add i 1 +end +c = ulong 1 +show n +while nextp n bound 24 +show n +c = add c 1 +end +emit tcount +emitu c +emit nl +ret 0 +end diff --git a/tasks/pernicious-numbers/expected.txt b/tasks/pernicious-numbers/expected.txt new file mode 100644 index 0000000..5e0600e --- /dev/null +++ b/tasks/pernicious-numbers/expected.txt @@ -0,0 +1,2 @@ +first 25 pernicious numbers: 3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36 +pernicious numbers from 888888877 to 888888888: 888888877 888888878 888888880 888888883 888888885 888888886 diff --git a/tasks/pernicious-numbers/oracle.py b/tasks/pernicious-numbers/oracle.py new file mode 100644 index 0000000..cd60c41 --- /dev/null +++ b/tasks/pernicious-numbers/oracle.py @@ -0,0 +1,12 @@ +# Independent reference for "Pernicious numbers" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +PRIMES = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61} +def pern(n): + return bin(n).count("1") in PRIMES +out, n = [], 0 +while len(out) < 25: + n += 1 + if pern(n): + out.append(n) +print("first 25 pernicious numbers: " + " ".join(map(str, out))) +print("pernicious numbers from 888888877 to 888888888: " + " ".join(str(n) for n in range(888888877, 888888889) if pern(n))) diff --git a/tasks/pernicious-numbers/pernicious-numbers.om b/tasks/pernicious-numbers/pernicious-numbers.om new file mode 100644 index 0000000..0f0d6cd --- /dev/null +++ b/tasks/pernicious-numbers/pernicious-numbers.om @@ -0,0 +1,80 @@ +note Rosetta Code task "Pernicious numbers": https://rosettacode.org/wiki/Pernicious_numbers +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text first "first 25 pernicious numbers:" +text range "pernicious numbers from 888888877 to 888888888:" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn popc n ulong to ulong +c = ulong 0 +v = n +while gt v 0 bound 64 +c = add c and v 1 +v = shr v 1 +end +ret c +end +note a pernicious number has a prime population count; counts here are at most 30, so trial division is enough +fn isprime n ulong to bool +if lt n 2 +ret false +end +d = ulong 2 +while le mul d d n bound 8 +if eq rem n d 0 +ret false +end +d = add d 1 +end +ret true +end +fn main to int +emit first +c = ulong 0 +n = ulong 1 +while lt c 25 bound 100 +if isprime popc n +emit sp +emitu n +c = add c 1 +end +n = add n 1 +end +emit nl +emit range +for i ulong 12 +n = add 888888877 i +if isprime popc n +emit sp +emitu n +end +end +emit nl +ret 0 +end diff --git a/tasks/pernicious-numbers/wolfram.txt b/tasks/pernicious-numbers/wolfram.txt new file mode 100644 index 0000000..d5c10a2 --- /dev/null +++ b/tasks/pernicious-numbers/wolfram.txt @@ -0,0 +1,13 @@ +Wolfram cross-check of tasks/pernicious-numbers/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = ce6a584e350fb8899870dbc726aef10cfc4458eba3fcb7ca091f6d6e204f7568. +agreement: YES +query: +pern[n_] := PrimeQ[DigitCount[n, 2, 1]]; +s = "first 25 pernicious numbers: " <> StringRiffle[ToString /@ Take[Select[Range[100], pern], 25], " "] <> "\n" <> + "pernicious numbers from 888888877 to 888888888: " <> StringRiffle[ToString /@ Select[Range[888888877, 888888888], pern], " "] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"ce6a584e350fb8899870dbc726aef10cfc4458eba3fcb7ca091f6d6e204f7568", "first 25 pernicious numbers: 3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36\npernicious numbers from 888888877 to 888888888: 888888877 888888878 888888880 888888883 888888885 888888886\n"} diff --git a/tasks/phrase-reversals/expected.txt b/tasks/phrase-reversals/expected.txt new file mode 100644 index 0000000..e4291c0 --- /dev/null +++ b/tasks/phrase-reversals/expected.txt @@ -0,0 +1,3 @@ +lasrever esarhp edoc attesor +attesor edoc esarhp lasrever +reversal phrase code rosetta diff --git a/tasks/phrase-reversals/oracle.py b/tasks/phrase-reversals/oracle.py new file mode 100644 index 0000000..6214840 --- /dev/null +++ b/tasks/phrase-reversals/oracle.py @@ -0,0 +1,6 @@ +# Independent reference for "Phrase reversals" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +s = "rosetta code phrase reversal" +print(s[::-1]) +print(" ".join(w[::-1] for w in s.split(" "))) +print(" ".join(reversed(s.split(" ")))) diff --git a/tasks/phrase-reversals/phrase-reversals.om b/tasks/phrase-reversals/phrase-reversals.om new file mode 100644 index 0000000..1e968a3 --- /dev/null +++ b/tasks/phrase-reversals/phrase-reversals.om @@ -0,0 +1,72 @@ +note Rosetta Code task "Phrase reversals": https://rosettacode.org/wiki/Phrase_reversals +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text msg "rosetta code phrase reversal" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note words splits a into (start, length) pairs at single spaces and returns how many +fn words a view uchar * st edit ulong 16 ln edit ulong 16 to ulong +k = ulong 0 +s = ulong 0 +for i ulong add len a 1 +e = eq i len a +if not e +e = eq get a i 32 +end +if e +put st k s +put ln k sub i s +k = add k 1 +s = add i 1 +end +end +ret k +end +fn revb a view uchar * to void +for i ulong len a +emitc get a sub sub len a 1 i +end +end +fn main to int +st = new ulong 16 0 +ln = new ulong 16 0 +k = words msg st ln +note 1: the whole string reversed +revb msg +emit nl +note 2: each word reversed, order kept +for w ulong k +if gt w 0 +emit sp +end +revb part msg get st w get ln w +end +emit nl +note 3: the word order reversed, words kept +for w ulong k +if gt w 0 +emit sp +end +x = sub sub k 1 w +emit part msg get st x get ln x +end +emit nl +ret 0 +end diff --git a/tasks/population-count/expected.txt b/tasks/population-count/expected.txt new file mode 100644 index 0000000..caa4fba --- /dev/null +++ b/tasks/population-count/expected.txt @@ -0,0 +1,3 @@ +popcount of 3^0 .. 3^29: 1 2 2 4 3 6 6 5 6 8 9 13 10 11 14 15 11 14 14 17 17 20 19 22 16 18 24 30 25 25 +first 30 evil numbers: 0 3 5 6 9 10 12 15 17 18 20 23 24 27 29 30 33 34 36 39 40 43 45 46 48 51 53 54 57 58 +first 30 odious numbers: 1 2 4 7 8 11 13 14 16 19 21 22 25 26 28 31 32 35 37 38 41 42 44 47 49 50 52 55 56 59 diff --git a/tasks/population-count/oracle.py b/tasks/population-count/oracle.py new file mode 100644 index 0000000..7a0f330 --- /dev/null +++ b/tasks/population-count/oracle.py @@ -0,0 +1,7 @@ +# Independent reference for "Population count" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import itertools +pc = lambda n: format(n, "b").count("1") +print("popcount of 3^0 .. 3^29: " + " ".join(str(pc(3 ** i)) for i in range(30))) +print("first 30 evil numbers: " + " ".join(map(str, itertools.islice((n for n in itertools.count() if pc(n) % 2 == 0), 30)))) +print("first 30 odious numbers: " + " ".join(map(str, itertools.islice((n for n in itertools.count() if pc(n) % 2 == 1), 30)))) diff --git a/tasks/population-count/population-count.om b/tasks/population-count/population-count.om new file mode 100644 index 0000000..f3038d3 --- /dev/null +++ b/tasks/population-count/population-count.om @@ -0,0 +1,73 @@ +note Rosetta Code task "Population count": https://rosettacode.org/wiki/Population_count +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text pw "popcount of 3^0 .. 3^29:" +text evil "first 30 evil numbers: " +text odious "first 30 odious numbers:" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note Kernighan: n & (n - 1) clears the lowest set bit +fn popc n ulong to ulong +c = ulong 0 +v = n +while gt v 0 bound 64 +v = and v sub v 1 +c = add c 1 +end +ret c +end +note evil numbers have an even population count, odious ones an odd count +fn list want ulong to void +c = ulong 0 +n = ulong 0 +while lt c 30 bound 100 +if eq rem popc n 2 want +emit sp +emitu n +c = add c 1 +end +n = add n 1 +end +emit nl +end +fn main to int +emit pw +p = ulong 1 +for i ulong 30 +emit sp +emitu popc p +p = mul p 3 +end +emit nl +emit evil +list 0 +emit odious +list 1 +ret 0 +end diff --git a/tasks/population-count/wolfram.txt b/tasks/population-count/wolfram.txt new file mode 100644 index 0000000..1fd1d97 --- /dev/null +++ b/tasks/population-count/wolfram.txt @@ -0,0 +1,14 @@ +Wolfram cross-check of tasks/population-count/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 6a02e4ea3fdb442b6ff091f2bd9eeb511ee21af495890d3f4dfee7c315b11a63. +agreement: YES +query: +pc[n_] := DigitCount[n, 2, 1]; +s = "popcount of 3^0 .. 3^29: " <> StringRiffle[ToString /@ pc /@ (3^Range[0, 29]), " "] <> "\n" <> + "first 30 evil numbers: " <> StringRiffle[ToString /@ Take[Select[Range[0, 100], EvenQ[pc[#]] &], 30], " "] <> "\n" <> + "first 30 odious numbers: " <> StringRiffle[ToString /@ Take[Select[Range[0, 100], OddQ[pc[#]] &], 30], " "] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"6a02e4ea3fdb442b6ff091f2bd9eeb511ee21af495890d3f4dfee7c315b11a63", "popcount of 3^0 .. 3^29: 1 2 2 4 3 6 6 5 6 8 9 13 10 11 14 15 11 14 14 17 17 20 19 22 16 18 24 30 25 25\nfirst 30 evil numbers: 0 3 5 6 9 10 12 15 17 18 20 23 24 27 29 30 33 34 36 39 40 43 45 46 48 51 53 54 57 58\nfirst 30 odious numbers: 1 2 4 7 8 11 13 14 16 19 21 22 25 26 28 31 32 35 37 38 41 42 44 47 49 50 52 55 56 59\n"} diff --git a/tasks/power-set/expected.txt b/tasks/power-set/expected.txt new file mode 100644 index 0000000..7fb91e0 --- /dev/null +++ b/tasks/power-set/expected.txt @@ -0,0 +1,17 @@ +{} +{1} +{2} +{1, 2} +{3} +{1, 3} +{2, 3} +{1, 2, 3} +{4} +{1, 4} +{2, 4} +{1, 2, 4} +{3, 4} +{1, 3, 4} +{2, 3, 4} +{1, 2, 3, 4} +subsets: 16 diff --git a/tasks/power-set/oracle.py b/tasks/power-set/oracle.py new file mode 100644 index 0000000..ceb9554 --- /dev/null +++ b/tasks/power-set/oracle.py @@ -0,0 +1,10 @@ +# Independent reference for "Power set" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# built by doubling: the subsets without the next element, then the same subsets with it added, +# which lists them in binary-counting order of their membership masks +subsets = [[]] +for x in [1, 2, 3, 4]: + subsets += [s + [x] for s in subsets] +for s in subsets: + print("{" + ", ".join(map(str, s)) + "}") +print("subsets:", len(subsets)) diff --git a/tasks/power-set/power-set.om b/tasks/power-set/power-set.om new file mode 100644 index 0000000..78f3470 --- /dev/null +++ b/tasks/power-set/power-set.om @@ -0,0 +1,62 @@ +note Rosetta Code task "Power set": https://rosettacode.org/wiki/Power_set +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tlb "{" +text trb "}" +text tcomma ", " +text tcount "subsets: " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note subset m of {1, 2, 3, 4} holds element i+1 when bit i of m is set; the 16 subsets are listed in +note the order of m, which is binary counting +fn subset m ulong to void +emit tlb +first = true +for i ulong 4 +if ne and shr m i 1 0 +if not first +emit tcomma +end +emitu add i 1 +first = false +end +end +emit trb +emit nl +end +fn main to int +for m ulong 16 +subset m +end +emit tcount +emitu 16 +emit nl +ret 0 +end diff --git a/tasks/primality-by-trial-division/expected.txt b/tasks/primality-by-trial-division/expected.txt new file mode 100644 index 0000000..33e6bbb --- /dev/null +++ b/tasks/primality-by-trial-division/expected.txt @@ -0,0 +1,10 @@ +primes below 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 +1 is not prime +2 is prime +9 is not prime +561 is not prime +7919 is prime +2147483647 is prime +999999999989 is prime +999999999999 is not prime +1000000000039 is prime diff --git a/tasks/primality-by-trial-division/oracle.py b/tasks/primality-by-trial-division/oracle.py new file mode 100644 index 0000000..a4d617a --- /dev/null +++ b/tasks/primality-by-trial-division/oracle.py @@ -0,0 +1,8 @@ +# Independent reference for "Primality by trial division" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import math +def prime(n): + return n >= 2 and all(n % d for d in range(2, math.isqrt(n) + 1)) +print("primes below 100: " + " ".join(str(n) for n in range(100) if prime(n))) +for n in [1, 2, 9, 561, 7919, 2147483647, 999999999989, 999999999999, 1000000000039]: + print(f"{n} is {'prime' if prime(n) else 'not prime'}") diff --git a/tasks/primality-by-trial-division/primality-by-trial-division.om b/tasks/primality-by-trial-division/primality-by-trial-division.om new file mode 100644 index 0000000..b473f84 --- /dev/null +++ b/tasks/primality-by-trial-division/primality-by-trial-division.om @@ -0,0 +1,80 @@ +note Rosetta Code task "Primality by trial division": https://rosettacode.org/wiki/Primality_by_trial_division +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text hdr "primes below 100:" +text yes " is prime\n" +text no " is not prime\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note trial division by 2, then by odd d while d * d <= n +note bound: the largest input is below 10^12 + 40, so d stays below 10^6 + 2 (at most 500001 odd values) and mul d d does not wrap +fn prime n ulong to bool +if lt n 2 +ret false +end +if eq rem n 2 0 +ret eq n 2 +end +d = ulong 3 +while le mul d d n bound 999999 +if eq rem n d 0 +ret false +end +d = add d 2 +end +ret true +end +fn tell n ulong to void +emitu n +if prime n +emit yes +else +emit no +end +end +fn main to int +emit hdr +for i ulong 100 +if prime i +emit sp +emitu i +end +end +emit nl +tell 1 +tell 2 +tell 9 +tell 561 +tell 7919 +tell 2147483647 +tell 999999999989 +tell 999999999999 +tell 1000000000039 +ret 0 +end diff --git a/tasks/primality-by-trial-division/wolfram.txt b/tasks/primality-by-trial-division/wolfram.txt new file mode 100644 index 0000000..1ca2e05 --- /dev/null +++ b/tasks/primality-by-trial-division/wolfram.txt @@ -0,0 +1,13 @@ +Wolfram cross-check of tasks/primality-by-trial-division/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 23da22a8ba264c1ffc8cd421eae1cf4b8010aa802c9d66f6da3990eb74104052. +agreement: YES +query: +s = "primes below 100: " <> StringRiffle[ToString /@ Select[Range[0, 99], PrimeQ], " "] <> "\n" <> + StringJoin[(ToString[#] <> If[PrimeQ[#], " is prime\n", " is not prime\n"]) & /@ + {1, 2, 9, 561, 7919, 2147483647, 999999999989, 999999999999, 1000000000039}]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"23da22a8ba264c1ffc8cd421eae1cf4b8010aa802c9d66f6da3990eb74104052", "primes below 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97\n1 is not prime\n2 is prime\n9 is not prime\n561 is not prime\n7919 is prime\n2147483647 is prime\n999999999989 is prime\n999999999999 is not prime\n1000000000039 is prime\n"} diff --git a/tasks/priority-queue/expected.txt b/tasks/priority-queue/expected.txt new file mode 100644 index 0000000..0483f65 --- /dev/null +++ b/tasks/priority-queue/expected.txt @@ -0,0 +1,10 @@ +insert 3 Clear drains +insert 4 Feed cat +insert 5 Make tea +insert 1 Solve RC tasks +insert 2 Tax return +pop 1 Solve RC tasks +pop 2 Tax return +pop 3 Clear drains +pop 4 Feed cat +pop 5 Make tea diff --git a/tasks/priority-queue/oracle.py b/tasks/priority-queue/oracle.py new file mode 100644 index 0000000..4df082c --- /dev/null +++ b/tasks/priority-queue/oracle.py @@ -0,0 +1,10 @@ +# Independent reference for "Priority queue" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import heapq +tasks = ["3 Clear drains", "4 Feed cat", "5 Make tea", "1 Solve RC tasks", "2 Tax return"] +h = [] +for t in tasks: + heapq.heappush(h, (int(t.split()[0]), t)) + print("insert", t) +while h: + print("pop", heapq.heappop(h)[1]) diff --git a/tasks/priority-queue/priority-queue.om b/tasks/priority-queue/priority-queue.om new file mode 100644 index 0000000..25d1122 --- /dev/null +++ b/tasks/priority-queue/priority-queue.om @@ -0,0 +1,123 @@ +note Rosetta Code task "Priority queue": https://rosettacode.org/wiki/Priority_queue +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tins "insert " +text tpop "pop " +text tdata "3 Clear drains|4 Feed cat|5 Make tea|1 Solve RC tasks|2 Tax return" +global heap ulong 16 +global pri ulong 16 +global hn ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a binary min-heap of task numbers keyed by priority: heap[0] has the smallest pri; the five tasks +note are the |-separated entries of tdata, each a priority digit and a name +fn entry k ulong to void +f = ulong 0 +st = ulong 0 +for i ulong len tdata +if eq get tdata i 124 +if eq f k +emit part tdata st sub i st +ret +end +f = add f 1 +st = add i 1 +end +end +emit part tdata st sub len tdata st +end +fn swap i ulong j ulong to void +t = get heap i +put heap i get heap j +put heap j t +end +fn less i ulong j ulong to bool +ret lt get pri get heap i get pri get heap j +end +note insert sifts the new task up; the index halves each step, so 16 steps bound it +fn insert k ulong p ulong to void +put pri k p +n = get hn 0 +put heap n k +put hn 0 add n 1 +i = n +while gt i 0 bound 16 +u = shr sub i 1 1 +if not less i u +break +end +swap i u +i = u +end +emit tins +entry k +emit nl +end +note popmin moves the last task to the root and sifts it down; the index doubles each step +fn popmin to void +n = sub get hn 0 1 +k = get heap 0 +put heap 0 get heap n +put hn 0 n +i = ulong 0 +while lt add mul i 2 1 n bound 16 +c = add mul i 2 1 +if lt add c 1 n +if less add c 1 c +c = add c 1 +end +end +if not less c i +break +end +swap i c +i = c +end +emit tpop +entry k +emit nl +end +fn main to int +st = ulong 0 +k = ulong 0 +for i ulong len tdata +if eq i st +insert k ulong sub get tdata i 48 +k = add k 1 +end +if eq get tdata i 124 +st = add i 1 +end +end +for x ulong k +popmin +end +ret 0 +end diff --git a/tasks/proper-divisors/expected.txt b/tasks/proper-divisors/expected.txt new file mode 100644 index 0000000..cb11c5f --- /dev/null +++ b/tasks/proper-divisors/expected.txt @@ -0,0 +1,11 @@ +1: +2: 1 +3: 1 +4: 1 2 +5: 1 +6: 1 2 3 +7: 1 +8: 1 2 4 +9: 1 3 +10: 1 2 5 +the number in 1..20000 with the most proper divisors: 15120 has 79 proper divisors diff --git a/tasks/proper-divisors/oracle.py b/tasks/proper-divisors/oracle.py new file mode 100644 index 0000000..ab23349 --- /dev/null +++ b/tasks/proper-divisors/oracle.py @@ -0,0 +1,10 @@ +# Independent reference for "Proper divisors" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import math +def proper(n): + small = [d for d in range(1, math.isqrt(n) + 1) if n % d == 0] + return sorted((set(small) | {n // d for d in small}) - {n}) +for n in range(1, 11): + print(f"{n}:" + "".join(f" {d}" for d in proper(n))) +best = max(range(1, 20001), key=lambda n: (len(proper(n)), -n)) +print(f"the number in 1..20000 with the most proper divisors: {best} has {len(proper(best))} proper divisors") diff --git a/tasks/proper-divisors/proper-divisors.om b/tasks/proper-divisors/proper-divisors.om new file mode 100644 index 0000000..d2d1403 --- /dev/null +++ b/tasks/proper-divisors/proper-divisors.om @@ -0,0 +1,76 @@ +note Rosetta Code task "Proper divisors": https://rosettacode.org/wiki/Proper_divisors +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text col ":" +text most "the number in 1..20000 with the most proper divisors: " +text has " has " +text pd " proper divisors\n" +note nd n counts the proper divisors of n, for n up to 20000 +global nd ulong 20001 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn main to int +note proper divisors of 1..10 by trial over 1..n-1 +for i ulong 10 +n = add i 1 +emitu n +emit col +for j ulong i +d = add j 1 +if eq rem n d 0 +emit sp +emitu d +end +end +emit nl +end +note each d counts once for each multiple 2d, 3d, ... up to 20000; fewer than 20000 multiples per d +d = ulong 1 +while le d 10000 bound 10000 +j = mul d 2 +while le j 20000 bound 20000 +put nd j add get nd j 1 +j = add j d +end +d = add d 1 +end +b = ulong 1 +for i ulong 20000 +n = add i 1 +if gt get nd n get nd b +b = n +end +end +emit most +emitu b +emit has +emitu get nd b +emit pd +ret 0 +end diff --git a/tasks/proper-divisors/wolfram.txt b/tasks/proper-divisors/wolfram.txt new file mode 100644 index 0000000..c0c9192 --- /dev/null +++ b/tasks/proper-divisors/wolfram.txt @@ -0,0 +1,15 @@ +Wolfram cross-check of tasks/proper-divisors/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 0056f6b77996c5288e405826503b7f6bff40d80132cfaa9fe7d92e6c2072de1b. +agreement: YES +query: +pd[n_] := Most[Divisors[n]]; +cnt = Table[DivisorSigma[0, n] - 1, {n, 1, 20000}]; +b = First[FirstPosition[cnt, Max[cnt]]]; +s = StringJoin[Table[ToString[n] <> ":" <> StringJoin[(" " <> ToString[#]) & /@ pd[n]] <> "\n", {n, 1, 10}]] <> + "the number in 1..20000 with the most proper divisors: " <> ToString[b] <> " has " <> ToString[Max[cnt]] <> " proper divisors\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"0056f6b77996c5288e405826503b7f6bff40d80132cfaa9fe7d92e6c2072de1b", "1:\n2: 1\n3: 1\n4: 1 2\n5: 1\n6: 1 2 3\n7: 1\n8: 1 2 4\n9: 1 3\n10: 1 2 5\nthe number in 1..20000 with the most proper divisors: 15120 has 79 proper divisors\n"} diff --git a/tasks/pythagorean-triples/expected.txt b/tasks/pythagorean-triples/expected.txt new file mode 100644 index 0000000..45dfa0c --- /dev/null +++ b/tasks/pythagorean-triples/expected.txt @@ -0,0 +1,6 @@ +perimeter <= 100: 17 triples, 7 primitive +perimeter <= 1000: 325 triples, 70 primitive +perimeter <= 10000: 4858 triples, 703 primitive +perimeter <= 100000: 64741 triples, 7026 primitive +perimeter <= 1000000: 808950 triples, 70229 primitive +perimeter <= 10000000: 9706567 triples, 702309 primitive diff --git a/tasks/pythagorean-triples/oracle.py b/tasks/pythagorean-triples/oracle.py new file mode 100644 index 0000000..23b19f3 --- /dev/null +++ b/tasks/pythagorean-triples/oracle.py @@ -0,0 +1,19 @@ +# Independent reference for "Pythagorean triples" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import math +# Berggren's tree of primitive triples: every primitive triple appears once, children have larger perimeters +M = [((1, -2, 2), (2, -1, 2), (2, -2, 3)), ((1, 2, 2), (2, 1, 2), (2, 2, 3)), ((-1, 2, 2), (-2, 1, 2), (-2, 2, 3))] +TOP = 10 ** 7 +per = [] +stack = [(3, 4, 5)] +while stack: + t = stack.pop() + p = sum(t) + if p > TOP: + continue + per.append(p) + for m in M: + stack.append(tuple(sum(r[i] * t[i] for i in range(3)) for r in m)) +for e in range(2, 8): + lim = 10 ** e + print(f"perimeter <= {lim}: {sum(lim // p for p in per if p <= lim)} triples, {sum(1 for p in per if p <= lim)} primitive") diff --git a/tasks/pythagorean-triples/pythagorean-triples.om b/tasks/pythagorean-triples/pythagorean-triples.om new file mode 100644 index 0000000..99fe57b --- /dev/null +++ b/tasks/pythagorean-triples/pythagorean-triples.om @@ -0,0 +1,85 @@ +note Rosetta Code task "Pythagorean triples": https://rosettacode.org/wiki/Pythagorean_triples +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text pre "perimeter <= " +text col ": " +text tr " triples, " +text pr " primitive\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn gcd a ulong b ulong to ulong +x = a +y = b +while ne y 0 bound 128 +t = rem x y +x = y +y = t +end +ret x +end +note Euclid: m > n > 0 coprime of opposite parity give each primitive triple once, with perimeter 2m(m+n); +note a primitive perimeter p counts for limit L once as primitive and L / p times among all triples +note bound: 2m(m+1) <= 10^7 needs m <= 2235 +fn main to int +pc = new ulong 7 0 +tc = new ulong 7 0 +m = ulong 2 +while le mul mul 2 m add m 1 10000000 bound 2236 +n = add rem m 2 1 +while lt n m bound 2236 +p = mul mul 2 m add m n +if gt p 10000000 +break +end +if eq gcd m n 1 +l = ulong 100 +for k ulong 6 +if le p l +put pc k add get pc k 1 +put tc k add get tc k div l p +end +l = mul l 10 +end +end +n = add n 2 +end +m = add m 1 +end +l = ulong 100 +for k ulong 6 +emit pre +emitu l +emit col +emitu get tc k +emit tr +emitu get pc k +emit pr +l = mul l 10 +end +ret 0 +end diff --git a/tasks/pythagorean-triples/wolfram.txt b/tasks/pythagorean-triples/wolfram.txt new file mode 100644 index 0000000..6ae9569 --- /dev/null +++ b/tasks/pythagorean-triples/wolfram.txt @@ -0,0 +1,13 @@ +Wolfram cross-check of tasks/pythagorean-triples/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 80386dfde3777fce64f7bf8e375099005fab8e5e2331032bac2a10a66c235ff1. +agreement: YES +query: +prims = Flatten[Table[If[OddQ[m - n] && CoprimeQ[m, n] && 2 m (m + n) <= 10^7, 2 m (m + n), Nothing], {m, 2, 2236}, {n, 1, m - 1}]]; +s = StringJoin[Table[With[{l = 10^e, ps = Select[prims, # <= 10^e &]}, + "perimeter <= " <> ToString[l] <> ": " <> ToString[Total[Quotient[l, ps]]] <> " triples, " <> ToString[Length[ps]] <> " primitive\n"], {e, 2, 7}]]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"80386dfde3777fce64f7bf8e375099005fab8e5e2331032bac2a10a66c235ff1", "perimeter <= 100: 17 triples, 7 primitive\nperimeter <= 1000: 325 triples, 70 primitive\nperimeter <= 10000: 4858 triples, 703 primitive\nperimeter <= 100000: 64741 triples, 7026 primitive\nperimeter <= 1000000: 808950 triples, 70229 primitive\nperimeter <= 10000000: 9706567 triples, 702309 primitive\n"} diff --git a/tasks/queue-definition/expected.txt b/tasks/queue-definition/expected.txt new file mode 100644 index 0000000..bd39da7 --- /dev/null +++ b/tasks/queue-definition/expected.txt @@ -0,0 +1,26 @@ +empty: yes +push 11 +push 22 +push 33 +push 44 +push 55 +push 66 +pop 11 +pop 22 +pop 33 +pop 44 +push -77 +push -88 +push -99 +push -110 +push -121 +empty: no +pop 55 +pop 66 +pop -77 +pop -88 +pop -99 +pop -110 +pop -121 +pop from an empty queue: error +empty: yes diff --git a/tasks/queue-definition/oracle.py b/tasks/queue-definition/oracle.py new file mode 100644 index 0000000..587e2bf --- /dev/null +++ b/tasks/queue-definition/oracle.py @@ -0,0 +1,26 @@ +# Independent reference for "Queue/Definition" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +from collections import deque +q = deque() +def empty(): + print("empty:", "no" if q else "yes") +def push(v): + q.append(v) + print("push", v) +def pop(): + if not q: + print("pop from an empty queue: error") + return False + print("pop", q.popleft()) + return True +empty() +for i in range(1, 7): + push(11 * i) +for _ in range(4): + pop() +for i in range(7, 12): + push(-11 * i) +empty() +while pop(): + pass +empty() diff --git a/tasks/queue-definition/queue-definition.om b/tasks/queue-definition/queue-definition.om new file mode 100644 index 0000000..e31bc26 --- /dev/null +++ b/tasks/queue-definition/queue-definition.om @@ -0,0 +1,107 @@ +note Rosetta Code task "Queue/Definition": https://rosettacode.org/wiki/Queue/Definition +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tpush "push " +text tpop "pop " +text tempty "empty: " +text tyes "yes" +text tno "no" +text terr "pop from an empty queue: error" +global ring long 8 +global head ulong 1 +global cnt ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note a FIFO queue in a ring of 8 slots: cnt elements starting at ring[head]; push writes behind the +note last one, pop takes ring[head]; a full ring stops the program (exit 2) +fn push v long to void +c = get cnt 0 +if eq c 8 +exit 2 +end +put ring rem add get head 0 c 8 v +put cnt 0 add c 1 +emit tpush +emiti v +emit nl +end +note pop returns false, and prints the error, when the queue is empty +fn pop to bool +c = get cnt 0 +if eq c 0 +emit terr +emit nl +ret false +end +h = get head 0 +emit tpop +emiti get ring h +emit nl +put head 0 rem add h 1 8 +put cnt 0 sub c 1 +ret true +end +fn empty to void +emit tempty +if eq get cnt 0 0 +emit tyes +else +emit tno +end +emit nl +end +note the ring wraps: 6 pushes, 4 pops and 5 more pushes cross slot 7 back to slot 0 +fn main to int +empty +for i ulong 6 +push mul long add i 1 11 +end +for i ulong 4 +ok = pop +end +for i ulong 5 +push mul long add i 7 -11 +end +empty +note 7 elements remain, so the 8th pop finds the queue empty and ends the loop +while pop bound 9 +end +empty +ret 0 +end diff --git a/tasks/queue-usage/expected.txt b/tasks/queue-usage/expected.txt new file mode 100644 index 0000000..7f7aef6 --- /dev/null +++ b/tasks/queue-usage/expected.txt @@ -0,0 +1,12 @@ +empty: yes +push 1 +push 2 +push 3 +pop 1 +empty: no +push 4 +pop 2 +pop 3 +pop 4 +empty: yes +pop from an empty queue: error diff --git a/tasks/queue-usage/oracle.py b/tasks/queue-usage/oracle.py new file mode 100644 index 0000000..8438011 --- /dev/null +++ b/tasks/queue-usage/oracle.py @@ -0,0 +1,21 @@ +# Independent reference for "Queue/Usage" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +queue = [] +def empty(): + print("empty:", "no" if queue else "yes") +def push(v): + queue.append(v) + print("push", v) +def pop(): + if not queue: + print("pop from an empty queue: error") + else: + print("pop", queue.pop(0)) +empty() +push(1); push(2); push(3) +pop() +empty() +push(4) +pop(); pop(); pop() +empty() +pop() diff --git a/tasks/queue-usage/queue-usage.om b/tasks/queue-usage/queue-usage.om new file mode 100644 index 0000000..bae5dd8 --- /dev/null +++ b/tasks/queue-usage/queue-usage.om @@ -0,0 +1,103 @@ +note Rosetta Code task "Queue/Usage": https://rosettacode.org/wiki/Queue/Usage +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tpush "push " +text tpop "pop " +text tempty "empty: " +text tyes "yes" +text tno "no" +text terr "pop from an empty queue: error" +global ring long 8 +global head ulong 1 +global cnt ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note a FIFO queue in a ring of 8 slots: cnt elements starting at ring[head]; push writes behind the +note last one, pop takes ring[head]; a full ring stops the program (exit 2) +fn push v long to void +c = get cnt 0 +if eq c 8 +exit 2 +end +put ring rem add get head 0 c 8 v +put cnt 0 add c 1 +emit tpush +emiti v +emit nl +end +note pop returns false, and prints the error, when the queue is empty +fn pop to bool +c = get cnt 0 +if eq c 0 +emit terr +emit nl +ret false +end +h = get head 0 +emit tpop +emiti get ring h +emit nl +put head 0 rem add h 1 8 +put cnt 0 sub c 1 +ret true +end +fn empty to void +emit tempty +if eq get cnt 0 0 +emit tyes +else +emit tno +end +emit nl +end +fn main to int +empty +push 1 +push 2 +push 3 +ok = pop +empty +push 4 +ok = pop +ok = pop +ok = pop +empty +ok = pop +ret 0 +end diff --git a/tasks/quicksort/expected.txt b/tasks/quicksort/expected.txt new file mode 100644 index 0000000..ebd497e --- /dev/null +++ b/tasks/quicksort/expected.txt @@ -0,0 +1,2 @@ +65 -12 0 33 -7 99 18 18 -40 5 71 2 -1 27 +-40 -12 -7 -1 0 2 5 18 18 27 33 65 71 99 diff --git a/tasks/quicksort/oracle.py b/tasks/quicksort/oracle.py new file mode 100644 index 0000000..c16c7f9 --- /dev/null +++ b/tasks/quicksort/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Sorting algorithms/Quicksort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [65, -12, 0, 33, -7, 99, 18, 18, -40, 5, 71, 2, -1, 27] +print(*a) +print(*sorted(a)) diff --git a/tasks/quicksort/quicksort.om b/tasks/quicksort/quicksort.om new file mode 100644 index 0000000..494cbfd --- /dev/null +++ b/tasks/quicksort/quicksort.om @@ -0,0 +1,115 @@ +note Rosetta Code task "Sorting algorithms/Quicksort": https://rosettacode.org/wiki/Sorting_algorithms/Quicksort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "65 -12 0 33 -7 99 18 18 -40 5 71 2 -1 27" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note qsort sorts vals[lo..hi-1] with a Lomuto partition around the last element; each call recurses +note on strictly smaller ranges, so the depth is at most hi-lo and the scan at most 16 passes +fn qsort lo ulong hi ulong to void +if le sub hi lo 1 +ret +end +p = get vals sub hi 1 +i = lo +j = lo +while lt j sub hi 1 bound 16 +if lt get vals j p +t = get vals i +put vals i get vals j +put vals j t +i = add i 1 +end +j = add j 1 +end +put vals sub hi 1 get vals i +put vals i p +qsort lo i +qsort add i 1 hi +end +fn main to int +n = parse data +show n +qsort 0 n +show n +ret 0 +end diff --git a/tasks/radix-sort/expected.txt b/tasks/radix-sort/expected.txt new file mode 100644 index 0000000..f3c924a --- /dev/null +++ b/tasks/radix-sort/expected.txt @@ -0,0 +1,2 @@ +170 45 75 -90 -802 24 2 66 0 1999 -1 333 +-802 -90 -1 0 2 24 45 66 75 170 333 1999 diff --git a/tasks/radix-sort/oracle.py b/tasks/radix-sort/oracle.py new file mode 100644 index 0000000..65fa2f1 --- /dev/null +++ b/tasks/radix-sort/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Sorting algorithms/Radix sort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [170, 45, 75, -90, -802, 24, 2, 66, 0, 1999, -1, 333] +print(*a) +print(*sorted(a)) diff --git a/tasks/radix-sort/radix-sort.om b/tasks/radix-sort/radix-sort.om new file mode 100644 index 0000000..2b68fac --- /dev/null +++ b/tasks/radix-sort/radix-sort.om @@ -0,0 +1,144 @@ +note Rosetta Code task "Sorting algorithms/Radix sort": https://rosettacode.org/wiki/Sorting_algorithms/Radix_sort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "170 45 75 -90 -802 24 2 66 0 1999 -1 333" +global vals long 16 +global key ulong 16 +global tk ulong 16 +global bk ulong 10 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note rxsort shifts every value by the minimum so the keys are unsigned, then sorts the keys with a +note stable least-significant-digit-first counting pass per decimal digit; 20 passes cover any ulong +fn rxsort n ulong to void +lo = get vals 0 +for i ulong n +if lt get vals i lo +lo = get vals i +end +end +mx = ulong 0 +for i ulong n +put key i ulong sub get vals i lo +if gt get key i mx +mx = get key i +end +end +e = ulong 1 +for p ulong 20 +for b ulong 10 +put bk b 0 +end +for i ulong n +dg = rem div get key i e 10 +put bk dg add get bk dg 1 +end +s = ulong 0 +for b ulong 10 +c = get bk b +put bk b s +s = add s c +end +for i ulong n +dg = rem div get key i e 10 +put tk get bk dg get key i +put bk dg add get bk dg 1 +end +for i ulong n +put key i get tk i +end +note stop when no key has a digit at 10*e; e > mx/10 means 10*e > mx, and 10*e is never formed past that +if gt e div mx 10 +break +end +e = mul e 10 +end +for i ulong n +put vals i add lo long get key i +end +end +fn main to int +n = parse data +show n +rxsort n +show n +ret 0 +end diff --git a/tasks/recamans-sequence/expected.txt b/tasks/recamans-sequence/expected.txt new file mode 100644 index 0000000..b6567ff --- /dev/null +++ b/tasks/recamans-sequence/expected.txt @@ -0,0 +1,3 @@ +first 15 terms: 0 1 3 6 2 7 13 20 12 21 11 22 10 23 9 +first term already seen: a(24) = 42 +terms a(0)..a(n) first contain every integer 0..1000 at n = 328002 diff --git a/tasks/recamans-sequence/oracle.py b/tasks/recamans-sequence/oracle.py new file mode 100644 index 0000000..98dd0bf --- /dev/null +++ b/tasks/recamans-sequence/oracle.py @@ -0,0 +1,17 @@ +# Independent reference for "Recaman's sequence" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a, seen, terms, dup = [0], {0}, [0], None +need = set(range(1, 1001)) +n = 0 +while need: + n += 1 + b = a[-1] - n + v = b if b > 0 and b not in seen else a[-1] + n + if v in seen and dup is None: + dup = (n, v) + seen.add(v) + need.discard(v) + a.append(v) +print("first 15 terms: " + " ".join(map(str, a[:15]))) +print(f"first term already seen: a({dup[0]}) = {dup[1]}") +print(f"terms a(0)..a(n) first contain every integer 0..1000 at n = {n}") diff --git a/tasks/recamans-sequence/recamans-sequence.om b/tasks/recamans-sequence/recamans-sequence.om new file mode 100644 index 0000000..bfcd6d8 --- /dev/null +++ b/tasks/recamans-sequence/recamans-sequence.om @@ -0,0 +1,83 @@ +note Rosetta Code task "Recaman's sequence": https://rosettacode.org/wiki/Recaman%27s_sequence +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text first "first 15 terms:" +text dup "first term already seen: a(" +text eqs ") = " +text cov "terms a(0)..a(n) first contain every integer 0..1000 at n = " +note sn v is 1 once v has appeared; the oracle's run shows no term before 0..1000 is covered exceeds 1942300 +global sn uchar 1942301 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a(0) = 0; a(n) = a(n-1) - n if that is positive and new, else a(n-1) + n +note bound: the cover is complete at n = 328002, below the 999999 loop bound +fn main to int +emit first +emit sp +emitu 0 +put sn 0 1 +a = ulong 0 +n = ulong 0 +d = ulong 0 +c = ulong 1 +while lt c 1001 bound 999999 +n = add n 1 +nw = add a n +if gt a n +if eq get sn sub a n 0 +nw = sub a n +end +end +a = nw +if eq get sn a 0 +if le a 1000 +c = add c 1 +end +else +if eq d 0 +d = n +emit nl +emit dup +emitu n +emit eqs +emitu a +emit nl +end +end +put sn a 1 +if lt n 15 +emit sp +emitu a +end +end +emit cov +emitu n +emit nl +ret 0 +end diff --git a/tasks/recamans-sequence/wolfram.txt b/tasks/recamans-sequence/wolfram.txt new file mode 100644 index 0000000..d63e1b1 --- /dev/null +++ b/tasks/recamans-sequence/wolfram.txt @@ -0,0 +1,18 @@ +Wolfram cross-check of tasks/recamans-sequence/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = db0c2a648e417ebc5b98c8477a0b7a8c0c4091e9bbb30b8261655e6dad95c1d6. +agreement: YES +query: +seen = CreateDataStructure["HashSet", {0}]; a = {0}; v = 0; n = 0; dup = None; need = 1000; +While[need > 0, n++; b = v - n; + v = If[b > 0 && ! seen["MemberQ", b], b, v + n]; + If[seen["MemberQ", v], If[dup === None, dup = {n, v}], If[v <= 1000, need--]]; + seen["Insert", v]; If[n < 15, AppendTo[a, v]]]; +s = "first 15 terms: " <> StringRiffle[ToString /@ a, " "] <> "\n" <> + "first term already seen: a(" <> ToString[dup[[1]]] <> ") = " <> ToString[dup[[2]]] <> "\n" <> + "terms a(0)..a(n) first contain every integer 0..1000 at n = " <> ToString[n] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"db0c2a648e417ebc5b98c8477a0b7a8c0c4091e9bbb30b8261655e6dad95c1d6", "first 15 terms: 0 1 3 6 2 7 13 20 12 21 11 22 10 23 9\nfirst term already seen: a(24) = 42\nterms a(0)..a(n) first contain every integer 0..1000 at n = 328002\n"} diff --git a/tasks/remove-duplicate-elements/expected.txt b/tasks/remove-duplicate-elements/expected.txt new file mode 100644 index 0000000..dd4b659 --- /dev/null +++ b/tasks/remove-duplicate-elements/expected.txt @@ -0,0 +1,2 @@ +4 9 4 1 -3 9 0 1 7 4 -3 12 0 9 5 +4 9 1 -3 0 7 12 5 diff --git a/tasks/remove-duplicate-elements/oracle.py b/tasks/remove-duplicate-elements/oracle.py new file mode 100644 index 0000000..5793079 --- /dev/null +++ b/tasks/remove-duplicate-elements/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Remove duplicate elements" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [4, 9, 4, 1, -3, 9, 0, 1, 7, 4, -3, 12, 0, 9, 5] +print(*a) +print(*dict.fromkeys(a)) diff --git a/tasks/remove-duplicate-elements/remove-duplicate-elements.om b/tasks/remove-duplicate-elements/remove-duplicate-elements.om new file mode 100644 index 0000000..67f4b90 --- /dev/null +++ b/tasks/remove-duplicate-elements/remove-duplicate-elements.om @@ -0,0 +1,112 @@ +note Rosetta Code task "Remove duplicate elements": https://rosettacode.org/wiki/Remove_duplicate_elements +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "4 9 4 1 -3 9 0 1 7 4 -3 12 0 9 5" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note dedup keeps the first copy of each value, in order: an element is kept when no earlier kept element +note equals it; O(n^2) with n at most 16, and it returns the new length +fn dedup n ulong to ulong +k = ulong 0 +for i ulong n +v = get vals i +seen = false +for j ulong k +if eq get vals j v +seen = true +break +end +end +if not seen +put vals k v +k = add k 1 +end +end +ret k +end +fn main to int +n = parse data +show n +k = dedup n +show k +ret 0 +end diff --git a/tasks/remove-vowels-from-a-string/expected.txt b/tasks/remove-vowels-from-a-string/expected.txt new file mode 100644 index 0000000..9cb5dd7 --- /dev/null +++ b/tasks/remove-vowels-from-a-string/expected.txt @@ -0,0 +1,2 @@ +Rosetta Code is a programming chrestomathy site, AEIOU and aeiou alike. +Rstt Cd s prgrmmng chrstmthy st, nd lk. diff --git a/tasks/remove-vowels-from-a-string/oracle.py b/tasks/remove-vowels-from-a-string/oracle.py new file mode 100644 index 0000000..d8163bb --- /dev/null +++ b/tasks/remove-vowels-from-a-string/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Remove vowels from a string" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +s = "Rosetta Code is a programming chrestomathy site, AEIOU and aeiou alike." +print(s) +print("".join(c for c in s if c not in "aeiouAEIOU")) diff --git a/tasks/remove-vowels-from-a-string/remove-vowels-from-a-string.om b/tasks/remove-vowels-from-a-string/remove-vowels-from-a-string.om new file mode 100644 index 0000000..9c87f9f --- /dev/null +++ b/tasks/remove-vowels-from-a-string/remove-vowels-from-a-string.om @@ -0,0 +1,43 @@ +note Rosetta Code task "Remove vowels from a string": https://rosettacode.org/wiki/Remove_vowels_from_a_string +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text msg "Rosetta Code is a programming chrestomathy site, AEIOU and aeiou alike." +text vow "aeiouAEIOU" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +fn isvowel c uchar to bool +for i ulong len vow +if eq c get vow i +ret true +end +end +ret false +end +fn main to int +emit msg +emit nl +for i ulong len msg +c = get msg i +if not isvowel c +emitc c +end +end +emit nl +ret 0 +end diff --git a/tasks/repeat-a-string/expected.txt b/tasks/repeat-a-string/expected.txt new file mode 100644 index 0000000..9cb2091 --- /dev/null +++ b/tasks/repeat-a-string/expected.txt @@ -0,0 +1,2 @@ +hahahahaha +***** diff --git a/tasks/repeat-a-string/oracle.py b/tasks/repeat-a-string/oracle.py new file mode 100644 index 0000000..624ff37 --- /dev/null +++ b/tasks/repeat-a-string/oracle.py @@ -0,0 +1,4 @@ +# Independent reference for "Repeat a string" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +print("ha" * 5) +print("*" * 5) diff --git a/tasks/repeat-a-string/repeat-a-string.om b/tasks/repeat-a-string/repeat-a-string.om new file mode 100644 index 0000000..238c082 --- /dev/null +++ b/tasks/repeat-a-string/repeat-a-string.om @@ -0,0 +1,35 @@ +note Rosetta Code task "Repeat a string": https://rosettacode.org/wiki/Repeat_a_string +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text ha "ha" +text star "*" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note repeat writes a, k times, into one buffer (up to 256 bytes, more traps 3) and prints it +fn repeat a view uchar * k ulong to void +b = new uchar 256 0 +p = ulong 0 +for j ulong k +for i ulong len a +put b p get a i +p = add p 1 +end +end +emit part b 0 p +emit nl +end +fn main to int +repeat ha 5 +repeat star 5 +ret 0 +end diff --git a/tasks/reverse-words-in-a-string/expected.txt b/tasks/reverse-words-in-a-string/expected.txt new file mode 100644 index 0000000..de243a8 --- /dev/null +++ b/tasks/reverse-words-in-a-string/expected.txt @@ -0,0 +1,10 @@ +------------ Fire and Ice ---------- + +Some say the world will end in fire, +Some say in ice. +From what I've tasted of desire +I hold with those who favor fire. + +... last paragraph elided ... + +----------------------- Robert Frost diff --git a/tasks/reverse-words-in-a-string/oracle.py b/tasks/reverse-words-in-a-string/oracle.py new file mode 100644 index 0000000..270fc48 --- /dev/null +++ b/tasks/reverse-words-in-a-string/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Reverse words in a string" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +lines = ['---------- Ice and Fire ------------', '', 'fire, in end will world the say Some', 'ice. in say Some', "desire of tasted I've what From", 'fire. favor who those with hold I', '', '... elided paragraph last ...', '', 'Frost Robert -----------------------'] +for line in lines: + print(" ".join(reversed(line.split()))) diff --git a/tasks/reverse-words-in-a-string/reverse-words-in-a-string.om b/tasks/reverse-words-in-a-string/reverse-words-in-a-string.om new file mode 100644 index 0000000..78cd18f --- /dev/null +++ b/tasks/reverse-words-in-a-string/reverse-words-in-a-string.om @@ -0,0 +1,59 @@ +note Rosetta Code task "Reverse words in a string": https://rosettacode.org/wiki/Reverse_words_in_a_string +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text poem "---------- Ice and Fire ------------\n\nfire, in end will world the say Some\nice. in say Some\ndesire of tasted I've what From\nfire. favor who those with hold I\n\n... elided paragraph last ...\n\nFrost Robert -----------------------\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note revline prints the space-separated words of a in reverse order, one space between them +fn revline a view uchar * to void +st = new ulong 64 0 +ln = new ulong 64 0 +k = ulong 0 +i = ulong 0 +while lt i len a bound 4096 +if eq get a i 32 +i = add i 1 +else +j = i +while lt j len a bound 4096 +if eq get a j 32 +break +end +j = add j 1 +end +put st k i +put ln k sub j i +k = add k 1 +i = j +end +end +for w ulong k +if gt w 0 +emit sp +end +x = sub sub k 1 w +emit part a get st x get ln x +end +emit nl +end +fn main to int +s = ulong 0 +for i ulong len poem +if eq get poem i 10 +revline part poem s sub i s +s = add i 1 +end +end +ret 0 +end diff --git a/tasks/run-length-encoding/expected.txt b/tasks/run-length-encoding/expected.txt new file mode 100644 index 0000000..fbc3dce --- /dev/null +++ b/tasks/run-length-encoding/expected.txt @@ -0,0 +1,2 @@ +12W1B12W3B24W1B14W +WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW diff --git a/tasks/run-length-encoding/oracle.py b/tasks/run-length-encoding/oracle.py new file mode 100644 index 0000000..da4d61e --- /dev/null +++ b/tasks/run-length-encoding/oracle.py @@ -0,0 +1,7 @@ +# Independent reference for "Run-length encoding" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import itertools, re +msg = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW" +e = "".join(f"{len(list(g))}{c}" for c, g in itertools.groupby(msg)) +print(e) +print("".join(c * int(n) for n, c in re.findall(r"(\d+)(\D)", e))) diff --git a/tasks/run-length-encoding/run-length-encoding.om b/tasks/run-length-encoding/run-length-encoding.om new file mode 100644 index 0000000..d77fed6 --- /dev/null +++ b/tasks/run-length-encoding/run-length-encoding.om @@ -0,0 +1,85 @@ +note Rosetta Code task "Run-length encoding": https://rosettacode.org/wiki/Run-length_encoding +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text msg "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note putnum writes n in decimal into r at p and returns the offset after it +fn putnum r edit uchar * p ulong n ulong to ulong +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +q = p +for j ulong sub 20 i +put r q get d add i j +q = add q 1 +end +ret q +end +note encode writes count then byte for every run of equal bytes; returns the length +fn encode a view uchar * r edit uchar * to ulong +p = ulong 0 +i = ulong 0 +while lt i len a bound 4096 +c = get a i +j = i +while lt j len a bound 4096 +if ne get a j c +break +end +j = add j 1 +end +p = putnum r p sub j i +put r p c +p = add p 1 +i = j +end +ret p +end +note decode reads a decimal count then one byte, and repeats the byte; returns the length +fn decode a view uchar * r edit uchar * to ulong +p = ulong 0 +v = ulong 0 +for i ulong len a +c = get a i +if and ge c 48 le c 57 +v = add mul v 10 ulong sub c 48 +else +for k ulong v +put r p c +p = add p 1 +end +v = 0 +end +end +ret p +end +fn main to int +e = new uchar 256 0 +d = new uchar 256 0 +n = encode msg e +emit part e 0 n +emit nl +m = decode part e 0 n d +emit part d 0 m +emit nl +ret 0 +end diff --git a/tasks/selection-sort/expected.txt b/tasks/selection-sort/expected.txt new file mode 100644 index 0000000..56d34b1 --- /dev/null +++ b/tasks/selection-sort/expected.txt @@ -0,0 +1,2 @@ +29 -3 72 15 0 -88 41 41 6 100 -1 +-88 -3 -1 0 6 15 29 41 41 72 100 diff --git a/tasks/selection-sort/oracle.py b/tasks/selection-sort/oracle.py new file mode 100644 index 0000000..75b48d6 --- /dev/null +++ b/tasks/selection-sort/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Sorting algorithms/Selection sort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [29, -3, 72, 15, 0, -88, 41, 41, 6, 100, -1] +print(*a) +print(*sorted(a)) diff --git a/tasks/selection-sort/selection-sort.om b/tasks/selection-sort/selection-sort.om new file mode 100644 index 0000000..783d640 --- /dev/null +++ b/tasks/selection-sort/selection-sort.om @@ -0,0 +1,107 @@ +note Rosetta Code task "Sorting algorithms/Selection sort": https://rosettacode.org/wiki/Sorting_algorithms/Selection_sort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "29 -3 72 15 0 -88 41 41 6 100 -1" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note ssort puts the smallest remaining element at position i, for i = 0 .. n-2 +fn ssort n ulong to void +for i ulong sub n 1 +m = i +for j ulong sub sub n i 1 +k = add add i j 1 +if lt get vals k get vals m +m = k +end +end +t = get vals i +put vals i get vals m +put vals m t +end +end +fn main to int +n = parse data +show n +ssort n +show n +ret 0 +end diff --git a/tasks/semiprime/expected.txt b/tasks/semiprime/expected.txt new file mode 100644 index 0000000..d53f0a9 --- /dev/null +++ b/tasks/semiprime/expected.txt @@ -0,0 +1,3 @@ +semiprimes below 100: 4 6 9 10 14 15 21 22 25 26 33 34 35 38 39 46 49 51 55 57 58 62 65 69 74 77 82 85 86 87 91 93 94 95 +semiprimes below 10000: 2625 +1679 = 23 * 73 is semiprime: yes diff --git a/tasks/semiprime/oracle.py b/tasks/semiprime/oracle.py new file mode 100644 index 0000000..d4a03ac --- /dev/null +++ b/tasks/semiprime/oracle.py @@ -0,0 +1,12 @@ +# Independent reference for "Semiprime" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +N = 10000 +sieve = [False, False] + [True] * (N - 2) +for i in range(2, 100): + if sieve[i]: + sieve[i * i::i] = [False] * len(sieve[i * i::i]) +primes = [p for p in range(N) if sieve[p]] +semi = set(p * q for i, p in enumerate(primes) for q in primes[i:] if p * q < N) +print("semiprimes below 100: " + " ".join(str(n) for n in sorted(semi) if n < 100)) +print(f"semiprimes below 10000: {len(semi)}") +print("1679 = 23 * 73 is semiprime: " + ("yes" if 1679 in semi else "no")) diff --git a/tasks/semiprime/semiprime.om b/tasks/semiprime/semiprime.om new file mode 100644 index 0000000..ad0770c --- /dev/null +++ b/tasks/semiprime/semiprime.om @@ -0,0 +1,84 @@ +note Rosetta Code task "Semiprime": https://rosettacode.org/wiki/Semiprime +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text lst "semiprimes below 100:" +text cnt "semiprimes below 10000: " +text tst "1679 = 23 * 73 is semiprime: " +text yes "yes\n" +text no "no\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note semiprime: the least factor d of n leaves a prime cofactor n / d +fn semi n ulong to bool +if lt n 4 +ret false +end +d = ulong 2 +while le mul d d n bound 100 +if eq rem n d 0 +m = div n d +e = ulong 2 +while le mul e e m bound 100 +if eq rem m e 0 +ret false +end +e = add e 1 +end +ret true +end +d = add d 1 +end +ret false +end +fn main to int +emit lst +for n ulong 100 +if semi n +emit sp +emitu n +end +end +emit nl +c = ulong 0 +for n ulong 10000 +if semi n +c = add c 1 +end +end +emit cnt +emitu c +emit nl +emit tst +if semi 1679 +emit yes +else +emit no +end +ret 0 +end diff --git a/tasks/semiprime/wolfram.txt b/tasks/semiprime/wolfram.txt new file mode 100644 index 0000000..09f6e80 --- /dev/null +++ b/tasks/semiprime/wolfram.txt @@ -0,0 +1,14 @@ +Wolfram cross-check of tasks/semiprime/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 14642a17d928ab317cada41a01feb83bf7e35bd037f137e4f31a2ebed4afe490. +agreement: YES +query: +sp = Select[Range[9999], PrimeOmega[#] == 2 &]; +s = "semiprimes below 100: " <> StringRiffle[ToString /@ Select[sp, # < 100 &], " "] <> "\n" <> + "semiprimes below 10000: " <> ToString[Length[sp]] <> "\n" <> + "1679 = 23 * 73 is semiprime: " <> If[PrimeOmega[1679] == 2, "yes", "no"] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"14642a17d928ab317cada41a01feb83bf7e35bd037f137e4f31a2ebed4afe490", "semiprimes below 100: 4 6 9 10 14 15 21 22 25 26 33 34 35 38 39 46 49 51 55 57 58 62 65 69 74 77 82 85 86 87 91 93 94 95\nsemiprimes below 10000: 2625\n1679 = 23 * 73 is semiprime: yes\n"} diff --git a/tasks/sha-1/expected.txt b/tasks/sha-1/expected.txt new file mode 100644 index 0000000..0d03a30 --- /dev/null +++ b/tasks/sha-1/expected.txt @@ -0,0 +1,5 @@ +da39a3ee5e6b4b0d3255bfef95601890afd80709 +a9993e364706816aba3e25717850c26c9cd0d89d +48c98f7e5a6e736d790ab740dfc3f51a61abe2b5 +2fd4e1c67a2d28fced849ee1bb76e7391b93eb12 +ed6c69d9e8b4373af86303dfaa3528dfbc129902 diff --git a/tasks/sha-1/oracle.py b/tasks/sha-1/oracle.py new file mode 100644 index 0000000..fa100b5 --- /dev/null +++ b/tasks/sha-1/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "SHA-1" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import hashlib +for s in ["", 'abc', 'Rosetta Code', 'The quick brown fox jumps over the lazy dog', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']: + print(hashlib.sha1(s.encode()).hexdigest()) diff --git a/tasks/sha-1/sha-1.om b/tasks/sha-1/sha-1.om new file mode 100644 index 0000000..9ed4ce9 --- /dev/null +++ b/tasks/sha-1/sha-1.om @@ -0,0 +1,142 @@ +note Rosetta Code task "SHA-1": https://rosettacode.org/wiki/SHA-1 +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text hexd "0123456789abcdef" +text m0 "abc" +text m1 "Rosetta Code" +text m2 "The quick brown fox jumps over the lazy dog" +text m3 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +global hs uint 5 +global ww uint 80 +global buf uchar 128 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note emith writes the byte v as two lower-case hex digits +fn emith v uchar to void +emitc get hexd shr v 4 +emitc get hexd and v 15 +end +note rnd is step i of 80 on the state (a, b, c, d, e) in hs, with the expanded schedule in ww +fn rnd i ulong to void +a = get hs 0 +b = get hs 1 +c = get hs 2 +d = get hs 3 +f = uint 0 +k = uint 0 +if lt i 20 +f = or and b c and not b d +k = 1518500249 +else +if lt i 40 +f = xor xor b c d +k = 1859775393 +else +if lt i 60 +f = or or and b c and b d and c d +k = 2400959708 +else +f = xor xor b c d +k = 3395469782 +end +end +end +t = add add add add rotl a uint 5 f get hs 4 k get ww i +put hs 4 d +put hs 3 c +put hs 2 rotl b uint 30 +put hs 1 a +put hs 0 t +end +note block loads the 64 bytes of buf at p as 16 big-endian words, extends them to 80 and runs the steps +fn block p ulong to void +for w ulong 16 +v = uint 0 +for j ulong 4 +v = or shl v 8 uint get buf add add p mul w 4 j +end +put ww w v +end +for w ulong 64 +x = add w 16 +v = xor get ww sub x 3 get ww sub x 8 +v = xor v get ww sub x 14 +v = xor v get ww w +put ww x rotl v uint 1 +end +sv = new uint 5 0 +for k ulong 5 +put sv k get hs k +end +for i ulong 80 +rnd i +end +for k ulong 5 +put hs k add get hs k get sv k +end +end +note digest prints the five state words big-endian in hex +fn digest to void +for k ulong 5 +v = get hs k +for j ulong 4 +s = mul uint sub 3 j 8 +emith uchar and shr v s 255 +end +end +emit nl +end +note sha1 pads a (up to 119 bytes) with 0x80, zeros and the bit length (big-endian) to one or two +note 64-byte blocks, hashes them and prints the digest in hex +fn sha1 a view uchar * to void +put hs 0 uint 1732584193 +put hs 1 uint 4023233417 +put hs 2 uint 2562383102 +put hs 3 uint 271733878 +put hs 4 uint 3285377520 +n = len a +for i ulong 128 +put buf i 0 +end +for i ulong n +put buf i get a i +end +put buf n 128 +t = ulong 64 +if ge n 56 +t = 128 +end +bits = mul n 8 +for j ulong 8 +put buf sub sub t 1 j uchar and shr bits mul j 8 255 +end +for b ulong div t 64 +block mul b 64 +end +digest +end +fn main to int +note the empty message, as a slice of length 0 +sha1 part m0 0 0 +sha1 m0 +sha1 m1 +sha1 m2 +sha1 m3 +ret 0 +end diff --git a/tasks/shell-sort/expected.txt b/tasks/shell-sort/expected.txt new file mode 100644 index 0000000..2695d6c --- /dev/null +++ b/tasks/shell-sort/expected.txt @@ -0,0 +1,2 @@ +54 26 93 17 77 31 44 55 20 -6 0 13 -21 93 8 +-21 -6 0 8 13 17 20 26 31 44 54 55 77 93 93 diff --git a/tasks/shell-sort/oracle.py b/tasks/shell-sort/oracle.py new file mode 100644 index 0000000..32016ad --- /dev/null +++ b/tasks/shell-sort/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Sorting algorithms/Shell sort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [54, 26, 93, 17, 77, 31, 44, 55, 20, -6, 0, 13, -21, 93, 8] +print(*a) +print(*sorted(a)) diff --git a/tasks/shell-sort/shell-sort.om b/tasks/shell-sort/shell-sort.om new file mode 100644 index 0000000..37f284c --- /dev/null +++ b/tasks/shell-sort/shell-sort.om @@ -0,0 +1,113 @@ +note Rosetta Code task "Sorting algorithms/Shell sort": https://rosettacode.org/wiki/Sorting_algorithms/Shell_sort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "54 26 93 17 77 31 44 55 20 -6 0 13 -21 93 8" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note shsort uses the gaps n/2, n/4, .. 1; each gap is a gapped insertion sort, whose inner walk moves +note down by gap at least once per pass, so 16 passes bound it; the gap halves, so 5 gaps suffice for 16 +fn shsort n ulong to void +g = shr n 1 +while gt g 0 bound 5 +for x ulong sub n g +i = add x g +t = get vals i +j = i +while ge j g bound 16 +if le get vals sub j g t +break +end +put vals j get vals sub j g +j = sub j g +end +put vals j t +end +g = shr g 1 +end +end +fn main to int +n = parse data +show n +shsort n +show n +ret 0 +end diff --git a/tasks/smith-numbers/expected.txt b/tasks/smith-numbers/expected.txt new file mode 100644 index 0000000..1af06b7 --- /dev/null +++ b/tasks/smith-numbers/expected.txt @@ -0,0 +1,3 @@ +Smith numbers below 10000: 376 +first 20: 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 391 438 454 483 +last 10: 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985 diff --git a/tasks/smith-numbers/oracle.py b/tasks/smith-numbers/oracle.py new file mode 100644 index 0000000..a1110ad --- /dev/null +++ b/tasks/smith-numbers/oracle.py @@ -0,0 +1,15 @@ +# Independent reference for "Smith numbers" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +ds = lambda n: sum(map(int, str(n))) +def factor(n): + f, p = [], 2 + while p * p <= n: + while n % p == 0: + f.append(p) + n //= p + p += 1 + return f + ([n] if n > 1 else []) +sm = [n for n in range(2, 10000) if len(factor(n)) > 1 and ds(n) == sum(ds(p) for p in factor(n))] +print(f"Smith numbers below 10000: {len(sm)}") +print("first 20: " + " ".join(map(str, sm[:20]))) +print("last 10: " + " ".join(map(str, sm[-10:]))) diff --git a/tasks/smith-numbers/smith-numbers.om b/tasks/smith-numbers/smith-numbers.om new file mode 100644 index 0000000..9165509 --- /dev/null +++ b/tasks/smith-numbers/smith-numbers.om @@ -0,0 +1,95 @@ +note Rosetta Code task "Smith numbers": https://rosettacode.org/wiki/Smith_numbers +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text cnt "Smith numbers below 10000: " +text first "first 20:" +text last "last 10:" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn dsum n ulong to ulong +s = ulong 0 +v = n +while gt v 0 bound 20 +s = add s rem v 10 +v = div v 10 +end +ret s +end +note a Smith number is composite and its digit sum equals the digit sums of its prime factors, with +note multiplicity; returns 0 for primes and 1 +fn smith n ulong to bool +v = n +d = ulong 2 +f = ulong 0 +k = ulong 0 +while le mul d d v bound 200 +if eq rem v d 0 +f = add f dsum d +k = add k 1 +v = div v d +else +d = add d 1 +end +end +if gt v 1 +f = add f dsum v +k = add k 1 +end +if lt k 2 +ret false +end +ret eq f dsum n +end +fn main to int +sm = new ulong 400 0 +c = ulong 0 +for i ulong 9998 +n = add i 2 +if smith n +put sm c n +c = add c 1 +end +end +emit cnt +emitu c +emit nl +emit first +for i ulong 20 +emit sp +emitu get sm i +end +emit nl +emit last +for i ulong 10 +emit sp +emitu get sm add sub c 10 i +end +emit nl +ret 0 +end diff --git a/tasks/smith-numbers/wolfram.txt b/tasks/smith-numbers/wolfram.txt new file mode 100644 index 0000000..bc711ee --- /dev/null +++ b/tasks/smith-numbers/wolfram.txt @@ -0,0 +1,16 @@ +Wolfram cross-check of tasks/smith-numbers/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 3774404002938e87d724cdf9c23973c3380bb6155e2bc1d42e06d8d370da71c4. +agreement: YES +query: +ds[n_] := Total[IntegerDigits[n]]; +smithQ[n_] := CompositeQ[n] && ds[n] == Total[(ds[#[[1]]] #[[2]]) & /@ FactorInteger[n]]; +sm = Select[Range[2, 9999], smithQ]; +s = "Smith numbers below 10000: " <> ToString[Length[sm]] <> "\n" <> + "first 20: " <> StringRiffle[ToString /@ Take[sm, 20], " "] <> "\n" <> + "last 10: " <> StringRiffle[ToString /@ Take[sm, -10], " "] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"3774404002938e87d724cdf9c23973c3380bb6155e2bc1d42e06d8d370da71c4", "Smith numbers below 10000: 376\nfirst 20: 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 391 438 454 483\nlast 10: 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985\n"} diff --git a/tasks/sort-an-integer-array/expected.txt b/tasks/sort-an-integer-array/expected.txt new file mode 100644 index 0000000..83d04ec --- /dev/null +++ b/tasks/sort-an-integer-array/expected.txt @@ -0,0 +1,2 @@ +2 4 3 1 2 -10 37 0 15 -10 8 100 5 +-10 -10 0 1 2 2 3 4 5 8 15 37 100 diff --git a/tasks/sort-an-integer-array/oracle.py b/tasks/sort-an-integer-array/oracle.py new file mode 100644 index 0000000..786feaf --- /dev/null +++ b/tasks/sort-an-integer-array/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Sort an integer array" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [2, 4, 3, 1, 2, -10, 37, 0, 15, -10, 8, 100, 5] +print(*a) +print(*sorted(a)) diff --git a/tasks/sort-an-integer-array/sort-an-integer-array.om b/tasks/sort-an-integer-array/sort-an-integer-array.om new file mode 100644 index 0000000..82469bb --- /dev/null +++ b/tasks/sort-an-integer-array/sort-an-integer-array.om @@ -0,0 +1,115 @@ +note Rosetta Code task "Sort an integer array": https://rosettacode.org/wiki/Sort_an_integer_array +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "2 4 3 1 2 -10 37 0 15 -10 8 100 5" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note Omega has no library sort, so this is a binary insertion sort: binary search finds where vals[i] +note goes among the sorted vals[0..i-1] (after equal values, so it is stable), then the tail shifts up; +note the search range halves each pass, so 16 elements need at most 5 passes (bound 8) +fn bisort n ulong to void +for i ulong n +v = get vals i +lo = ulong 0 +hi = i +while lt lo hi bound 8 +m = add lo shr sub hi lo 1 +if le get vals m v +lo = add m 1 +else +hi = m +end +end +for x ulong sub i lo +k = sub i x +put vals k get vals sub k 1 +end +put vals lo v +end +end +fn main to int +n = parse data +show n +bisort n +show n +ret 0 +end diff --git a/tasks/sort-disjoint-sublist/expected.txt b/tasks/sort-disjoint-sublist/expected.txt new file mode 100644 index 0000000..184fe30 --- /dev/null +++ b/tasks/sort-disjoint-sublist/expected.txt @@ -0,0 +1,3 @@ +7 6 5 4 3 2 1 0 +indices: 6 1 7 +7 0 5 4 3 2 1 6 diff --git a/tasks/sort-disjoint-sublist/oracle.py b/tasks/sort-disjoint-sublist/oracle.py new file mode 100644 index 0000000..904d060 --- /dev/null +++ b/tasks/sort-disjoint-sublist/oracle.py @@ -0,0 +1,10 @@ +# Independent reference for "Sort disjoint sublist" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +values = [7, 6, 5, 4, 3, 2, 1, 0] +indices = [6, 1, 7] +print(*values) +print("indices:", *indices) +ix = sorted(set(indices)) +for i, v in zip(ix, sorted(values[i] for i in ix)): + values[i] = v +print(*values) diff --git a/tasks/sort-disjoint-sublist/sort-disjoint-sublist.om b/tasks/sort-disjoint-sublist/sort-disjoint-sublist.om new file mode 100644 index 0000000..c0515a6 --- /dev/null +++ b/tasks/sort-disjoint-sublist/sort-disjoint-sublist.om @@ -0,0 +1,134 @@ +note Rosetta Code task "Sort disjoint sublist": https://rosettacode.org/wiki/Sort_disjoint_sublist +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "7 6 5 4 3 2 1 0" +text tidx "indices: " +global vals long 8 +global idx ulong 3 +global sub3 long 3 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note sorts the three indices, gathers their values, sorts those, and writes them back in index order; +note both sorts are three compare-exchanges, enough for three elements +fn cx3u to void +for p ulong 3 +i = rem p 2 +if gt get idx i get idx add i 1 +t = get idx i +put idx i get idx add i 1 +put idx add i 1 t +end +end +end +fn cx3l to void +for p ulong 3 +i = rem p 2 +if gt get sub3 i get sub3 add i 1 +t = get sub3 i +put sub3 i get sub3 add i 1 +put sub3 add i 1 t +end +end +end +fn main to int +n = parse data +put idx 0 6 +put idx 1 1 +put idx 2 7 +show n +emit tidx +emitu get idx 0 +emit sp +emitu get idx 1 +emit sp +emitu get idx 2 +emit nl +cx3u +for i ulong 3 +put sub3 i get vals get idx i +end +cx3l +for i ulong 3 +put vals get idx i get sub3 i +end +show n +ret 0 +end diff --git a/tasks/sort-three-variables/expected.txt b/tasks/sort-three-variables/expected.txt new file mode 100644 index 0000000..06ad56d --- /dev/null +++ b/tasks/sort-three-variables/expected.txt @@ -0,0 +1,12 @@ +x = 77444 +y = -12 +z = 0 +x = -12 +y = 0 +z = 77444 +x = lions, tigers, and +y = bears, oh my! +z = (from the "Wizard of OZ") +x = (from the "Wizard of OZ") +y = bears, oh my! +z = lions, tigers, and diff --git a/tasks/sort-three-variables/oracle.py b/tasks/sort-three-variables/oracle.py new file mode 100644 index 0000000..c9de978 --- /dev/null +++ b/tasks/sort-three-variables/oracle.py @@ -0,0 +1,6 @@ +# Independent reference for "Sort three variables" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for vals in ([77444, -12, 0], ['lions, tigers, and', 'bears, oh my!', '(from the "Wizard of OZ")']): + for x in (vals, sorted(vals, key=lambda s: s.encode() if isinstance(s, str) else s)): + for name, v in zip("xyz", x): + print(f"{name} = {v}") diff --git a/tasks/sort-three-variables/sort-three-variables.om b/tasks/sort-three-variables/sort-three-variables.om new file mode 100644 index 0000000..a1ea249 --- /dev/null +++ b/tasks/sort-three-variables/sort-three-variables.om @@ -0,0 +1,172 @@ +note Rosetta Code task "Sort three variables": https://rosettacode.org/wiki/Sort_three_variables +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tx "x = " +text ty "y = " +text tz "z = " +text s0 "lions, tigers, and" +text s1 "bears, oh my!" +text s2 "(from the \"Wizard of OZ\")" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note sort3 orders three longs with three compare-exchanges and prints them as x, y, z +fn sort3 x long y long z long to void +if gt x y +t = x +x = y +y = t +end +if gt y z +t = y +y = z +z = t +end +if gt x y +t = x +x = y +y = t +end +emit tx +emiti x +emit nl +emit ty +emiti y +emit nl +emit tz +emiti z +emit nl +end +note str writes text number k (0, 1 or 2); a text cannot be chosen by index, so this is an if chain +fn str k ulong to void +if eq k 0 +emit s0 +end +if eq k 1 +emit s1 +end +if eq k 2 +emit s2 +end +end +note cmpb compares two byte strings: negative, zero or positive, like C's strcmp on unsigned bytes +fn cmpb a view uchar * b view uchar * to long +for i ulong len a +if ge i len b +ret 1 +end +if ne get a i get b i +ret sub long get a i long get b i +end +end +if lt len a len b +ret -1 +end +ret 0 +end +note gtk says whether text j sorts after text k; the three pairs are spelled out for the same reason +fn gtk j ulong k ulong to bool +c = long 0 +if and eq j 0 eq k 1 +c = cmpb s0 s1 +end +if and eq j 1 eq k 0 +c = cmpb s1 s0 +end +if and eq j 0 eq k 2 +c = cmpb s0 s2 +end +if and eq j 2 eq k 0 +c = cmpb s2 s0 +end +if and eq j 1 eq k 2 +c = cmpb s1 s2 +end +if and eq j 2 eq k 1 +c = cmpb s2 s1 +end +ret gt c 0 +end +fn showk x ulong y ulong z ulong to void +emit tx +str x +emit nl +emit ty +str y +emit nl +emit tz +str z +emit nl +end +note sorts the strings the same way, by swapping which text each variable names +fn sorts to void +x = ulong 0 +y = ulong 1 +z = ulong 2 +showk x y z +if gtk x y +t = x +x = y +y = t +end +if gtk y z +t = y +y = z +z = t +end +if gtk x y +t = x +x = y +y = t +end +showk x y z +end +fn main to int +emit tx +emiti 77444 +emit nl +emit ty +emiti -12 +emit nl +emit tz +emiti 0 +emit nl +sort3 77444 -12 0 +sorts +ret 0 +end diff --git a/tasks/soundex/expected.txt b/tasks/soundex/expected.txt new file mode 100644 index 0000000..c223d02 --- /dev/null +++ b/tasks/soundex/expected.txt @@ -0,0 +1,12 @@ +Robert R163 +Rupert R163 +Rubin R150 +Ashcraft A261 +Ashcroft A261 +Tymczak T522 +Pfister P236 +Honeyman H555 +Lee L000 +Gutierrez G362 +Jackson J250 +VanDeusen V532 diff --git a/tasks/soundex/oracle.py b/tasks/soundex/oracle.py new file mode 100644 index 0000000..235a0cc --- /dev/null +++ b/tasks/soundex/oracle.py @@ -0,0 +1,25 @@ +# Independent reference for "Soundex" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +groups = {"bfpv": "1", "cgjkqsxz": "2", "dt": "3", "l": "4", "mn": "5", "r": "6", "hw": "h"} +def code(c): + for k, v in groups.items(): + if c in k: + return v + return "0" +def soundex(name): + s = name.lower() + out = s[0].upper() + prev = code(s[0]) + for c in s[1:]: + d = code(c) + if d == "h": + continue + if d == "0": + prev = d + continue + if d != prev: + out += d + prev = d + return (out + "000")[:4] +for name in ['Robert', 'Rupert', 'Rubin', 'Ashcraft', 'Ashcroft', 'Tymczak', 'Pfister', 'Honeyman', 'Lee', 'Gutierrez', 'Jackson', 'VanDeusen']: + print(name, soundex(name)) diff --git a/tasks/soundex/soundex.om b/tasks/soundex/soundex.om new file mode 100644 index 0000000..b72d6e7 --- /dev/null +++ b/tasks/soundex/soundex.om @@ -0,0 +1,79 @@ +note Rosetta Code task "Soundex": https://rosettacode.org/wiki/Soundex +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +note code of each letter a..z: 0 vowel (and y), 1..6 consonant groups, 7 h and w (ignored) +text codes "01230127022455012623017202" +text n0 "Robert" +text n1 "Rupert" +text n2 "Rubin" +text n3 "Ashcraft" +text n4 "Ashcroft" +text n5 "Tymczak" +text n6 "Pfister" +text n7 "Honeyman" +text n8 "Lee" +text n9 "Gutierrez" +text n10 "Jackson" +text n11 "VanDeusen" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note American Soundex: keep the first letter, code the rest, drop repeats of the same code (h and w +note do not separate them, vowels do), pad with zeros to four characters +fn soundex a view uchar * to void +o = new uchar 4 48 +c = get a 0 +if ge c 97 +c = sub c 32 +end +put o 0 c +prev = get codes sub c 65 +k = ulong 1 +for i ulong sub len a 1 +x = get a add i 1 +if ge x 97 +x = sub x 32 +end +d = get codes sub x 65 +if eq d 48 +prev = d +else +if and ne d 55 ne d prev +if lt k 4 +put o k d +k = add k 1 +end +prev = d +end +end +end +emit a +emit sp +emit o +emit nl +end +fn main to int +soundex n0 +soundex n1 +soundex n2 +soundex n3 +soundex n4 +soundex n5 +soundex n6 +soundex n7 +soundex n8 +soundex n9 +soundex n10 +soundex n11 +ret 0 +end diff --git a/tasks/spiral-matrix/expected.txt b/tasks/spiral-matrix/expected.txt new file mode 100644 index 0000000..9dfcb68 --- /dev/null +++ b/tasks/spiral-matrix/expected.txt @@ -0,0 +1,14 @@ + 0 1 2 3 4 + 15 16 17 18 5 + 14 23 24 19 6 + 13 22 21 20 7 + 12 11 10 9 8 + + 0 1 2 3 4 5 6 7 + 27 28 29 30 31 32 33 8 + 26 47 48 49 50 51 34 9 + 25 46 59 60 61 52 35 10 + 24 45 58 63 62 53 36 11 + 23 44 57 56 55 54 37 12 + 22 43 42 41 40 39 38 13 + 21 20 19 18 17 16 15 14 diff --git a/tasks/spiral-matrix/oracle.py b/tasks/spiral-matrix/oracle.py new file mode 100644 index 0000000..ec9daa8 --- /dev/null +++ b/tasks/spiral-matrix/oracle.py @@ -0,0 +1,22 @@ +# Independent reference for "Spiral matrix" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# peel the outer ring and recurse on the inner square: ring numbers go right, down, left, up +def spiral(n): + m = [[0] * n for _ in range(n)] + k = 0 + for layer in range((n + 1) // 2): + lo, hi = layer, n - 1 - layer + if lo == hi: + m[lo][lo] = k + break + cells = ([(lo, c) for c in range(lo, hi)] + [(r, hi) for r in range(lo, hi)] + + [(hi, c) for c in range(hi, lo, -1)] + [(r, lo) for r in range(hi, lo, -1)]) + for r, c in cells: + m[r][c] = k + k += 1 + return m +for i, n in enumerate((5, 8)): + if i: + print() + for row in spiral(n): + print("".join(f"{v:3}" for v in row)) diff --git a/tasks/spiral-matrix/spiral-matrix.om b/tasks/spiral-matrix/spiral-matrix.om new file mode 100644 index 0000000..4a9394a --- /dev/null +++ b/tasks/spiral-matrix/spiral-matrix.om @@ -0,0 +1,99 @@ +note Rosetta Code task "Spiral matrix": https://rosettacode.org/wiki/Spiral_matrix +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +global grid ulong 64 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt w sub 20 i +i = sub 20 w +end +emit part d i sub 20 i +end +note the walk starts at the top left going right and turns clockwise whenever the next cell is off the +note grid or already numbered; grid holds number+1 so that 0 means empty +fn spiral n ulong to void +for i ulong mul n n +put grid i 0 +end +r = long 0 +c = long 0 +dr = long 0 +dc = long 1 +for k ulong mul n n +put grid add mul ulong r n ulong c add k 1 +nr = add r dr +nc = add c dc +if blocked nr nc n +t = dr +dr = dc +dc = neg t +nr = add r dr +nc = add c dc +end +r = nr +c = nc +end +end +note blocked says whether (r, c) is outside the n x n grid or already numbered +fn blocked r long c long n ulong to bool +if or or lt r 0 ge r long n or lt c 0 ge c long n +ret true +end +ret ne get grid add mul ulong r n ulong c 0 +end +fn show n ulong to void +for i ulong n +for j ulong n +emitw sub get grid add mul i n j 1 3 +end +emit nl +end +end +fn main to int +spiral 5 +show 5 +emit nl +spiral 8 +show 8 +ret 0 +end diff --git a/tasks/square-free-integers/expected.txt b/tasks/square-free-integers/expected.txt new file mode 100644 index 0000000..7cb7fa8 --- /dev/null +++ b/tasks/square-free-integers/expected.txt @@ -0,0 +1,7 @@ +square-free integers from 1 to 145: 1 2 3 5 6 7 10 11 13 14 15 17 19 21 22 23 26 29 30 31 33 34 35 37 38 39 41 42 43 46 47 51 53 55 57 58 59 61 62 65 66 67 69 70 71 73 74 77 78 79 82 83 85 86 87 89 91 93 94 95 97 101 102 103 105 106 107 109 110 111 113 114 115 118 119 122 123 127 129 130 131 133 134 137 138 139 141 142 143 145 +square-free integers from 1000000000000 to 1000000000145: 1000000000001 1000000000002 1000000000003 1000000000005 1000000000006 1000000000007 1000000000009 1000000000011 1000000000013 1000000000014 1000000000015 1000000000018 1000000000019 1000000000021 1000000000022 1000000000023 1000000000027 1000000000029 1000000000030 1000000000031 1000000000033 1000000000037 1000000000038 1000000000039 1000000000041 1000000000042 1000000000043 1000000000045 1000000000046 1000000000047 1000000000049 1000000000051 1000000000054 1000000000055 1000000000057 1000000000058 1000000000059 1000000000061 1000000000063 1000000000065 1000000000066 1000000000067 1000000000069 1000000000070 1000000000073 1000000000074 1000000000077 1000000000078 1000000000079 1000000000081 1000000000082 1000000000085 1000000000086 1000000000087 1000000000090 1000000000091 1000000000093 1000000000094 1000000000095 1000000000097 1000000000099 1000000000101 1000000000102 1000000000103 1000000000105 1000000000106 1000000000109 1000000000111 1000000000113 1000000000114 1000000000115 1000000000117 1000000000118 1000000000119 1000000000121 1000000000122 1000000000123 1000000000126 1000000000127 1000000000129 1000000000130 1000000000133 1000000000135 1000000000137 1000000000138 1000000000139 1000000000141 1000000000142 1000000000145 +square-free integers up to 100: 61 +square-free integers up to 1000: 608 +square-free integers up to 10000: 6083 +square-free integers up to 100000: 60794 +square-free integers up to 1000000: 607926 diff --git a/tasks/square-free-integers/oracle.py b/tasks/square-free-integers/oracle.py new file mode 100644 index 0000000..c27a5f8 --- /dev/null +++ b/tasks/square-free-integers/oracle.py @@ -0,0 +1,30 @@ +# Independent reference for "Square-free integers" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import math +def squarefree(n): + m, p = n, 2 + while p * p <= m: + if m % p == 0: + m //= p + if m % p == 0: + return False + p += 1 + return True +print("square-free integers from 1 to 145: " + " ".join(str(n) for n in range(1, 146) if squarefree(n))) +B = 10 ** 12 +print(f"square-free integers from {B} to {B + 145}: " + " ".join(str(n) for n in range(B, B + 146) if squarefree(n))) +# counts by the Moebius formula: Q(x) = sum mu(d) * floor(x / d^2) +N = 1000 +mu = [1] * (N + 1) +isp = [True] * (N + 1) +for p in range(2, N + 1): + if isp[p]: + for j in range(p, N + 1, p): + if j > p: + isp[j] = False + mu[j] = -mu[j] + for j in range(p * p, N + 1, p * p): + mu[j] = 0 +for e in range(2, 7): + x = 10 ** e + print(f"square-free integers up to {x}: {sum(mu[d] * (x // (d * d)) for d in range(1, math.isqrt(x) + 1))}") diff --git a/tasks/square-free-integers/square-free-integers.om b/tasks/square-free-integers/square-free-integers.om new file mode 100644 index 0000000..4a4015b --- /dev/null +++ b/tasks/square-free-integers/square-free-integers.om @@ -0,0 +1,105 @@ +note Rosetta Code task "Square-free integers": https://rosettacode.org/wiki/Square-free_integers +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text lo "square-free integers from 1 to 145:" +text hi "square-free integers from 1000000000000 to 1000000000145:" +text upto "square-free integers up to " +text col ": " +note cp marks composites below 10^6 (a prime sieve); sq n becomes 1 when some p^2 divides n, n <= 10^6; +note wn marks the window 10^12 .. 10^12 + 145 the same way. Any square factor of a number up to +note 10^12 + 145 has a prime p <= 10^6 + 1 (p^2 > 10^12 + 145 otherwise), and 10^6 + 1 = 101 * 9901 is composite +global cp uchar 1000001 +global sq uchar 1000001 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn main to int +wn = new uchar 146 0 +base = ulong 1000000000000 +p = ulong 2 +while le p 1000000 bound 999999 +if eq get cp p 0 +if le mul p p 1000000 +j = mul p p +while le j 1000000 bound 999999 +put cp j 1 +j = add j p +end +end +q = mul p p +j = q +while le j 1000000 bound 999999 +put sq j 1 +j = add j q +end +r = rem base q +j = ulong 0 +if gt r 0 +j = sub q r +end +while le j 145 bound 146 +put wn j 1 +j = add j q +end +end +p = add p 1 +end +emit lo +for i ulong 145 +if eq get sq add i 1 0 +emit sp +emitu add i 1 +end +end +emit nl +emit hi +for i ulong 146 +if eq get wn i 0 +emit sp +emitu add base i +end +end +emit nl +c = ulong 0 +lim = ulong 100 +for i ulong 1000000 +n = add i 1 +if eq get sq n 0 +c = add c 1 +end +if eq n lim +emit upto +emitu lim +emit col +emitu c +emit nl +lim = mul lim 10 +end +end +ret 0 +end diff --git a/tasks/square-free-integers/wolfram.txt b/tasks/square-free-integers/wolfram.txt new file mode 100644 index 0000000..c0c8ee5 --- /dev/null +++ b/tasks/square-free-integers/wolfram.txt @@ -0,0 +1,14 @@ +Wolfram cross-check of tasks/square-free-integers/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 788c762929c7078277374feffe8a023e4d1794ba91bdf2af641a5e025238da09. +agreement: YES +query: +b = 10^12; +s = "square-free integers from 1 to 145: " <> StringRiffle[ToString /@ Select[Range[145], SquareFreeQ], " "] <> "\n" <> + "square-free integers from " <> ToString[b] <> " to " <> ToString[b + 145] <> ": " <> StringRiffle[ToString /@ Select[Range[b, b + 145], SquareFreeQ], " "] <> "\n" <> + StringJoin[Table["square-free integers up to " <> ToString[10^e] <> ": " <> ToString[Sum[MoebiusMu[d] Quotient[10^e, d^2], {d, 1, Floor[Sqrt[10^e]]}]] <> "\n", {e, 2, 6}]]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"788c762929c7078277374feffe8a023e4d1794ba91bdf2af641a5e025238da09", "square-free integers from 1 to 145: 1 2 3 5 6 7 10 11 13 14 15 17 19 21 22 23 26 29 30 31 33 34 35 37 38 39 41 42 43 46 47 51 53 55 57 58 59 61 62 65 66 67 69 70 71 73 74 77 78 79 82 83 85 86 87 89 91 93 94 95 97 101 102 103 105 106 107 109 110 111 113 114 115 118 119 122 123 127 129 130 131 133 134 137 138 139 141 142 143 145\nsquare-free integers from 1000000000000 to 1000000000145: 1000000000001 1000000000002 1000000000003 1000000000005 1000000000006 1000000000007 1000000000009 1000000000011 1000000000013 1000000000014 1000000000015 1000000000018 1000000000019 1000000000021 1000000000022 1000000000023 1000000000027 1000000000029 1000000000030 1000000000031 1000000000033 1000000000037 1000000000038 1000000000039 1000000000041 1000000000042 1000000000043 1000000000045 1000000000046 1000000000047 1000000000049 1000000000051 1000000000054 1000000000055 1000000000057 1000000000058 1000000000059 1000000000061 1000000000063 1000000000065 1000000000066 1000000000067 1000000000069 1000000000070 1000000000073 1000000000074 1000000000077 1000000000078 1000000000079 1000000000081 1000000000082 1000000000085 1000000000086 1000000000087 1000000000090 1000000000091 1000000000093 1000000000094 1000000000095 1000000000097 1000000000099 1000000000101 1000000000102 1000000000103 1000000000105 1000000000106 1000000000109 1000000000111 1000000000113 1000000000114 1000000000115 1000000000117 1000000000118 1000000000119 1000000000121 1000000000122 1000000000123 1000000000126 1000000000127 1000000000129 1000000000130 1000000000133 1000000000135 1000000000137 1000000000138 1000000000139 1000000000141 1000000000142 1000000000145\nsquare-free integers up to 100: 61\nsquare-free integers up to 1000: 608\nsquare-free integers up to 10000: 6083\nsquare-free integers up to 100000: 60794\nsquare-free integers up to 1000000: 607926\n"} diff --git a/tasks/stack/expected.txt b/tasks/stack/expected.txt new file mode 100644 index 0000000..e4ed0a1 --- /dev/null +++ b/tasks/stack/expected.txt @@ -0,0 +1,11 @@ +empty: yes +push 10 +push -20 +push 30 +peek 30 +size 3 +pop 30 +pop -20 +empty: no +pop 10 +empty: yes diff --git a/tasks/stack/oracle.py b/tasks/stack/oracle.py new file mode 100644 index 0000000..2acbe26 --- /dev/null +++ b/tasks/stack/oracle.py @@ -0,0 +1,16 @@ +# Independent reference for "Stack" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +stack = [] +def empty(): + print("empty:", "no" if stack else "yes") +empty() +for v in (10, -20, 30): + stack.append(v) + print("push", v) +print("peek", stack[-1]) +print("size", len(stack)) +print("pop", stack.pop()) +print("pop", stack.pop()) +empty() +print("pop", stack.pop()) +empty() diff --git a/tasks/stack/stack.om b/tasks/stack/stack.om new file mode 100644 index 0000000..fe8549a --- /dev/null +++ b/tasks/stack/stack.om @@ -0,0 +1,109 @@ +note Rosetta Code task "Stack": https://rosettacode.org/wiki/Stack +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tpush "push " +text tpop "pop " +text tpeek "peek " +text tempty "empty: " +text tsize "size " +text tyes "yes" +text tno "no" +global stk long 16 +global top ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note a fixed-capacity stack of 16 longs: stk[0..top-1], the top element last; push on a full stack and +note pop on an empty one stop the program (exit 2), since there is no heap to grow into +fn push v long to void +t = get top 0 +if eq t 16 +exit 2 +end +put stk t v +put top 0 add t 1 +emit tpush +emiti v +emit nl +end +fn pop to long +t = get top 0 +if eq t 0 +exit 2 +end +put top 0 sub t 1 +v = get stk sub t 1 +emit tpop +emiti v +emit nl +ret v +end +fn peek to long +v = get stk sub get top 0 1 +emit tpeek +emiti v +emit nl +ret v +end +fn empty to bool +e = eq get top 0 0 +emit tempty +if e +emit tyes +else +emit tno +end +emit nl +ret e +end +fn main to int +e = empty +push 10 +push -20 +push 30 +v = peek +emit tsize +emitu get top 0 +emit nl +v = pop +v = pop +e = empty +v = pop +e = empty +ret 0 +end diff --git a/tasks/stern-brocot-sequence/expected.txt b/tasks/stern-brocot-sequence/expected.txt new file mode 100644 index 0000000..b3db704 --- /dev/null +++ b/tasks/stern-brocot-sequence/expected.txt @@ -0,0 +1,13 @@ +first 15: 1 1 2 1 3 2 3 1 4 3 5 2 5 3 4 +1 first appears at 1 +2 first appears at 3 +3 first appears at 5 +4 first appears at 9 +5 first appears at 11 +6 first appears at 33 +7 first appears at 19 +8 first appears at 21 +9 first appears at 35 +10 first appears at 39 +100 first appears at 1179 +gcd of each consecutive pair of the first 1000 is 1: yes diff --git a/tasks/stern-brocot-sequence/oracle.py b/tasks/stern-brocot-sequence/oracle.py new file mode 100644 index 0000000..4023d54 --- /dev/null +++ b/tasks/stern-brocot-sequence/oracle.py @@ -0,0 +1,13 @@ +# Independent reference for "Stern-Brocot sequence" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +from math import gcd +# Stern's diatomic recurrence: a(1) = 1, a(2n) = a(n), a(2n+1) = a(n) + a(n+1); the Stern-Brocot +# sequence of the task is a(1), a(2), ... +a = [0, 1] +for n in range(2, 1201): + a.append(a[n // 2] if n % 2 == 0 else a[n // 2] + a[n // 2 + 1]) +s = a[1:] +print("first 15:", *s[:15]) +for v in list(range(1, 11)) + [100]: + print(f"{v} first appears at {s.index(v) + 1}") +print("gcd of each consecutive pair of the first 1000 is 1:", "yes" if all(gcd(x, y) == 1 for x, y in zip(s[:1000], s[1:1000])) else "no") diff --git a/tasks/stern-brocot-sequence/stern-brocot-sequence.om b/tasks/stern-brocot-sequence/stern-brocot-sequence.om new file mode 100644 index 0000000..9ca4e23 --- /dev/null +++ b/tasks/stern-brocot-sequence/stern-brocot-sequence.om @@ -0,0 +1,105 @@ +note Rosetta Code task "Stern-Brocot sequence": https://rosettacode.org/wiki/Stern-Brocot_sequence +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tfirst "first 15: " +text tat " first appears at " +text tgcd "gcd of each consecutive pair of the first 1000 is 1: " +text tyes "yes" +text tno "no" +global sb ulong 1200 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note start 1, 1; for each k from 1 on, append sb[k] + sb[k-1] and then sb[k], until 1200 members +fn build to void +put sb 0 1 +put sb 1 1 +n = ulong 2 +k = ulong 1 +note each pass adds two members, so 600 passes fill 1200 +while lt n 1200 bound 600 +put sb n add get sb k get sb sub k 1 +put sb add n 1 get sb k +n = add n 2 +k = add k 1 +end +end +note find gives the 1-based position of v's first appearance, 0 if not within the 1200 +fn find v ulong to ulong +for i ulong 1200 +if eq get sb i v +ret add i 1 +end +end +ret 0 +end +fn gcd a ulong b ulong to ulong +note Euclid's remainders shrink at least as fast as Fibonacci numbers, so 100 steps cover any ulong +while ne b 0 bound 100 +t = rem a b +a = b +b = t +end +ret a +end +fn show v ulong to void +emitu v +emit tat +emitu find v +emit nl +end +fn main to int +build +emit tfirst +for i ulong 15 +if gt i 0 +emit sp +end +emitu get sb i +end +emit nl +for i ulong 10 +show add i 1 +end +show 100 +ok = true +for i ulong 999 +if ne gcd get sb i get sb add i 1 1 +ok = false +end +end +emit tgcd +if ok +emit tyes +else +emit tno +end +emit nl +ret 0 +end diff --git a/tasks/stooge-sort/expected.txt b/tasks/stooge-sort/expected.txt new file mode 100644 index 0000000..f0c1ace --- /dev/null +++ b/tasks/stooge-sort/expected.txt @@ -0,0 +1,2 @@ +1 4 5 3 -6 3 7 10 -2 -5 7 5 9 -3 7 +-6 -5 -3 -2 1 3 3 4 5 5 7 7 7 9 10 diff --git a/tasks/stooge-sort/oracle.py b/tasks/stooge-sort/oracle.py new file mode 100644 index 0000000..f364c4b --- /dev/null +++ b/tasks/stooge-sort/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Sorting algorithms/Stooge sort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [1, 4, 5, 3, -6, 3, 7, 10, -2, -5, 7, 5, 9, -3, 7] +print(*a) +print(*sorted(a)) diff --git a/tasks/stooge-sort/stooge-sort.om b/tasks/stooge-sort/stooge-sort.om new file mode 100644 index 0000000..e99b9c0 --- /dev/null +++ b/tasks/stooge-sort/stooge-sort.om @@ -0,0 +1,107 @@ +note Rosetta Code task "Sorting algorithms/Stooge sort": https://rosettacode.org/wiki/Sorting_algorithms/Stooge_sort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "1 4 5 3 -6 3 7 10 -2 -5 7 5 9 -3 7" +global vals long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note stsort orders vals[i] and vals[j], then for 3 or more elements sorts the first two thirds, the last +note two thirds and the first two thirds again; each call works on a range of at most 2/3 the size, so it ends +fn stsort i ulong j ulong to void +if gt get vals i get vals j +t = get vals i +put vals i get vals j +put vals j t +end +if ge sub j i 2 +k = div add sub j i 1 3 +stsort i sub j k +stsort add i k j +stsort i sub j k +end +end +fn main to int +n = parse data +show n +stsort 0 sub n 1 +show n +ret 0 +end diff --git a/tasks/strand-sort/expected.txt b/tasks/strand-sort/expected.txt new file mode 100644 index 0000000..b633408 --- /dev/null +++ b/tasks/strand-sort/expected.txt @@ -0,0 +1,2 @@ +5 1 4 2 0 9 6 3 8 7 -2 11 +-2 0 1 2 3 4 5 6 7 8 9 11 diff --git a/tasks/strand-sort/oracle.py b/tasks/strand-sort/oracle.py new file mode 100644 index 0000000..22bbe87 --- /dev/null +++ b/tasks/strand-sort/oracle.py @@ -0,0 +1,23 @@ +# Independent reference for "Sorting algorithms/Strand sort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +def strand_sort(a): + a = list(a) + out = [] + while a: + strand, rest = [a[0]], [] + for v in a[1:]: + (strand if v >= strand[-1] else rest).append(v) + merged = [] + i = j = 0 + while i < len(out) or j < len(strand): + if j >= len(strand) or (i < len(out) and out[i] <= strand[j]): + merged.append(out[i]); i += 1 + else: + merged.append(strand[j]); j += 1 + out, a = merged, rest + return out +a = [5, 1, 4, 2, 0, 9, 6, 3, 8, 7, -2, 11] +print(*a) +s = strand_sort(a) +assert s == sorted(a) +print(*s) diff --git a/tasks/strand-sort/strand-sort.om b/tasks/strand-sort/strand-sort.om new file mode 100644 index 0000000..fb35a9a --- /dev/null +++ b/tasks/strand-sort/strand-sort.om @@ -0,0 +1,167 @@ +note Rosetta Code task "Sorting algorithms/Strand sort": https://rosettacode.org/wiki/Sorting_algorithms/Strand_sort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "5 1 4 2 0 9 6 3 8 7 -2 11" +global vals long 16 +global rest long 16 +global strand long 16 +global res long 16 +global mrg long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note pull takes an increasing strand out of rest[0..m-1] into strand, keeps the others in order at the +note front of rest, and returns the strand length +fn pull m ulong to ulong +s = ulong 0 +k = ulong 0 +for i ulong m +v = get rest i +take = false +if eq s 0 +take = true +else +if ge v get strand sub s 1 +take = true +end +end +if take +put strand s v +s = add s 1 +else +put rest k v +k = add k 1 +end +end +ret s +end +note mergein merges strand[0..s-1] into res[0..r-1] through mrg and returns the new length +fn mergein r ulong s ulong to ulong +i = ulong 0 +j = ulong 0 +k = ulong 0 +while lt k add r s bound 32 +left = false +if ge j s +left = true +else +if lt i r +if le get res i get strand j +left = true +end +end +end +if left +put mrg k get res i +i = add i 1 +else +put mrg k get strand j +j = add j 1 +end +k = add k 1 +end +for x ulong k +put res x get mrg x +end +ret k +end +note stsort repeats pull and merge; each strand removes at least one element, so n rounds suffice +fn stsort n ulong to void +for i ulong n +put rest i get vals i +end +m = n +r = ulong 0 +while gt m 0 bound 16 +s = pull m +m = sub m s +r = mergein r s +end +for i ulong n +put vals i get res i +end +end +fn main to int +n = parse data +show n +stsort n +show n +ret 0 +end diff --git a/tasks/string-append/expected.txt b/tasks/string-append/expected.txt new file mode 100644 index 0000000..8ab686e --- /dev/null +++ b/tasks/string-append/expected.txt @@ -0,0 +1 @@ +Hello, World! diff --git a/tasks/string-append/oracle.py b/tasks/string-append/oracle.py new file mode 100644 index 0000000..8ef8d72 --- /dev/null +++ b/tasks/string-append/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "String append" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +s = "Hello," +s += " World!" +print(s) diff --git a/tasks/string-append/string-append.om b/tasks/string-append/string-append.om new file mode 100644 index 0000000..b6046e7 --- /dev/null +++ b/tasks/string-append/string-append.om @@ -0,0 +1,33 @@ +note Rosetta Code task "String append": https://rosettacode.org/wiki/String_append +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text s0 "Hello," +text more " World!" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note copyto copies s into d at offset off and returns the offset after it (past the end traps 3) +fn copyto d edit uchar * off ulong s view uchar * to ulong +for i ulong len s +put d add off i get s i +end +ret add off len s +end +fn main to int +note the string lives in a 64-byte buffer b with length n; appending copies after byte n +b = new uchar 64 0 +n = copyto b 0 s0 +n = copyto b n more +emit part b 0 n +emit nl +ret 0 +end diff --git a/tasks/string-case/expected.txt b/tasks/string-case/expected.txt new file mode 100644 index 0000000..bbecf6f --- /dev/null +++ b/tasks/string-case/expected.txt @@ -0,0 +1,3 @@ +ALPHABETA +alphabeta +ALPHAbeta diff --git a/tasks/string-case/oracle.py b/tasks/string-case/oracle.py new file mode 100644 index 0000000..2fde9b1 --- /dev/null +++ b/tasks/string-case/oracle.py @@ -0,0 +1,6 @@ +# Independent reference for "String case" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +s = "alphaBETA" +print(s.upper()) +print(s.lower()) +print(s.swapcase()) diff --git a/tasks/string-case/string-case.om b/tasks/string-case/string-case.om new file mode 100644 index 0000000..cb3227b --- /dev/null +++ b/tasks/string-case/string-case.om @@ -0,0 +1,42 @@ +note Rosetta Code task "String case": https://rosettacode.org/wiki/String_case +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text s "alphaBETA" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note upper, lower and swap change ASCII letters only; strings up to 256 bytes (longer traps 3) +fn conv a view uchar * mode ulong to void +r = new uchar 256 0 +for i ulong len a +c = get a i +up = and ge c 65 le c 90 +lo = and ge c 97 le c 122 +if and lo ne mode 1 +c = sub c 32 +else +if and up ne mode 0 +c = add c 32 +end +end +put r i c +end +emit part r 0 len a +emit nl +end +fn main to int +note mode 0 upper, 1 lower, 2 swap case +conv s 0 +conv s 1 +conv s 2 +ret 0 +end diff --git a/tasks/string-comparison/expected.txt b/tasks/string-comparison/expected.txt new file mode 100644 index 0000000..0465b6b --- /dev/null +++ b/tasks/string-comparison/expected.txt @@ -0,0 +1,4 @@ +alpha beta eq=no lt=yes gt=no equal-ignoring-case=no +Hello hello eq=no lt=yes gt=no equal-ignoring-case=yes +abc abcd eq=no lt=yes gt=no equal-ignoring-case=no +abcd abc eq=no lt=no gt=yes equal-ignoring-case=no diff --git a/tasks/string-comparison/oracle.py b/tasks/string-comparison/oracle.py new file mode 100644 index 0000000..df8ae83 --- /dev/null +++ b/tasks/string-comparison/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "String comparison" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +yn = {True: "yes", False: "no"} +for a, b in [("alpha", "beta"), ("Hello", "hello"), ("abc", "abcd"), ("abcd", "abc")]: + print(f"{a} {b} eq={yn[a == b]} lt={yn[a < b]} gt={yn[a > b]} equal-ignoring-case={yn[a.lower() == b.lower()]}") diff --git a/tasks/string-comparison/string-comparison.om b/tasks/string-comparison/string-comparison.om new file mode 100644 index 0000000..c5d4970 --- /dev/null +++ b/tasks/string-comparison/string-comparison.om @@ -0,0 +1,87 @@ +note Rosetta Code task "String comparison": https://rosettacode.org/wiki/String_comparison +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text a1 "alpha" +text b1 "beta" +text a2 "Hello" +text b2 "hello" +text a3 "abc" +text b3 "abcd" +text teq " eq=" +text tlt " lt=" +text tgt " gt=" +text tci " equal-ignoring-case=" +text tyes "yes" +text tno "no" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +fn emitb b bool to void +if b +emit tyes +else +emit tno +end +end +note cmp is byte order: -1, 0 or 1; a proper prefix sorts first; ci folds ASCII case first +fn cmp a view uchar * b view uchar * ci bool to int +n = len a +if lt len b n +n = len b +end +for i ulong n +x = get a i +y = get b i +if and ci and ge x 65 le x 90 +x = add x 32 +end +if and ci and ge y 65 le y 90 +y = add y 32 +end +if lt x y +ret -1 +end +if gt x y +ret 1 +end +end +if lt len a len b +ret -1 +end +if gt len a len b +ret 1 +end +ret 0 +end +fn show a view uchar * b view uchar * to void +c = cmp a b false +emit a +emit sp +emit b +emit teq +emitb eq c 0 +emit tlt +emitb lt c 0 +emit tgt +emitb gt c 0 +emit tci +emitb eq cmp a b true 0 +emit nl +end +fn main to int +show a1 b1 +show a2 b2 +show a3 b3 +show b3 a3 +ret 0 +end diff --git a/tasks/string-concatenation/expected.txt b/tasks/string-concatenation/expected.txt new file mode 100644 index 0000000..852116a --- /dev/null +++ b/tasks/string-concatenation/expected.txt @@ -0,0 +1,2 @@ +hello +hello literal diff --git a/tasks/string-concatenation/oracle.py b/tasks/string-concatenation/oracle.py new file mode 100644 index 0000000..50a0891 --- /dev/null +++ b/tasks/string-concatenation/oracle.py @@ -0,0 +1,6 @@ +# Independent reference for "String concatenation" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +s = "hello" +print(s) +s2 = s + " literal" +print(s2) diff --git a/tasks/string-concatenation/string-concatenation.om b/tasks/string-concatenation/string-concatenation.om new file mode 100644 index 0000000..7737273 --- /dev/null +++ b/tasks/string-concatenation/string-concatenation.om @@ -0,0 +1,34 @@ +note Rosetta Code task "String concatenation": https://rosettacode.org/wiki/String_concatenation +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text str "hello" +text lit " literal" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note copyto copies s into d at offset off and returns the offset after it (past the end traps 3) +fn copyto d edit uchar * off ulong s view uchar * to ulong +for i ulong len s +put d add off i get s i +end +ret add off len s +end +fn main to int +emit str +emit nl +b = new uchar 64 0 +p = copyto b 0 str +p = copyto b p lit +emit part b 0 p +emit nl +ret 0 +end diff --git a/tasks/string-length/expected.txt b/tasks/string-length/expected.txt new file mode 100644 index 0000000..af420e2 --- /dev/null +++ b/tasks/string-length/expected.txt @@ -0,0 +1,6 @@ +møøse +7 bytes 5 characters +𝔘𝔫𝔦𝔠𝔬𝔡𝔢 +28 bytes 7 characters +J̲o̲s̲é̲ +13 bytes 8 characters diff --git a/tasks/string-length/oracle.py b/tasks/string-length/oracle.py new file mode 100644 index 0000000..1ca8007 --- /dev/null +++ b/tasks/string-length/oracle.py @@ -0,0 +1,6 @@ +# Independent reference for "String length" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import sys +out = sys.stdout.buffer +for s in ["møøse", "𝔘𝔫𝔦𝔠𝔬𝔡𝔢", "J̲o̲s̲é̲"]: + out.write(f"{s}\n{len(s.encode('utf-8'))} bytes {len(s)} characters\n".encode("utf-8")) diff --git a/tasks/string-length/string-length.om b/tasks/string-length/string-length.om new file mode 100644 index 0000000..0f4daa7 --- /dev/null +++ b/tasks/string-length/string-length.om @@ -0,0 +1,56 @@ +note Rosetta Code task "String length": https://rosettacode.org/wiki/String_length +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text tb " bytes " +text tc " characters\n" +text s1 "møøse" +text s2 "𝔘𝔫𝔦𝔠𝔬𝔡𝔢" +text s3 "J̲o̲s̲é̲" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a UTF-8 code point starts at every byte that is not a continuation byte 10xxxxxx +fn show a view uchar * to void +n = ulong 0 +for i ulong len a +if ne and get a i 192 128 +n = add n 1 +end +end +emit a +emit nl +emitu len a +emit tb +emitu n +emit tc +end +fn main to int +show s1 +show s2 +show s3 +ret 0 +end diff --git a/tasks/string-matching/expected.txt b/tasks/string-matching/expected.txt new file mode 100644 index 0000000..d7622fa --- /dev/null +++ b/tasks/string-matching/expected.txt @@ -0,0 +1,3 @@ +starts with: no +found at: 2 6 +ends with: yes diff --git a/tasks/string-matching/oracle.py b/tasks/string-matching/oracle.py new file mode 100644 index 0000000..9b5b66b --- /dev/null +++ b/tasks/string-matching/oracle.py @@ -0,0 +1,7 @@ +# Independent reference for "String matching" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +s, t = "tacoloco", "co" +yn = {True: "yes", False: "no"} +print("starts with:", yn[s.startswith(t)]) +print("found at:" + "".join(f" {i}" for i in range(len(s)) if s.startswith(t, i))) +print("ends with:", yn[s.endswith(t)]) diff --git a/tasks/string-matching/string-matching.om b/tasks/string-matching/string-matching.om new file mode 100644 index 0000000..56d1835 --- /dev/null +++ b/tasks/string-matching/string-matching.om @@ -0,0 +1,74 @@ +note Rosetta Code task "String matching": https://rosettacode.org/wiki/String_matching +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text hay "tacoloco" +text nd "co" +text tst "starts with: " +text tct "found at:" +text ten "ends with: " +text tyes "yes" +text tno "no" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn emitb b bool to void +if b +emit tyes +else +emit tno +end +end +note at tells whether t occurs in s at offset i (false when it would run past the end) +fn at2 s view uchar * t view uchar * i ulong to bool +if gt add i len t len s +ret false +end +for j ulong len t +if ne get s add i j get t j +ret false +end +end +ret true +end +fn main to int +emit tst +emitb at2 hay nd 0 +emit nl +emit tct +for i ulong len hay +if at2 hay nd i +emit sp +emitu i +end +end +emit nl +emit ten +emitb at2 hay nd sub len hay len nd +emit nl +ret 0 +end diff --git a/tasks/string-prepend/expected.txt b/tasks/string-prepend/expected.txt new file mode 100644 index 0000000..3fa0d4b --- /dev/null +++ b/tasks/string-prepend/expected.txt @@ -0,0 +1 @@ +Hello, World diff --git a/tasks/string-prepend/oracle.py b/tasks/string-prepend/oracle.py new file mode 100644 index 0000000..069baad --- /dev/null +++ b/tasks/string-prepend/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "String prepend" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +s = "World" +s = "Hello, " + s +print(s) diff --git a/tasks/string-prepend/string-prepend.om b/tasks/string-prepend/string-prepend.om new file mode 100644 index 0000000..cd77d4e --- /dev/null +++ b/tasks/string-prepend/string-prepend.om @@ -0,0 +1,42 @@ +note Rosetta Code task "String prepend": https://rosettacode.org/wiki/String_prepend +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text s0 "World" +text pre "Hello, " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note copyto copies s into d at offset off and returns the offset after it (past the end traps 3) +fn copyto d edit uchar * off ulong s view uchar * to ulong +for i ulong len s +put d add off i get s i +end +ret add off len s +end +note prepend moves the n bytes of b right by len p (last byte first), then copies p in front +fn prepend b edit uchar * n ulong p view uchar * to ulong +k = len p +for i ulong n +j = sub sub n 1 i +put b add j k get b j +end +r = copyto b 0 p +ret add n k +end +fn main to int +b = new uchar 64 0 +n = copyto b 0 s0 +n = prepend b n pre +emit part b 0 n +emit nl +ret 0 +end diff --git a/tasks/strip-a-set-of-characters-from-a-string/expected.txt b/tasks/strip-a-set-of-characters-from-a-string/expected.txt new file mode 100644 index 0000000..9c3fcad --- /dev/null +++ b/tasks/strip-a-set-of-characters-from-a-string/expected.txt @@ -0,0 +1 @@ +Sh ws soul strppr. Sh took my hrt! diff --git a/tasks/strip-a-set-of-characters-from-a-string/oracle.py b/tasks/strip-a-set-of-characters-from-a-string/oracle.py new file mode 100644 index 0000000..2899bcd --- /dev/null +++ b/tasks/strip-a-set-of-characters-from-a-string/oracle.py @@ -0,0 +1,4 @@ +# Independent reference for "Strip a set of characters from a string" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +s = "She was a soul stripper. She took my heart!" +print("".join(c for c in s if c not in "aei")) diff --git a/tasks/strip-a-set-of-characters-from-a-string/strip-a-set-of-characters-from-a-string.om b/tasks/strip-a-set-of-characters-from-a-string/strip-a-set-of-characters-from-a-string.om new file mode 100644 index 0000000..99dd26e --- /dev/null +++ b/tasks/strip-a-set-of-characters-from-a-string/strip-a-set-of-characters-from-a-string.om @@ -0,0 +1,43 @@ +note Rosetta Code task "Strip a set of characters from a string": https://rosettacode.org/wiki/Strip_a_set_of_characters_from_a_string +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text msg "She was a soul stripper. She took my heart!" +text set1 "aei" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note stripchars writes the bytes of a that are not in set s +fn strip a view uchar * s view uchar * to void +for i ulong len a +c = get a i +k = true +for j ulong len s +if eq c get s j +k = false +end +end +if k +emitc c +end +end +emit nl +end +fn main to int +strip msg set1 +ret 0 +end diff --git a/tasks/strip-comments-from-a-string/expected.txt b/tasks/strip-comments-from-a-string/expected.txt new file mode 100644 index 0000000..ef028d9 --- /dev/null +++ b/tasks/strip-comments-from-a-string/expected.txt @@ -0,0 +1,3 @@ +<<>> +<<>> +<<>> diff --git a/tasks/strip-comments-from-a-string/oracle.py b/tasks/strip-comments-from-a-string/oracle.py new file mode 100644 index 0000000..3fb5a36 --- /dev/null +++ b/tasks/strip-comments-from-a-string/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Strip comments from a string" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import re +for s in ["apples, pears # and bananas", "apples, pears ; and bananas", " no comment here "]: + print("<<<" + re.split("[#;]", s, maxsplit=1)[0].strip(" \t") + ">>>") diff --git a/tasks/strip-comments-from-a-string/strip-comments-from-a-string.om b/tasks/strip-comments-from-a-string/strip-comments-from-a-string.om new file mode 100644 index 0000000..f4fe757 --- /dev/null +++ b/tasks/strip-comments-from-a-string/strip-comments-from-a-string.om @@ -0,0 +1,55 @@ +note Rosetta Code task "Strip comments from a string": https://rosettacode.org/wiki/Strip_comments_from_a_string +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text lq "<<<" +text rq ">>>" +text s1 "apples, pears # and bananas" +text s2 "apples, pears ; and bananas" +text s3 " no comment here " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note cut at the first '#' or ';', then drop whitespace (space, tab) at both ends of what is left +fn strip a view uchar * to void +e = len a +for i ulong len a +c = get a i +if and eq e len a or eq c 35 eq c 59 +e = i +end +end +s = ulong 0 +while lt s e bound 4096 +c = get a s +if and ne c 32 ne c 9 +break +end +s = add s 1 +end +while gt e s bound 4096 +c = get a sub e 1 +if and ne c 32 ne c 9 +break +end +e = sub e 1 +end +emit lq +emit part a s sub e s +emit rq +emit nl +end +fn main to int +strip s1 +strip s2 +strip s3 +ret 0 +end diff --git a/tasks/strip-whitespace-from-a-string/expected.txt b/tasks/strip-whitespace-from-a-string/expected.txt new file mode 100644 index 0000000..3820822 --- /dev/null +++ b/tasks/strip-whitespace-from-a-string/expected.txt @@ -0,0 +1,6 @@ +[ hello world + ] +[hello world + ] +[ hello world] +[hello world] diff --git a/tasks/strip-whitespace-from-a-string/oracle.py b/tasks/strip-whitespace-from-a-string/oracle.py new file mode 100644 index 0000000..6d833b5 --- /dev/null +++ b/tasks/strip-whitespace-from-a-string/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "Strip whitespace from a string/Top and tail" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +s = " \t hello world \t\n " +for t in (s, s.lstrip(), s.rstrip(), s.strip()): + print("[" + t + "]") diff --git a/tasks/strip-whitespace-from-a-string/strip-whitespace-from-a-string.om b/tasks/strip-whitespace-from-a-string/strip-whitespace-from-a-string.om new file mode 100644 index 0000000..5cd98b0 --- /dev/null +++ b/tasks/strip-whitespace-from-a-string/strip-whitespace-from-a-string.om @@ -0,0 +1,53 @@ +note Rosetta Code task "Strip whitespace from a string/Top and tail": https://rosettacode.org/wiki/Strip_whitespace_from_a_string/Top_and_tail +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text lb "[" +text rb "]" +text msg " \t hello world \t\n " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note white: space, tab, newline, vertical tab, form feed, carriage return +fn white c uchar to bool +ret or eq c 32 and ge c 9 le c 13 +end +fn lead a view uchar * to ulong +for i ulong len a +if not white get a i +ret i +end +end +ret len a +end +fn trail a view uchar * to ulong +for i ulong len a +if not white get a sub sub len a 1 i +ret sub len a i +end +end +ret 0 +end +fn show a view uchar * to void +emit lb +emit a +emit rb +emit nl +end +fn main to int +l = lead msg +t = trail msg +show msg +show part msg l sub len msg l +show part msg 0 t +show part msg l sub t l +ret 0 +end diff --git a/tasks/substitution-cipher/expected.txt b/tasks/substitution-cipher/expected.txt new file mode 100644 index 0000000..45774d7 --- /dev/null +++ b/tasks/substitution-cipher/expected.txt @@ -0,0 +1,3 @@ +the quick brown fox jumps over the lazy dog, 42 times +cei jvaql hkdtf udz yvoxr dsik cei npbw gdm, 42 caoir +the quick brown fox jumps over the lazy dog, 42 times diff --git a/tasks/substitution-cipher/oracle.py b/tasks/substitution-cipher/oracle.py new file mode 100644 index 0000000..4158051 --- /dev/null +++ b/tasks/substitution-cipher/oracle.py @@ -0,0 +1,11 @@ +# Independent reference for "Substitution cipher" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import string +key = "phqgiumeaylnofdxjkrcvstzwb" +msg = "the quick brown fox jumps over the lazy dog, 42 times" +enc = str.maketrans(string.ascii_lowercase, key) +dec = str.maketrans(key, string.ascii_lowercase) +e = msg.translate(enc) +print(msg) +print(e) +print(e.translate(dec)) diff --git a/tasks/substitution-cipher/substitution-cipher.om b/tasks/substitution-cipher/substitution-cipher.om new file mode 100644 index 0000000..f895579 --- /dev/null +++ b/tasks/substitution-cipher/substitution-cipher.om @@ -0,0 +1,56 @@ +note Rosetta Code task "Substitution cipher": https://rosettacode.org/wiki/Substitution_cipher +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +note key: plain letter a..z maps to the letter at the same place here +text key "phqgiumeaylnofdxjkrcvstzwb" +text msg "the quick brown fox jumps over the lazy dog, 42 times" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note enc maps each lower-case letter through key; other bytes are copied +fn enc a view uchar * r edit uchar * to void +for i ulong len a +c = get a i +if and ge c 97 le c 122 +c = get key sub c 97 +end +put r i c +end +end +note dec builds the inverse table first, then maps through it +fn dec a view uchar * r edit uchar * to void +inv = new uchar 26 0 +for j ulong 26 +put inv sub get key j 97 add uchar j 97 +end +for i ulong len a +c = get a i +if and ge c 97 le c 122 +c = get inv sub c 97 +end +put r i c +end +end +fn main to int +n = len msg +e = new uchar 256 0 +d = new uchar 256 0 +emit msg +emit nl +enc msg part e 0 n +emit part e 0 n +emit nl +dec part e 0 n part d 0 n +emit part d 0 n +emit nl +ret 0 +end diff --git a/tasks/substring/expected.txt b/tasks/substring/expected.txt new file mode 100644 index 0000000..b227bca --- /dev/null +++ b/tasks/substring/expected.txt @@ -0,0 +1,5 @@ +cde +cdefgh +abcdefg +def +cde diff --git a/tasks/substring/oracle.py b/tasks/substring/oracle.py new file mode 100644 index 0000000..c2e19ca --- /dev/null +++ b/tasks/substring/oracle.py @@ -0,0 +1,9 @@ +# Independent reference for "Substring" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +s = "abcdefgh" +n, m = 2, 3 +print(s[n:n + m]) +print(s[n:]) +print(s[:-1]) +print(s[s.index("d"):][:m]) +print(s[s.index("cd"):][:m]) diff --git a/tasks/substring/substring.om b/tasks/substring/substring.om new file mode 100644 index 0000000..bfcf6c2 --- /dev/null +++ b/tasks/substring/substring.om @@ -0,0 +1,48 @@ +note Rosetta Code task "Substring": https://rosettacode.org/wiki/Substring +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text str "abcdefgh" +text cd "cd" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note find returns the first index where t occurs in s; a missing t traps 9 +fn find s view uchar * t view uchar * to ulong +for i ulong add sub len s len t 1 +m = true +for j ulong len t +if ne get s add i j get t j +m = false +end +end +if m +ret i +end +end +trap 9 +end +fn line a view uchar * to void +emit a +emit nl +end +fn main to int +n = ulong 2 +m = ulong 3 +note n characters in, m long; from n to the end; all but the last; from d, m long; from cd, m long +line part str n m +line part str n sub len str n +line part str 0 sub len str 1 +c = new uchar 1 100 +line part str find str c m +line part str find str cd m +ret 0 +end diff --git a/tasks/sudoku/expected.txt b/tasks/sudoku/expected.txt new file mode 100644 index 0000000..d04cf67 --- /dev/null +++ b/tasks/sudoku/expected.txt @@ -0,0 +1,23 @@ +8 5 0 | 0 0 2 | 4 0 0 +7 2 0 | 0 0 0 | 0 0 9 +0 0 4 | 0 0 0 | 0 0 0 +------+-------+------ +0 0 0 | 1 0 7 | 0 0 2 +3 0 5 | 0 0 0 | 9 0 0 +0 4 0 | 0 0 0 | 0 0 0 +------+-------+------ +0 0 0 | 0 8 0 | 0 7 0 +0 1 7 | 0 0 0 | 0 0 0 +0 0 0 | 0 3 6 | 0 4 0 +solved: +8 5 9 | 6 1 2 | 4 3 7 +7 2 3 | 8 5 4 | 1 6 9 +1 6 4 | 3 7 9 | 5 2 8 +------+-------+------ +9 8 6 | 1 4 7 | 3 5 2 +3 7 5 | 2 6 8 | 9 1 4 +2 4 1 | 5 9 3 | 7 8 6 +------+-------+------ +4 3 2 | 9 8 1 | 6 7 5 +6 1 7 | 4 2 5 | 8 9 3 +5 9 8 | 7 3 6 | 2 4 1 diff --git a/tasks/sudoku/oracle.py b/tasks/sudoku/oracle.py new file mode 100644 index 0000000..bc4654e --- /dev/null +++ b/tasks/sudoku/oracle.py @@ -0,0 +1,30 @@ +# Independent reference for "Sudoku" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# constraint search on candidate sets: always branch on the empty cell with the fewest candidates +puzzle = [int(c) for c in "850002400720000009004000000000107002305000900040000000000080070017000000000036040"] +def peers(c): + r, k = divmod(c, 9) + br, bk = r // 3 * 3, k // 3 * 3 + return {r * 9 + i for i in range(9)} | {i * 9 + k for i in range(9)} | {(br + i // 3) * 9 + bk + i % 3 for i in range(9)} +PEERS = [peers(c) - {c} for c in range(81)] +def solve(g): + empty = [c for c in range(81) if g[c] == 0] + if not empty: + return g + c = min(empty, key=lambda c: 9 - len({g[p] for p in PEERS[c]} - {0})) + for d in sorted(set(range(1, 10)) - {g[p] for p in PEERS[c]}): + r = solve(g[:c] + [d] + g[c + 1:]) + if r: + return r + return None +def show(g): + for r in range(9): + if r and r % 3 == 0: + print("------+-------+------") + print(" | ".join(" ".join(str(g[r * 9 + k]) for k in range(b, b + 3)) for b in (0, 3, 6))) +show(puzzle) +sol = solve(puzzle) +for unit in [[r * 9 + i for i in range(9)] for r in range(9)] + [[i * 9 + k for i in range(9)] for k in range(9)]: + assert sorted(sol[c] for c in unit) == list(range(1, 10)) +print("solved:") +show(sol) diff --git a/tasks/sudoku/sudoku.om b/tasks/sudoku/sudoku.om new file mode 100644 index 0000000..12c4e3d --- /dev/null +++ b/tasks/sudoku/sudoku.om @@ -0,0 +1,110 @@ +note Rosetta Code task "Sudoku": https://rosettacode.org/wiki/Sudoku +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text puzzle "850002400720000009004000000000107002305000900040000000000080070017000000000036040" +text tbar "------+-------+------\n" +text tsep "| " +text tsolved "solved:\n" +global grid ulong 81 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note ok says whether digit d may go in cell c: not already in its row, column or 3x3 box +fn ok c ulong d ulong to bool +r = div c 9 +k = rem c 9 +b = add mul mul div r 3 3 9 mul div k 3 3 +for i ulong 9 +if eq get grid add mul r 9 i d +ret false +end +if eq get grid add mul i 9 k d +ret false +end +q = add b add mul div i 3 9 rem i 3 +if eq get grid q d +ret false +end +end +ret true +end +note solve fills the first empty cell at or after c with each digit that fits and recurses; on a dead +note end it empties the cell again (backtracking); the recursion is at most 81 deep +fn solve c ulong to bool +p = c +while lt p 81 bound 81 +if eq get grid p 0 +break +end +p = add p 1 +end +if eq p 81 +ret true +end +for x ulong 9 +d = add x 1 +if ok p d +put grid p d +if solve add p 1 +ret true +end +end +end +put grid p 0 +ret false +end +fn show to void +for r ulong 9 +if and gt r 0 eq rem r 3 0 +emit tbar +end +for k ulong 9 +if and gt k 0 eq rem k 3 0 +emit tsep +end +emitu get grid add mul r 9 k +if lt k 8 +emit sp +end +end +emit nl +end +end +fn main to int +for i ulong 81 +put grid i sub ulong get puzzle i 48 +end +show +if not solve 0 +exit 1 +end +emit tsolved +show +ret 0 +end diff --git a/tasks/sum-and-product-of-an-array/expected.txt b/tasks/sum-and-product-of-an-array/expected.txt new file mode 100644 index 0000000..0ca5a66 --- /dev/null +++ b/tasks/sum-and-product-of-an-array/expected.txt @@ -0,0 +1,2 @@ +sum 55 +product 3628800 diff --git a/tasks/sum-and-product-of-an-array/oracle.py b/tasks/sum-and-product-of-an-array/oracle.py new file mode 100644 index 0000000..61d5178 --- /dev/null +++ b/tasks/sum-and-product-of-an-array/oracle.py @@ -0,0 +1,6 @@ +# Independent reference for "Sum and product of an array" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import math +a = list(range(1, 11)) +print("sum", sum(a)) +print("product", math.prod(a)) diff --git a/tasks/sum-and-product-of-an-array/sum-and-product-of-an-array.om b/tasks/sum-and-product-of-an-array/sum-and-product-of-an-array.om new file mode 100644 index 0000000..59c52c8 --- /dev/null +++ b/tasks/sum-and-product-of-an-array/sum-and-product-of-an-array.om @@ -0,0 +1,51 @@ +note Rosetta Code task "Sum and product of an array": https://rosettacode.org/wiki/Sum_and_product_of_an_array +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text tsum "sum " +text tprod "product " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn main to int +a = new ulong 10 0 +for i ulong 10 +put a i add i 1 +end +s = ulong 0 +p = ulong 1 +for i ulong 10 +s = add s get a i +p = mul p get a i +end +emit tsum +emitu s +emit nl +emit tprod +emitu p +emit nl +ret 0 +end diff --git a/tasks/sum-digits-of-an-integer/expected.txt b/tasks/sum-digits-of-an-integer/expected.txt new file mode 100644 index 0000000..af12174 --- /dev/null +++ b/tasks/sum-digits-of-an-integer/expected.txt @@ -0,0 +1,7 @@ +1 (base 10): digit sum 1 +1234 (base 10): digit sum 10 +fe (base 16): digit sum 29 +f0e (base 16): digit sum 29 +377 (base 8): digit sum 17 +1111111111111111111111111111111111111111111111111111111111111111 (base 2): digit sum 64 +3w5e11264sgsf (base 36): digit sum 155 diff --git a/tasks/sum-digits-of-an-integer/oracle.py b/tasks/sum-digits-of-an-integer/oracle.py new file mode 100644 index 0000000..45b1f08 --- /dev/null +++ b/tasks/sum-digits-of-an-integer/oracle.py @@ -0,0 +1,14 @@ +# Independent reference for "Sum digits of an integer" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import string +DIG = string.digits + string.ascii_lowercase +def digits(n, b): + out = [] + while True: + n, r = divmod(n, b) + out.append(r) + if n == 0: + return out[::-1] +for n, b in [(1, 10), (1234, 10), (0xfe, 16), (0xf0e, 16), (255, 8), (2**64 - 1, 2), (2**64 - 1, 36)]: + ds = digits(n, b) + print(f"{''.join(DIG[d] for d in ds)} (base {b}): digit sum {sum(ds)}") diff --git a/tasks/sum-digits-of-an-integer/sum-digits-of-an-integer.om b/tasks/sum-digits-of-an-integer/sum-digits-of-an-integer.om new file mode 100644 index 0000000..839c7e8 --- /dev/null +++ b/tasks/sum-digits-of-an-integer/sum-digits-of-an-integer.om @@ -0,0 +1,82 @@ +note Rosetta Code task "Sum digits of an integer": https://rosettacode.org/wiki/Sum_digits_of_an_integer +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text dig "0123456789abcdefghijklmnopqrstuvwxyz" +text bs " (base " +text eqs "): digit sum " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitc writes the single byte c +fn emitc c uchar to void +b = new uchar 1 c +emit b +end +note emitb writes n in base b (2..36); a ulong has at most 64 digits in base 2 +fn emitb n ulong b ulong to void +buf = new uchar 64 0 +k = ulong 0 +v = n +while true bound 64 +put buf k get dig rem v b +k = add k 1 +v = div v b +if eq v 0 +break +end +end +for i ulong k +emitc get buf sub sub k 1 i +end +end +fn dsum n ulong b ulong to ulong +s = ulong 0 +v = n +while gt v 0 bound 64 +s = add s rem v b +v = div v b +end +ret s +end +fn tell n ulong b ulong to void +emitb n b +emit bs +emitu b +emit eqs +emitu dsum n b +emit nl +end +fn main to int +tell 1 10 +tell 1234 10 +tell 0xfe 16 +tell 0xf0e 16 +tell 255 8 +tell 18446744073709551615 2 +tell 18446744073709551615 36 +ret 0 +end diff --git a/tasks/sum-digits-of-an-integer/wolfram.txt b/tasks/sum-digits-of-an-integer/wolfram.txt new file mode 100644 index 0000000..b07122a --- /dev/null +++ b/tasks/sum-digits-of-an-integer/wolfram.txt @@ -0,0 +1,12 @@ +Wolfram cross-check of tasks/sum-digits-of-an-integer/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 091b1b052688f0d229078e8e44edaa1bec8fb4d49daf302d0a34610f2a9d2f86. +agreement: YES +query: +f[{n_, b_}] := IntegerString[n, b] <> " (base " <> ToString[b] <> "): digit sum " <> ToString[Total[IntegerDigits[n, b]]] <> "\n"; +s = StringJoin[f /@ {{1, 10}, {1234, 10}, {254, 16}, {3854, 16}, {255, 8}, {2^64 - 1, 2}, {2^64 - 1, 36}}]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"091b1b052688f0d229078e8e44edaa1bec8fb4d49daf302d0a34610f2a9d2f86", "1 (base 10): digit sum 1\n1234 (base 10): digit sum 10\nfe (base 16): digit sum 29\nf0e (base 16): digit sum 29\n377 (base 8): digit sum 17\n1111111111111111111111111111111111111111111111111111111111111111 (base 2): digit sum 64\n3w5e11264sgsf (base 36): digit sum 155\n"} diff --git a/tasks/sum-multiples-of-3-and-5/expected.txt b/tasks/sum-multiples-of-3-and-5/expected.txt new file mode 100644 index 0000000..575badb --- /dev/null +++ b/tasks/sum-multiples-of-3-and-5/expected.txt @@ -0,0 +1,10 @@ +by loop, below 1000: 233168 +by formula, below 10^1: 23 +by formula, below 10^2: 2318 +by formula, below 10^3: 233168 +by formula, below 10^4: 23331668 +by formula, below 10^5: 2333316668 +by formula, below 10^6: 233333166668 +by formula, below 10^7: 23333331666668 +by formula, below 10^8: 2333333316666668 +by formula, below 10^9: 233333333166666668 diff --git a/tasks/sum-multiples-of-3-and-5/oracle.py b/tasks/sum-multiples-of-3-and-5/oracle.py new file mode 100644 index 0000000..b4cf350 --- /dev/null +++ b/tasks/sum-multiples-of-3-and-5/oracle.py @@ -0,0 +1,6 @@ +# Independent reference for "Sum multiples of 3 and 5" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +print(f"by loop, below 1000: {sum(set(range(0, 1000, 3)) | set(range(0, 1000, 5)))}") +for e in range(1, 10): + n = 10 ** e + print(f"by formula, below 10^{e}: {sum(i for i in range(n) if i % 3 == 0 or i % 5 == 0) if e <= 6 else (sum(range(0, n, 3)) + sum(range(0, n, 5)) - sum(range(0, n, 15)))}") diff --git a/tasks/sum-multiples-of-3-and-5/sum-multiples-of-3-and-5.om b/tasks/sum-multiples-of-3-and-5/sum-multiples-of-3-and-5.om new file mode 100644 index 0000000..2aed043 --- /dev/null +++ b/tasks/sum-multiples-of-3-and-5/sum-multiples-of-3-and-5.om @@ -0,0 +1,60 @@ +note Rosetta Code task "Sum multiples of 3 and 5": https://rosettacode.org/wiki/Sum_multiples_of_3_and_5 +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text loop "by loop, below 1000: " +text pre "by formula, below 10^" +text col ": " +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note the multiples of k below n add up to k * m * (m + 1) / 2 with m = (n - 1) / k; inclusion-exclusion +note removes the multiples of 15 counted twice. Below 10^9 every product stays under 2^64 +fn sm k ulong n ulong to ulong +m = div sub n 1 k +ret div mul mul k m add m 1 2 +end +fn main to int +s = ulong 0 +for i ulong 1000 +if or eq rem i 3 0 eq rem i 5 0 +s = add s i +end +end +emit loop +emitu s +emit nl +n = ulong 10 +for e ulong 9 +emit pre +emitu add e 1 +emit col +emitu sub add sm 3 n sm 5 n sm 15 n +emit nl +n = mul n 10 +end +ret 0 +end diff --git a/tasks/sum-multiples-of-3-and-5/wolfram.txt b/tasks/sum-multiples-of-3-and-5/wolfram.txt new file mode 100644 index 0000000..525fa7b --- /dev/null +++ b/tasks/sum-multiples-of-3-and-5/wolfram.txt @@ -0,0 +1,13 @@ +Wolfram cross-check of tasks/sum-multiples-of-3-and-5/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 9c5883ef8e77d0ad2b32028a0c148c15a51a0098504f1ba2b5b79b80c5bad182. +agreement: YES +query: +f[n_] := Sum[k, {k, 0, n - 1, 3}] + Sum[k, {k, 0, n - 1, 5}] - Sum[k, {k, 0, n - 1, 15}]; +s = "by loop, below 1000: " <> ToString[Total[Select[Range[999], Divisible[#, 3] || Divisible[#, 5] &]]] <> "\n" <> + StringJoin[Table["by formula, below 10^" <> ToString[e] <> ": " <> ToString[f[10^e]] <> "\n", {e, 1, 9}]]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"9c5883ef8e77d0ad2b32028a0c148c15a51a0098504f1ba2b5b79b80c5bad182", "by loop, below 1000: 233168\nby formula, below 10^1: 23\nby formula, below 10^2: 2318\nby formula, below 10^3: 233168\nby formula, below 10^4: 23331668\nby formula, below 10^5: 2333316668\nby formula, below 10^6: 233333166668\nby formula, below 10^7: 23333331666668\nby formula, below 10^8: 2333333316666668\nby formula, below 10^9: 233333333166666668\n"} diff --git a/tasks/temperature-conversion/expected.txt b/tasks/temperature-conversion/expected.txt new file mode 100644 index 0000000..bf54da1 --- /dev/null +++ b/tasks/temperature-conversion/expected.txt @@ -0,0 +1,25 @@ +K 21.00 +C -252.15 +F -421.87 +R 37.80 + +K 0.00 +C -273.15 +F -459.67 +R 0.00 + +K 273.15 +C 0.00 +F 32.00 +R 491.67 + +K 373.15 +C 100.00 +F 212.00 +R 671.67 + +K 310.15 +C 37.00 +F 98.60 +R 558.27 + diff --git a/tasks/temperature-conversion/oracle.py b/tasks/temperature-conversion/oracle.py new file mode 100644 index 0000000..de79b23 --- /dev/null +++ b/tasks/temperature-conversion/oracle.py @@ -0,0 +1,14 @@ +# Independent reference for "Temperature conversion" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +from fractions import Fraction as F +def fmt(x): + c = round(x * 100) + assert c == x * 100 + return ("-" if c < 0 else "") + f"{abs(c) // 100}.{abs(c) % 100:02d}" +for k100 in [2100, 0, 27315, 37315, 31015]: + k = F(k100, 100) + print("K ", fmt(k)) + print("C ", fmt(k - F(27315, 100))) + print("F ", fmt(k * F(9, 5) - F(45967, 100))) + print("R ", fmt(k * F(9, 5))) + print() diff --git a/tasks/temperature-conversion/temperature-conversion.om b/tasks/temperature-conversion/temperature-conversion.om new file mode 100644 index 0000000..fbda682 --- /dev/null +++ b/tasks/temperature-conversion/temperature-conversion.om @@ -0,0 +1,78 @@ +note Rosetta Code task "Temperature conversion": https://rosettacode.org/wiki/Temperature_conversion +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text tk "K " +text tc "C " +text tf "F " +text tr "R " +text minus "-" +text dot "." +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitc2 writes v hundredths (signed) as a decimal with two places +fn emitc2 v long to void +u = ulong 0 +if lt v 0 +emit minus +u = ulong neg v +else +u = ulong v +end +emitu div u 100 +emit dot +f = rem u 100 +if lt f 10 +emitu 0 +end +emitu f +end +note k is kelvin in hundredths, a multiple of 5 so every scale below is exact in hundredths: +note C = K - 273.15, F = K * 9/5 - 459.67, R = K * 9/5 +fn show k long to void +emit tk +emitc2 k +emit nl +emit tc +emitc2 sub k 27315 +emit nl +emit tf +emitc2 sub div mul k 9 5 45967 +emit nl +emit tr +emitc2 div mul k 9 5 +emit nl +emit nl +end +fn main to int +show 2100 +show 0 +show 27315 +show 37315 +show 31015 +ret 0 +end diff --git a/tasks/the-twelve-days-of-christmas/expected.txt b/tasks/the-twelve-days-of-christmas/expected.txt new file mode 100644 index 0000000..67b6435 --- /dev/null +++ b/tasks/the-twelve-days-of-christmas/expected.txt @@ -0,0 +1,113 @@ +On the first day of Christmas, +My true love sent to me: +A partridge in a pear tree. + +On the second day of Christmas, +My true love sent to me: +Two turtle doves, +And a partridge in a pear tree. + +On the third day of Christmas, +My true love sent to me: +Three french hens, +Two turtle doves, +And a partridge in a pear tree. + +On the fourth day of Christmas, +My true love sent to me: +Four calling birds, +Three french hens, +Two turtle doves, +And a partridge in a pear tree. + +On the fifth day of Christmas, +My true love sent to me: +Five golden rings, +Four calling birds, +Three french hens, +Two turtle doves, +And a partridge in a pear tree. + +On the sixth day of Christmas, +My true love sent to me: +Six geese a-laying, +Five golden rings, +Four calling birds, +Three french hens, +Two turtle doves, +And a partridge in a pear tree. + +On the seventh day of Christmas, +My true love sent to me: +Seven swans a-swimming, +Six geese a-laying, +Five golden rings, +Four calling birds, +Three french hens, +Two turtle doves, +And a partridge in a pear tree. + +On the eighth day of Christmas, +My true love sent to me: +Eight maids a-milking, +Seven swans a-swimming, +Six geese a-laying, +Five golden rings, +Four calling birds, +Three french hens, +Two turtle doves, +And a partridge in a pear tree. + +On the ninth day of Christmas, +My true love sent to me: +Nine ladies dancing, +Eight maids a-milking, +Seven swans a-swimming, +Six geese a-laying, +Five golden rings, +Four calling birds, +Three french hens, +Two turtle doves, +And a partridge in a pear tree. + +On the tenth day of Christmas, +My true love sent to me: +Ten lords a-leaping, +Nine ladies dancing, +Eight maids a-milking, +Seven swans a-swimming, +Six geese a-laying, +Five golden rings, +Four calling birds, +Three french hens, +Two turtle doves, +And a partridge in a pear tree. + +On the eleventh day of Christmas, +My true love sent to me: +Eleven pipers piping, +Ten lords a-leaping, +Nine ladies dancing, +Eight maids a-milking, +Seven swans a-swimming, +Six geese a-laying, +Five golden rings, +Four calling birds, +Three french hens, +Two turtle doves, +And a partridge in a pear tree. + +On the twelfth day of Christmas, +My true love sent to me: +Twelve drummers drumming, +Eleven pipers piping, +Ten lords a-leaping, +Nine ladies dancing, +Eight maids a-milking, +Seven swans a-swimming, +Six geese a-laying, +Five golden rings, +Four calling birds, +Three french hens, +Two turtle doves, +And a partridge in a pear tree. diff --git a/tasks/the-twelve-days-of-christmas/oracle.py b/tasks/the-twelve-days-of-christmas/oracle.py new file mode 100644 index 0000000..3487cfd --- /dev/null +++ b/tasks/the-twelve-days-of-christmas/oracle.py @@ -0,0 +1,11 @@ +# Independent reference for "The Twelve Days of Christmas" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +days = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'eleventh', 'twelfth'] +gifts = ['A partridge in a pear tree.', 'Two turtle doves,', 'Three french hens,', 'Four calling birds,', 'Five golden rings,', 'Six geese a-laying,', 'Seven swans a-swimming,', 'Eight maids a-milking,', 'Nine ladies dancing,', 'Ten lords a-leaping,', 'Eleven pipers piping,', 'Twelve drummers drumming,'] +for d in range(12): + if d: + print() + print(f"On the {days[d]} day of Christmas,") + print("My true love sent to me:") + for g in range(d, -1, -1): + print("And a partridge in a pear tree." if g == 0 and d > 0 else gifts[g]) diff --git a/tasks/the-twelve-days-of-christmas/the-twelve-days-of-christmas.om b/tasks/the-twelve-days-of-christmas/the-twelve-days-of-christmas.om new file mode 100644 index 0000000..fc82b03 --- /dev/null +++ b/tasks/the-twelve-days-of-christmas/the-twelve-days-of-christmas.om @@ -0,0 +1,60 @@ +note Rosetta Code task "The Twelve Days of Christmas": https://rosettacode.org/wiki/The_Twelve_Days_of_Christmas +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text on "On the " +text day " day of Christmas,\nMy true love sent to me:\n" +text last "And a partridge in a pear tree." +text days "first\nsecond\nthird\nfourth\nfifth\nsixth\nseventh\neighth\nninth\ntenth\neleventh\ntwelfth\n" +text gifts "A partridge in a pear tree.\nTwo turtle doves,\nThree french hens,\nFour calling birds,\nFive golden rings,\nSix geese a-laying,\nSeven swans a-swimming,\nEight maids a-milking,\nNine ladies dancing,\nTen lords a-leaping,\nEleven pipers piping,\nTwelve drummers drumming,\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note word writes element k of a newline-terminated list, without the newline +fn word t view uchar * k ulong to void +s = ulong 0 +m = ulong 0 +for i ulong len t +if eq get t i 10 +if eq m k +emit part t s sub i s +ret +end +m = add m 1 +s = add i 1 +end +end +trap 9 +end +note verse d lists gifts d down to 0; from the second day on the partridge line starts "And a" +fn verse d ulong to void +emit on +word days d +emit day +for k ulong add d 1 +g = sub d k +if and eq g 0 gt d 0 +emit last +else +word gifts g +end +emit nl +end +end +fn main to int +for d ulong 12 +if gt d 0 +emit nl +end +verse d +end +ret 0 +end diff --git a/tasks/thue-morse/expected.txt b/tasks/thue-morse/expected.txt new file mode 100644 index 0000000..53ae57c --- /dev/null +++ b/tasks/thue-morse/expected.txt @@ -0,0 +1,7 @@ +0 +01 +0110 +01101001 +0110100110010110 +01101001100101101001011001101001 +0110100110010110100101100110100110010110011010010110100110010110 diff --git a/tasks/thue-morse/oracle.py b/tasks/thue-morse/oracle.py new file mode 100644 index 0000000..9c20bef --- /dev/null +++ b/tasks/thue-morse/oracle.py @@ -0,0 +1,4 @@ +# Independent reference for "Thue-Morse" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for k in range(7): + print("".join(str(bin(i).count("1") % 2) for i in range(2 ** k))) diff --git a/tasks/thue-morse/thue-morse.om b/tasks/thue-morse/thue-morse.om new file mode 100644 index 0000000..c4c7d00 --- /dev/null +++ b/tasks/thue-morse/thue-morse.om @@ -0,0 +1,38 @@ +note Rosetta Code task "Thue-Morse": https://rosettacode.org/wiki/Thue-Morse +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes the single byte c +fn emitc c uchar to void +b = new uchar 1 c +emit b +end +note each step appends the bitwise complement of the sequence so far; 7 steps reach 64 terms +fn main to int +t = new uchar 64 0 +n = ulong 1 +for s ulong 7 +for i ulong n +emitc add get t i uchar 48 +end +emit nl +if lt n 64 +for i ulong n +put t add n i sub uchar 1 get t i +end +n = mul n 2 +end +end +ret 0 +end diff --git a/tasks/thue-morse/wolfram.txt b/tasks/thue-morse/wolfram.txt new file mode 100644 index 0000000..d59ed3b --- /dev/null +++ b/tasks/thue-morse/wolfram.txt @@ -0,0 +1,11 @@ +Wolfram cross-check of tasks/thue-morse/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = b79f272ebc2af3b3a700417016e8f75b1b66bc463375254f8928c3430fa0d9ab. +agreement: YES +query: +s = StringJoin[Table[StringJoin[ToString /@ ThueMorse[Range[0, 2^k - 1]]] <> "\n", {k, 0, 6}]]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"b79f272ebc2af3b3a700417016e8f75b1b66bc463375254f8928c3430fa0d9ab", "0\n01\n0110\n01101001\n0110100110010110\n01101001100101101001011001101001\n0110100110010110100101100110100110010110011010010110100110010110\n"} diff --git a/tasks/tokenize-a-string/expected.txt b/tasks/tokenize-a-string/expected.txt new file mode 100644 index 0000000..1b38034 --- /dev/null +++ b/tasks/tokenize-a-string/expected.txt @@ -0,0 +1 @@ +Hello.How.Are.You.Today diff --git a/tasks/tokenize-a-string/oracle.py b/tasks/tokenize-a-string/oracle.py new file mode 100644 index 0000000..79215e0 --- /dev/null +++ b/tasks/tokenize-a-string/oracle.py @@ -0,0 +1,3 @@ +# Independent reference for "Tokenize a string" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +print(".".join("Hello,How,Are,You,Today".split(","))) diff --git a/tasks/tokenize-a-string/tokenize-a-string.om b/tasks/tokenize-a-string/tokenize-a-string.om new file mode 100644 index 0000000..5f1bf32 --- /dev/null +++ b/tasks/tokenize-a-string/tokenize-a-string.om @@ -0,0 +1,40 @@ +note Rosetta Code task "Tokenize a string": https://rosettacode.org/wiki/Tokenize_a_string +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text dot "." +text msg "Hello,How,Are,You,Today" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note splits at every comma, keeps the pieces as (start, length) pairs, then joins them with periods +fn main to int +st = new ulong 64 0 +ln = new ulong 64 0 +k = ulong 0 +s = ulong 0 +for i ulong add len msg 1 +if or eq i len msg eq get msg rem i len msg 44 +put st k s +put ln k sub i s +k = add k 1 +s = add i 1 +end +end +for j ulong k +if gt j 0 +emit dot +end +emit part msg get st j get ln j +end +emit nl +ret 0 +end diff --git a/tasks/topological-sort/expected.txt b/tasks/topological-sort/expected.txt new file mode 100644 index 0000000..bb2540b --- /dev/null +++ b/tasks/topological-sort/expected.txt @@ -0,0 +1,2 @@ +ieee dware dw02 dw05 dw06 dw07 gtech dw01 dw04 std ramlib std_cell_lib synopsys des_system_lib dw03 +un-orderable: diff --git a/tasks/topological-sort/oracle.py b/tasks/topological-sort/oracle.py new file mode 100644 index 0000000..78daa12 --- /dev/null +++ b/tasks/topological-sort/oracle.py @@ -0,0 +1,31 @@ +# Independent reference for "Topological sort" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import heapq +groups = [('des_system_lib', 'std synopsys std_cell_lib des_system_lib dw02 dw01 ramlib ieee'), ('dw01', 'ieee dw01 dware gtech'), ('dw02', 'ieee dw02 dware'), ('dw03', 'std synopsys dware dw03 dw02 dw01 ieee gtech'), ('dw04', 'dw04 ieee dw01 dware gtech'), ('dw05', 'dw05 ieee dware'), ('dw06', 'dw06 ieee dware'), ('dw07', 'ieee dware'), ('dware', 'ieee dware'), ('gtech', 'ieee gtech'), ('ramlib', 'std ieee'), ('std_cell_lib', 'ieee std_cell_lib'), ('synopsys', '')] +deps = {} +for lib, ds in groups: + deps.setdefault(lib, set()) + for d in ds.split(): + deps.setdefault(d, set()) + if d != lib: + deps[lib].add(d) +users = {n: set() for n in deps} +for lib, ds in deps.items(): + for d in ds: + users[d].add(lib) +indeg = {n: len(ds) for n, ds in deps.items()} +ready = [n for n, k in indeg.items() if k == 0] +heapq.heapify(ready) +order = [] +while ready: + n = heapq.heappop(ready) + order.append(n) + for u in users[n]: + indeg[u] -= 1 + if indeg[u] == 0: + heapq.heappush(ready, u) +# the order is checked directly: every library comes after everything it depends on +pos = {n: i for i, n in enumerate(order)} +assert all(pos[d] < pos[lib] for lib in order for d in deps[lib]) +print(" ".join(order)) +print("un-orderable:" + "".join(" " + n for n in sorted(set(deps) - set(order)))) diff --git a/tasks/topological-sort/topological-sort.om b/tasks/topological-sort/topological-sort.om new file mode 100644 index 0000000..d0aa6c6 --- /dev/null +++ b/tasks/topological-sort/topological-sort.om @@ -0,0 +1,161 @@ +note Rosetta Code task "Topological sort": https://rosettacode.org/wiki/Topological_sort +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text data "des_system_lib std synopsys std_cell_lib des_system_lib dw02 dw01 ramlib ieee;dw01 ieee dw01 dware gtech;dw02 ieee dw02 dware;dw03 std synopsys dware dw03 dw02 dw01 ieee gtech;dw04 dw04 ieee dw01 dware gtech;dw05 dw05 ieee dware;dw06 dw06 ieee dware;dw07 ieee dware;dware ieee dware;gtech ieee gtech;ramlib std ieee;std_cell_lib ieee std_cell_lib;synopsys;" +text tcyc "un-orderable:" +global nst ulong 32 +global nln ulong 32 +global dep uchar 1024 +global indeg ulong 32 +global sent uchar 32 +global nn ulong 1 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note data lists one library per ;-terminated group: the library, then what it depends on; a library +note that names itself is ignored, as the task asks. Names are kept as (start, length) in data. +note same says whether the names at (a, la) and (b, lb) in data are the same bytes +fn same a ulong la ulong b ulong lb ulong to bool +if ne la lb +ret false +end +for i ulong la +if ne get data add a i get data add b i +ret false +end +end +ret true +end +note before says whether name i sorts before name j, comparing bytes as C's strcmp does +fn before i ulong j ulong to bool +a = get nst i +b = get nst j +la = get nln i +lb = get nln j +for k ulong la +if ge k lb +ret false +end +if ne get data add a k get data add b k +ret lt get data add a k get data add b k +end +end +ret lt la lb +end +note intern returns the number of the name at (s, l), adding it to the table if it is new +fn intern s ulong l ulong to ulong +n = get nn 0 +for i ulong n +if same get nst i get nln i s l +ret i +end +end +put nst n s +put nln n l +put nn 0 add n 1 +ret n +end +note load reads every group: the first word is the library u, each later word v adds the edge v -> u +fn load to void +first = true +u = ulong 0 +st = ulong 0 +for i ulong len data +c = get data i +if or eq c 32 eq c 59 +if gt i st +v = intern st sub i st +if first +u = v +first = false +else +addedge v u +end +end +st = add i 1 +if eq c 59 +first = true +end +end +end +end +fn addedge v ulong u ulong to void +if ne v u +if eq get dep add mul v 32 u 0 +put dep add mul v 32 u 1 +put indeg u add get indeg u 1 +end +end +end +fn name i ulong to void +emit part data get nst i get nln i +end +note Kahn's algorithm: output the smallest ready name (in-degree 0), then release what depends on it; +note each round outputs one name, so nn rounds suffice; names never output are part of a cycle +fn main to int +load +n = get nn 0 +for r ulong n +b = n +for i ulong n +if and eq get sent i 0 eq get indeg i 0 +if eq b n +b = i +else +if before i b +b = i +end +end +end +end +if eq b n +break +end +put sent b 1 +if gt r 0 +emit sp +end +name b +for j ulong n +if eq get dep add mul b 32 j 1 +put indeg j sub get indeg j 1 +end +end +end +emit nl +emit tcyc +for i ulong n +if eq get sent i 0 +emit sp +name i +end +end +emit nl +ret 0 +end diff --git a/tasks/topswops/expected.txt b/tasks/topswops/expected.txt new file mode 100644 index 0000000..bb6ce18 --- /dev/null +++ b/tasks/topswops/expected.txt @@ -0,0 +1,10 @@ +n most swops +1 0 +2 1 +3 2 +4 4 +5 7 +6 10 +7 16 +8 22 +9 30 diff --git a/tasks/topswops/oracle.py b/tasks/topswops/oracle.py new file mode 100644 index 0000000..7fdf0ec --- /dev/null +++ b/tasks/topswops/oracle.py @@ -0,0 +1,15 @@ +# Independent reference for "Topswops" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# brute force over every deck; the reversal is a list slice, the decks come from itertools +from itertools import permutations +def swops(deck): + deck = list(deck) + c = 0 + while deck[0] != 1: + m = deck[0] + deck[:m] = deck[:m][::-1] + c += 1 + return c +print("n most swops") +for n in range(1, 10): + print(f"{n}{max(swops(p) for p in permutations(range(1, n + 1))):12}") diff --git a/tasks/topswops/topswops.om b/tasks/topswops/topswops.om new file mode 100644 index 0000000..9c30f35 --- /dev/null +++ b/tasks/topswops/topswops.om @@ -0,0 +1,131 @@ +note Rosetta Code task "Topswops": https://rosettacode.org/wiki/Topswops +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text thead "n most swops\n" +global perm ulong 10 +global deck ulong 10 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt w sub 20 i +i = sub 20 w +end +emit part d i sub 20 i +end +note nextp: the next permutation in lexicographic order, false after the last +fn nextp n ulong to bool +if lt n 2 +ret false +end +i = sub n 1 +while gt i 0 bound 10 +if lt get perm sub i 1 get perm i +break +end +i = sub i 1 +end +if eq i 0 +ret false +end +j = sub n 1 +while le get perm j get perm sub i 1 bound 10 +j = sub j 1 +end +t = get perm sub i 1 +put perm sub i 1 get perm j +put perm j t +k = sub n 1 +while lt i k bound 10 +t2 = get perm i +put perm i get perm k +put perm k t2 +i = add i 1 +k = sub k 1 +end +ret true +end +note swops plays the game on a copy of perm (cards 1..n): while the top card is m > 1, reverse the top +note m cards; it ends because the deck's lexicographic order rises with each reversal, and the known +note maxima for n <= 9 are at most 30, so 1000 rounds are plenty +fn swops n ulong to ulong +for i ulong n +put deck i get perm i +end +c = ulong 0 +while ne get deck 0 1 bound 1000 +m = get deck 0 +for x ulong shr m 1 +t = get deck x +put deck x get deck sub sub m x 1 +put deck sub sub m x 1 t +end +c = add c 1 +end +ret c +end +note most tries every one of the n! decks (9! = 362880 at most) +fn most n ulong to ulong +for i ulong n +put perm i add i 1 +end +b = ulong 0 +while true bound 362880 +s = swops n +if gt s b +b = s +end +if not nextp n +break +end +end +ret b +end +fn main to int +emit thead +for x ulong 9 +n = add x 1 +emitu n +emitw most n 12 +emit nl +end +ret 0 +end diff --git a/tasks/totient-function/expected.txt b/tasks/totient-function/expected.txt new file mode 100644 index 0000000..f67cc74 --- /dev/null +++ b/tasks/totient-function/expected.txt @@ -0,0 +1,29 @@ +phi(1) = 1 +phi(2) = 1, prime +phi(3) = 2, prime +phi(4) = 2 +phi(5) = 4, prime +phi(6) = 2 +phi(7) = 6, prime +phi(8) = 4 +phi(9) = 6 +phi(10) = 4 +phi(11) = 10, prime +phi(12) = 4 +phi(13) = 12, prime +phi(14) = 6 +phi(15) = 8 +phi(16) = 8 +phi(17) = 16, prime +phi(18) = 6 +phi(19) = 18, prime +phi(20) = 8 +phi(21) = 12 +phi(22) = 10 +phi(23) = 22, prime +phi(24) = 8 +phi(25) = 20 +primes up to 100: 25 +primes up to 1000: 168 +primes up to 10000: 1229 +primes up to 100000: 9592 diff --git a/tasks/totient-function/oracle.py b/tasks/totient-function/oracle.py new file mode 100644 index 0000000..77d6514 --- /dev/null +++ b/tasks/totient-function/oracle.py @@ -0,0 +1,14 @@ +# Independent reference for "Totient function" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import math +for n in range(1, 26): + phi = sum(1 for k in range(1, n + 1) if math.gcd(n, k) == 1) + print(f"phi({n}) = {phi}" + (", prime" if phi == n - 1 else "")) +N = 100000 +s = bytearray([1]) * (N + 1) +s[0] = s[1] = 0 +for i in range(2, math.isqrt(N) + 1): + if s[i]: + s[i * i::i] = bytes(len(range(i * i, N + 1, i))) +for lim in [100, 1000, 10000, 100000]: + print(f"primes up to {lim}: {sum(s[:lim + 1])}") diff --git a/tasks/totient-function/totient-function.om b/tasks/totient-function/totient-function.om new file mode 100644 index 0000000..89c5846 --- /dev/null +++ b/tasks/totient-function/totient-function.om @@ -0,0 +1,87 @@ +note Rosetta Code task "Totient function": https://rosettacode.org/wiki/Totient_function +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text phi "phi(" +text eqs ") = " +text prm ", prime" +text cnt "primes up to " +text col ": " +note ph n is Euler's totient of n for n up to 100000 +global ph ulong 100001 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note sieve: start with ph n = n; each prime p (still ph p = p when reached) scales its multiples by (1 - 1/p) +note bound: each p touches at most 100000 / p multiples +fn sieve to void +for i ulong 100001 +put ph i i +end +p = ulong 2 +while le p 100000 bound 100000 +if eq get ph p p +j = p +while le j 100000 bound 100000 +put ph j sub get ph j div get ph j p +j = add j p +end +end +p = add p 1 +end +end +note n > 1 is prime exactly when phi(n) = n - 1 +fn main to int +sieve +for i ulong 25 +n = add i 1 +emit phi +emitu n +emit eqs +emitu get ph n +if eq get ph n sub n 1 +emit prm +end +emit nl +end +lim = ulong 100 +c = ulong 0 +for i ulong 99999 +n = add i 2 +if eq get ph n sub n 1 +c = add c 1 +end +if eq n lim +emit cnt +emitu lim +emit col +emitu c +emit nl +lim = mul lim 10 +end +end +ret 0 +end diff --git a/tasks/totient-function/wolfram.txt b/tasks/totient-function/wolfram.txt new file mode 100644 index 0000000..ea4225d --- /dev/null +++ b/tasks/totient-function/wolfram.txt @@ -0,0 +1,12 @@ +Wolfram cross-check of tasks/totient-function/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = bcd1b48efefe270ac88d52ad00f41541e4abd6eecf999b7a19e02814cf80e20b. +agreement: YES +query: +s = StringJoin[Table["phi(" <> ToString[n] <> ") = " <> ToString[EulerPhi[n]] <> If[PrimeQ[n], ", prime", ""] <> "\n", {n, 1, 25}]] <> + StringJoin[("primes up to " <> ToString[#] <> ": " <> ToString[PrimePi[#]] <> "\n") & /@ {100, 1000, 10000, 100000}]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"bcd1b48efefe270ac88d52ad00f41541e4abd6eecf999b7a19e02814cf80e20b", "phi(1) = 1\nphi(2) = 1, prime\nphi(3) = 2, prime\nphi(4) = 2\nphi(5) = 4, prime\nphi(6) = 2\nphi(7) = 6, prime\nphi(8) = 4\nphi(9) = 6\nphi(10) = 4\nphi(11) = 10, prime\nphi(12) = 4\nphi(13) = 12, prime\nphi(14) = 6\nphi(15) = 8\nphi(16) = 8\nphi(17) = 16, prime\nphi(18) = 6\nphi(19) = 18, prime\nphi(20) = 8\nphi(21) = 12\nphi(22) = 10\nphi(23) = 22, prime\nphi(24) = 8\nphi(25) = 20\nprimes up to 100: 25\nprimes up to 1000: 168\nprimes up to 10000: 1229\nprimes up to 100000: 9592\n"} diff --git a/tasks/tree-traversal/expected.txt b/tasks/tree-traversal/expected.txt new file mode 100644 index 0000000..86de517 --- /dev/null +++ b/tasks/tree-traversal/expected.txt @@ -0,0 +1,4 @@ +preorder: 1 2 4 7 5 3 6 8 9 +inorder: 7 4 2 5 1 8 6 9 3 +postorder: 7 4 5 2 8 9 6 3 1 +level-order: 1 2 3 4 5 6 7 8 9 diff --git a/tasks/tree-traversal/oracle.py b/tasks/tree-traversal/oracle.py new file mode 100644 index 0000000..d37333d --- /dev/null +++ b/tasks/tree-traversal/oracle.py @@ -0,0 +1,21 @@ +# Independent reference for "Tree traversal" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +from collections import deque +tree = (1, (2, (4, (7, None, None), None), (5, None, None)), (3, (6, (8, None, None), (9, None, None)), None)) +def pre(t): + return [] if t is None else [t[0]] + pre(t[1]) + pre(t[2]) +def ino(t): + return [] if t is None else ino(t[1]) + [t[0]] + ino(t[2]) +def post(t): + return [] if t is None else post(t[1]) + post(t[2]) + [t[0]] +def level(t): + out, q = [], deque([t]) + while q: + n = q.popleft() + if n: + out.append(n[0]); q.extend(n[1:]) + return out +print("preorder: ", *pre(tree)) +print("inorder: ", *ino(tree)) +print("postorder: ", *post(tree)) +print("level-order:", *level(tree)) diff --git a/tasks/tree-traversal/tree-traversal.om b/tasks/tree-traversal/tree-traversal.om new file mode 100644 index 0000000..8f70046 --- /dev/null +++ b/tasks/tree-traversal/tree-traversal.om @@ -0,0 +1,118 @@ +note Rosetta Code task "Tree traversal": https://rosettacode.org/wiki/Tree_traversal +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tpre "preorder: " +text tin "inorder: " +text tpost "postorder: " +text tlevel "level-order:" +global lft ulong 10 +global rgt ulong 10 +global queue ulong 10 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note the task's tree, nodes 1..9 with 0 for no child: +note 1 +note / \ +note 2 3 +note / \ / +note 4 5 6 +note / / \ +note 7 8 9 +fn kids n ulong l ulong r ulong to void +put lft n l +put rgt n r +end +fn pre n ulong to void +if eq n 0 +ret +end +emit sp +emitu n +pre get lft n +pre get rgt n +end +fn ino n ulong to void +if eq n 0 +ret +end +ino get lft n +emit sp +emitu n +ino get rgt n +end +fn post n ulong to void +if eq n 0 +ret +end +post get lft n +post get rgt n +emit sp +emitu n +end +note level order reads nodes from a queue; each node enters it once, so 9 passes suffice +fn level n ulong to void +put queue 0 n +h = ulong 0 +t = ulong 1 +while lt h t bound 9 +v = get queue h +h = add h 1 +emit sp +emitu v +if ne get lft v 0 +put queue t get lft v +t = add t 1 +end +if ne get rgt v 0 +put queue t get rgt v +t = add t 1 +end +end +end +fn main to int +kids 1 2 3 +kids 2 4 5 +kids 3 6 0 +kids 4 7 0 +kids 6 8 9 +emit tpre +pre 1 +emit nl +emit tin +ino 1 +emit nl +emit tpost +post 1 +emit nl +emit tlevel +level 1 +emit nl +ret 0 +end diff --git a/tasks/twin-primes/expected.txt b/tasks/twin-primes/expected.txt new file mode 100644 index 0000000..9b00fed --- /dev/null +++ b/tasks/twin-primes/expected.txt @@ -0,0 +1,8 @@ +twin prime pairs below 10: 2 +twin prime pairs below 100: 8 +twin prime pairs below 1000: 35 +twin prime pairs below 10000: 205 +twin prime pairs below 100000: 1224 +twin prime pairs below 1000000: 8169 +twin prime pairs below 10000000: 58980 +twin prime pairs below 100000000: 440312 diff --git a/tasks/twin-primes/oracle.py b/tasks/twin-primes/oracle.py new file mode 100644 index 0000000..3f180dc --- /dev/null +++ b/tasks/twin-primes/oracle.py @@ -0,0 +1,14 @@ +# Independent reference for "Twin primes" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import bisect, itertools +N = 10**8 +s = bytearray([1]) * N +s[0] = s[1] = 0 +for i in range(2, 10**4): + if s[i]: + s[i * i::i] = bytes(len(range(i * i, N, i))) +ps = list(itertools.compress(range(N), s)) +# the larger member of each twin pair, so "below lim" means that member < lim +big = [p + 2 for p in ps if p + 2 < N and s[p + 2]] +for e in range(1, 9): + print(f"twin prime pairs below {10**e}: {bisect.bisect_left(big, 10**e)}") diff --git a/tasks/twin-primes/twin-primes.om b/tasks/twin-primes/twin-primes.om new file mode 100644 index 0000000..997ba43 --- /dev/null +++ b/tasks/twin-primes/twin-primes.om @@ -0,0 +1,88 @@ +note Rosetta Code task "Twin primes": https://rosettacode.org/wiki/Twin_primes +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text pre "twin prime pairs below " +text colon ": " +note odd-only bit sieve below 10^8: odd q is bit q >> 1, set when q is composite; 5 * 10^7 bits in 6250000 bytes +global cb uchar 6250000 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +fn mark q ulong to void +b = shr q 1 +put cb shr b 3 or get cb shr b 3 shl uchar 1 and b 7 +end +note isp is for odd q below 10^8 only (3, 5, 7, ...) +fn isp q ulong to bool +b = shr q 1 +ret eq and get cb shr b 3 shl uchar 1 and b 7 0 +end +note bound: odd p with p * p < 10^8 means p < 10^4; an odd prime p marks at most 10^8 / (2p) < 1.7 * 10^7 multiples, +note done as up to 1000 chunks of 999990 marks each +fn sieve to void +mark 1 +p = ulong 3 +while lt mul p p 100000000 bound 5000 +if isp p +s = mul p 2 +m = mul p p +while lt m 100000000 bound 1000 +c = ulong 0 +while and lt m 100000000 lt c 999990 bound 999999 +mark m +m = add m s +c = add c 1 +end +end +end +p = add p 2 +end +end +note a pair (p, p + 2) counts below lim when p + 2 < lim; p = 2j + 3 walks every odd p from 3 +fn main to int +sieve +lim = ulong 10 +cnt = ulong 0 +for j ulong 50000000 +p = add mul j 2 3 +while ge add p 2 lim bound 9 +emit pre +emitu lim +emit colon +emitu cnt +emit nl +lim = mul lim 10 +end +if gt lim 100000000 +break +end +if and isp p isp add p 2 +cnt = add cnt 1 +end +end +ret 0 +end diff --git a/tasks/twin-primes/wolfram.txt b/tasks/twin-primes/wolfram.txt new file mode 100644 index 0000000..2e01dd6 --- /dev/null +++ b/tasks/twin-primes/wolfram.txt @@ -0,0 +1,13 @@ +Wolfram cross-check of tasks/twin-primes/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 43773fe72f7d43cac99bcdd68f29ccac14b794d40825027aa6ea2346671224d1. +agreement: YES +query: +ps = Prime[Range[PrimePi[10^8]]]; +big = Select[ps + 2, # < 10^8 && PrimeQ[#] &]; +s = StringJoin[Table["twin prime pairs below " <> ToString[10^e] <> ": " <> ToString[Count[big, x_ /; x < 10^e]] <> "\n", {e, 1, 8}]]; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"43773fe72f7d43cac99bcdd68f29ccac14b794d40825027aa6ea2346671224d1", "twin prime pairs below 10: 2\ntwin prime pairs below 100: 8\ntwin prime pairs below 1000: 35\ntwin prime pairs below 10000: 205\ntwin prime pairs below 100000: 1224\ntwin prime pairs below 1000000: 8169\ntwin prime pairs below 10000000: 58980\ntwin prime pairs below 100000000: 440312\n"} diff --git a/tasks/url-decoding/expected.txt b/tasks/url-decoding/expected.txt new file mode 100644 index 0000000..cf07887 --- /dev/null +++ b/tasks/url-decoding/expected.txt @@ -0,0 +1,3 @@ +http://foo bar/ +google.com/search?q=`Abdu'l-Bahá +%25 diff --git a/tasks/url-decoding/oracle.py b/tasks/url-decoding/oracle.py new file mode 100644 index 0000000..00417a7 --- /dev/null +++ b/tasks/url-decoding/oracle.py @@ -0,0 +1,5 @@ +# Independent reference for "URL decoding" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import sys, urllib.parse +for s in ["http%3A%2F%2Ffoo%20bar%2F", "google.com/search?q=%60Abdu%27l-Bah%C3%A1", "%25%32%35"]: + sys.stdout.buffer.write(urllib.parse.unquote_to_bytes(s) + b"\n") diff --git a/tasks/url-decoding/url-decoding.om b/tasks/url-decoding/url-decoding.om new file mode 100644 index 0000000..971cf5b --- /dev/null +++ b/tasks/url-decoding/url-decoding.om @@ -0,0 +1,56 @@ +note Rosetta Code task "URL decoding": https://rosettacode.org/wiki/URL_decoding +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text u1 "http%3A%2F%2Ffoo%20bar%2F" +text u2 "google.com/search?q=%60Abdu%27l-Bah%C3%A1" +text u3 "%25%32%35" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note hexv gives the value of one hex digit, either case; anything else traps 9 +fn hexv c uchar to uchar +if and ge c 48 le c 57 +ret sub c 48 +end +if and ge c 65 le c 70 +ret sub c 55 +end +if and ge c 97 le c 102 +ret sub c 87 +end +trap 9 +end +note dec turns every %XY into the byte 0xXY; the result bytes are written as they are (here UTF-8) +fn dec a view uchar * to void +o = new uchar 256 0 +p = ulong 0 +i = ulong 0 +while lt i len a bound 4096 +c = get a i +if eq c 37 +c = or shl hexv get a add i 1 4 hexv get a add i 2 +i = add i 3 +else +i = add i 1 +end +put o p c +p = add p 1 +end +emit part o 0 p +emit nl +end +fn main to int +dec u1 +dec u2 +dec u3 +ret 0 +end diff --git a/tasks/url-encoding/expected.txt b/tasks/url-encoding/expected.txt new file mode 100644 index 0000000..2de8d6c --- /dev/null +++ b/tasks/url-encoding/expected.txt @@ -0,0 +1,4 @@ +http%3A%2F%2Ffoo%20bar%2F +http%3A%2F%2Ffoo%20bar%2F +mailto%3Ajoe%40example%2Ecom%3Fsubject%3Da%26b%20c%7Ed%5Fe%2Ef%2Dg +mailto%3Ajoe%40example.com%3Fsubject%3Da%26b%20c~d_e.f-g diff --git a/tasks/url-encoding/oracle.py b/tasks/url-encoding/oracle.py new file mode 100644 index 0000000..10e4ace --- /dev/null +++ b/tasks/url-encoding/oracle.py @@ -0,0 +1,6 @@ +# Independent reference for "URL encoding" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import urllib.parse +for s in ["http://foo bar/", "mailto:joe@example.com?subject=a&b c~d_e.f-g"]: + print("".join(c if c.isascii() and c.isalnum() else "%%%02X" % ord(c) for c in s)) + print(urllib.parse.quote(s, safe="")) diff --git a/tasks/url-encoding/url-encoding.om b/tasks/url-encoding/url-encoding.om new file mode 100644 index 0000000..e53ea32 --- /dev/null +++ b/tasks/url-encoding/url-encoding.om @@ -0,0 +1,56 @@ +note Rosetta Code task "URL encoding": https://rosettacode.org/wiki/URL_encoding +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text pct "%" +text hexd "0123456789ABCDEF" +text u1 "http://foo bar/" +text u2 "mailto:joe@example.com?subject=a&b c~d_e.f-g" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitc writes one byte +fn emitc c uchar to void +b = new uchar 1 0 +put b 0 c +emit b +end +note keep says whether c is written as itself: letters and digits always, - . _ ~ only in the +note RFC 3986 variant (rfc true); every other byte becomes % and two upper-case hex digits +fn keep c uchar rfc bool to bool +if or or and ge c 48 le c 57 and ge c 65 le c 90 and ge c 97 le c 122 +ret true +end +if and rfc or or eq c 45 eq c 46 or eq c 95 eq c 126 +ret true +end +ret false +end +fn enc a view uchar * rfc bool to void +for i ulong len a +c = get a i +if keep c rfc +emitc c +else +emit pct +emitc get hexd shr c 4 +emitc get hexd and c 15 +end +end +emit nl +end +fn main to int +enc u1 false +enc u1 true +enc u2 false +enc u2 true +ret 0 +end diff --git a/tasks/van-eck-sequence/expected.txt b/tasks/van-eck-sequence/expected.txt new file mode 100644 index 0000000..2c757eb --- /dev/null +++ b/tasks/van-eck-sequence/expected.txt @@ -0,0 +1,2 @@ +terms 1 to 10: 0 0 1 0 2 0 2 2 1 6 +terms 991 to 1000: 4 7 30 25 67 225 488 0 10 136 diff --git a/tasks/van-eck-sequence/oracle.py b/tasks/van-eck-sequence/oracle.py new file mode 100644 index 0000000..8996f0f --- /dev/null +++ b/tasks/van-eck-sequence/oracle.py @@ -0,0 +1,8 @@ +# Independent reference for "Van Eck sequence" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +a = [0] +for n in range(999): + prev = [m for m in range(n) if a[m] == a[n]] + a.append(n - prev[-1] if prev else 0) +print("terms 1 to 10: " + " ".join(map(str, a[:10]))) +print("terms 991 to 1000: " + " ".join(map(str, a[990:1000]))) diff --git a/tasks/van-eck-sequence/van-eck-sequence.om b/tasks/van-eck-sequence/van-eck-sequence.om new file mode 100644 index 0000000..efbec65 --- /dev/null +++ b/tasks/van-eck-sequence/van-eck-sequence.om @@ -0,0 +1,60 @@ +note Rosetta Code task "Van Eck sequence": https://rosettacode.org/wiki/Van_Eck_sequence +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text first "terms 1 to 10:" +text last "terms 991 to 1000:" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note a(0) = 0; a(n+1) = n - m where m < n is the last earlier index with a(m) = a(n), else 0 +note ls v is one more than the last index holding v (0: not seen); every term is below 1000, its index +fn main to int +a = new ulong 1000 0 +ls = new ulong 1000 0 +for n ulong 999 +v = get a n +w = get ls v +if gt w 0 +put a add n 1 sub n sub w 1 +end +put ls v add n 1 +end +emit first +for i ulong 10 +emit sp +emitu get a i +end +emit nl +emit last +for i ulong 10 +emit sp +emitu get a add 990 i +end +emit nl +ret 0 +end diff --git a/tasks/van-eck-sequence/wolfram.txt b/tasks/van-eck-sequence/wolfram.txt new file mode 100644 index 0000000..2bdd521 --- /dev/null +++ b/tasks/van-eck-sequence/wolfram.txt @@ -0,0 +1,14 @@ +Wolfram cross-check of tasks/van-eck-sequence/expected.txt +tool: WolframLanguageEvaluator (Wolfram MCP, https://agenttools.wolfram.com/mcp), 2026-10-27 +The query builds the task's whole output text from Wolfram built-ins, independently of omc and +of oracle.py, and returns {SHA-256 of that text, the text}. It agrees when that SHA-256 equals +sha256(expected.txt) = 6ac868c55af375dff6884e0129b20bb64266b9e904f2f0258fb56a4d03454b1d. +agreement: YES +query: +next[l_] := Append[l, With[{p = Flatten[Position[Most[l], Last[l]]]}, If[p === {}, 0, Length[l] - Last[p]]]]; +a = Nest[next, {0}, 999]; +s = "terms 1 to 10: " <> StringRiffle[ToString /@ a[[1 ;; 10]], " "] <> "\n" <> + "terms 991 to 1000: " <> StringRiffle[ToString /@ a[[991 ;; 1000]], " "] <> "\n"; +{Hash[s, "SHA256", "HexString"], s} +result: +Out[1]= {"6ac868c55af375dff6884e0129b20bb64266b9e904f2f0258fb56a4d03454b1d", "terms 1 to 10: 0 0 1 0 2 0 2 2 1 6\nterms 991 to 1000: 4 7 30 25 67 225 488 0 10 136\n"} diff --git a/tasks/vigenere-cipher/expected.txt b/tasks/vigenere-cipher/expected.txt new file mode 100644 index 0000000..0690050 --- /dev/null +++ b/tasks/vigenere-cipher/expected.txt @@ -0,0 +1,3 @@ +BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH +WMCEEIKLGRPIFVMEUGXQPWQVIOIAVEYXUEKFKBTALVXTGAFXYEVKPAGY +BEWARETHEJABBERWOCKMYSONTHEJAWSTHATBITETHECLAWSTHATCATCH diff --git a/tasks/vigenere-cipher/oracle.py b/tasks/vigenere-cipher/oracle.py new file mode 100644 index 0000000..bb2c57b --- /dev/null +++ b/tasks/vigenere-cipher/oracle.py @@ -0,0 +1,10 @@ +# Independent reference for "Vigenère cipher" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +key = "VIGENERECIPHER" +msg = "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!" +t = "".join(c.upper() for c in msg if c.isalpha()) +e = "".join(chr((ord(c) - 65 + ord(key[i % len(key)]) - 65) % 26 + 65) for i, c in enumerate(t)) +d = "".join(chr((ord(c) - 65 - (ord(key[i % len(key)]) - 65)) % 26 + 65) for i, c in enumerate(e)) +print(t) +print(e) +print(d) diff --git a/tasks/vigenere-cipher/vigenere-cipher.om b/tasks/vigenere-cipher/vigenere-cipher.om new file mode 100644 index 0000000..2b946c5 --- /dev/null +++ b/tasks/vigenere-cipher/vigenere-cipher.om @@ -0,0 +1,60 @@ +note Rosetta Code task "Vigenère cipher": https://rosettacode.org/wiki/Vigen%C3%A8re_cipher +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text key "VIGENERECIPHER" +text msg "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!" +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note clean keeps the letters of a, upper-cased, in r and returns how many +fn clean a view uchar * r edit uchar * to ulong +n = ulong 0 +for i ulong len a +c = get a i +if and ge c 97 le c 122 +c = sub c 32 +end +if and ge c 65 le c 90 +put r n c +n = add n 1 +end +end +ret n +end +note vig adds (dir 0) or subtracts (dir 1) the key letters mod 26, the key repeating +fn vig a view uchar * r edit uchar * dir ulong to void +for i ulong len a +k = sub get key rem i len key 65 +c = sub get a i 65 +if eq dir 0 +c = rem add c k 26 +else +c = rem sub add c 26 k 26 +end +put r i add c 65 +end +end +fn main to int +t = new uchar 256 0 +e = new uchar 256 0 +d = new uchar 256 0 +n = clean msg t +emit part t 0 n +emit nl +vig part t 0 n part e 0 n 0 +emit part e 0 n +emit nl +vig part e 0 n part d 0 n 1 +emit part d 0 n +emit nl +ret 0 +end diff --git a/tasks/water-collected-between-towers/expected.txt b/tasks/water-collected-between-towers/expected.txt new file mode 100644 index 0000000..0a02a30 --- /dev/null +++ b/tasks/water-collected-between-towers/expected.txt @@ -0,0 +1,14 @@ +1 5 3 7 2 +water: 2 +5 3 7 2 6 4 5 9 1 2 +water: 14 +2 6 3 5 2 8 1 4 2 2 5 3 5 7 4 1 +water: 35 +5 5 5 5 +water: 0 +5 6 7 8 +water: 0 +8 7 7 6 +water: 0 +6 7 10 7 6 +water: 0 diff --git a/tasks/water-collected-between-towers/oracle.py b/tasks/water-collected-between-towers/oracle.py new file mode 100644 index 0000000..5e764ef --- /dev/null +++ b/tasks/water-collected-between-towers/oracle.py @@ -0,0 +1,10 @@ +# Independent reference for "Water collected between towers" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +for towers in [[1, 5, 3, 7, 2], [5, 3, 7, 2, 6, 4, 5, 9, 1, 2], [2, 6, 3, 5, 2, 8, 1, 4, 2, 2, 5, 3, 5, 7, 4, 1], [5, 5, 5, 5], [5, 6, 7, 8], [8, 7, 7, 6], [6, 7, 10, 7, 6]]: + print(*towers) + # fill level by level: at height h, count the empty cells strictly between the outermost towers >= h + water = 0 + for h in range(1, max(towers) + 1): + idx = [i for i, t in enumerate(towers) if t >= h] + water += sum(1 for i in range(idx[0], idx[-1] + 1) if towers[i] < h) + print("water:", water) diff --git a/tasks/water-collected-between-towers/water-collected-between-towers.om b/tasks/water-collected-between-towers/water-collected-between-towers.om new file mode 100644 index 0000000..b3ec1b6 --- /dev/null +++ b/tasks/water-collected-between-towers/water-collected-between-towers.om @@ -0,0 +1,156 @@ +note Rosetta Code task "Water collected between towers": https://rosettacode.org/wiki/Water_collected_between_towers +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text d0 "1 5 3 7 2" +text d1 "5 3 7 2 6 4 5 9 1 2" +text d2 "2 6 3 5 2 8 1 4 2 2 5 3 5 7 4 1" +text d3 "5 5 5 5" +text d4 "5 6 7 8" +text d5 "8 7 7 6" +text d6 "6 7 10 7 6" +text twater "water: " +global vals long 16 +global lmax long 16 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow +fn emiti n long to void +if lt n 0 +emit minus +emitu add bitcast ulong not n 1 +else +emitu ulong n +end +end +note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and +note returns how many; one pass over s, so the loop is bounded by len s +fn parse s view uchar * to ulong +n = ulong 0 +v = long 0 +sg = long 1 +dig = false +for i ulong len s +c = get s i +if eq c 45 +sg = long -1 +else +if and ge c 48 le c 57 +v = add mul v 10 long sub c 48 +dig = true +else +if dig +put vals n mul sg v +n = add n 1 +end +v = long 0 +sg = long 1 +dig = false +end +end +end +if dig +put vals n mul sg v +n = add n 1 +end +ret n +end +note show writes vals[0..n-1] separated by spaces, then a newline +fn show n ulong to void +for i ulong n +if gt i 0 +emit sp +end +emiti get vals i +end +emit nl +end +note water: a column holds min(highest to its left, highest to its right) minus its own height; +note lmax[i] is the running maximum from the left, the right maximum is kept while walking back +fn water n ulong to long +m = long 0 +for i ulong n +if gt get vals i m +m = get vals i +end +put lmax i m +end +r = long 0 +w = long 0 +for x ulong n +i = sub sub n x 1 +if gt get vals i r +r = get vals i +end +h = get lmax i +if lt r h +h = r +end +w = add w sub h get vals i +end +ret w +end +fn main to int +n = parse d0 +show n +emit twater +emiti water n +emit nl +n = parse d1 +show n +emit twater +emiti water n +emit nl +n = parse d2 +show n +emit twater +emiti water n +emit nl +n = parse d3 +show n +emit twater +emiti water n +emit nl +n = parse d4 +show n +emit twater +emiti water n +emit nl +n = parse d5 +show n +emit twater +emiti water n +emit nl +n = parse d6 +show n +emit twater +emiti water n +emit nl +ret 0 +end diff --git a/tasks/wireworld/expected.txt b/tasks/wireworld/expected.txt new file mode 100644 index 0000000..82cc68c --- /dev/null +++ b/tasks/wireworld/expected.txt @@ -0,0 +1,60 @@ +generation 0 +tH......... +. . + ... +. . +Ht.. ...... +generation 1 +.tH........ +H . + ... +H . +t... ...... +generation 2 +H.tH....... +t . + ... +t . +.H.. ...... +generation 3 +tH.tH...... +. H + ... +. . +HtH. ...... +generation 4 +.tH.tH..... +H t + HHH +H . +t.tH ...... +generation 5 +H.tH.tH.... +t . + ttt +t . +.H.t ...... +generation 6 +tH.tH.tH... +. H + ... +. . +HtH. ...... +generation 7 +.tH.tH.tH.. +H t + HHH +H . +t.tH ...... +generation 8 +H.tH.tH.tH. +t . + ttt +t . +.H.t ...... +generation 9 +tH.tH.tH.tH +. H + ... +. . +HtH. ...... diff --git a/tasks/wireworld/oracle.py b/tasks/wireworld/oracle.py new file mode 100644 index 0000000..967983e --- /dev/null +++ b/tasks/wireworld/oracle.py @@ -0,0 +1,14 @@ +# Independent reference for "Wireworld" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +rows = ["tH.........", ". . ", " ... ", ". . ", "Ht.. ......"] +def step(g): + def heads(r, c): + return sum(1 for dr in (-1, 0, 1) for dc in (-1, 0, 1) + if (dr or dc) and 0 <= r + dr < len(g) and 0 <= c + dc < len(g[0]) and g[r + dr][c + dc] == "H") + nxt = {"H": "t", "t": ".", " ": " "} + return ["".join(nxt.get(ch) or ("H" if heads(r, c) in (1, 2) else ".") for c, ch in enumerate(row)) + for r, row in enumerate(g)] +for gen in range(10): + print("generation", gen) + print("\n".join(rows)) + rows = step(rows) diff --git a/tasks/wireworld/wireworld.om b/tasks/wireworld/wireworld.om new file mode 100644 index 0000000..9fea5b5 --- /dev/null +++ b/tasks/wireworld/wireworld.om @@ -0,0 +1,103 @@ +note Rosetta Code task "Wireworld": https://rosettacode.org/wiki/Wireworld +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +text tstart "tH.........|. . | ... |. . |Ht.. ......" +text tgen "generation " +global cur uchar 55 +global nxt uchar 55 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitb writes the single byte c +fn emitb c ulong to void +d = new uchar 1 0 +put d 0 uchar c +emit d +end +note the 5 x 11 board from the task: H electron head, t tail, . conductor, space empty; a head becomes a +note tail, a tail a conductor, and a conductor a head when 1 or 2 of its 8 neighbours are heads +fn heads r long c long to ulong +n = ulong 0 +for a ulong 3 +for b ulong 3 +y = sub add r long a 1 +x = sub add c long b 1 +if and and ge y 0 lt y 5 and ge x 0 lt x 11 +if eq get cur add mul ulong y 11 ulong x 72 +n = add n 1 +end +end +end +end +ret n +end +fn rule k ulong n ulong to uchar +v = get cur k +if eq v 72 +ret 116 +end +if eq v 116 +ret 46 +end +if and eq v 46 or eq n 1 eq n 2 +ret 72 +end +ret v +end +fn step to void +for r ulong 5 +for c ulong 11 +k = add mul r 11 c +put nxt k rule k heads long r long c +end +end +for i ulong 55 +put cur i get nxt i +end +end +fn main to int +for r ulong 5 +for c ulong 11 +put cur add mul r 11 c get tstart add mul r 12 c +end +end +for g ulong 10 +emit tgen +emitu g +emit nl +for i ulong 55 +emitb ulong get cur i +if eq rem i 11 10 +emit nl +end +end +step +end +ret 0 +end diff --git a/tasks/word-wrap/expected.txt b/tasks/word-wrap/expected.txt new file mode 100644 index 0000000..ab900de --- /dev/null +++ b/tasks/word-wrap/expected.txt @@ -0,0 +1,25 @@ +In olden times when wishing still helped one, there lived a king whose +daughters were all beautiful, but the youngest was so beautiful that the +sun itself, which has seen so much, was astonished whenever it shone in +her face. Close by the king's castle lay a great dark forest, and under +an old lime-tree in the forest was a well, and when the day was very +warm, the king's child went out into the forest and sat down by the side +of the cool fountain, and when she was bored she took a golden ball, and +threw it up on high and caught it, and this ball was her favorite +plaything. +---- +In olden times when wishing still helped +one, there lived a king whose daughters +were all beautiful, but the youngest was +so beautiful that the sun itself, which +has seen so much, was astonished +whenever it shone in her face. Close by +the king's castle lay a great dark +forest, and under an old lime-tree in +the forest was a well, and when the day +was very warm, the king's child went out +into the forest and sat down by the side +of the cool fountain, and when she was +bored she took a golden ball, and threw +it up on high and caught it, and this +ball was her favorite plaything. diff --git a/tasks/word-wrap/oracle.py b/tasks/word-wrap/oracle.py new file mode 100644 index 0000000..56cdb77 --- /dev/null +++ b/tasks/word-wrap/oracle.py @@ -0,0 +1,7 @@ +# Independent reference for "Word wrap" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +import textwrap +para = "In olden times when wishing still helped one, there lived a king whose daughters were all beautiful, but the youngest was so beautiful that the sun itself, which has seen so much, was astonished whenever it shone in her face. Close by the king's castle lay a great dark forest, and under an old lime-tree in the forest was a well, and when the day was very warm, the king's child went out into the forest and sat down by the side of the cool fountain, and when she was bored she took a golden ball, and threw it up on high and caught it, and this ball was her favorite plaything." +print("\n".join(textwrap.wrap(para, 72, break_on_hyphens=False, break_long_words=False))) +print("----") +print("\n".join(textwrap.wrap(para, 40, break_on_hyphens=False, break_long_words=False))) diff --git a/tasks/word-wrap/word-wrap.om b/tasks/word-wrap/word-wrap.om new file mode 100644 index 0000000..ec7bc2d --- /dev/null +++ b/tasks/word-wrap/word-wrap.om @@ -0,0 +1,57 @@ +note Rosetta Code task "Word wrap": https://rosettacode.org/wiki/Word_wrap +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text rule "----\n" +text para "In olden times when wishing still helped one, there lived a king whose daughters were all beautiful, but the youngest was so beautiful that the sun itself, which has seen so much, was astonished whenever it shone in her face. Close by the king's castle lay a great dark forest, and under an old lime-tree in the forest was a well, and when the day was very warm, the king's child went out into the forest and sat down by the side of the cool fountain, and when she was bored she took a golden ball, and threw it up on high and caught it, and this ball was her favorite plaything." +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note greedy wrap: a word goes on the current line when it still fits in width w, else starts a new one +fn wrap a view uchar * w ulong to void +cur = ulong 0 +i = ulong 0 +while lt i len a bound 4096 +if eq get a i 32 +i = add i 1 +else +j = i +while lt j len a bound 4096 +if eq get a j 32 +break +end +j = add j 1 +end +l = sub j i +if eq cur 0 +cur = l +else +if le add add cur 1 l w +emit sp +cur = add add cur 1 l +else +emit nl +cur = l +end +end +emit part a i l +i = j +end +end +emit nl +end +fn main to int +wrap para 72 +emit rule +wrap para 40 +ret 0 +end diff --git a/tasks/zig-zag-matrix/expected.txt b/tasks/zig-zag-matrix/expected.txt new file mode 100644 index 0000000..b941087 --- /dev/null +++ b/tasks/zig-zag-matrix/expected.txt @@ -0,0 +1,12 @@ + 0 1 5 6 14 + 2 4 7 13 15 + 3 8 12 16 21 + 9 11 17 20 22 + 10 18 19 23 24 + + 0 1 5 6 14 15 + 2 4 7 13 16 25 + 3 8 12 17 24 26 + 9 11 18 23 27 32 + 10 19 22 28 31 33 + 20 21 29 30 34 35 diff --git a/tasks/zig-zag-matrix/oracle.py b/tasks/zig-zag-matrix/oracle.py new file mode 100644 index 0000000..018854e --- /dev/null +++ b/tasks/zig-zag-matrix/oracle.py @@ -0,0 +1,15 @@ +# Independent reference for "Zig-zag matrix" (Python 3, standard library only). +# expected.txt is this program's stdout; omc never produced it. +# sort the cells by (diagonal, position along it in the direction of travel) +def zigzag(n): + cells = sorted(((i, j) for i in range(n) for j in range(n)), + key=lambda p: (p[0] + p[1], -p[0] if (p[0] + p[1]) % 2 == 0 else p[0])) + m = [[0] * n for _ in range(n)] + for k, (i, j) in enumerate(cells): + m[i][j] = k + return m +for t, n in enumerate((5, 6)): + if t: + print() + for row in zigzag(n): + print("".join(f"{v:3}" for v in row)) diff --git a/tasks/zig-zag-matrix/zig-zag-matrix.om b/tasks/zig-zag-matrix/zig-zag-matrix.om new file mode 100644 index 0000000..cf07a2d --- /dev/null +++ b/tasks/zig-zag-matrix/zig-zag-matrix.om @@ -0,0 +1,84 @@ +note Rosetta Code task "Zig-zag matrix": https://rosettacode.org/wiki/Zig-zag_matrix +note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +om2 +text nl "\n" +text sp " " +text minus "-" +global grid ulong 64 +note emit writes every byte of a to stdout; a short write is retried, an error exits 1 +fn emit a view uchar * to void +o = ulong 0 +while lt o len a bound 4096 +k = write 1 a o sub len a o +if le k 0 +exit 1 +end +o = add o ulong k +end +end +note emitu writes n in decimal; 20 digits hold every ulong +fn emitu n ulong to void +d = new uchar 20 0 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +emit part d i sub 20 i +end +note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) +fn emitw n ulong w ulong to void +d = new uchar 20 32 +i = ulong 20 +v = n +while true bound 20 +i = sub i 1 +put d i uchar add rem v 10 48 +v = div v 10 +if eq v 0 +break +end +end +if gt w sub 20 i +i = sub 20 w +end +emit part d i sub 20 i +end +note number the cells along the anti-diagonals d = i + j; even diagonals run up and to the right +note (i falling), odd ones down and to the left (i rising) +fn zigzag n ulong to void +k = ulong 0 +for d ulong sub mul n 2 1 +for x ulong add d 1 +i = x +if eq rem d 2 0 +i = sub d x +end +if and lt i n le sub d i sub n 1 +put grid add mul i n sub d i k +k = add k 1 +end +end +end +end +fn show n ulong to void +for i ulong n +for j ulong n +emitw get grid add mul i n j 3 +end +emit nl +end +end +fn main to int +zigzag 5 +show 5 +emit nl +zigzag 6 +show 6 +ret 0 +end