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/Cartesian_product_of_two_or_more_lists
Cartesian product of two or more lists
Task Show one or more idiomatic ways of generating the Cartesian product of two arbitrary lists in your language. Demonstrate that your function/method correctly returns: {1, 2} × {3, 4} = {(1, 3), (1, 4), (2, 3), (2, 4)} and, in contrast: {3, 4} × {1, 2} = {(3, 1), (3, 2), (4, 1), (4, 2)} Also demonstrate, using y...
#Ada
Ada
with Ada.Text_IO; use Ada.Text_Io; with Ada.Containers.Doubly_Linked_Lists; with Ada.Strings.Fixed;   procedure Cartesian is   type Element_Type is new Long_Integer;   package Lists is new Ada.Containers.Doubly_Linked_Lists (Element_Type); package List_Lists is new Ada.Containers.Doubly_Linked_Lis...
http://rosettacode.org/wiki/Casting_out_nines
Casting out nines
Task   (in three parts) Part 1 Write a procedure (say c o 9 ( x ) {\displaystyle {\mathit {co9}}(x)} ) which implements Casting Out Nines as described by returning the checksum for x {\displaystyle x} . Demonstrate the procedure using the examples given there, or others you may consider lucky. Par...
#11l
11l
F CastOut(Base, Start, End) V ran = (0 .< Base - 1).filter(y -> y % (@Base - 1) == (y * y) % (@Base - 1)) V (x, y) = divmod(Start, Base - 1) [Int] r L L(n) ran V k = (Base - 1) * x + n I k < Start L.continue I k > End R r r.append(k) x+...
http://rosettacode.org/wiki/Catamorphism
Catamorphism
Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value. Task Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language. See also Wikipedia article: ...
#Arturo
Arturo
; find the sum, with seed:0 (default) print fold [1 2 3 4] => (+)   ; find the product, with seed:1 print fold [1 2 3 4] .seed:1 => (*)
http://rosettacode.org/wiki/Chaocipher
Chaocipher
Description The Chaocipher was invented by J.F.Byrne in 1918 and, although simple by modern cryptographic standards, does not appear to have been broken until the algorithm was finally disclosed by his family in 2010. The algorithm is described in this paper by M.Rubin in 2010 and there is a C# implementation here. ...
#J
J
reset =: verb define LEFT =: 'HXUCZVAMDSLKPEFJRIGTWOBNYQ' RIGHT =: 'PTLNBQDEOYSFAVZKGJRIHWXUMC' )   enc =: verb define z =. LEFT {~ i =. RIGHT i. y permute {. i z )   dec =: verb define z =. RIGHT {~ i =. LEFT i. y permute {. i z )   permute =: verb define LEFT =: LEFT |.~ - y LEFT =: ...
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle
Catalan numbers/Pascal's triangle
Task Print out the first   15   Catalan numbers by extracting them from Pascal's triangle. See   Catalan Numbers and the Pascal Triangle.     This method enables calculation of Catalan Numbers using only addition and subtraction.   Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl...
#ERRE
ERRE
  PROGRAM CATALAN   !$DOUBLE   DIM CATALAN[50]   FUNCTION ODD(X) ODD=FRC(X/2)<>0 END FUNCTION   PROCEDURE GETCATALAN(L) LOCAL J,K,W LOCAL DIM PASTRI[100]   L=L*2 PASTRI[0]=1 J=0 WHILE J<L DO J+=1 K=INT((J+1)/2) PASTRI[K]=PASTRI[K-1] FOR W=K TO 1 STEP -1 DO ...
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle
Catalan numbers/Pascal's triangle
Task Print out the first   15   Catalan numbers by extracting them from Pascal's triangle. See   Catalan Numbers and the Pascal Triangle.     This method enables calculation of Catalan Numbers using only addition and subtraction.   Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl...
#F.23
F#
  let mutable nm=uint64(1) let mutable dm=uint64(1) let mutable a=uint64(1)   printf "1, " for i = 2 to 15 do nm<-uint64(1) dm<-uint64(1) for k = 2 to i do nm <-uint64( uint64(nm) * (uint64(i)+uint64(k))) dm <-uint64( uint64(dm) * uint64(k)) let a = uint64(uint64(nm)/uint64(dm)) prin...
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#D
D
import std.stdio;   void main() { string dog = "Benjamin"; // identifiers that start with capital letters are type names string Dog = "Samba"; string DOG = "Bernie"; writefln("There are three dogs named ", dog, ", ", Dog, ", and ", DOG, "'"); }
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#dc
dc
[Benjamin]sd [Samba]sD [The two dogs are named ]P ldP [ and ]P lDP [. ]P
http://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists
Cartesian product of two or more lists
Task Show one or more idiomatic ways of generating the Cartesian product of two arbitrary lists in your language. Demonstrate that your function/method correctly returns: {1, 2} × {3, 4} = {(1, 3), (1, 4), (2, 3), (2, 4)} and, in contrast: {3, 4} × {1, 2} = {(3, 1), (3, 2), (4, 1), (4, 2)} Also demonstrate, using y...
#APL
APL
cart ← ,∘.,
http://rosettacode.org/wiki/Casting_out_nines
Casting out nines
Task   (in three parts) Part 1 Write a procedure (say c o 9 ( x ) {\displaystyle {\mathit {co9}}(x)} ) which implements Casting Out Nines as described by returning the checksum for x {\displaystyle x} . Demonstrate the procedure using the examples given there, or others you may consider lucky. Par...
#360_Assembly
360 Assembly
* Casting out nines 08/02/2017 CASTOUT CSECT USING CASTOUT,R13 base register B 72(R15) skip savearea DC 17F'0' savearea STM R14,R12,12(R13) prolog ST R13,4(R15) " <- ST R15,8(R13) " -> LR ...
http://rosettacode.org/wiki/Catamorphism
Catamorphism
Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value. Task Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language. See also Wikipedia article: ...
#BASIC
BASIC
arraybase 1 global n dim n = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}   print " +: "; " "; cat(10, "+") print " -: "; " "; cat(10, "-") print " *: "; " "; cat(10, "*") print " /: "; " "; cat(10, "/") print " ^: "; " "; cat(10, "^") print "max: "; " "; cat(10, "max") print "min: "; " "; cat(10, "min") print "avg: "; " "; ca...
http://rosettacode.org/wiki/Chaocipher
Chaocipher
Description The Chaocipher was invented by J.F.Byrne in 1918 and, although simple by modern cryptographic standards, does not appear to have been broken until the algorithm was finally disclosed by his family in 2010. The algorithm is described in this paper by M.Rubin in 2010 and there is a C# implementation here. ...
#JavaScript
JavaScript
const L_ALPHABET = "HXUCZVAMDSLKPEFJRIGTWOBNYQ"; const R_ALPHABET = "PTLNBQDEOYSFAVZKGJRIHWXUMC";   const ENCRYPT = 0; const DECRYPT = 1;   function setCharAt(str, index, chr) { if (index > str.length - 1) return str; return str.substr(0, index) + chr + str.substr(index + 1); }   function chao(text, mode, show_...
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle
Catalan numbers/Pascal's triangle
Task Print out the first   15   Catalan numbers by extracting them from Pascal's triangle. See   Catalan Numbers and the Pascal Triangle.     This method enables calculation of Catalan Numbers using only addition and subtraction.   Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl...
#Factor
Factor
USING: arrays grouping io kernel math prettyprint sequences ; IN: rosetta-code.catalan-pascal   : next-row ( seq -- seq' ) 2 clump [ sum ] map 1 prefix 1 suffix ;   : pascal ( n -- seq ) 1 - { { 1 } } swap [ dup last next-row suffix ] times ;   15 2 * pascal [ length odd? ] filter [ dup length 1 = [ 1 ] ...
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#Delphi
Delphi
program CaseSensitiveIdentifiers;   {$APPTYPE CONSOLE}   var dog: string; begin dog := 'Benjamin'; Dog := 'Samba'; DOG := 'Bernie'; Writeln('There is just one dog named ' + dog); end.
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#DWScript
DWScript
  var dog : String;   dog := 'Benjamin'; Dog := 'Samba'; DOG := 'Bernie';   PrintLn('There is just one dog named ' + dog);
http://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists
Cartesian product of two or more lists
Task Show one or more idiomatic ways of generating the Cartesian product of two arbitrary lists in your language. Demonstrate that your function/method correctly returns: {1, 2} × {3, 4} = {(1, 3), (1, 4), (2, 3), (2, 4)} and, in contrast: {3, 4} × {1, 2} = {(3, 1), (3, 2), (4, 1), (4, 2)} Also demonstrate, using y...
#AppleScript
AppleScript
-- CARTESIAN PRODUCTS ---------------------------------------------------------   -- Two lists:   -- cartProd :: [a] -> [b] -> [(a, b)] on cartProd(xs, ys) script on |λ|(x) script on |λ|(y) [[x, y]] end |λ| end script co...
http://rosettacode.org/wiki/Catalan_numbers
Catalan numbers
Catalan numbers You are encouraged to solve this task according to the task description, using any language you may know. Catalan numbers are a sequence of numbers which can be defined directly: C n = 1 n + 1 ( 2 n n ) = ( 2 n ) ! ( n + 1 ) ! n !  for  n ≥ 0. {\displaystyle C...
#11l
11l
V c = 1 L(n) 1..15 print(c) c = 2 * (2 * n - 1) * c I/ (n + 1)
http://rosettacode.org/wiki/Casting_out_nines
Casting out nines
Task   (in three parts) Part 1 Write a procedure (say c o 9 ( x ) {\displaystyle {\mathit {co9}}(x)} ) which implements Casting Out Nines as described by returning the checksum for x {\displaystyle x} . Demonstrate the procedure using the examples given there, or others you may consider lucky. Par...
#Action.21
Action!
INT FUNC Power(INT a,b) INT i,res   res=1 FOR i=1 TO b DO res==*a OD RETURN (res)   PROC Main() DEFINE BASE="10" DEFINE N="2" INT i,max,count,total,perc   max=Power(BASE,N) count=0 total=0 FOR i=1 TO max DO total==+1 IF i MOD (BASE-1)=(i*i) MOD (BASE-1) THEN count==+1 Pri...
http://rosettacode.org/wiki/Catamorphism
Catamorphism
Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value. Task Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language. See also Wikipedia article: ...
#BBC_BASIC
BBC BASIC
  DIM a(4) a() = 1, 2, 3, 4, 5 PRINT FNreduce(a(), "+") PRINT FNreduce(a(), "-") PRINT FNreduce(a(), "*") END   DEF FNreduce(arr(), op$) REM!Keep tmp, arr() LOCAL I%, tmp tmp = arr(0) FOR I% = 1 TO DIM(arr(), 1) tmp = EVAL("tmp " + op$ + " arr(I%...
http://rosettacode.org/wiki/Chaocipher
Chaocipher
Description The Chaocipher was invented by J.F.Byrne in 1918 and, although simple by modern cryptographic standards, does not appear to have been broken until the algorithm was finally disclosed by his family in 2010. The algorithm is described in this paper by M.Rubin in 2010 and there is a C# implementation here. ...
#Julia
Julia
const leftalphabet = "HXUCZVAMDSLKPEFJRIGTWOBNYQ" const rightalphabet = "PTLNBQDEOYSFAVZKGJRIHWXUMC"   function chacocoding(text, encoding, verbose=false) left, right = Vector{Char}(leftalphabet), Vector{Char}(rightalphabet) len, coded = length(text), similar(Vector{Char}(text)) for i in 1:len verbo...
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle
Catalan numbers/Pascal's triangle
Task Print out the first   15   Catalan numbers by extracting them from Pascal's triangle. See   Catalan Numbers and the Pascal Triangle.     This method enables calculation of Catalan Numbers using only addition and subtraction.   Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl...
#FreeBASIC
FreeBASIC
' version 15-09-2015 ' compile with: fbc -s console   #Define size 31 ' (N * 2 + 1)   Sub pascal_triangle(rows As Integer, Pas_tri() As ULongInt)   Dim As Integer x, y   For x = 1 To rows Pas_tri(1,x) = 1 Pas_tri(x,1) = 1 Next   For x = 2 To rows For y = 2 To rows...
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#D.C3.A9j.C3.A0_Vu
Déjà Vu
local :dog "Benjamin" local :Dog "Samba" local :DOG "Bernie"   !print( "There are three dogs named " dog ", " Dog " and " DOG "." )
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#EchoLisp
EchoLisp
  (define dog "Benjamin") (define Dog "Samba") (define DOG "Bernie")   (printf "The three dogs are named %a, %a and %a. " dog Dog DOG) The three dogs are named Benjamin, Samba and Bernie.  
http://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists
Cartesian product of two or more lists
Task Show one or more idiomatic ways of generating the Cartesian product of two arbitrary lists in your language. Demonstrate that your function/method correctly returns: {1, 2} × {3, 4} = {(1, 3), (1, 4), (2, 3), (2, 4)} and, in contrast: {3, 4} × {1, 2} = {(3, 1), (3, 2), (4, 1), (4, 2)} Also demonstrate, using y...
#Arturo
Arturo
loop [ [[1 2][3 4]] [[3 4][1 2]] [[1 2][]] [[][1 2]] [[1776 1789][7 12][4 14 23][0 1]] [[1 2 3][30][500 100]] [[1 2 3][][500 100]] ] 'lst [ print as.code product.cartesian lst ]
http://rosettacode.org/wiki/Catalan_numbers
Catalan numbers
Catalan numbers You are encouraged to solve this task according to the task description, using any language you may know. Catalan numbers are a sequence of numbers which can be defined directly: C n = 1 n + 1 ( 2 n n ) = ( 2 n ) ! ( n + 1 ) ! n !  for  n ≥ 0. {\displaystyle C...
#360_Assembly
360 Assembly
CATALAN CSECT 08/09/2015 USING CATALAN,R15 LA R7,1 c=1 LA R6,1 i=1 LOOPI CH R6,=H'15' do i=1 to 15 BH ELOOPI XDECO R6,PG edit i LR R5,R6 i SLA R5,1 ...
http://rosettacode.org/wiki/Casting_out_nines
Casting out nines
Task   (in three parts) Part 1 Write a procedure (say c o 9 ( x ) {\displaystyle {\mathit {co9}}(x)} ) which implements Casting Out Nines as described by returning the checksum for x {\displaystyle x} . Demonstrate the procedure using the examples given there, or others you may consider lucky. Par...
#ALGOL_68
ALGOL 68
BEGIN # casting out nines - translated from the Action! sample # INT base = 10; INT n = 2; INT count := 0; INT total := 0; FOR i TO base ^ n DO total +:= 1; IF i MOD ( base - 1 ) = ( i * i ) MOD ( base - 1 ) THEN count +:= 1; print( ( whole( i, 0 ), ...
http://rosettacode.org/wiki/Catamorphism
Catamorphism
Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value. Task Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language. See also Wikipedia article: ...
#BCPL
BCPL
get "libhdr"   let reduce(f, v, len, seed) = len = 0 -> seed, reduce(f, v+1, len-1, f(!v, seed))   let start() be $( let add(x, y) = x+y let mul(x, y) = x*y   let nums = table 1,2,3,4,5,6,7   writef("%N*N", reduce(add, nums, 7, 0)) writef("%N*N", reduce(mul, nums, 7, 1)) $)
http://rosettacode.org/wiki/Chaocipher
Chaocipher
Description The Chaocipher was invented by J.F.Byrne in 1918 and, although simple by modern cryptographic standards, does not appear to have been broken until the algorithm was finally disclosed by his family in 2010. The algorithm is described in this paper by M.Rubin in 2010 and there is a C# implementation here. ...
#Kotlin
Kotlin
// Version 1.2.40   enum class Mode { ENCRYPT, DECRYPT }   object Chao { private val lAlphabet = "HXUCZVAMDSLKPEFJRIGTWOBNYQ" private val rAlphabet = "PTLNBQDEOYSFAVZKGJRIHWXUMC"   fun exec(text: String, mode: Mode, showSteps: Boolean = false): String { var left = lAlphabet var right = rAlp...
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle
Catalan numbers/Pascal's triangle
Task Print out the first   15   Catalan numbers by extracting them from Pascal's triangle. See   Catalan Numbers and the Pascal Triangle.     This method enables calculation of Catalan Numbers using only addition and subtraction.   Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl...
#Go
Go
package main   import "fmt"   func main() { const n = 15 t := [n + 2]uint64{0, 1} for i := 1; i <= n; i++ { for j := i; j > 1; j-- { t[j] += t[j-1] } t[i+1] = t[i] for j := i + 1; j > 1; j-- { t[j] += t[j-1] } fmt.Printf("%2d : %d\n", i...
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#Elena
Elena
import extensions;   public program() { var dog := "Benjamin"; var Dog := "Samba"; var DOG := "Bernie"; console.printLineFormatted("The three dogs are named {0}, {1} and {2}", dog, Dog, DOG) }
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#Elixir
Elixir
dog = "Benjamin" doG = "Samba" dOG = "Bernie" IO.puts "The three dogs are named #{dog}, #{doG} and #{dOG}."
http://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists
Cartesian product of two or more lists
Task Show one or more idiomatic ways of generating the Cartesian product of two arbitrary lists in your language. Demonstrate that your function/method correctly returns: {1, 2} × {3, 4} = {(1, 3), (1, 4), (2, 3), (2, 4)} and, in contrast: {3, 4} × {1, 2} = {(3, 1), (3, 2), (4, 1), (4, 2)} Also demonstrate, using y...
#Bracmat
Bracmat
( ( mul = R a b A B .  :?R & !arg:(.?A) (.?B) & (  !A  :  ? ( %@?a &  !B  :  ? ( (%@?b|(.?b)) & !R (.!a !b):?R & ~ )  ? ) ...
http://rosettacode.org/wiki/Catalan_numbers
Catalan numbers
Catalan numbers You are encouraged to solve this task according to the task description, using any language you may know. Catalan numbers are a sequence of numbers which can be defined directly: C n = 1 n + 1 ( 2 n n ) = ( 2 n ) ! ( n + 1 ) ! n !  for  n ≥ 0. {\displaystyle C...
#ABAP
ABAP
  report z_catalan_numbers.   class catalan_numbers definition. public section. class-methods: get_nth_number importing i_n type int4 returning value(r_catalan_number) type int4. endclass.   class catalan_numbers implementation. method get_nth_number...
http://rosettacode.org/wiki/Casting_out_nines
Casting out nines
Task   (in three parts) Part 1 Write a procedure (say c o 9 ( x ) {\displaystyle {\mathit {co9}}(x)} ) which implements Casting Out Nines as described by returning the checksum for x {\displaystyle x} . Demonstrate the procedure using the examples given there, or others you may consider lucky. Par...
#Arturo
Arturo
N: 2 base: 10 c1: 0 c2: 0   loop 1..(base^N)-1 'k [ c1: c1 + 1   if (k%base-1)= (k*k)%base-1 [ c2: c2 + 1 prints ~"|k| " ] ]   print "" print ["Trying" c2 "numbers instead of" c1 "numbers saves" 100.0 - 100.0*c2//c1 "%"]
http://rosettacode.org/wiki/Catamorphism
Catamorphism
Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value. Task Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language. See also Wikipedia article: ...
#BQN
BQN
•Show +´ 30‿1‿20‿2‿10 •Show +˝ 30‿1‿20‿2‿10 •Show tab ← (2+↕5) |⌜ 9+↕3 •Show +˝ tab
http://rosettacode.org/wiki/Chaocipher
Chaocipher
Description The Chaocipher was invented by J.F.Byrne in 1918 and, although simple by modern cryptographic standards, does not appear to have been broken until the algorithm was finally disclosed by his family in 2010. The algorithm is described in this paper by M.Rubin in 2010 and there is a C# implementation here. ...
#Lua
Lua
-- Chaocipher, in Lua, 6/19/2020 db local Chaocipher = { ct = "HXUCZVAMDSLKPEFJRIGTWOBNYQ", pt = "PTLNBQDEOYSFAVZKGJRIHWXUMC", encrypt = function(self, text) return self:_encdec(text, true) end, decrypt = function(self, text) return self:_encdec(text, false) end, _encdec = function(self, text, encflag) lo...
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle
Catalan numbers/Pascal's triangle
Task Print out the first   15   Catalan numbers by extracting them from Pascal's triangle. See   Catalan Numbers and the Pascal Triangle.     This method enables calculation of Catalan Numbers using only addition and subtraction.   Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl...
#Groovy
Groovy
  class Catalan { public static void main(String[] args) { BigInteger N = 15; BigInteger k,n,num,den; BigInteger catalan; print(1); for(n=2;n<=N;n++) { num = 1; den = 1; for(k=2;k<=n;k++) { num = num*(n+k);...
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#Erlang
Erlang
  -module( case_sensitivity_of_identifiers ).   -export( [task/0] ).   task() -> catch dog = "Benjamin", % Function will crash without catch Dog = "Samba", DOG = "Bernie", io:fwrite( "The three dogs are named ~s, ~s and ~s~n", [dog, Dog, DOG] ).  
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#Euphoria
Euphoria
-- These variables are all different sequence dog = "Benjamin" sequence Dog = "Samba" sequence DOG = "Bernie" printf( 1, "The three dogs are named %s, %s and %s\n", {dog, Dog, DOG} )
http://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists
Cartesian product of two or more lists
Task Show one or more idiomatic ways of generating the Cartesian product of two arbitrary lists in your language. Demonstrate that your function/method correctly returns: {1, 2} × {3, 4} = {(1, 3), (1, 4), (2, 3), (2, 4)} and, in contrast: {3, 4} × {1, 2} = {(3, 1), (3, 2), (4, 1), (4, 2)} Also demonstrate, using y...
#C
C
  #include<string.h> #include<stdlib.h> #include<stdio.h>   void cartesianProduct(int** sets, int* setLengths, int* currentSet, int numSets, int times){ int i,j;   if(times==numSets){ printf("("); for(i=0;i<times;i++){ printf("%d,",currentSet[i]); } printf("\b),"); } else{ for(j=0;j<setLengths[times];j...
http://rosettacode.org/wiki/Catalan_numbers
Catalan numbers
Catalan numbers You are encouraged to solve this task according to the task description, using any language you may know. Catalan numbers are a sequence of numbers which can be defined directly: C n = 1 n + 1 ( 2 n n ) = ( 2 n ) ! ( n + 1 ) ! n !  for  n ≥ 0. {\displaystyle C...
#Action.21
Action!
INCLUDE "D2:REAL.ACT" ;from the Action! Tool Ki   PROC Main() REAL c,rnom,rden BYTE n,nom,den   Put(125) PutE() ;clear the screen IntToReal(1,c)   FOR n=1 TO 15 DO nom=(n LSH 1-1) LSH 1 den=n+1 IntToReal(nom,rnom) IntToReal(den,rden) RealMult(c,rnom,c) RealDiv(c,rden,c) PrintF("C...
http://rosettacode.org/wiki/Casting_out_nines
Casting out nines
Task   (in three parts) Part 1 Write a procedure (say c o 9 ( x ) {\displaystyle {\mathit {co9}}(x)} ) which implements Casting Out Nines as described by returning the checksum for x {\displaystyle x} . Demonstrate the procedure using the examples given there, or others you may consider lucky. Par...
#AWK
AWK
  # syntax: GAWK -f CASTING_OUT_NINES.AWK # converted from C BEGIN { base = 10 for (k=1; k<=base^2; k++) { c1++ if (k % (base-1) == (k*k) % (base-1)) { c2++ printf("%d ",k) } } printf("\nTrying %d numbers instead of %d numbers saves %.2f%%\n",c2,c1,100-(100*c2/c1)) ...
http://rosettacode.org/wiki/Catmull%E2%80%93Clark_subdivision_surface
Catmull–Clark subdivision surface
Implement the Catmull-Clark surface subdivision (description on Wikipedia), which is an algorithm that maps from a surface (described as a set of points and a set of polygons with vertices at those points) to another more refined surface. The resulting surface will always consist of a mesh of quadrilaterals. The proce...
#C
C
vertex face_point(face f) { int i; vertex v;   if (!f->avg) { f->avg = vertex_new(); foreach(i, v, f->v) if (!i) f->avg->pos = v->pos; else vadd(f->avg->pos, v->pos);   vdiv(f->avg->pos, len(f->v)); } return f->avg; }   #define hole_edge(e) (len(e->f)==1) vertex edge_point(edge e) { int i; face f;...
http://rosettacode.org/wiki/Carmichael_3_strong_pseudoprimes
Carmichael 3 strong pseudoprimes
A lot of composite numbers can be separated from primes by Fermat's Little Theorem, but there are some that completely confound it. The   Miller Rabin Test   uses a combination of Fermat's Little Theorem and Chinese Division Theorem to overcome this. The purpose of this task is to investigate such numbers using a met...
#11l
11l
F mod_(n, m) R ((n % m) + m) % m   F is_prime(n) I n C (2, 3) R 1B E I n < 2 | n % 2 == 0 | n % 3 == 0 R 0B V div = 5 V inc = 2 L div ^ 2 <= n I n % div == 0 R 0B div += inc inc = 6 - inc R 1B   L(p) 2 .< 62 I !is_prime(p) L.continue L(h3) 2 .< p ...
http://rosettacode.org/wiki/Catamorphism
Catamorphism
Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value. Task Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language. See also Wikipedia article: ...
#Bracmat
Bracmat
( ( fold = f xs init first rest .  !arg:(?f.?xs.?init) & ( !xs:&!init |  !xs:%?first ?rest & !f$(!first.fold$(!f.!rest.!init)) ) ) & out $ ( fold $ ( (=a b.!arg:(?a.?b)&!a+!b) . 1 2 3 4 5 . 0 ) ) & (product=a b.!arg:(?a.?b)&!a*!b) & out$(fold$(pr...
http://rosettacode.org/wiki/Chaocipher
Chaocipher
Description The Chaocipher was invented by J.F.Byrne in 1918 and, although simple by modern cryptographic standards, does not appear to have been broken until the algorithm was finally disclosed by his family in 2010. The algorithm is described in this paper by M.Rubin in 2010 and there is a C# implementation here. ...
#Mathematica.2FWolfram_Language
Mathematica/Wolfram Language
ClearAll[ichaoalphabet, iMoveToFront, ChaoCipher] ichaoalphabet = CharacterRange["A", "Z"]; iMoveToFront[l_List, sel_] := Module[{p}, p = FirstPosition[l, sel]; RotateLeft[l, p - 1] ] ChaoCipher::wrongcipheralpha = "The cipher alphabet `1` is not a permutation of \ \"A\"\[LongDash]\"Z\"."; ChaoCipher::wrongpla...
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle
Catalan numbers/Pascal's triangle
Task Print out the first   15   Catalan numbers by extracting them from Pascal's triangle. See   Catalan Numbers and the Pascal Triangle.     This method enables calculation of Catalan Numbers using only addition and subtraction.   Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl...
#Haskell
Haskell
import System.Environment (getArgs)   -- Pascal's triangle. pascal :: [[Integer]] pascal = [1] : map (\row -> 1 : zipWith (+) row (tail row) ++ [1]) pascal   -- The Catalan numbers from Pascal's triangle. This uses a method from -- http://www.cut-the-knot.org/arithmetic/algebra/CatalanInPascal.shtml -- (see "Grimaldi"...
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#F.23
F#
let dog = "Benjamin" let Dog = "Samba" let DOG = "Bernie" printfn "There are three dogs named %s, %s and %s" dog Dog DOG
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#Factor
Factor
USING: formatting locals ; IN: scratchpad [let "Benjamin" :> dog "Samba"  :> Dog "Bernie"  :> DOG { dog Dog DOG } "There are three dogs named %s, %s, and %s." vprintf ]
http://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists
Cartesian product of two or more lists
Task Show one or more idiomatic ways of generating the Cartesian product of two arbitrary lists in your language. Demonstrate that your function/method correctly returns: {1, 2} × {3, 4} = {(1, 3), (1, 4), (2, 3), (2, 4)} and, in contrast: {3, 4} × {1, 2} = {(3, 1), (3, 2), (4, 1), (4, 2)} Also demonstrate, using y...
#C.23
C#
using System; public class Program { public static void Main() { int[] empty = new int[0]; int[] list1 = { 1, 2 }; int[] list2 = { 3, 4 }; int[] list3 = { 1776, 1789 }; int[] list4 = { 7, 12 }; int[] list5 = { 4, 14, 23 }; int[] list6 = { 0, 1 }; i...
http://rosettacode.org/wiki/Catalan_numbers
Catalan numbers
Catalan numbers You are encouraged to solve this task according to the task description, using any language you may know. Catalan numbers are a sequence of numbers which can be defined directly: C n = 1 n + 1 ( 2 n n ) = ( 2 n ) ! ( n + 1 ) ! n !  for  n ≥ 0. {\displaystyle C...
#Ada
Ada
with Ada.Text_IO; use Ada.Text_IO;   procedure Test_Catalan is function Catalan (N : Natural) return Natural is Result : Positive := 1; begin for I in 1..N loop Result := Result * 2 * (2 * I - 1) / (I + 1); end loop; return Result; end Catalan; begin for N in 0..15 loop ...
http://rosettacode.org/wiki/Casting_out_nines
Casting out nines
Task   (in three parts) Part 1 Write a procedure (say c o 9 ( x ) {\displaystyle {\mathit {co9}}(x)} ) which implements Casting Out Nines as described by returning the checksum for x {\displaystyle x} . Demonstrate the procedure using the examples given there, or others you may consider lucky. Par...
#C
C
#include <stdio.h> #include <math.h>   int main() { const int N = 2; int base = 10; int c1 = 0; int c2 = 0; int k;   for (k = 1; k < pow(base, N); k++) { c1++; if (k % (base - 1) == (k * k) % (base - 1)) { c2++; printf("%d ", k); } }   prin...
http://rosettacode.org/wiki/Catmull%E2%80%93Clark_subdivision_surface
Catmull–Clark subdivision surface
Implement the Catmull-Clark surface subdivision (description on Wikipedia), which is an algorithm that maps from a surface (described as a set of points and a set of polygons with vertices at those points) to another more refined surface. The resulting surface will always consist of a mesh of quadrilaterals. The proce...
#Go
Go
package main   import ( "fmt" "sort" )   type ( Point [3]float64 Face []int   Edge struct { pn1 int // point number 1 pn2 int // point number 2 fn1 int // face number 1 fn2 int // face number 2 cp Point // center point }   PointEx struct { ...
http://rosettacode.org/wiki/Carmichael_3_strong_pseudoprimes
Carmichael 3 strong pseudoprimes
A lot of composite numbers can be separated from primes by Fermat's Little Theorem, but there are some that completely confound it. The   Miller Rabin Test   uses a combination of Fermat's Little Theorem and Chinese Division Theorem to overcome this. The purpose of this task is to investigate such numbers using a met...
#Ada
Ada
with Ada.Text_IO, Miller_Rabin;   procedure Nemesis is   type Number is range 0 .. 2**40-1; -- sufficiently large for the task   function Is_Prime(N: Number) return Boolean is package MR is new Miller_Rabin(Number); use MR; begin return MR.Is_Prime(N) = Probably_Prime; end Is_Prime;   begin f...
http://rosettacode.org/wiki/Catamorphism
Catamorphism
Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value. Task Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language. See also Wikipedia article: ...
#C
C
#include <stdio.h>   typedef int (*intFn)(int, int);   int reduce(intFn fn, int size, int *elms) { int i, val = *elms; for (i = 1; i < size; ++i) val = fn(val, elms[i]); return val; }   int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } int mul(int a, int b) { return a *...
http://rosettacode.org/wiki/Chaocipher
Chaocipher
Description The Chaocipher was invented by J.F.Byrne in 1918 and, although simple by modern cryptographic standards, does not appear to have been broken until the algorithm was finally disclosed by his family in 2010. The algorithm is described in this paper by M.Rubin in 2010 and there is a C# implementation here. ...
#Nim
Nim
import strformat   type Mode = enum Encrypt Decrypt   const lAlphabet: string = "HXUCZVAMDSLKPEFJRIGTWOBNYQ" const rAlphabet: string = "PTLNBQDEOYSFAVZKGJRIHWXUMC"   proc chao(text: string, mode: Mode, verbose: bool = false): string = var left = lAlphabet var right = rAlphabet var eText = newSeq[char](t...
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle
Catalan numbers/Pascal's triangle
Task Print out the first   15   Catalan numbers by extracting them from Pascal's triangle. See   Catalan Numbers and the Pascal Triangle.     This method enables calculation of Catalan Numbers using only addition and subtraction.   Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl...
#Icon_and_Unicon
Icon and Unicon
link math   procedure main(A) limit := (integer(A[1])|15)+1 every write(right(binocoef(i := 2*seq(0)\limit,i/2)-binocoef(i,i/2+1),30)) end
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#Forth
Forth
: DOG ." Benjamin" ; : Dog ." Samba" ; : dog ." Bernie" ; : HOWMANYDOGS ." There is just one dog named " DOG ; HOWMANYDOGS
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#Fortran
Fortran
program Example implicit none   character(8) :: dog, Dog, DOG   dog = "Benjamin" Dog = "Samba" DOG = "Bernie"   if (dog == DOG) then write(*,*) "There is just one dog named ", dog else write(*,*) "The three dogs are named ", dog, Dog, " and ", DOG end if   end program Example
http://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists
Cartesian product of two or more lists
Task Show one or more idiomatic ways of generating the Cartesian product of two arbitrary lists in your language. Demonstrate that your function/method correctly returns: {1, 2} × {3, 4} = {(1, 3), (1, 4), (2, 3), (2, 4)} and, in contrast: {3, 4} × {1, 2} = {(3, 1), (3, 2), (4, 1), (4, 2)} Also demonstrate, using y...
#C.2B.2B
C++
  #include <iostream> #include <vector> #include <algorithm>   void print(const std::vector<std::vector<int>>& v) { std::cout << "{ "; for (const auto& p : v) { std::cout << "("; for (const auto& e : p) { std::cout << e << " "; } std::cout << ") "; } std::cout << "}" << std::endl; }   auto...
http://rosettacode.org/wiki/Catalan_numbers
Catalan numbers
Catalan numbers You are encouraged to solve this task according to the task description, using any language you may know. Catalan numbers are a sequence of numbers which can be defined directly: C n = 1 n + 1 ( 2 n n ) = ( 2 n ) ! ( n + 1 ) ! n !  for  n ≥ 0. {\displaystyle C...
#ALGOL_68
ALGOL 68
# calculate the first few catalan numbers, using LONG INT values # # (64-bit quantities in Algol 68G which can handle up to C23) #   # returns n!/k! # PROC factorial over factorial = ( INT n, k )LONG INT: IF k > n THEN 0 ELIF k =...
http://rosettacode.org/wiki/Canonicalize_CIDR
Canonicalize CIDR
Task Implement a function or program that, given a range of IPv4 addresses in CIDR notation (dotted-decimal/network-bits), will return/output the same range in canonical form. That is, the IP address portion of the output CIDR block must not contain any set (1) bits in the host part of the address. Example Given ...
#11l
11l
F cidr_parse(str) V (addr_str, m_str) = str.split(‘/’) V (a, b, c, d) = addr_str.split(‘.’).map(Int) V m = Int(m_str) I m < 1 | m > 32 | a < 0 | a > 255 | b < 0 | b > 255 | c < 0 | c > 255 | d < 0 | d > 255 R (0, 0) V mask = (-)((1 << (32 - m)) - 1) V address = (a << 24) + (b...
http://rosettacode.org/wiki/Casting_out_nines
Casting out nines
Task   (in three parts) Part 1 Write a procedure (say c o 9 ( x ) {\displaystyle {\mathit {co9}}(x)} ) which implements Casting Out Nines as described by returning the checksum for x {\displaystyle x} . Demonstrate the procedure using the examples given there, or others you may consider lucky. Par...
#C.2B.2B
C++
// Casting Out Nines // // Nigel Galloway. June 24th., 2012 // #include <iostream> int main() { int Base = 10; const int N = 2; int c1 = 0; int c2 = 0; for (int k=1; k<pow((double)Base,N); k++){ c1++; if (k%(Base-1) == (k*k)%(Base-1)){ c2++; std::cout << k << " "; } } std::cout << "\nTrying " << c2 <...
http://rosettacode.org/wiki/Catmull%E2%80%93Clark_subdivision_surface
Catmull–Clark subdivision surface
Implement the Catmull-Clark surface subdivision (description on Wikipedia), which is an algorithm that maps from a surface (described as a set of points and a set of polygons with vertices at those points) to another more refined surface. The resulting surface will always consist of a mesh of quadrilaterals. The proce...
#Haskell
Haskell
{-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE ScopedTypeVariables #-}   import Data.Array import Data.Foldable (length, concat, sum) import Data.List (genericLength) import Data.Maybe (mapMaybe) import Prelude hiding (length, concat, sum) import qualified Data.Map.Strict as Map   {- A SimpleMesh consists of ...
http://rosettacode.org/wiki/Carmichael_3_strong_pseudoprimes
Carmichael 3 strong pseudoprimes
A lot of composite numbers can be separated from primes by Fermat's Little Theorem, but there are some that completely confound it. The   Miller Rabin Test   uses a combination of Fermat's Little Theorem and Chinese Division Theorem to overcome this. The purpose of this task is to investigate such numbers using a met...
#ALGOL_68
ALGOL 68
# sieve of Eratosthene: sets s[i] to TRUE if i is prime, FALSE otherwise # PROC sieve = ( REF[]BOOL s )VOID: BEGIN # start with everything flagged as prime # FOR i TO UPB s DO s[ i ] := TRUE OD; # sieve out the non-primes ...
http://rosettacode.org/wiki/Catamorphism
Catamorphism
Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value. Task Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language. See also Wikipedia article: ...
#C.23
C#
var nums = Enumerable.Range(1, 10);   int summation = nums.Aggregate((a, b) => a + b);   int product = nums.Aggregate((a, b) => a * b);   string concatenation = nums.Aggregate(String.Empty, (a, b) => a.ToString() + b.ToString());   Console.WriteLine("{0} {1} {2}", summation, product, concatenation);
http://rosettacode.org/wiki/Chaocipher
Chaocipher
Description The Chaocipher was invented by J.F.Byrne in 1918 and, although simple by modern cryptographic standards, does not appear to have been broken until the algorithm was finally disclosed by his family in 2010. The algorithm is described in this paper by M.Rubin in 2010 and there is a C# implementation here. ...
#Objeck
Objeck
class Chaocipher { L_ALPHABET : static : Char[]; R_ALPHABET : static : Char[];   function : Main(args : String[]) ~ Nil { L_ALPHABET := "HXUCZVAMDSLKPEFJRIGTWOBNYQ"->ToCharArray(); R_ALPHABET := "PTLNBQDEOYSFAVZKGJRIHWXUMC"->ToCharArray(); plainText := "WELLDONEISBETTERTHANWELLSAID"->ToCharArray();   ...
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle
Catalan numbers/Pascal's triangle
Task Print out the first   15   Catalan numbers by extracting them from Pascal's triangle. See   Catalan Numbers and the Pascal Triangle.     This method enables calculation of Catalan Numbers using only addition and subtraction.   Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl...
#J
J
Catalan=. }:@:(}.@:((<0 1)&|:) - }:@:((<0 1)&|:@:(2&|.)))@:(i. +/\@]^:[ #&1)@:(2&+)
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#FreeBASIC
FreeBASIC
' FB 1.05.0 Win64   ' FreeBASIC is case-insensitive Dim dog As String dog = "Benjamin" Dog = "Samba" DOG = "Bernie" Print "There is just one dog, named "; dog Sleep
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#Frink
Frink
dog = "Benjamin" Dog = "Samba" DOG = "Bernie" println["There are three dogs named $dog, $Dog and $DOG"]
http://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists
Cartesian product of two or more lists
Task Show one or more idiomatic ways of generating the Cartesian product of two arbitrary lists in your language. Demonstrate that your function/method correctly returns: {1, 2} × {3, 4} = {(1, 3), (1, 4), (2, 3), (2, 4)} and, in contrast: {3, 4} × {1, 2} = {(3, 1), (3, 2), (4, 1), (4, 2)} Also demonstrate, using y...
#Clojure
Clojure
  (ns clojure.examples.product (:gen-class) (:require [clojure.pprint :as pp]))   (defn cart [colls] "Compute the cartesian product of list of lists" (if (empty? colls) '(()) (for [more (cart (rest colls)) x (first colls)] (cons x more))))  
http://rosettacode.org/wiki/Catalan_numbers
Catalan numbers
Catalan numbers You are encouraged to solve this task according to the task description, using any language you may know. Catalan numbers are a sequence of numbers which can be defined directly: C n = 1 n + 1 ( 2 n n ) = ( 2 n ) ! ( n + 1 ) ! n !  for  n ≥ 0. {\displaystyle C...
#ALGOL_W
ALGOL W
begin  % print the catalan numbers up to C15 % integer Cprev; Cprev := 1; % C0 % write( s_w := 0, i_w := 3, 0, ": ", i_w := 9, Cprev ); for n := 1 until 15 do begin Cprev := round( ( ( ( 4 * n ) - 2 ) / ( n + 1 ) ) * Cprev ); write( s_w := 0, i_w := 3, n, ": ", i_w := 9, Cprev ); ...
http://rosettacode.org/wiki/Canonicalize_CIDR
Canonicalize CIDR
Task Implement a function or program that, given a range of IPv4 addresses in CIDR notation (dotted-decimal/network-bits), will return/output the same range in canonical form. That is, the IP address portion of the output CIDR block must not contain any set (1) bits in the host part of the address. Example Given ...
#ALGOL_68
ALGOL 68
BEGIN # show IPv4 addresses in CIDR notation in canonical form # # mode to hold an IPv4 address in CIDR notation # MODE CIDR = STRUCT( BITS address , INT network bits , BOOL valid , STRING error ); # returns a CID...
http://rosettacode.org/wiki/Casting_out_nines
Casting out nines
Task   (in three parts) Part 1 Write a procedure (say c o 9 ( x ) {\displaystyle {\mathit {co9}}(x)} ) which implements Casting Out Nines as described by returning the checksum for x {\displaystyle x} . Demonstrate the procedure using the examples given there, or others you may consider lucky. Par...
#C.23
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text;   namespace CastingOutNines { public static class Helper { public static string AsString<T>(this IEnumerable<T> e) { var it = e.GetEnumerator();   StringBuilder builder = new StringBuilder(); ...
http://rosettacode.org/wiki/Catmull%E2%80%93Clark_subdivision_surface
Catmull–Clark subdivision surface
Implement the Catmull-Clark surface subdivision (description on Wikipedia), which is an algorithm that maps from a surface (described as a set of points and a set of polygons with vertices at those points) to another more refined surface. The resulting surface will always consist of a mesh of quadrilaterals. The proce...
#J
J
avg=: +/ % #   havePoints=: e."1/~ i.@#   catmullclark=:3 :0 'mesh points'=. y face_point=. avg"2 mesh{points point_face=. |: mesh havePoints points avg_face_points=. point_face avg@#"1 2 face_point edges=. ~.,/ meshEdges=. mesh /:~@,"+1|."1 mesh edge_face=. *./"2 edges e."0 1/ mesh edge_center=. avg"2 ed...
http://rosettacode.org/wiki/Carmichael_3_strong_pseudoprimes
Carmichael 3 strong pseudoprimes
A lot of composite numbers can be separated from primes by Fermat's Little Theorem, but there are some that completely confound it. The   Miller Rabin Test   uses a combination of Fermat's Little Theorem and Chinese Division Theorem to overcome this. The purpose of this task is to investigate such numbers using a met...
#AWK
AWK
  # syntax: GAWK -f CARMICHAEL_3_STRONG_PSEUDOPRIMES.AWK # converted from C BEGIN { printf("%5s%8s%8s%13s\n","P1","P2","P3","PRODUCT") for (p1=2; p1<62; p1++) { if (!is_prime(p1)) { continue } for (h3=1; h3<p1; h3++) { for (d=1; d<h3+p1; d++) { if ((h3+p1)*(p1-1)%d == 0 && mod(-p1*...
http://rosettacode.org/wiki/Catamorphism
Catamorphism
Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value. Task Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language. See also Wikipedia article: ...
#C.2B.2B
C++
#include <iostream> #include <numeric> #include <functional> #include <vector>   int main() { std::vector<int> nums = { 1, 2, 3, 4, 5 }; auto nums_added = std::accumulate(std::begin(nums), std::end(nums), 0, std::plus<int>()); auto nums_other = std::accumulate(std::begin(nums), std::end(nums), 0, [](const int& a, co...
http://rosettacode.org/wiki/Chaocipher
Chaocipher
Description The Chaocipher was invented by J.F.Byrne in 1918 and, although simple by modern cryptographic standards, does not appear to have been broken until the algorithm was finally disclosed by his family in 2010. The algorithm is described in this paper by M.Rubin in 2010 and there is a C# implementation here. ...
#Pascal
Pascal
program chaocipher(input, output);   const { This denotes a `set` literal: } alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; { The `card` function is an Extended Pascal (ISO 10206) extension. } alphabetCardinality = car...
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle
Catalan numbers/Pascal's triangle
Task Print out the first   15   Catalan numbers by extracting them from Pascal's triangle. See   Catalan Numbers and the Pascal Triangle.     This method enables calculation of Catalan Numbers using only addition and subtraction.   Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl...
#Java
Java
public class Test { public static void main(String[] args) { int N = 15; int[] t = new int[N + 2]; t[1] = 1;   for (int i = 1; i <= N; i++) {   for (int j = i; j > 1; j--) t[j] = t[j] + t[j - 1];   t[i + 1] = t[i];   for (int j = i ...
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#Gambas
Gambas
Public Sub Main() Dim dog As String   Dog = "Benjamin" DOG = "Samba" dog = "Bernie" Print "There is just one dog, named "; dog   End
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#GAP
GAP
# GAP is case sensitive ThreeDogs := function() local dog, Dog, DOG; dog := "Benjamin"; Dog := "Samba"; DOG := "Bernie"; if dog = DOG then Print("There is just one dog named ", dog, "\n"); else Print("The three dogs are named ", dog, ", ", Dog, " and ", DOG, "\n"); fi; end;   ThreeDogs(); # The three dogs ar...
http://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists
Cartesian product of two or more lists
Task Show one or more idiomatic ways of generating the Cartesian product of two arbitrary lists in your language. Demonstrate that your function/method correctly returns: {1, 2} × {3, 4} = {(1, 3), (1, 4), (2, 3), (2, 4)} and, in contrast: {3, 4} × {1, 2} = {(3, 1), (3, 2), (4, 1), (4, 2)} Also demonstrate, using y...
#Common_Lisp
Common Lisp
(defun cartesian-product (s1 s2) "Compute the cartesian product of two sets represented as lists" (loop for x in s1 nconc (loop for y in s2 collect (list x y))))  
http://rosettacode.org/wiki/Catalan_numbers
Catalan numbers
Catalan numbers You are encouraged to solve this task according to the task description, using any language you may know. Catalan numbers are a sequence of numbers which can be defined directly: C n = 1 n + 1 ( 2 n n ) = ( 2 n ) ! ( n + 1 ) ! n !  for  n ≥ 0. {\displaystyle C...
#APL
APL
{(!2×⍵)÷(!⍵+1)×!⍵}(⍳15)-1
http://rosettacode.org/wiki/Cantor_set
Cantor set
Task Draw a Cantor set. See details at this Wikipedia webpage:   Cantor set
#11l
11l
V WIDTH = 81 V HEIGHT = 5   F cantor(start, len, index) V seg = len I/ 3 I seg == 0 R L(it) 0 .< :HEIGHT - index V i = index + it L(jt) 0 .< seg V j = start + seg + jt V pos = i * :WIDTH + j  :lines[pos] = ‘ ’ cantor(start, seg, index + 1) cantor(start + seg * ...
http://rosettacode.org/wiki/Canny_edge_detector
Canny edge detector
Task Write a program that performs so-called canny edge detection on an image. A possible algorithm consists of the following steps: Noise reduction.   May be performed by Gaussian filter.   Compute intensity gradient   (matrices G x {\displaystyle G_{x}} and G y {\displaystyle G_{y}} ) ...
#C
C
#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <float.h> #include <math.h> #include <string.h> #include <stdbool.h> #include <assert.h>   #define MAX_BRIGHTNESS 255   // C99 doesn't define M_PI (GNU-C99 does) #define M_PI 3.14159265358979323846264338327   /* * Loading part taken from * http://www...
http://rosettacode.org/wiki/Canonicalize_CIDR
Canonicalize CIDR
Task Implement a function or program that, given a range of IPv4 addresses in CIDR notation (dotted-decimal/network-bits), will return/output the same range in canonical form. That is, the IP address portion of the output CIDR block must not contain any set (1) bits in the host part of the address. Example Given ...
#C
C
#include <stdbool.h> #include <stdio.h> #include <stdint.h>   typedef struct cidr_tag { uint32_t address; unsigned int mask_length; } cidr_t;   // Convert a string in CIDR format to an IPv4 address and netmask, // if possible. Also performs CIDR canonicalization. bool cidr_parse(const char* str, cidr_t* cidr) {...
http://rosettacode.org/wiki/Casting_out_nines
Casting out nines
Task   (in three parts) Part 1 Write a procedure (say c o 9 ( x ) {\displaystyle {\mathit {co9}}(x)} ) which implements Casting Out Nines as described by returning the checksum for x {\displaystyle x} . Demonstrate the procedure using the examples given there, or others you may consider lucky. Par...
#Common_Lisp
Common Lisp
;;A macro was used to ensure that the filter is inlined. ;;Larry Hignight. Last updated on 7/3/2012. (defmacro kaprekar-number-filter (n &optional (base 10)) `(= (mod ,n (1- ,base)) (mod (* ,n ,n) (1- ,base))))   (defun test (&key (start 1) (stop 10000) (base 10) (collect t)) (let ((count 0) (nums)) (loop f...
http://rosettacode.org/wiki/Catmull%E2%80%93Clark_subdivision_surface
Catmull–Clark subdivision surface
Implement the Catmull-Clark surface subdivision (description on Wikipedia), which is an algorithm that maps from a surface (described as a set of points and a set of polygons with vertices at those points) to another more refined surface. The resulting surface will always consist of a mesh of quadrilaterals. The proce...
#Julia
Julia
using Makie, Statistics   # Point3f0 is a 3-tuple of 32-bit floats for 3-dimensional space, and all Points are 3D. Point = Point3f0   # a Face is defined by the points that are its vertices, in order. Face = Vector{Point}   # an Edge is a line segment where the points are sorted struct Edge p1::Point p2::Point ...
http://rosettacode.org/wiki/Catmull%E2%80%93Clark_subdivision_surface
Catmull–Clark subdivision surface
Implement the Catmull-Clark surface subdivision (description on Wikipedia), which is an algorithm that maps from a surface (described as a set of points and a set of polygons with vertices at those points) to another more refined surface. The resulting surface will always consist of a mesh of quadrilaterals. The proce...
#Mathematica.2FWolfram_Language
Mathematica/Wolfram Language
subSetQ[large_,small_] := MemberQ[large,small] subSetQ[large_,small_List] := And@@(MemberQ[large,#]&/@small)   containing[groupList_,item_]:= Flatten[Position[groupList,group_/;subSetQ[group,item]]]   ReplaceFace[face_]:=Transpose[Prepend[Transpose[{#[[1]],face,#[[2]]}&/@Transpose[Partition[face,2,1,1]//{#,RotateRight[...
http://rosettacode.org/wiki/Carmichael_3_strong_pseudoprimes
Carmichael 3 strong pseudoprimes
A lot of composite numbers can be separated from primes by Fermat's Little Theorem, but there are some that completely confound it. The   Miller Rabin Test   uses a combination of Fermat's Little Theorem and Chinese Division Theorem to overcome this. The purpose of this task is to investigate such numbers using a met...
#BASIC256
BASIC256
  for i = 3 to max_sieve step 2 isprime[i] = 1 next i   isprime[2] = 1 for i = 3 to sqr(max_sieve) step 2 if isprime[i] = 1 then for j = i * i to max_sieve step i * 2 isprime[j] = 0 next j end if next i   subroutine carmichael3(p1) if isprime[p1] <> 0 then for h3 = 1 to p1 -1 t1 = (h3 + p1) * (p1 -1) ...
http://rosettacode.org/wiki/Carmichael_3_strong_pseudoprimes
Carmichael 3 strong pseudoprimes
A lot of composite numbers can be separated from primes by Fermat's Little Theorem, but there are some that completely confound it. The   Miller Rabin Test   uses a combination of Fermat's Little Theorem and Chinese Division Theorem to overcome this. The purpose of this task is to investigate such numbers using a met...
#C
C
  #include <stdio.h>   /* C's % operator actually calculates the remainder of a / b so we need a * small adjustment so it works as expected for negative values */ #define mod(n,m) ((((n) % (m)) + (m)) % (m))   int is_prime(unsigned int n) { if (n <= 3) { return n > 1; } else if (!(n % 2) || !(n % 3...
http://rosettacode.org/wiki/Catamorphism
Catamorphism
Reduce is a function or method that is used to take the values in an array or a list and apply a function to successive members of the list to produce (or reduce them to), a single value. Task Show how reduce (or foldl or foldr etc), work (or would be implemented) in your language. See also Wikipedia article: ...
#Clojure
Clojure
; Basic usage > (reduce * '(1 2 3 4 5)) 120 ; Using an initial value > (reduce + 100 '(1 2 3 4 5)) 115  
http://rosettacode.org/wiki/Chaocipher
Chaocipher
Description The Chaocipher was invented by J.F.Byrne in 1918 and, although simple by modern cryptographic standards, does not appear to have been broken until the algorithm was finally disclosed by his family in 2010. The algorithm is described in this paper by M.Rubin in 2010 and there is a C# implementation here. ...
#Perl
Perl
sub init { @left = split '', 'HXUCZVAMDSLKPEFJRIGTWOBNYQ'; @right = split '', 'PTLNBQDEOYSFAVZKGJRIHWXUMC'; }   sub encode { my($letter) = @_; my $index = index join('', @right), $letter; my $enc = $left[$index]; left_permute($index); right_permute($index); $enc }   sub decode { m...
http://rosettacode.org/wiki/Catalan_numbers/Pascal%27s_triangle
Catalan numbers/Pascal's triangle
Task Print out the first   15   Catalan numbers by extracting them from Pascal's triangle. See   Catalan Numbers and the Pascal Triangle.     This method enables calculation of Catalan Numbers using only addition and subtraction.   Catalan's Triangle for a Number Triangle that generates Catalan Numbers using onl...
#JavaScript
JavaScript
var n = 15; for (var t = [0, 1], i = 1; i <= n; i++) { for (var j = i; j > 1; j--) t[j] += t[j - 1]; t[i + 1] = t[i]; for (var j = i + 1; j > 1; j--) t[j] += t[j - 1]; document.write(i == 1 ? '' : ', ', t[i + 1] - t[i]); }
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#Go
Go
package dogs   import "fmt"   // Three variables, three different names. // (It wouldn't compile if the compiler saw the variable names as the same.) var dog = "Salt" var Dog = "Pepper" var DOG = "Mustard"   func PackageSees() map[*string]int { // Print dogs visible from here. fmt.Println("Package sees:", dog, ...
http://rosettacode.org/wiki/Case-sensitivity_of_identifiers
Case-sensitivity of identifiers
Three dogs (Are there three dogs or one dog?) is a code snippet used to illustrate the lettercase sensitivity of the programming language. For a case-sensitive language, the identifiers dog, Dog and DOG are all different and we should get the output: The three dogs are named Benjamin, Samba and Bernie. For a language...
#Groovy
Groovy
def dog = "Benjamin", Dog = "Samba", DOG = "Bernie" println (dog == DOG ? "There is one dog named ${dog}" : "There are three dogs named ${dog}, ${Dog} and ${DOG}.")
http://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists
Cartesian product of two or more lists
Task Show one or more idiomatic ways of generating the Cartesian product of two arbitrary lists in your language. Demonstrate that your function/method correctly returns: {1, 2} × {3, 4} = {(1, 3), (1, 4), (2, 3), (2, 4)} and, in contrast: {3, 4} × {1, 2} = {(3, 1), (3, 2), (4, 1), (4, 2)} Also demonstrate, using y...
#Crystal
Crystal
def cartesian_product(a, b) return a.flat_map { |i| b.map { |j| [i, j] } } end     def cartesian_product(l) if l.size <= 1 return l elsif l.size == 2 return cartesian_product(l[0], l[1]) end   return l[0].flat_map { |i| cartesian_product(l[1..]).map { |j| [i, j]....
http://rosettacode.org/wiki/Catalan_numbers
Catalan numbers
Catalan numbers You are encouraged to solve this task according to the task description, using any language you may know. Catalan numbers are a sequence of numbers which can be defined directly: C n = 1 n + 1 ( 2 n n ) = ( 2 n ) ! ( n + 1 ) ! n !  for  n ≥ 0. {\displaystyle C...
#Arturo
Arturo
catalan: function [n][ if? n=0 -> 1 else -> div (catalan n-1) * (4*n)-2 n+1 ]   loop 0..15 [i][ print [ pad.right to :string i 5 pad.left to :string catalan i 20 ] ]
http://rosettacode.org/wiki/Cantor_set
Cantor set
Task Draw a Cantor set. See details at this Wikipedia webpage:   Cantor set
#Action.21
Action!
PROC FillRect(INT x,y,w,h) INT i   FOR i=y TO y+h-1 DO Plot(x,i) DrawTo(x+w-1,i) OD RETURN   PROC DrawCantor(INT x0,y0,h,level) INT x,y,i,j,w,w2,h2   w=1 FOR i=0 TO level-1 DO w==*3 OD   Color=1 y=y0 FOR i=0 TO level DO FillRect(x0,y,w,h) y==+h*2 OD   Color=0 w2=1 h2=h*2 ...
http://rosettacode.org/wiki/Cantor_set
Cantor set
Task Draw a Cantor set. See details at this Wikipedia webpage:   Cantor set
#Ada
Ada
with Ada.Text_IO;   procedure Cantor_Set is   subtype Level_Range is Integer range 1 .. 5; Image : array (Level_Range) of String (1 .. 81) := (others => (others => ' '));   procedure Cantor (Level : Natural; Length : Natural; Start : Natural) is begin if Level in Level_Range then Image (Level...