omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
GitHub aien-dev/omega-rosetta#2 (code only)
Fetch it
curl -fsSL https://interplane.io/drakestapleton/omega-rosetta/patches/b37c5bf7eeba02d2.bundle -o b37c5bf7eeba02d2.bundle git fetch b37c5bf7eeba02d2.bundle refs/pull/2/head
Provenance
github-import 1
aien-dev/omega-rosetta
2
a5b7330a32eb3c2d62dfcd804483d79decbe64a7
{"kind":"github-import","owner":"aien-dev","repo":"omega-rosetta","number":2,"url":"https://github.com/aien-dev/omega-rosetta/pull/2","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:39:59Z","metadata":"unavailable","state_derived":true}
Changes
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/<name>/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/<name>/<name>.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/<name>/<name>.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/<name>/.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/<name>` or `exercises/<name>` | +| `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/<name>/<name>.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/<name>/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 <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/<name>) and per exercise (exercises/<name>), 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +1 4 9 16 25 36 49 64 81 100 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +A yes +BARK yes +BOOK no +TREAT yes +COMMON no +SQUAD yes +CONFUSE yes +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +deficient: 15043 +perfect: 4 +abundant: 4953 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +220 284 +1184 1210 +2620 2924 +5020 5564 +6232 6368 +10744 10856 +12285 14595 +17296 18416 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +first 20 anti-primes: 1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +1 2 3 4 5 6 7 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +TQ== +TWE= +TWFu +SGVsbG8sIFdvcmxkIQ== +VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g= +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +5 3 1 7 4 1 1 20 0 9 +0 1 1 1 3 4 5 7 9 20 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +3 -1 4 1 5 9 2 +-1 1 2 3 4 5 9 +shuffles: 2314 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +Esp bftnv mczhy qzi Ufxapo zgpc esp wlkj Ozr +The quick brown fox Jumped over the lazy Dog +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +a 97 +97 a +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +5 1 -4 2 8 0 2 -9 13 7 3 +-9 -4 0 1 2 2 3 5 7 8 13 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +iced iced +iced jam +iced plain +jam jam +jam plain +plain plain +choices of 3 from 10 kinds: 220 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +{} +{ABC} +{ABC and DEF} +{ABC, DEF, G and H} +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +generation 0 +... +### +... +generation 1 +.#. +.#. +.#. +generation 2 +... +### +... + +generation 0 +.#...... +... +........ +..#..... +...#.... +.###.... +........ +........ +........ +........ +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +source: Hello +copy: Jello +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +vowels 22, consonants 31 +distinct vowels 5, consonants 13 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +3 +2 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +ways to make 100 cents from 1, 5, 10, 25: 242 +ways to make 100000 cents from 1, 5, 10, 25, 50, 100: 13398445413854501 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +<<<>>> 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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +<<<>>> 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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +<<<>>> 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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +"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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +rule 90 +...............#............... +..............#.#.............. +.............#...#............. +............#.#.#.#............ +...........#.......#........... +..........#.#.....#.#.......... +.........#...#...#...#......... +........#.#.#.#.#.#.#.#........ +.......#...............#....... +......#.#.............#.#...... +.....#...#...........#...#..... +....#.#.#.#.........#.#.#.#.... +...#.......#.......#.......#... +..#.#.....#.#.....#.#.....#.#.. +... +.......#######.#............... +......##.....###............... +.....###....##.#............... +....##.#...#####............... +...#####..##...#............... +..##...#.###..##............... +.###..####.#.###............... +##.#.##..#####.#............... +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +s is empty +s is not empty: abc +s is empty +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +-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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +17 34 KEPT +8 68 STRUCK +4 136 STRUCK +2 272 STRUCK +1 544 KEPT +17 x 34 = 578 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +C(5, 3) = 10 +C(10, 0) = 1 +C(10, 10) = 1 +C(33, 17) = 1166803110 +C(60, 30) = 118264581564861424 +C(67, 33) = 14226520737620288370 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> + 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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +abcdefghijklmnopqrstuvwxyz +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +34 2 -17 9 9 0 51 -3 28 6 +-17 -3 0 2 6 9 9 28 34 51 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +-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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +first 20: 1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36 +1691st: 2125764000 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +first 8 happy numbers: 1 7 10 13 19 23 28 31 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +Goodbye, World! +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +978-0596528126: good +978-0596528120: bad +978-1788399081: good +978-1788399083: bad +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +aAAbbbb aA 3 +ZZ z 0 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +map +compass +water +sandwich +glucose +banana +suntan cream +waterproof trousers +waterproof overclothes +note-case +sunglasses +socks +total weight 396 +total value 1030 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> + 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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +steps until the ant leaves the grid: 11669 +black cells, rows and columns 28..78 0..79 +..........................................##..############..##.................. +.........................................#..####..........#..##................. +........................................###...##............##.#................ +........................................#.#..#.........#..#....#................ +....................................##..##.#.#.........###.......#.............. +.................................###.#..#...#.....#.....##.##..###.............. +..................................#.#..###..##.####.##...#.#..#.##..##.......... +..................................#.###.##..#.##..###.#.#.....###...###......... +................................#.....#...#####.#.#..####..#...###.#.#.#........ +...............................###.##...#.####..##.##.######.#.###.#...#........ +...............................#.###.#.##.#.#.##.##.##.#...#####.###.##......... +...................................#.#...#.##.###...#...#.#..####....#.##....... +................................#..#.........##.##...#..##.....##.#.....##...... +... +.###...#..##........................................................##......##.. +#..###..#.#..........................................................##......... +.###.#..##...................................................................... +#.#.#.#.#....................................................................... +.####.##........................................................................ +.#.##.#......................................................................... +..####.......................................................................... +...##........................................................................... +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +kitten sitting 3 +rosettacode raisethysword 8 +saturday sunday 3 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +[interspecies|interstellar|interstate] -> "inters" +[throne|throne] -> "throne" +[throne|dungeon] -> "" +[throne||throne] -> "" +[cheese] -> "cheese" +[] -> "" +[prefix|suffix] -> "" +[foo|foobar] -> "foo" +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +tsitest +1234 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +1 +11 +21 +1211 +111221 +312211 +13112221 +1113213211 +31131211131221 +13211311123113112211 +11131221133112132113212221 +3113112221232112111312211312113211 +1321132132111213122112311311222113111221131221 +11131221131211131231121113112221121321132132211331222113112211 +311311222113111231131112132112311321322112111312211312111322212311322113212221 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +1 +13 +9 +1 +16 +13 +17 +0 +5 +5 +12 +14 +14 +8 +13 +... +0 +5 +15 +14 +6 +15 +14 +10 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +1, 2, 3, 4, 5 +6, 7, 8, 9, 10 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +1 +2 +3 +4 +5 +6 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +10 +9 +8 +7 +6 +5 +4 +3 +2 +1 +0 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +2, 4, 6, 8, who do we appreciate? +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +* +** +*** +**** +***** +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +apple +banana +cherry +date +elderberry +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +1, 2, 3, 4, 5, 6, 7, 8, 9, 10 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +1024 +512 +256 +128 +64 +32 +16 +8 +4 +2 +1 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +Mersenne primes 2^p - 1 for p <= 63, by the Lucas-Lehmer test: +M2 M3 M5 M7 M13 M17 M19 M31 M61 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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) +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +49927398716 valid +49927398717 invalid +1234567812345678 invalid +1234567812345670 valid +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> + 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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> + 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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> + -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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +maximum path sum: 321 +maximum path sum: 1320 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> ++---+---+---+---+---+---+---+---+---+---+ +| | | | ++ +---+---+---+---+ + + +---+---+ +| | | | | ++---+---+---+ + +---+ +---+---+ + +| | | | | ++ + + +---+---+---+---+---+ + + +| | | | | | ++ +---+---+ +---+---+ + +---+---+ +| | | | | | ++---+ + +---+ +---+ +---+---+ + +| | | ++---+---+---+---+---+---+---+---+---+---+ +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> ++---+---+---+---+---+---+---+---+---+---+ +| * | | | ++ +---+---+---+---+ + + +---+---+ +| * * * * | | | | ++---+---+---+ + +---+ +---+---+ + +| * * | * * | | | ++ + + +---+---+---+---+---+ + + +| * | * * | * * * * | | | ++ +---+---+ +---+---+ + +---+---+ +| * * | * * | | * | | ++---+ + +---+ +---+ +---+---+ + +| * * | * * * * | ++---+---+---+---+---+---+---+---+---+---+ +cells on the path from top left to bottom right: 27 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +d41d8cd98f00b204e9800998ecf8427e +9e107d9d372bb6826bd81d3542a419d6 +e38ca1d920c4b8b8d3946b2c72f01680 +cca1bf66b09554e10f837838c3d3efb1 +0f5c6c4e740bfcc08c3c26ccb2673d46 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +a^b mod 10^40 = 1527229998585248450016808958343740453059 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +generation 0: _###_##_#_#_#_#__#__ +generation 1: _#_#####_#_#_#______ +generation 2: __##___##_#_#_______ +generation 3: __##___###_#________ +generation 4: __##___#_##_________ +generation 5: __##____###_________ +generation 6: __##____#_#_________ +generation 7: __##_____#__________ +generation 8: __##________________ +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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}) +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +aegilops +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +"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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +23 -7 61 4 0 38 15 -7 90 2 11 +-7 -7 0 2 4 11 15 23 38 61 90 +flips: 16 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +"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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> + 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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +4 65 2 -31 0 99 83 782 1 4 -31 18 +-31 -31 0 1 2 4 4 18 65 83 99 782 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +8 -2 15 3 0 6 +-2 0 3 6 8 15 +permutations tried: 209 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +lasrever esarhp edoc attesor +attesor edoc esarhp lasrever +reversal phrase code rosetta +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +{} +{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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +170 45 75 -90 -802 24 2 66 0 1999 -1 333 +-802 -90 -1 0 2 24 45 66 75 170 333 1999 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +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 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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 +</syntaxhighlight> +{{out}} +<pre> +4 9 4 1 -3 9 0 1 7 4 -3 12 0 9 5 +4 9 1 -3 0 7 12 5 +</pre> 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 (<code>emit</code> and friends) are part of the program. +<syntaxhighlight lang="text"> +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