task_url
stringlengths
30
116
task_name
stringlengths
2
86
task_description
stringlengths
0
14.4k
language_url
stringlengths
2
53
language_name
stringlengths
1
52
code
stringlengths
0
61.9k
http://rosettacode.org/wiki/Letter_frequency
Letter frequency
Task Open a text file and count the occurrences of each letter. Some of these programs count all characters (including punctuation), but some only count letters A to Z. Other tasks related to string operations: Metrics Array length String length Copy a string Empty string  (assignment) Counting Word frequen...
#8080_Assembly
8080 Assembly
bdos: equ 5 ; CP/M syscalls putch: equ 2 ; Print a character puts: equ 9 ; Print a string fopen: equ 15 ; Open a file fread: equ 20 ; Read a file fcb: equ 5ch ; FCB for file given on command line dma: equ 80h ; Default DMA org 100h ; CP/M loads the program starting at page 1 ;; Zero out pages two and three ...
http://rosettacode.org/wiki/Loop_over_multiple_arrays_simultaneously
Loop over multiple arrays simultaneously
Task Loop over multiple arrays   (or lists or tuples or whatever they're called in your language)   and display the   i th   element of each. Use your language's   "for each"   loop if it has one, otherwise iterate through the collection in order with some other loop. For this example, loop over the arrays: ...
#VBA
VBA
' Loop over multiple arrays simultaneously - VBA - 08/02/2021 Sub Main() a = Array("a","b","c") b = Array("A","B","C") c = Array(1,2,3) For i = LBound(a) To UBound(a) buf = buf & vbCrLf & a(i) & b(i) & c(i) Next Debug.Print Mid(buf,3) End Sub
http://rosettacode.org/wiki/List_comprehensions
List comprehensions
A list comprehension is a special syntax in some programming languages to describe lists. It is similar to the way mathematicians describe sets, with a set comprehension, hence the name. Some attributes of a list comprehension are: They should be distinct from (nested) for loops and the use of map and filter functio...
#Java
Java
// Boilerplate import java.util.Arrays; import java.util.List; import static java.util.function.Function.identity; import static java.util.stream.Collectors.toList; import static java.util.stream.IntStream.range; public interface PythagComp{ static void main(String... args){ System.out.println(run(20)); ...
http://rosettacode.org/wiki/Loops/For
Loops/For
“For”   loops are used to make some block of code be iterated a number of times, setting a variable or parameter to a monotonically increasing integer value for each execution of the block of code. Common extensions of this allow other counting patterns or iterating over abstract structures other than the integers. ...
#TorqueScript
TorqueScript
for(%i = 0; %i < 5; %i++) { for(%x = %i; %x < 5; %x++) {  %string = %string @ "*"; echo(%string); } }
http://rosettacode.org/wiki/Loops/For
Loops/For
“For”   loops are used to make some block of code be iterated a number of times, setting a variable or parameter to a monotonically increasing integer value for each execution of the block of code. Common extensions of this allow other counting patterns or iterating over abstract structures other than the integers. ...
#TransFORTH
TransFORTH
: PRINTSTARS ( ROWS -- ) 1 + 1 DO I 0 DO PRINT " * " LOOP CR LOOP ; 5 PRINTSTARS
http://rosettacode.org/wiki/Left_factorials
Left factorials
Left factorials,   !n,   may refer to either   subfactorials   or to   factorial sums; the same notation can be confusingly seen being used for the two different definitions. Sometimes,   subfactorials   (also known as derangements)   may use any of the notations:   !n`   !n   n¡ (It may not be vis...
#C.23
C#
  using System; using System.Numerics;   namespace LeftFactorial { class Program { static void Main(string[] args) { for (int i = 0; i <= 10; i++) { Console.WriteLine(string.Format("!{0} = {1}", i, LeftFactorial(i))); }   for (int j...
http://rosettacode.org/wiki/Linear_congruential_generator
Linear congruential generator
The linear congruential generator is a very simple example of a random number generator. All linear congruential generators use this formula: r n + 1 = a × r n + c ( mod m ) {\displaystyle r_{n+1}=a\times r_{n}+c{\pmod {m}}} Where: r 0 {\displaystyle r_{0}} is a seed. r 1 ...
#Bracmat
Bracmat
( 2^31:?RANDMAX & 2^-16:?rshift & (randBSD=mod$(!seed*1103515245+12345.!RANDMAX):?seed) & ( randMS = div $ ((mod$(!seed*214013+2531011.!RANDMAX):?seed)*!rshift.1) ) & out$\nBSD & 0:?seed & 0:?i & whl'(1+!i:~>10:?i&out$!randBSD) & out$\nMicrosoft & 0:?seed & 0:?i & whl'(1+!i:~>10:?i&out$!randMS) )
http://rosettacode.org/wiki/Leonardo_numbers
Leonardo numbers
Leonardo numbers   are also known as the   Leonardo series. The   Leonardo numbers   are a sequence of numbers defined by: L(0) = 1 [1st equation] L(1) = 1 [2nd equation] L(n) = L(n-1) + L(n-2) + 1 ...
#JavaScript
JavaScript
const leoNum = (c, l0 = 1, l1 = 1, add = 1) => new Array(c).fill(add).reduce( (p, c, i) => i > 1 ? ( p.push(p[i - 1] + p[i - 2] + c) && p ) : p, [l0, l1] );   console.log(leoNum(25)); console.log(leoNum(25, 0, 1, 0));
http://rosettacode.org/wiki/Levenshtein_distance
Levenshtein distance
This page uses content from Wikipedia. The original article was at Levenshtein distance. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) In information theory and computer science, the Levenshtein ...
#AppleScript
AppleScript
set dist to findLevenshteinDistance for "sunday" against "saturday" to findLevenshteinDistance for s1 against s2 script o property l : s1 property m : s2 end script if s1 = s2 then return 0 set ll to length of s1 set lm to length of s2 if ll = 0 then return lm if lm = 0 then ...
http://rosettacode.org/wiki/Letter_frequency
Letter frequency
Task Open a text file and count the occurrences of each letter. Some of these programs count all characters (including punctuation), but some only count letters A to Z. Other tasks related to string operations: Metrics Array length String length Copy a string Empty string  (assignment) Counting Word frequen...
#8th
8th
    needs map/iter   8 var, numtasks var tasks   0 args "Give filename as param" thrownull f:slurp >s s:len numtasks @ n:/ n:ceil 1 a:close s:/ a:len numtasks ! constant work   m:new constant result   : print-character-count \ m s -- m swap over m:@ rot s:size 1 n:= over -1 s:@ nip 31 n:< and if -1 s:@ n...
http://rosettacode.org/wiki/Loop_over_multiple_arrays_simultaneously
Loop over multiple arrays simultaneously
Task Loop over multiple arrays   (or lists or tuples or whatever they're called in your language)   and display the   i th   element of each. Use your language's   "for each"   loop if it has one, otherwise iterate through the collection in order with some other loop. For this example, loop over the arrays: ...
#VBScript
VBScript
' Loop over multiple arrays simultaneously - VBScript - 08/02/2021 a = Array("a","b","c") b = Array("A","B","C") c = Array(1,2,3) For i = LBound(a) To UBound(a) buf = buf & vbCrLf & a(i) & b(i) & c(i) Next WScript.Echo Mid(buf,3)
http://rosettacode.org/wiki/List_comprehensions
List comprehensions
A list comprehension is a special syntax in some programming languages to describe lists. It is similar to the way mathematicians describe sets, with a set comprehension, hence the name. Some attributes of a list comprehension are: They should be distinct from (nested) for loops and the use of map and filter functio...
#JavaScript
JavaScript
// USING A LIST MONAD DIRECTLY, WITHOUT SPECIAL SYNTAX FOR LIST COMPREHENSIONS   (function (n) {   return mb(r(1, n), function (x) { // x <- [1..n] return mb(r(1 + x, n), function (y) { // y <- [1+x..n] return mb(r(1 + y, n), function (z) { // z <- [1+y..n]   return x * x + y * y === z * z ? [...
http://rosettacode.org/wiki/Loops/For
Loops/For
“For”   loops are used to make some block of code be iterated a number of times, setting a variable or parameter to a monotonically increasing integer value for each execution of the block of code. Common extensions of this allow other counting patterns or iterating over abstract structures other than the integers. ...
#TUSCRIPT
TUSCRIPT
  $$ MODE TUSCRIPT m="" LOOP n=1,5 m=APPEND (m,"","*") PRINT m ENDLOOP  
http://rosettacode.org/wiki/Loops/For
Loops/For
“For”   loops are used to make some block of code be iterated a number of times, setting a variable or parameter to a monotonically increasing integer value for each execution of the block of code. Common extensions of this allow other counting patterns or iterating over abstract structures other than the integers. ...
#TypeScript
TypeScript
for (let i: number = 0; i < 5; ++i) { let line: string = "" for(let j: number = 0; j <= i; ++j) { line += "*" } console.log(line) }  
http://rosettacode.org/wiki/Left_factorials
Left factorials
Left factorials,   !n,   may refer to either   subfactorials   or to   factorial sums; the same notation can be confusingly seen being used for the two different definitions. Sometimes,   subfactorials   (also known as derangements)   may use any of the notations:   !n`   !n   n¡ (It may not be vis...
#C.2B.2B
C++
  #include <vector> #include <string> #include <algorithm> #include <iostream> #include <sstream> using namespace std;   #if 1 // optimized for 64-bit architecture typedef unsigned long usingle; typedef unsigned long long udouble; const int word_len = 32; #else // optimized for 32-bit architecture typedef unsigned shor...
http://rosettacode.org/wiki/Linear_congruential_generator
Linear congruential generator
The linear congruential generator is a very simple example of a random number generator. All linear congruential generators use this formula: r n + 1 = a × r n + c ( mod m ) {\displaystyle r_{n+1}=a\times r_{n}+c{\pmod {m}}} Where: r 0 {\displaystyle r_{0}} is a seed. r 1 ...
#C
C
#include <stdio.h>   /* always assuming int is at least 32 bits */ int rand(); int rseed = 0;   inline void srand(int x) { rseed = x; }   #ifndef MS_RAND #define RAND_MAX ((1U << 31) - 1)   inline int rand() { return rseed = (rseed * 1103515245 + 12345) & RAND_MAX; }   #else /* MS rand */   #define RAND_MAX_32 ((1U <...
http://rosettacode.org/wiki/Leonardo_numbers
Leonardo numbers
Leonardo numbers   are also known as the   Leonardo series. The   Leonardo numbers   are a sequence of numbers defined by: L(0) = 1 [1st equation] L(1) = 1 [2nd equation] L(n) = L(n-1) + L(n-2) + 1 ...
#jq
jq
def Leonardo(zero; one; incr): def leo: if . == 0 then zero elif . == 1 then one else ((.-1) |leo) + ((.-2) | leo) + incr end; leo;
http://rosettacode.org/wiki/Leonardo_numbers
Leonardo numbers
Leonardo numbers   are also known as the   Leonardo series. The   Leonardo numbers   are a sequence of numbers defined by: L(0) = 1 [1st equation] L(1) = 1 [2nd equation] L(n) = L(n-1) + L(n-2) + 1 ...
#Julia
Julia
function L(n, add::Int=1, firsts::Vector=[1, 1]) l = max(maximum(n) .+ 1, length(firsts)) r = Vector{Int}(l) r[1:length(firsts)] = firsts for i in 3:l r[i] = r[i - 1] + r[i - 2] + add end return r[n .+ 1] end   # Task 1 println("First 25 Leonardo numbers: ", join(L(0:24), ", "))   # Task...
http://rosettacode.org/wiki/Leap_year
Leap year
Task Determine whether a given year is a leap year in the Gregorian calendar. See also Leap year (wiki)
#11l
11l
F is_leap_year(year) I year % 100 == 0 R year % 400 == 0 R year % 4 == 0
http://rosettacode.org/wiki/Levenshtein_distance
Levenshtein distance
This page uses content from Wikipedia. The original article was at Levenshtein distance. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) In information theory and computer science, the Levenshtein ...
#Arc
Arc
(def levenshtein (str1 str2) (withs l1 len.str1 l2 len.str2 row range0:inc.l1   (times j l2 (let next list.j (times i l1 (push (inc:min car.next ((if (is str1.i str2.j) dec id) car.row) (car:zap cdr row)) ...
http://rosettacode.org/wiki/Letter_frequency
Letter frequency
Task Open a text file and count the occurrences of each letter. Some of these programs count all characters (including punctuation), but some only count letters A to Z. Other tasks related to string operations: Metrics Array length String length Copy a string Empty string  (assignment) Counting Word frequen...
#ACL2
ACL2
(defun increment-alist (tbl key) (cond ((endp tbl) (list (cons key 1))) ((eql (car (first tbl)) key) (cons (cons key (1+ (cdr (first tbl)))) (rest tbl))) (t (cons (first tbl) (increment-alist (rest tbl) key)))))   (defun freq-table (xs) (if (endp xs) ...
http://rosettacode.org/wiki/Loop_over_multiple_arrays_simultaneously
Loop over multiple arrays simultaneously
Task Loop over multiple arrays   (or lists or tuples or whatever they're called in your language)   and display the   i th   element of each. Use your language's   "for each"   loop if it has one, otherwise iterate through the collection in order with some other loop. For this example, loop over the arrays: ...
#Visual_Basic_.NET
Visual Basic .NET
  Module Program Sub Main() Dim a As Char() = {"a"c, "b"c, "c"c} Dim b As Char() = {"A"c, "B"c, "C"c} Dim c As Integer() = {1, 2, 3}   Dim minLength = {a.Length, b.Length, c.Length}.Min() For i = 0 To minLength - 1 Console.WriteLine(a(i) & b(i) & c(i)) Nex...
http://rosettacode.org/wiki/List_comprehensions
List comprehensions
A list comprehension is a special syntax in some programming languages to describe lists. It is similar to the way mathematicians describe sets, with a set comprehension, hence the name. Some attributes of a list comprehension are: They should be distinct from (nested) for loops and the use of map and filter functio...
#jq
jq
def triples(n): range(1;n+1) as $x | range($x;n+1) as $y | range($y;n+1) as $z | select($x*$x + $y*$y == $z*$z) | [$x, $y, $z] ;  
http://rosettacode.org/wiki/Loops/For
Loops/For
“For”   loops are used to make some block of code be iterated a number of times, setting a variable or parameter to a monotonically increasing integer value for each execution of the block of code. Common extensions of this allow other counting patterns or iterating over abstract structures other than the integers. ...
#UNIX_Shell
UNIX Shell
#!/bin/sh # Using a while control construct to emulate a for loop   l="1" # Set the counters to one while [ "$l" -le 5 ] # Loop while the counter is less than five do m="1" while [ "$m" -le "$l" ] # Loop while the counter is less than five do printf "*" m=`expr "$m" + 1` # Incr...
http://rosettacode.org/wiki/Left_factorials
Left factorials
Left factorials,   !n,   may refer to either   subfactorials   or to   factorial sums; the same notation can be confusingly seen being used for the two different definitions. Sometimes,   subfactorials   (also known as derangements)   may use any of the notations:   !n`   !n   n¡ (It may not be vis...
#Clojure
Clojure
(ns left-factorial (:gen-class))   (defn left-factorial [n] " Compute by updating the state [fact summ] for each k, where k equals 1 to n Update is next state is [k*fact (summ+k)" (second (reduce (fn [[fact summ] k] [(*' fact k) (+ summ fact)]) [1 0] (range 1 (inc n)))))   (doseq...
http://rosettacode.org/wiki/Linear_congruential_generator
Linear congruential generator
The linear congruential generator is a very simple example of a random number generator. All linear congruential generators use this formula: r n + 1 = a × r n + c ( mod m ) {\displaystyle r_{n+1}=a\times r_{n}+c{\pmod {m}}} Where: r 0 {\displaystyle r_{0}} is a seed. r 1 ...
#C.23
C#
using System; using System.Collections.Generic; using System.Linq; using static System.Console;   namespace LinearCongruentialGenerator { static class LinearCongruentialGenerator { static int _seed = (int)DateTime.Now.Ticks; // from bad random gens might as well have bad seed! static int _bsdCur...
http://rosettacode.org/wiki/Leonardo_numbers
Leonardo numbers
Leonardo numbers   are also known as the   Leonardo series. The   Leonardo numbers   are a sequence of numbers defined by: L(0) = 1 [1st equation] L(1) = 1 [2nd equation] L(n) = L(n-1) + L(n-2) + 1 ...
#Kotlin
Kotlin
// version 1.1.2   fun leonardo(n: Int, l0: Int = 1, l1: Int = 1, add: Int = 1): IntArray { val leo = IntArray(n) leo[0] = l0 leo[1] = l1 for (i in 2 until n) leo[i] = leo[i - 1] + leo[i - 2] + add return leo }   fun main(args: Array<String>) { println("The first 25 Leonardo numbers with L[0] = ...
http://rosettacode.org/wiki/Leonardo_numbers
Leonardo numbers
Leonardo numbers   are also known as the   Leonardo series. The   Leonardo numbers   are a sequence of numbers defined by: L(0) = 1 [1st equation] L(1) = 1 [2nd equation] L(n) = L(n-1) + L(n-2) + 1 ...
#Lua
Lua
function leoNums (n, L0, L1, add) local L0, L1, add = L0 or 1, L1 or 1, add or 1 local lNums, nextNum = {L0, L1} while #lNums < n do nextNum = lNums[#lNums] + lNums[#lNums - 1] + add table.insert(lNums, nextNum) end return lNums end   function show (msg, t) print(msg .. ":") for i, x in ipairs(t) ...
http://rosettacode.org/wiki/Leap_year
Leap year
Task Determine whether a given year is a leap year in the Gregorian calendar. See also Leap year (wiki)
#360_Assembly
360 Assembly
(1) a 4-digit year (CCYY) (2) an 8-byte work area
http://rosettacode.org/wiki/Leap_year
Leap year
Task Determine whether a given year is a leap year in the Gregorian calendar. See also Leap year (wiki)
#68000_Assembly
68000 Assembly
;Example move.l #2018,d0 bsr leap_year addi.l #28,d1  ; # days in February 2018 rts ; Leap Year ; ; Input ; d0=year ; ; Output ; d1=1 if leap year, 0 if not leap year ; zero flag clear if leap year, set if not ; leap_year: cmpi.l #1752,d0 ble.s not_leap...
http://rosettacode.org/wiki/Levenshtein_distance
Levenshtein distance
This page uses content from Wikipedia. The original article was at Levenshtein distance. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) In information theory and computer science, the Levenshtein ...
#Arturo
Arturo
print levenshtein "kitten" "sitting"
http://rosettacode.org/wiki/Letter_frequency
Letter frequency
Task Open a text file and count the occurrences of each letter. Some of these programs count all characters (including punctuation), but some only count letters A to Z. Other tasks related to string operations: Metrics Array length String length Copy a string Empty string  (assignment) Counting Word frequen...
#Action.21
Action!
INCLUDE "D2:PRINTF.ACT" ;from the Action! Tool Kit   CARD ARRAY histogram(256)   PROC Clear() INT i   FOR i=0 TO 255 DO histogram(i)=0 OD RETURN   PROC ProcessLine(CHAR ARRAY line) INT i   FOR i=1 TO line(0) DO histogram(line(i))==+1 OD RETURN   PROC ProcessFile(CHAR ARRAY fname) CHAR ARRAY line(255...
http://rosettacode.org/wiki/Loop_over_multiple_arrays_simultaneously
Loop over multiple arrays simultaneously
Task Loop over multiple arrays   (or lists or tuples or whatever they're called in your language)   and display the   i th   element of each. Use your language's   "for each"   loop if it has one, otherwise iterate through the collection in order with some other loop. For this example, loop over the arrays: ...
#Visual_FoxPro
Visual FoxPro
  LOCAL i As Integer, n As Integer, c As String LOCAL ARRAY a1[3], a2[3], a3[4], a[3] *!* Populate the arrays and store the array lengths in a a1[1] = "a" a1[2] = "b" a1[3] = "c" a[1] = ALEN(a1) a2[1] = "A" a2[2] = "B" a2[3] = "C" a[2] = ALEN(a2) a3[1] = "1" a3[2] = "2" a3[3] = "3" a3[4] = "4" a[3] = ALEN(a3) *!* Find ...
http://rosettacode.org/wiki/List_comprehensions
List comprehensions
A list comprehension is a special syntax in some programming languages to describe lists. It is similar to the way mathematicians describe sets, with a set comprehension, hence the name. Some attributes of a list comprehension are: They should be distinct from (nested) for loops and the use of map and filter functio...
#Julia
Julia
  julia> n = 20 20   julia> [(x, y, z) for x = 1:n for y = x:n for z = y:n if x^2 + y^2 == z^2] 6-element Array{Tuple{Int64,Int64,Int64},1}: (3,4,5) (5,12,13) (6,8,10) (8,15,17) (9,12,15) (12,16,20)  
http://rosettacode.org/wiki/Loops/For
Loops/For
“For”   loops are used to make some block of code be iterated a number of times, setting a variable or parameter to a monotonically increasing integer value for each execution of the block of code. Common extensions of this allow other counting patterns or iterating over abstract structures other than the integers. ...
#UnixPipes
UnixPipes
yes \ | cat -n | (while read n ; do [ $n -gt 5 ] && exit 0; yes \* | head -n $n | xargs -n $n echo done)
http://rosettacode.org/wiki/Left_factorials
Left factorials
Left factorials,   !n,   may refer to either   subfactorials   or to   factorial sums; the same notation can be confusingly seen being used for the two different definitions. Sometimes,   subfactorials   (also known as derangements)   may use any of the notations:   !n`   !n   n¡ (It may not be vis...
#Common_Lisp
Common Lisp
  (defun fact (n) (reduce #'* (loop for i from 1 to n collect i)))   (defun left-fac (n) (reduce #'+ (loop for i below n collect (fact i))))   (format t "0 -> 10~&") (format t "~a~&" (loop for i upto 10 collect (left-fac i))) (format t "20 -> 110 by 10~&") (format t "~{~a~&~}" (loop for i from 20 upto 110 by 10 col...
http://rosettacode.org/wiki/Linear_congruential_generator
Linear congruential generator
The linear congruential generator is a very simple example of a random number generator. All linear congruential generators use this formula: r n + 1 = a × r n + c ( mod m ) {\displaystyle r_{n+1}=a\times r_{n}+c{\pmod {m}}} Where: r 0 {\displaystyle r_{0}} is a seed. r 1 ...
#C.2B.2B
C++
#include <iostream>   //-------------------------------------------------------------------------------------------------- using namespace std;   //-------------------------------------------------------------------------------------------------- class mRND { public: void seed( unsigned int s ) { _seed = s; }   pro...
http://rosettacode.org/wiki/Leonardo_numbers
Leonardo numbers
Leonardo numbers   are also known as the   Leonardo series. The   Leonardo numbers   are a sequence of numbers defined by: L(0) = 1 [1st equation] L(1) = 1 [2nd equation] L(n) = L(n-1) + L(n-2) + 1 ...
#Maple
Maple
L := proc(n, L_0, L_1, add) if n = 0 then return L_0; elif n = 1 then return L_1; else return L(n - 1) + L(n - 2) + add; end if; end proc:   Leonardo := n -> (L(1, 1, 1),[seq(0..n - 1)])   Fibonacci := n -> (L(0, 1, 0), [seq(0..n - 1)])
http://rosettacode.org/wiki/Leonardo_numbers
Leonardo numbers
Leonardo numbers   are also known as the   Leonardo series. The   Leonardo numbers   are a sequence of numbers defined by: L(0) = 1 [1st equation] L(1) = 1 [2nd equation] L(n) = L(n-1) + L(n-2) + 1 ...
#Mathematica.2FWolfram_Language
Mathematica/Wolfram Language
L[0,L0_:1,___]:=L0 L[1,L0_:1,L1_:1,___]:=L1 L[n_,L0_:1,L1_:1,add_:1]:=L[n-1,L0,L1,add]+L[n-2,L0,L1,add]+add   L/@(Range[25]-1) L[#,0,1,0]&/@(Range[25]-1)
http://rosettacode.org/wiki/Leap_year
Leap year
Task Determine whether a given year is a leap year in the Gregorian calendar. See also Leap year (wiki)
#8080_Assembly
8080 Assembly
org 100h jmp test ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Check if a year is a leap year. ;; Input: HL = year. ;; Output: Carry flag set if HL is a leap year. ;; Registers used: all. leap: mvi a,3 ; Divisible by 4? ana l ; If not, not a leap year, rnz ; Return w/carry cleared m...
http://rosettacode.org/wiki/Leap_year
Leap year
Task Determine whether a given year is a leap year in the Gregorian calendar. See also Leap year (wiki)
#Action.21
Action!
BYTE FUNC IsLeapYear(CARD year) IF year MOD 100=0 THEN IF year MOD 400=0 THEN RETURN (1) ELSE RETURN (0) FI FI   IF year MOD 4=0 THEN RETURN (1) FI RETURN (0)   PROC Main() CARD ARRAY t=[1900 1901 2000 2001 2004 2020 2021] BYTE i,leap CARD year   FOR i=0 TO 6 DO year=t...
http://rosettacode.org/wiki/Levenshtein_distance
Levenshtein distance
This page uses content from Wikipedia. The original article was at Levenshtein distance. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) In information theory and computer science, the Levenshtein ...
#AutoHotkey
AutoHotkey
levenshtein(s, t){ If s = return StrLen(t) If t = return strLen(s) If SubStr(s, 1, 1) = SubStr(t, 1, 1) return levenshtein(SubStr(s, 2), SubStr(t, 2)) a := Levenshtein(SubStr(s, 2), SubStr(t, 2)) b := Levenshtein(s, SubStr(t, 2)) c := Levenshtein(SubStr(s, 2), t ) If (a > b) a := b ...
http://rosettacode.org/wiki/Letter_frequency
Letter frequency
Task Open a text file and count the occurrences of each letter. Some of these programs count all characters (including punctuation), but some only count letters A to Z. Other tasks related to string operations: Metrics Array length String length Copy a string Empty string  (assignment) Counting Word frequen...
#Ada
Ada
with Ada.Text_IO;   procedure Letter_Frequency is Counters: array (Character) of Natural := (others => 0); -- initialize all Counters to 0 C: Character; File: Ada.Text_IO.File_Type;   begin Ada.Text_IO.Open(File, Mode => Ada.Text_IO.In_File, Name => "letter_frequency.adb"); while not Ada.Text_...
http://rosettacode.org/wiki/Loop_over_multiple_arrays_simultaneously
Loop over multiple arrays simultaneously
Task Loop over multiple arrays   (or lists or tuples or whatever they're called in your language)   and display the   i th   element of each. Use your language's   "for each"   loop if it has one, otherwise iterate through the collection in order with some other loop. For this example, loop over the arrays: ...
#Vlang
Vlang
fn main() { arrays := [['a','b','c'],['A','B','C'],['1','2','3']] for i in 0..arrays[0].len { println('${arrays[0][i]}${arrays[1][i]}${arrays[2][i]}') } }
http://rosettacode.org/wiki/List_comprehensions
List comprehensions
A list comprehension is a special syntax in some programming languages to describe lists. It is similar to the way mathematicians describe sets, with a set comprehension, hence the name. Some attributes of a list comprehension are: They should be distinct from (nested) for loops and the use of map and filter functio...
#Kotlin
Kotlin
// version 1.0.6   fun pythagoreanTriples(n: Int) = (1..n).flatMap { x -> (x..n).flatMap { y -> (y..n).filter { z -> x * x + y * y == z * z }.map { Triple(x, y, it) } } }   fun main(args: Array<String>) { println(pythagoreanTriples(20)) }
http://rosettacode.org/wiki/Loops/For
Loops/For
“For”   loops are used to make some block of code be iterated a number of times, setting a variable or parameter to a monotonically increasing integer value for each execution of the block of code. Common extensions of this allow other counting patterns or iterating over abstract structures other than the integers. ...
#Ursa
Ursa
# # for loop #   decl int i j for (set i 0) (< i 5) (inc i) for (set j 0) (< j (int (+ i 1))) (inc j) out "*" console end for out endl console end for
http://rosettacode.org/wiki/Left_factorials
Left factorials
Left factorials,   !n,   may refer to either   subfactorials   or to   factorial sums; the same notation can be confusingly seen being used for the two different definitions. Sometimes,   subfactorials   (also known as derangements)   may use any of the notations:   !n`   !n   n¡ (It may not be vis...
#D
D
import std.stdio, std.bigint, std.range, std.algorithm, std.conv;   BigInt leftFact(in uint n) pure nothrow /*@safe*/ { BigInt result = 0, factorial = 1; foreach (immutable i; 1 .. n + 1) { result += factorial; factorial *= i; } return result; }   void main() { writeln("First 11 left...
http://rosettacode.org/wiki/Linear_congruential_generator
Linear congruential generator
The linear congruential generator is a very simple example of a random number generator. All linear congruential generators use this formula: r n + 1 = a × r n + c ( mod m ) {\displaystyle r_{n+1}=a\times r_{n}+c{\pmod {m}}} Where: r 0 {\displaystyle r_{0}} is a seed. r 1 ...
#Clojure
Clojure
    (defn iterator [a b] (fn[x] (mod (+ (* a x) b) (bit-shift-left 1 31))))   (def bsd (drop 1 (iterate (iterator 1103515245 12345) 0)))   (def ms (drop 1 (for [x (iterate (iterator 214013 2531011) 0)] (bit-shift-right x 16))))   (take 10 bsd) ;-> (12345 1406932606 654583775 1449466924 229283573 1109335178 105155045...
http://rosettacode.org/wiki/Leonardo_numbers
Leonardo numbers
Leonardo numbers   are also known as the   Leonardo series. The   Leonardo numbers   are a sequence of numbers defined by: L(0) = 1 [1st equation] L(1) = 1 [2nd equation] L(n) = L(n-1) + L(n-2) + 1 ...
#min
min
(over over + rolldown pop pick +) :next (('print dip " " print! next) 25 times newline) :leo   "First 25 Leonardo numbers:" puts! 1 1 1 leo "First 25 Leonardo numbers with add=0, L(0)=0, L(1)=1:" puts! 0 0 1 leo
http://rosettacode.org/wiki/Leonardo_numbers
Leonardo numbers
Leonardo numbers   are also known as the   Leonardo series. The   Leonardo numbers   are a sequence of numbers defined by: L(0) = 1 [1st equation] L(1) = 1 [2nd equation] L(n) = L(n-1) + L(n-2) + 1 ...
#Modula-2
Modula-2
MODULE Leonardo; FROM FormatString IMPORT FormatString; FROM Terminal IMPORT WriteString,WriteLn,ReadChar;   PROCEDURE leonardo(a,b,step,num : INTEGER); VAR buf : ARRAY[0..63] OF CHAR; i,temp : INTEGER; BEGIN FOR i:=1 TO num DO IF i=1 THEN FormatString(" %i", buf, a); WriteSt...
http://rosettacode.org/wiki/Leap_year
Leap year
Task Determine whether a given year is a leap year in the Gregorian calendar. See also Leap year (wiki)
#ActionScript
ActionScript
public function isLeapYear(year:int):Boolean { if (year % 100 == 0) { return (year % 400 == 0); } return (year % 4 == 0); }
http://rosettacode.org/wiki/Law_of_cosines_-_triples
Law of cosines - triples
The Law of cosines states that for an angle γ, (gamma) of any triangle, if the sides adjacent to the angle are A and B and the side opposite is C; then the lengths of the sides are related by this formula: A2 + B2 - 2ABcos(γ) = C2 Specific angles For an angle of of   90º   this becomes the more familiar ...
#11l
11l
-V n = 13   F method1(n) V squares = (0..n).map(x -> x ^ 2) V sqrset = Set(squares) Set[(Int, Int, Int)] tri90, tri60, tri120   L(a) 1..n V a2 = squares[a] L(b) 1..a V b2 = squares[b] V c2 = a2 + b2 I c2 C sqrset tri90.add(tuple_sorted((a, b, Int(sqrt(c2)))...
http://rosettacode.org/wiki/Levenshtein_distance
Levenshtein distance
This page uses content from Wikipedia. The original article was at Levenshtein distance. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) In information theory and computer science, the Levenshtein ...
#AWK
AWK
#!/usr/bin/awk -f   BEGIN { a = "kitten"; b = "sitting"; d = levenshteinDistance(a, b); p = d == 1 ? "" : "s"; printf("%s -> %s after %d edit%s\n", a, b, d, p); exit; }   function levenshteinDistance(s1, s2, s1First, s2First, s1Rest, s2Rest, distA, distB, distC, minDist) {   # If eit...
http://rosettacode.org/wiki/Letter_frequency
Letter frequency
Task Open a text file and count the occurrences of each letter. Some of these programs count all characters (including punctuation), but some only count letters A to Z. Other tasks related to string operations: Metrics Array length String length Copy a string Empty string  (assignment) Counting Word frequen...
#Aikido
Aikido
import ctype   var letters = new int [26]   var s = openin (args[0]) while (!s.eof()) { var ch = s.getchar() if (s.eof()) { break } if (ctype.isalpha (ch)) { var n = cast<int>(ctype.tolower(ch) - 'a') ++letters[n] } }   foreach i letters.size() { println (cast<char>('a' +...
http://rosettacode.org/wiki/Loop_over_multiple_arrays_simultaneously
Loop over multiple arrays simultaneously
Task Loop over multiple arrays   (or lists or tuples or whatever they're called in your language)   and display the   i th   element of each. Use your language's   "for each"   loop if it has one, otherwise iterate through the collection in order with some other loop. For this example, loop over the arrays: ...
#Wart
Wart
each (x X n) (zip '(a b c) '(A B C) '(1 2 3)) prn x X n
http://rosettacode.org/wiki/Loop_over_multiple_arrays_simultaneously
Loop over multiple arrays simultaneously
Task Loop over multiple arrays   (or lists or tuples or whatever they're called in your language)   and display the   i th   element of each. Use your language's   "for each"   loop if it has one, otherwise iterate through the collection in order with some other loop. For this example, loop over the arrays: ...
#Wren
Wren
var a1 = ["a", "b", "c"] var a2 = ["A", "B", "C"] var a3 = [1, 2, 3] for (i in a3) System.print("%(a1[i-1])%(a2[i-1])%(i)")
http://rosettacode.org/wiki/List_comprehensions
List comprehensions
A list comprehension is a special syntax in some programming languages to describe lists. It is similar to the way mathematicians describe sets, with a set comprehension, hence the name. Some attributes of a list comprehension are: They should be distinct from (nested) for loops and the use of map and filter functio...
#Lasso
Lasso
#!/usr/bin/lasso9   local(n = 20) local(triples = with x in generateSeries(1, #n), y in generateSeries(#x, #n), z in generateSeries(#y, #n) where #x*#x + #y*#y == #z*#z select (:#x, #y, #z) ) #triples->join('\n')
http://rosettacode.org/wiki/Loops/For
Loops/For
“For”   loops are used to make some block of code be iterated a number of times, setting a variable or parameter to a monotonically increasing integer value for each execution of the block of code. Common extensions of this allow other counting patterns or iterating over abstract structures other than the integers. ...
#Vala
Vala
int main (string[] args) { for (var i = 1; i <= 5; i++) { for (var j = 1; j <= i; j++) { stdout.putc ('*'); } stdout.putc ('\n'); } return 0; }
http://rosettacode.org/wiki/Left_factorials
Left factorials
Left factorials,   !n,   may refer to either   subfactorials   or to   factorial sums; the same notation can be confusingly seen being used for the two different definitions. Sometimes,   subfactorials   (also known as derangements)   may use any of the notations:   !n`   !n   n¡ (It may not be vis...
#EchoLisp
EchoLisp
  (lib 'bigint) (define (!n n) (if (zero? n) 0 (+ (!n (1- n)) (factorial (1- n))))) (remember '!n)  
http://rosettacode.org/wiki/Left_factorials
Left factorials
Left factorials,   !n,   may refer to either   subfactorials   or to   factorial sums; the same notation can be confusingly seen being used for the two different definitions. Sometimes,   subfactorials   (also known as derangements)   may use any of the notations:   !n`   !n   n¡ (It may not be vis...
#Elixir
Elixir
defmodule LeftFactorial do def calc(0), do: 0 def calc(n) do {result, _factorial} = Enum.reduce(1..n, {0, 1}, fn i,{res, fact} -> {res + fact, fact * i} end) result end end   Enum.each(0..10, fn i -> IO.puts "!#{i} = #{LeftFactorial.calc(i)}" end) Enum.each(Enum.take_every(20..110, 10), fn i -...
http://rosettacode.org/wiki/Linear_congruential_generator
Linear congruential generator
The linear congruential generator is a very simple example of a random number generator. All linear congruential generators use this formula: r n + 1 = a × r n + c ( mod m ) {\displaystyle r_{n+1}=a\times r_{n}+c{\pmod {m}}} Where: r 0 {\displaystyle r_{0}} is a seed. r 1 ...
#Common_Lisp
Common Lisp
(defun make-rng (&key (seed 0) (mode nil)) "returns an RNG according to :seed and :mode keywords default mode: bsd default seed: 0 (should be 1 actually)" (if (eql mode 'ms) #'(lambda () (ash (setf seed (mod (+ (* 214013 seed) 2531011) (expt 2 31))) -16)) #'(lambda () (setf seed (mod (+ (* seed 1103515...
http://rosettacode.org/wiki/Leonardo_numbers
Leonardo numbers
Leonardo numbers   are also known as the   Leonardo series. The   Leonardo numbers   are a sequence of numbers defined by: L(0) = 1 [1st equation] L(1) = 1 [2nd equation] L(n) = L(n-1) + L(n-2) + 1 ...
#Nim
Nim
import strformat   proc leonardoNumbers(count: int, L0: int = 1, L1: int = 1, ADD: int = 1) = var t = 0 var (L0_loc, L1_loc) = (L0, L1) for i in 0..<count: write(stdout, fmt"{L0_loc:7}") t = L0_loc + L1_loc + ADD L0_loc = L1_loc L1_loc = t if i mod 5 == 4: write(std...
http://rosettacode.org/wiki/Leonardo_numbers
Leonardo numbers
Leonardo numbers   are also known as the   Leonardo series. The   Leonardo numbers   are a sequence of numbers defined by: L(0) = 1 [1st equation] L(1) = 1 [2nd equation] L(n) = L(n-1) + L(n-2) + 1 ...
#Perl
Perl
no warnings 'experimental::signatures'; use feature 'signatures';   sub leonardo ($n, $l0 = 1, $l1 = 1, $add = 1) { ($l0, $l1) = ($l1, $l0+$l1+$add) for 1..$n; $l0; }   my @L = map { leonardo($_) } 0..24; print "Leonardo[1,1,1]: @L\n"; my @F = map { leonardo($_,0,1,0) } 0..24; print "Leonardo[0,1,0]: @F\n";
http://rosettacode.org/wiki/Leap_year
Leap year
Task Determine whether a given year is a leap year in the Gregorian calendar. See also Leap year (wiki)
#Ada
Ada
-- Incomplete code, just a sniplet to do the task. Can be used in any package or method. -- Adjust the type of Year if you use a different one. function Is_Leap_Year (Year : Integer) return Boolean is begin if Year rem 100 = 0 then return Year rem 400 = 0; else return Year rem 4 = 0; end if; end Is...
http://rosettacode.org/wiki/Law_of_cosines_-_triples
Law of cosines - triples
The Law of cosines states that for an angle γ, (gamma) of any triangle, if the sides adjacent to the angle are A and B and the side opposite is C; then the lengths of the sides are related by this formula: A2 + B2 - 2ABcos(γ) = C2 Specific angles For an angle of of   90º   this becomes the more familiar ...
#Action.21
Action!
PROC Test(INT max,angle,coeff) BYTE count,a,b,c   PrintF("gamma=%B degrees:%E",angle) count=0 FOR a=1 TO max DO FOR b=1 TO a DO FOR c=1 TO max DO IF a*a+b*b-coeff*a*b=c*c THEN PrintF("(%B,%B,%B) ",a,b,c) count==+1 FI OD OD OD PrintF("%Enumb...
http://rosettacode.org/wiki/Law_of_cosines_-_triples
Law of cosines - triples
The Law of cosines states that for an angle γ, (gamma) of any triangle, if the sides adjacent to the angle are A and B and the side opposite is C; then the lengths of the sides are related by this formula: A2 + B2 - 2ABcos(γ) = C2 Specific angles For an angle of of   90º   this becomes the more familiar ...
#Ada
Ada
with Ada.Text_IO; with Ada.Containers.Ordered_Maps;   procedure Law_Of_Cosines is   type Angle_Kind is (Angle_60, Angle_90, Angle_120);   function Is_Triangle (A, B, C : in Positive; Angle  : in Angle_Kind) return Boolean is A2 : constant Positive := A**2; B2 : constant Po...
http://rosettacode.org/wiki/Latin_Squares_in_reduced_form/Randomizing_using_Jacobson_and_Matthews%E2%80%99_Technique
Latin Squares in reduced form/Randomizing using Jacobson and Matthews’ Technique
Section 3.3 of [Generalised 2-designs with Block Size 3(Andy L. Drizen)] describes a method of generating Latin Squares of order n attributed to Jacobson and Matthews. The purpose of this task is to produce a function which given a valid Latin Square transforms it to another using this method. part 1 Use one of the 4 ...
#F.23
F#
  // Jacobson and Matthews technique for generating Latin Squares. Nigel Galloway: August 5th., 2019 let R=let N=System.Random() in (fun n->N.Next(n))   let jmLS α X0= let X0=Array2D.copy X0 let N=let N=[|[0..α-1];[α-1..(-1)..0]|] in (fun()->N.[R 2]) let rec randLS i j z n g s= X0.[i,g]<-s; X0.[n,j]<-s if...
http://rosettacode.org/wiki/Levenshtein_distance
Levenshtein distance
This page uses content from Wikipedia. The original article was at Levenshtein distance. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) In information theory and computer science, the Levenshtein ...
#BBC_BASIC
BBC BASIC
PRINT "'kitten' -> 'sitting' has distance " ; PRINT ; FNlevenshtein("kitten", "sitting") PRINT "'rosettacode' -> 'raisethysword' has distance " ; PRINT ; FNlevenshtein("rosettacode", "raisethysword") END   DEF FNlevenshtein(s$, t$) LOCAL i%, j%, m%, d%() DIM d%(LENs$, LEN...
http://rosettacode.org/wiki/Letter_frequency
Letter frequency
Task Open a text file and count the occurrences of each letter. Some of these programs count all characters (including punctuation), but some only count letters A to Z. Other tasks related to string operations: Metrics Array length String length Copy a string Empty string  (assignment) Counting Word frequen...
#Aime
Aime
file f; index x; integer c;   f.affix("unixdict.txt");   while ((c = f.pick) ^ -1) { x[c] += 1; }   c = 'A'; while (c <= 'Z') { o_form("%c: /w5/\n", c, x[c] += x[c + 'a' - 'A'] += 0); c += 1; }
http://rosettacode.org/wiki/Loop_over_multiple_arrays_simultaneously
Loop over multiple arrays simultaneously
Task Loop over multiple arrays   (or lists or tuples or whatever they're called in your language)   and display the   i th   element of each. Use your language's   "for each"   loop if it has one, otherwise iterate through the collection in order with some other loop. For this example, loop over the arrays: ...
#X86_Assembly
X86 Assembly
  extern _printf   section .data arr1 dd 3,"a","b","c" ;first dword saves the length arr2 dd 3,"A","B","C" arr3 dd 3,"1","2","3"   section .bss arrLocation resd 4 tempOutput resd 2   section .text global _main _main: mov [arrLocation], dword arr1 mov [arrLocation+4], dword arr2 mov [arrL...
http://rosettacode.org/wiki/List_comprehensions
List comprehensions
A list comprehension is a special syntax in some programming languages to describe lists. It is similar to the way mathematicians describe sets, with a set comprehension, hence the name. Some attributes of a list comprehension are: They should be distinct from (nested) for loops and the use of map and filter functio...
#Lua
Lua
  LC={} LC.__index = LC   function LC:new(o) o = o or {} setmetatable(o, self) return o end   function LC:add_iter(func) local prev_iter = self.iter self.iter = coroutine.wrap( (prev_iter == nil) and (function() func{} end) or (function() for arg in prev_iter do func(arg) end end)) return self end  ...
http://rosettacode.org/wiki/Loops/For
Loops/For
“For”   loops are used to make some block of code be iterated a number of times, setting a variable or parameter to a monotonically increasing integer value for each execution of the block of code. Common extensions of this allow other counting patterns or iterating over abstract structures other than the integers. ...
#VBA
VBA
Option Explicit Sub LoopEx() Dim i As Long, j As Long, s As String For i = 1 To 5 s = "" For j = 1 To i s = s + "*" Next Debug.Print s Next End Sub
http://rosettacode.org/wiki/Left_factorials
Left factorials
Left factorials,   !n,   may refer to either   subfactorials   or to   factorial sums; the same notation can be confusingly seen being used for the two different definitions. Sometimes,   subfactorials   (also known as derangements)   may use any of the notations:   !n`   !n   n¡ (It may not be vis...
#F.23
F#
  // Generate Sequence of Left Factorials: Nigel Galloway, March 5th., 2019. let LF=Seq.unfold(fun (Σ,n,g)->Some(Σ,(Σ+n,n*g,g+1I))) (0I,1I,1I)  
http://rosettacode.org/wiki/Left_factorials
Left factorials
Left factorials,   !n,   may refer to either   subfactorials   or to   factorial sums; the same notation can be confusingly seen being used for the two different definitions. Sometimes,   subfactorials   (also known as derangements)   may use any of the notations:   !n`   !n   n¡ (It may not be vis...
#Factor
Factor
USING: formatting fry io kernel math math.factorials math.functions math.parser math.ranges sequences ; IN: rosetta-code.left-factorials   : left-factorial ( n -- m ) <iota> [ n! ] map-sum ;   : print-left-factorials ( seq quot -- ) '[ dup left-factorial @ [ number>string "!" prepend ] dip "...
http://rosettacode.org/wiki/Linear_congruential_generator
Linear congruential generator
The linear congruential generator is a very simple example of a random number generator. All linear congruential generators use this formula: r n + 1 = a × r n + c ( mod m ) {\displaystyle r_{n+1}=a\times r_{n}+c{\pmod {m}}} Where: r 0 {\displaystyle r_{0}} is a seed. r 1 ...
#D
D
struct LinearCongruentialGenerator { enum uint RAND_MAX = (1U << 31) - 1; uint seed = 0;   uint randBSD() pure nothrow @nogc { seed = (seed * 1_103_515_245 + 12_345) & RAND_MAX; return seed; }   uint randMS() pure nothrow @nogc { seed = (seed * 214_013 + 2_531_011) & RAND_MAX...
http://rosettacode.org/wiki/Leonardo_numbers
Leonardo numbers
Leonardo numbers   are also known as the   Leonardo series. The   Leonardo numbers   are a sequence of numbers defined by: L(0) = 1 [1st equation] L(1) = 1 [2nd equation] L(n) = L(n-1) + L(n-2) + 1 ...
#Phix
Phix
with javascript_semantics function leonardo(integer n, l1=1, l2=1, step=1) -- return the first n leonardo numbers, starting {l1,l2}, with step as the add number sequence res = {l1,l2} while length(res)<n do res = append(res,res[$]+res[$-1]+step) end while return res end function ?{"Leonardo",leo...
http://rosettacode.org/wiki/Leap_year
Leap year
Task Determine whether a given year is a leap year in the Gregorian calendar. See also Leap year (wiki)
#ALGOL_60
ALGOL 60
begin integer year;   integer procedure mod(i,j); value i,j; integer i,j; mod:=i-(i div j)*j;   boolean procedure isLeapYear(year); value year; integer year; isLeapYear:=mod(year,400)=0 or (mod(year,4)=0 and mod(year,100) notequal 0);   for year := 1899, 1900, 1901, 1902, 1903, 1904, 1905, 1999, 2000, 2001, 2002,...
http://rosettacode.org/wiki/Least_common_multiple
Least common multiple
Least common multiple You are encouraged to solve this task according to the task description, using any language you may know. Task Compute the   least common multiple   (LCM)   of two integers. Given   m   and   n,   the least common multiple is the smallest positive integer that has both   m   and   n   as factor...
#11l
11l
F gcd(=a, =b) L b != 0 (a, b) = (b, a % b) R a   F lcm(m, n) R m I/ gcd(m, n) * n   print(lcm(12, 18))
http://rosettacode.org/wiki/Law_of_cosines_-_triples
Law of cosines - triples
The Law of cosines states that for an angle γ, (gamma) of any triangle, if the sides adjacent to the angle are A and B and the side opposite is C; then the lengths of the sides are related by this formula: A2 + B2 - 2ABcos(γ) = C2 Specific angles For an angle of of   90º   this becomes the more familiar ...
#ALGOL_68
ALGOL 68
BEGIN # find all integer sided 90, 60 and 120 degree triangles by finding integer solutions for # # a^2 + b^2 = c^2, a^2 + b^2 - ab = c^2, a^2 + b^2 + ab = c^2 where a, b, c in 1 .. 13 # INT max side = 13; # max triangle side to consider # INT max square = max s...
http://rosettacode.org/wiki/Latin_Squares_in_reduced_form/Randomizing_using_Jacobson_and_Matthews%E2%80%99_Technique
Latin Squares in reduced form/Randomizing using Jacobson and Matthews’ Technique
Section 3.3 of [Generalised 2-designs with Block Size 3(Andy L. Drizen)] describes a method of generating Latin Squares of order n attributed to Jacobson and Matthews. The purpose of this task is to produce a function which given a valid Latin Square transforms it to another using this method. part 1 Use one of the 4 ...
#Go
Go
package main   import ( "fmt" "math/rand" "time" )   type ( vector []int matrix []vector cube []matrix )   func toReduced(m matrix) matrix { n := len(m) r := make(matrix, n) for i := 0; i < n; i++ { r[i] = make(vector, n) copy(r[i], m[i]) } for j := 0; j < n...
http://rosettacode.org/wiki/Levenshtein_distance
Levenshtein distance
This page uses content from Wikipedia. The original article was at Levenshtein distance. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) In information theory and computer science, the Levenshtein ...
#BQN
BQN
Levenshtein ← { 𝕨 𝕊"": ≠𝕨; ""𝕊 𝕩: ≠𝕩; 𝕨 𝕊 𝕩: Tail←1⊸↓ 𝕨 =○⊑◶⟨1+·⌊´ 𝕊○Tail ∾ Tail⊸𝕊 ∾ 𝕊⟜Tail, 𝕊○Tail⟩ 𝕩 }
http://rosettacode.org/wiki/Letter_frequency
Letter frequency
Task Open a text file and count the occurrences of each letter. Some of these programs count all characters (including punctuation), but some only count letters A to Z. Other tasks related to string operations: Metrics Array length String length Copy a string Empty string  (assignment) Counting Word frequen...
#ALGOL_68
ALGOL 68
  BEGIN [0:max abs char]INT histogram; FOR i FROM 0 TO max abs char DO histogram[i] := 0 OD; FILE input file; STRING input file name = "Letter_frequency.a68"; IF open (input file, input file name, stand in channel) /= 0 THEN put (stand error, ("Cannot open ", input file name, newline)); stop ...
http://rosettacode.org/wiki/Loop_over_multiple_arrays_simultaneously
Loop over multiple arrays simultaneously
Task Loop over multiple arrays   (or lists or tuples or whatever they're called in your language)   and display the   i th   element of each. Use your language's   "for each"   loop if it has one, otherwise iterate through the collection in order with some other loop. For this example, loop over the arrays: ...
#XBasic
XBasic
' Loop over multiple arrays simultaneously PROGRAM "loopoverarrays"   DECLARE FUNCTION Entry()   FUNCTION Entry() DIM arr1$[2], arr2$[2], arr3%[2] arr1$[0] = "a": arr1$[1] = "b": arr1$[2] = "c" arr2$[0] = "A": arr2$[1] = "B": arr2$[2] = "C" arr3%[0] = 1: arr3%[1] = 2: arr3%[2] = 3 FOR i% = 0 TO 2 PRIN...
http://rosettacode.org/wiki/List_comprehensions
List comprehensions
A list comprehension is a special syntax in some programming languages to describe lists. It is similar to the way mathematicians describe sets, with a set comprehension, hence the name. Some attributes of a list comprehension are: They should be distinct from (nested) for loops and the use of map and filter functio...
#Mathematica.2FWolfram_Language
Mathematica/Wolfram Language
Select[Tuples[Range[100], 3], #1[[1]]^2 + #1[[2]]^2 == #1[[3]]^2 &]
http://rosettacode.org/wiki/Loops/For
Loops/For
“For”   loops are used to make some block of code be iterated a number of times, setting a variable or parameter to a monotonically increasing integer value for each execution of the block of code. Common extensions of this allow other counting patterns or iterating over abstract structures other than the integers. ...
#VBScript
VBScript
Option Explicit Dim i, j, s For i = 1 To 5 s = "" For j = 1 To i s = s + "*" Next WScript.Echo s Next
http://rosettacode.org/wiki/Left_factorials
Left factorials
Left factorials,   !n,   may refer to either   subfactorials   or to   factorial sums; the same notation can be confusingly seen being used for the two different definitions. Sometimes,   subfactorials   (also known as derangements)   may use any of the notations:   !n`   !n   n¡ (It may not be vis...
#Forth
Forth
36000 CONSTANT #DIGITS \ Enough for !10000 CREATE S #DIGITS ALLOT S #DIGITS ERASE VARIABLE S# CREATE F #DIGITS ALLOT F #DIGITS ERASE VARIABLE F# 1 F C! 1 F# ! \ F = 1 = 0!   \ "Bignums": represented by two cells on the stack: \ 1) An address pointing to the least-significant unit \ 2) An integer size representi...
http://rosettacode.org/wiki/Linear_congruential_generator
Linear congruential generator
The linear congruential generator is a very simple example of a random number generator. All linear congruential generators use this formula: r n + 1 = a × r n + c ( mod m ) {\displaystyle r_{n+1}=a\times r_{n}+c{\pmod {m}}} Where: r 0 {\displaystyle r_{0}} is a seed. r 1 ...
#dc
dc
[* * lrx -- (random number from 0 to 2147483647) * * Returns a number from the BSD rand() sequence. * Seeded by storing a seed in register R. *]sz [lR 1103515245 * 12345 + 2147483648 % d sR]sr   [* Set seed to 1, then print the first 3 random numbers. *]sz 1 sR lrx psz lrx psz lrx psz
http://rosettacode.org/wiki/Leonardo_numbers
Leonardo numbers
Leonardo numbers   are also known as the   Leonardo series. The   Leonardo numbers   are a sequence of numbers defined by: L(0) = 1 [1st equation] L(1) = 1 [2nd equation] L(n) = L(n-1) + L(n-2) + 1 ...
#Picat
Picat
go => println([leonardo(I) : I in 0..24]), println([leonardo(0,1,0,I) : I in 0..24]).   leonardo(N) = leonardo(1,1,1,N). table leonardo(I1,_I2,_Add,0) = I1. leonardo(_I1,I2,_Add,1) = I2. leonardo(I1,I2,Add,N) = leonardo(I1,I2,Add,N-1) + leonardo(I1,I2,Add,N-2) + Add.
http://rosettacode.org/wiki/Leonardo_numbers
Leonardo numbers
Leonardo numbers   are also known as the   Leonardo series. The   Leonardo numbers   are a sequence of numbers defined by: L(0) = 1 [1st equation] L(1) = 1 [2nd equation] L(n) = L(n-1) + L(n-2) + 1 ...
#PicoLisp
PicoLisp
(de leo (A B C) (default A 1 B 1 C 1) (make (do 25 (inc 'B (+ (link (swap 'A B)) C) ) ) ) )   (println 'Leonardo (leo)) (println 'Fibonacci (leo 0 1 0))
http://rosettacode.org/wiki/Leap_year
Leap year
Task Determine whether a given year is a leap year in the Gregorian calendar. See also Leap year (wiki)
#ALGOL_68
ALGOL 68
MODE YEAR = INT, MONTH = INT, DAY = INT;   PROC year days = (YEAR year)DAY: # Ignore 1752 CE for the moment # ( month days(year, 2) = 28 | 365 | 366 );   PROC month days = (YEAR year, MONTH month) DAY: ( month | 31, 28 + ABS (year MOD 4 = 0 AND year MOD 100 /= 0 OR year MOD 400 = 0), 31, 30,...
http://rosettacode.org/wiki/Least_common_multiple
Least common multiple
Least common multiple You are encouraged to solve this task according to the task description, using any language you may know. Task Compute the   least common multiple   (LCM)   of two integers. Given   m   and   n,   the least common multiple is the smallest positive integer that has both   m   and   n   as factor...
#360_Assembly
360 Assembly
LCM CSECT USING LCM,R15 use calling register L R6,A a L R7,B b LR R8,R6 c=a LOOPW LR R4,R8 c SRDA R4,32 shift to next reg DR R4,R7 c/b ...
http://rosettacode.org/wiki/Least_common_multiple
Least common multiple
Least common multiple You are encouraged to solve this task according to the task description, using any language you may know. Task Compute the   least common multiple   (LCM)   of two integers. Given   m   and   n,   the least common multiple is the smallest positive integer that has both   m   and   n   as factor...
#8th
8th
  : gcd \ a b -- gcd dup 0 n:= if drop ;; then tuck \ b a b n:mod \ b a-mod-b recurse ;   : lcm \ m n 2dup \ m n m n n:* \ m n m*n n:abs \ m n abs(m*n) -rot \ abs(m*n) m n gcd \ abs(m*n) gcd(m.n) n:/mod \ abs / gcd nip \ abs div gcd ;   : demo \ n m -- 2dup "LCM of " . . " and " . . " = " . lcm . ;  ...
http://rosettacode.org/wiki/Law_of_cosines_-_triples
Law of cosines - triples
The Law of cosines states that for an angle γ, (gamma) of any triangle, if the sides adjacent to the angle are A and B and the side opposite is C; then the lengths of the sides are related by this formula: A2 + B2 - 2ABcos(γ) = C2 Specific angles For an angle of of   90º   this becomes the more familiar ...
#AWK
AWK
  # syntax: GAWK -f LAW_OF_COSINES_-_TRIPLES.AWK # converted from C BEGIN { description[1] = "90 degrees, a*a + b*b = c*c" description[2] = "60 degrees, a*a + b*b - a*b = c*c" description[3] = "120 degrees, a*a + b*b + a*b = c*c" split("0,1,-1",coeff,",") main(13,1,0) main(1000,0,1) # 10,000 tak...
http://rosettacode.org/wiki/Latin_Squares_in_reduced_form/Randomizing_using_Jacobson_and_Matthews%E2%80%99_Technique
Latin Squares in reduced form/Randomizing using Jacobson and Matthews’ Technique
Section 3.3 of [Generalised 2-designs with Block Size 3(Andy L. Drizen)] describes a method of generating Latin Squares of order n attributed to Jacobson and Matthews. The purpose of this task is to produce a function which given a valid Latin Square transforms it to another using this method. part 1 Use one of the 4 ...
#Julia
Julia
const Cube = Vector{Vector{Vector{Int}}} const Mat = Vector{Vector{Int}}   function reduced(m::Mat) n = length(m) r = deepcopy(m) for j in 1:n-1 if r[1][j] != j for k in j+1:n if r[1][k] == j for i in 1:n r[i][j], r[i][k] = r[i]...
http://rosettacode.org/wiki/Latin_Squares_in_reduced_form/Randomizing_using_Jacobson_and_Matthews%E2%80%99_Technique
Latin Squares in reduced form/Randomizing using Jacobson and Matthews’ Technique
Section 3.3 of [Generalised 2-designs with Block Size 3(Andy L. Drizen)] describes a method of generating Latin Squares of order n attributed to Jacobson and Matthews. The purpose of this task is to produce a function which given a valid Latin Square transforms it to another using this method. part 1 Use one of the 4 ...
#Nim
Nim
import random, sequtils, strformat, tables, times   type Vector = seq[int] Matrix = seq[Vector] Cube = seq[Matrix] Array16 = array[16, int] Array25 = array[25, int]     func newCube(m: Matrix; n: Positive): Cube = result.setLen(n) for i in 0..<n: result[i].setLen(n) for j in 0..<n: result[i]...
http://rosettacode.org/wiki/Latin_Squares_in_reduced_form/Randomizing_using_Jacobson_and_Matthews%E2%80%99_Technique
Latin Squares in reduced form/Randomizing using Jacobson and Matthews’ Technique
Section 3.3 of [Generalised 2-designs with Block Size 3(Andy L. Drizen)] describes a method of generating Latin Squares of order n attributed to Jacobson and Matthews. The purpose of this task is to produce a function which given a valid Latin Square transforms it to another using this method. part 1 Use one of the 4 ...
#Phix
Phix
function shuffleCube(sequence c) integer n = length(c), rx, ry, rz bool bProper = true while true do rx = rand(n) ry = rand(n) rz = rand(n) if c[rx][ry][rz] == 0 then exit end if end while while true do integer ox, oy, oz for i=1 to n do ox...
http://rosettacode.org/wiki/Levenshtein_distance
Levenshtein distance
This page uses content from Wikipedia. The original article was at Levenshtein distance. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) In information theory and computer science, the Levenshtein ...
#Bracmat
Bracmat
(levenshtein= lev cache . ( lev = s s0 s1 t t0 t1 L a b c val key . (cache..find)$(str$!arg:?key):(?.?val) & !val |  !arg:(?s,?t) & ( !s:&@(!t:? [?L) | !t:&@(!s:? [?L) ) & (cache..insert)$(!key.!L) & !L |  !arg:(@(?:%?...
http://rosettacode.org/wiki/Letter_frequency
Letter frequency
Task Open a text file and count the occurrences of each letter. Some of these programs count all characters (including punctuation), but some only count letters A to Z. Other tasks related to string operations: Metrics Array length String length Copy a string Empty string  (assignment) Counting Word frequen...
#APL
APL
freq←{(⍪∪⍵),+/(∪⍵)∘.⍷⍵}   freq 0 1 2 3 2 3 4 3 4 4 4 0 1 1 1 2 2 3 3 4 4   freq 'balloon' b 1 a 1 l 2 o 2 n 1
http://rosettacode.org/wiki/Loop_over_multiple_arrays_simultaneously
Loop over multiple arrays simultaneously
Task Loop over multiple arrays   (or lists or tuples or whatever they're called in your language)   and display the   i th   element of each. Use your language's   "for each"   loop if it has one, otherwise iterate through the collection in order with some other loop. For this example, loop over the arrays: ...
#XPL0
XPL0
string 0; \use zero terminated strings include c:\cxpl\codes; \intrinsic 'code' declarations char A1, A2; int A3, I; [A1:= "abc"; A2:= "ABC"; A3:= [1,2,3]; for I:= 0 to 2 do [ChOut(0, A1(I)); ChOut(0, A2(I)); IntOut(0, A3(I)); CrLf(0); ]; ]
http://rosettacode.org/wiki/List_comprehensions
List comprehensions
A list comprehension is a special syntax in some programming languages to describe lists. It is similar to the way mathematicians describe sets, with a set comprehension, hence the name. Some attributes of a list comprehension are: They should be distinct from (nested) for loops and the use of map and filter functio...
#MATLAB_.2F_Octave
MATLAB / Octave
N = 20 [a,b] = meshgrid(1:N, 1:N); c = sqrt(a.^2 + b.^2); [x,y] = find(c == fix(c)); disp([x, y, sqrt(x.^2 + y.^2)])