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/Look-and-say_sequence
Look-and-say sequence
The   Look and say sequence   is a recursively defined sequence of numbers studied most notably by   John Conway. The   look-and-say sequence   is also known as the   Morris Number Sequence,   after cryptographer Robert Morris,   and the puzzle   What is the next number in the sequence 1,   11,   21,   1211,   11122...
#Frink
Frink
  LookAndSay = "12211123" println["Starting Value: " + LookAndSay]   LASStr = { |LaS| length[LaS@0] + length[LaS@1] + LaS@0 /* The returned results from the Regex are divided between a distinct matching character and any following identical characters. For example, a string of 2222 would be retu...
http://rosettacode.org/wiki/Longest_increasing_subsequence
Longest increasing subsequence
Calculate and show here a longest increasing subsequence of the list: { 3 , 2 , 6 , 4 , 5 , 1 } {\displaystyle \{3,2,6,4,5,1\}} And of the list: { 0 , 8 , 4 , 12 , 2 , 10 , 6 , 14 , 1 , 9 , 5 , 13 , 3 , 11 , 7 , 15 } {\displaystyle \{0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15\}} Note that a list may have m...
#Tcl
Tcl
package require Tcl 8.6   proc longestIncreasingSubsequence {sequence} { # Get the increasing subsequences (and their lengths) set subseq [list 1 [lindex $sequence 0]] foreach value $sequence { set max {} foreach {len item} $subseq { if {[lindex $item end] < $value} { if {[llength [lappend item $va...
http://rosettacode.org/wiki/Loops/Continue
Loops/Continue
Task Show the following output using one loop. 1, 2, 3, 4, 5 6, 7, 8, 9, 10 Try to achieve the result by forcing the next iteration within the loop upon a specific condition, if your language allows it. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while...
#Octave
Octave
v = ""; for i = 1:10 v = sprintf("%s%d", v, i); if ( mod(i, 5) == 0 ) disp(v) v = ""; continue endif v = sprintf("%s, ", v); endfor
http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers
Luhn test of credit card numbers
The Luhn test is used by some credit card companies to distinguish valid credit card numbers from what could be a random selection of digits. Those companies using credit card numbers that can be validated by the Luhn test have numbers that pass the following test: Reverse the order of the digits in the number. Tak...
#SequenceL
SequenceL
  main(args(2)) := sum(luhnTest(asciiToInt(args[1]) - asciiToInt('0'))) mod 10 = 0;   s2Mapping := [0,2,4,6,8,1,3,5,7,9];   luhnTest(x(1))[i] := x[i] when i mod 2 = size(x) mod 2 else s2Mapping[x[i] + 1];  
http://rosettacode.org/wiki/Loops/While
Loops/While
Task Start an integer value at   1024. Loop while it is greater than zero. Print the value (with a newline) and divide it by two each time through the loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For...
#Trith
Trith
1024 [dup print 2 / floor] [dup 0 >] while drop
http://rosettacode.org/wiki/Loops/While
Loops/While
Task Start an integer value at   1024. Loop while it is greater than zero. Print the value (with a newline) and divide it by two each time through the loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For...
#TUSCRIPT
TUSCRIPT
$$ MODE TUSCRIPT i=1024 LOOP PRINT i i=i/2 IF (i==0) EXIT ENDLOOP
http://rosettacode.org/wiki/Loops/Do-while
Loops/Do-while
Start with a value at 0. Loop while value mod 6 is not equal to 0. Each time through the loop, add 1 to the value then print it. The loop must execute at least once. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loo...
#Scheme
Scheme
(let loop ((i 1)) (display i) (if (positive? (modulo i 6)) (loop (+ i 1))))
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. ...
#LabVIEW
LabVIEW
  {def loops_for {lambda {:i :n} {if {>= :i :n} then (end of loop) else {br}{S.map {lambda {} *} {S.serie 0 :i}} {loops_for {+ :i 1} :n}}}} -> loops_for   {loops_for 0 5} -> * * * * * * * * * * * * * * * (end of loop)   a simpler way, using {S.map function {S.serie start end [step]}   {S.map {lamb...
http://rosettacode.org/wiki/Loops/For_with_a_specified_step
Loops/For with a specified step
Task Demonstrate a   for-loop   where the step-value is greater than one. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For with a specified step   Loops/Foreach   Loops/Increment loop index within loop b...
#PowerShell
PowerShell
for ($i = 0; $i -lt 10; $i += 2) { $i }
http://rosettacode.org/wiki/Long_multiplication
Long multiplication
Task Explicitly implement   long multiplication. This is one possible approach to arbitrary-precision integer algebra. For output, display the result of   264 * 264. Optionally, verify your result against builtin arbitrary precision support. The decimal representation of   264   is: 18,446,744,073,709,551,61...
#Java
Java
public class LongMult {   private static byte[] stringToDigits(String num) { byte[] result = new byte[num.length()]; for (int i = 0; i < num.length(); i++) { char c = num.charAt(i); if (c < '0' || c > '9') { throw new IllegalArgumentException("Invalid digit " + c + " found at position " + i); } ...
http://rosettacode.org/wiki/Literals/String
Literals/String
Task Show literal specification of characters and strings. If supported, show how the following work:   verbatim strings   (quotes where escape sequences are quoted literally)   here-strings   Also, discuss which quotes expand variables. Related tasks   Special characters   Here document Other tasks r...
#Lisaac
Lisaac
c1 := 'a'; c2 := '\n'; // newline c3 := '\''; // quote c4 := '\101o'; // octal c5 := '\10\'; // decimal c6 := '\0Ah\'; // hexadecimal c7 := '\10010110b\'; // binary
http://rosettacode.org/wiki/Literals/Integer
Literals/Integer
Some programming languages have ways of expressing integer literals in bases other than the normal base ten. Task Show how integer literals can be expressed in as many bases as your language allows. Note:   this should not involve the calling of any functions/methods, but should be interpreted by the compiler or...
#Limbo
Limbo
implement Command;   include "sys.m"; sys: Sys;   include "draw.m";   include "sh.m";   init(nil: ref Draw->Context, nil: list of string) { sys = load Sys Sys->PATH;   sys->print("%d\n", 2r1111); # binary sys->print("%d\n", 8r17); # octal sys->print("%d\n", 15); # decimal sys->print("%d\n", 16rF); # hexade...
http://rosettacode.org/wiki/Literals/Integer
Literals/Integer
Some programming languages have ways of expressing integer literals in bases other than the normal base ten. Task Show how integer literals can be expressed in as many bases as your language allows. Note:   this should not involve the calling of any functions/methods, but should be interpreted by the compiler or...
#LiveCode
LiveCode
put 0x1 + 0xff
http://rosettacode.org/wiki/Logical_operations
Logical operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#FunL
FunL
def logical( a, b ) = println( """ a and b = ${a and b} a or b = ${a or b} not a = ${not a} a xor b = ${a xor b} """ )   for i <- [false, true], j <- [false, true] do logical( i, j )
http://rosettacode.org/wiki/Logical_operations
Logical operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#FutureBasic
FutureBasic
window 1, @"Logical Operations", (0,0,480,270)   Boolean a, b   text ,,,,, 43   print @"In FB, the Boolean constants _true or YES = 1, _false or NO = 0" print fn StringByPaddingToLength( @"", 39, @"-", 0 )   print @"a\tb\tand\tor\txor\tnand\tnor" print fn StringByPaddingToLength( @"", 39, @"-", 0 )   a = NO  : b = NO  ...
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: ...
#Forth
Forth
create a char a , char b , char c , create b char A , char B , char C , create c char 1 , char 2 , char 3 ,   : main 3 0 do cr a i cells + @ emit b i cells + @ emit c i cells + @ emit loop cr a b c 3 0 do cr 3 0 do rot dup @ emit cell+ loop loop drop drop drop ;
http://rosettacode.org/wiki/Loops/Break
Loops/Break
Task Show a loop which prints random numbers (each number newly generated each loop) from 0 to 19 (inclusive). If a number is 10, stop the loop after printing it, and do not generate any further numbers. Otherwise, generate and print a second random number before restarting the loop. If the number 10 is never gener...
#Lambdatalk
Lambdatalk
  {def loops_break {lambda {:n} {if {= :n 10} then :n -> end of loop else :n {loops_break {round {* 20 {random}}}}}}} -> loops_break   {loops_break 0} -> 0 16 8 5 9 17 9 18 1 18 1 1 12 13 15 1 10 -> end of loop  
http://rosettacode.org/wiki/Longest_common_subsequence
Longest common subsequence
Introduction Define a subsequence to be any output string obtained by deleting zero or more symbols from an input string. The Longest Common Subsequence (LCS) is a subsequence of maximum length common to two or more strings. Let A ≡ A[0]… A[m - 1] and B ≡ B[0]… B[n - 1], m < n be strings drawn from an alphabet Σ of ...
#Oz
Oz
declare fun {LCS Xs Ys} case [Xs Ys] of [nil _] then nil [] [_ nil] then nil [] [X|Xr Y|Yr] andthen X==Y then X|{LCS Xr Yr} [] [_|Xr _|Yr] then {Longest {LCS Xs Yr} {LCS Xr Ys}} end end   fun {Longest Xs Ys} if {Length Xs} > {Le...
http://rosettacode.org/wiki/Look-and-say_sequence
Look-and-say sequence
The   Look and say sequence   is a recursively defined sequence of numbers studied most notably by   John Conway. The   look-and-say sequence   is also known as the   Morris Number Sequence,   after cryptographer Robert Morris,   and the puzzle   What is the next number in the sequence 1,   11,   21,   1211,   11122...
#Gambas
Gambas
Public Sub Main() Dim i, j, cnt As Integer Dim txt$, curr$, result$ As String   txt$ = "1211" i = 1   Print "Sequence: " & txt$ & " = "; Repeat j = 1 result$ = "" Repeat curr$ = Mid(txt$, j, 1) cnt = 0 Repeat Inc cnt Inc j Until Mid(txt$, j, ...
http://rosettacode.org/wiki/Longest_increasing_subsequence
Longest increasing subsequence
Calculate and show here a longest increasing subsequence of the list: { 3 , 2 , 6 , 4 , 5 , 1 } {\displaystyle \{3,2,6,4,5,1\}} And of the list: { 0 , 8 , 4 , 12 , 2 , 10 , 6 , 14 , 1 , 9 , 5 , 13 , 3 , 11 , 7 , 15 } {\displaystyle \{0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15\}} Note that a list may have m...
#VBScript
VBScript
  Function LIS(arr) n = UBound(arr) Dim p() ReDim p(n) Dim m() ReDim m(n) l = 0 For i = 0 To n lo = 1 hi = l Do While lo <= hi middle = Int((lo+hi)/2) If arr(m(middle)) < arr(i) Then lo = middle + 1 Else hi = middle - 1 End If Loop newl = lo p(i) = m(newl-1) m(newl) = i If new...
http://rosettacode.org/wiki/Loops/Continue
Loops/Continue
Task Show the following output using one loop. 1, 2, 3, 4, 5 6, 7, 8, 9, 10 Try to achieve the result by forcing the next iteration within the loop upon a specific condition, if your language allows it. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while...
#Oforth
Oforth
: loopCont | i | 10 loop: i [ i dup print 5 mod ifZero: [ printcr continue ] "," . ] ;
http://rosettacode.org/wiki/Loops/Continue
Loops/Continue
Task Show the following output using one loop. 1, 2, 3, 4, 5 6, 7, 8, 9, 10 Try to achieve the result by forcing the next iteration within the loop upon a specific condition, if your language allows it. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while...
#Ol
Ol
  (let loop ((i 1)) (when (less? i 11) (call/cc (lambda (continue) (display i) (when (zero? (mod i 5)) (print) (continue #f)) (display ", "))) (loop (+ i 1))))  
http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers
Luhn test of credit card numbers
The Luhn test is used by some credit card companies to distinguish valid credit card numbers from what could be a random selection of digits. Those companies using credit card numbers that can be validated by the Luhn test have numbers that pass the following test: Reverse the order of the digits in the number. Tak...
#Shen
Shen
  (define mapi _ _ [] -> [] F N [X | Xs] -> [(F N X) | (mapi F (+ N 1) Xs)])   (define double X -> (let Y (* 2 X) (if (> Y 9) (- Y 9) Y)))   (define luhn? Number -> (let Exploded (explode Number) Digits (map (/. H (- (string->n H) 48)) Exploded) Reversed (reverse Digits) D...
http://rosettacode.org/wiki/Loops/While
Loops/While
Task Start an integer value at   1024. Loop while it is greater than zero. Print the value (with a newline) and divide it by two each time through the loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For...
#Unicon
Unicon
variables numeric I endvariables   I = 1024 while (I > 0) putmess I I = (I/2)[trunc] endwhile
http://rosettacode.org/wiki/Loops/While
Loops/While
Task Start an integer value at   1024. Loop while it is greater than zero. Print the value (with a newline) and divide it by two each time through the loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For...
#Uniface
Uniface
variables numeric I endvariables   I = 1024 while (I > 0) putmess I I = (I/2)[trunc] endwhile
http://rosettacode.org/wiki/Loops/While
Loops/While
Task Start an integer value at   1024. Loop while it is greater than zero. Print the value (with a newline) and divide it by two each time through the loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For...
#UNIX_Shell
UNIX Shell
x=1024 while [[ $x -gt 0 ]]; do echo $x x=$(( $x/2 )) done
http://rosettacode.org/wiki/Loops/Do-while
Loops/Do-while
Start with a value at 0. Loop while value mod 6 is not equal to 0. Each time through the loop, add 1 to the value then print it. The loop must execute at least once. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loo...
#Scilab
Scilab
v=0 while %T v=v+1 printf("%2d ",v) if modulo(v,6)==0 then break; end end printf("\n")
http://rosettacode.org/wiki/Loops/Do-while
Loops/Do-while
Start with a value at 0. Loop while value mod 6 is not equal to 0. Each time through the loop, add 1 to the value then print it. The loop must execute at least once. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loo...
#Seed7
Seed7
$ include "seed7_05.s7i";   const proc: main is func local var integer: number is 0; begin repeat incr(number); writeln(number) until number rem 6 = 0 end func;
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. ...
#Lambdatalk
Lambdatalk
  {def loops_for {lambda {:i :n} {if {>= :i :n} then (end of loop) else {br}{S.map {lambda {} *} {S.serie 0 :i}} {loops_for {+ :i 1} :n}}}} -> loops_for   {loops_for 0 5} -> * * * * * * * * * * * * * * * (end of loop)   a simpler way, using {S.map function {S.serie start end [step]}   {S.map {lamb...
http://rosettacode.org/wiki/Loops/For_with_a_specified_step
Loops/For with a specified step
Task Demonstrate a   for-loop   where the step-value is greater than one. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For with a specified step   Loops/Foreach   Loops/Increment loop index within loop b...
#Prolog
Prolog
for(Lo,Hi,Step,Lo) :- Step>0, Lo=<Hi. for(Lo,Hi,Step,Val) :- Step>0, plus(Lo,Step,V), V=<Hi, !, for(V,Hi,Step,Val).   example :- for(0,10,2,Val), write(Val), write(' '), fail. example.
http://rosettacode.org/wiki/Long_multiplication
Long multiplication
Task Explicitly implement   long multiplication. This is one possible approach to arbitrary-precision integer algebra. For output, display the result of   264 * 264. Optionally, verify your result against builtin arbitrary precision support. The decimal representation of   264   is: 18,446,744,073,709,551,61...
#JavaScript
JavaScript
function mult(strNum1,strNum2){   var a1 = strNum1.split("").reverse(); var a2 = strNum2.toString().split("").reverse(); var aResult = new Array;   for ( var iterNum1 = 0; iterNum1 < a1.length; iterNum1++ ) { for ( var iterNum2 = 0; iterNum2 < a2.length; iterNum2++ ) { var idxIter = ...
http://rosettacode.org/wiki/Literals/String
Literals/String
Task Show literal specification of characters and strings. If supported, show how the following work:   verbatim strings   (quotes where escape sequences are quoted literally)   here-strings   Also, discuss which quotes expand variables. Related tasks   Special characters   Here document Other tasks r...
#LiveCode
LiveCode
put "Literal string" -- Literal string put char 1 of "Literal string" -- L put char 1 to 7 of "Literal string" -- Literal put word 1 of "Literal string" -- Literal put quote & "string" & quote -- "string"
http://rosettacode.org/wiki/Literals/String
Literals/String
Task Show literal specification of characters and strings. If supported, show how the following work:   verbatim strings   (quotes where escape sequences are quoted literally)   here-strings   Also, discuss which quotes expand variables. Related tasks   Special characters   Here document Other tasks r...
#Logo
Logo
print "Hello\,\ world print "|Hello, world|
http://rosettacode.org/wiki/Literals/String
Literals/String
Task Show literal specification of characters and strings. If supported, show how the following work:   verbatim strings   (quotes where escape sequences are quoted literally)   here-strings   Also, discuss which quotes expand variables. Related tasks   Special characters   Here document Other tasks r...
#Lua
Lua
singlequotestring = 'can contain "double quotes"' doublequotestring = "can contain 'single quotes'" longstring = [[can contain newlines]] longstring2 = [==[ can contain [[ other ]=] longstring " and ' string [===[ qualifiers]==]
http://rosettacode.org/wiki/Literals/Integer
Literals/Integer
Some programming languages have ways of expressing integer literals in bases other than the normal base ten. Task Show how integer literals can be expressed in as many bases as your language allows. Note:   this should not involve the calling of any functions/methods, but should be interpreted by the compiler or...
#Logo
Logo
  :- object(integers).   :- public(show/0).   show :- write('Binary 0b11110101101 = '), write(0b11110101101), nl, write('Octal 0o3655 = '), write(0o3655), nl, write('Decimal 1965 = '), write(1965), nl, write('Hexadecimal 0x7AD = '), write(0x...
http://rosettacode.org/wiki/Literals/Integer
Literals/Integer
Some programming languages have ways of expressing integer literals in bases other than the normal base ten. Task Show how integer literals can be expressed in as many bases as your language allows. Note:   this should not involve the calling of any functions/methods, but should be interpreted by the compiler or...
#Logtalk
Logtalk
  :- object(integers).   :- public(show/0).   show :- write('Binary 0b11110101101 = '), write(0b11110101101), nl, write('Octal 0o3655 = '), write(0o3655), nl, write('Decimal 1965 = '), write(1965), nl, write('Hexadecimal 0x7AD = '), write(0x...
http://rosettacode.org/wiki/Logical_operations
Logical operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#GAP
GAP
Logical := function(a, b) return [ a or b, a and b, not a ]; end;   Logical(true, true); # [ true, true, false ]   Logical(true, false); # [ true, false, false ]   Logical(false, true); # [ true, false, true ]   Logical(false, false); # [ false, false, true ]
http://rosettacode.org/wiki/Logical_operations
Logical operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#gecho
gecho
3 4 and
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: ...
#Fortran
Fortran
program main implicit none   integer,parameter :: n_vals = 3 character(len=*),dimension(n_vals),parameter :: ls = ['a','b','c'] character(len=*),dimension(n_vals),parameter :: us = ['A','B','C'] integer,dimension(n_vals),parameter :: ns = [1,2,3]   integer :: i !counter   do i=1,n_vals write(*,'(...
http://rosettacode.org/wiki/Loops/Break
Loops/Break
Task Show a loop which prints random numbers (each number newly generated each loop) from 0 to 19 (inclusive). If a number is 10, stop the loop after printing it, and do not generate any further numbers. Otherwise, generate and print a second random number before restarting the loop. If the number 10 is never gener...
#Lang5
Lang5
do 20 ? int dup . 10 == if break then 20 ? int . loop
http://rosettacode.org/wiki/Loops/Break
Loops/Break
Task Show a loop which prints random numbers (each number newly generated each loop) from 0 to 19 (inclusive). If a number is 10, stop the loop after printing it, and do not generate any further numbers. Otherwise, generate and print a second random number before restarting the loop. If the number 10 is never gener...
#langur
langur
for { val .i = random 0..19 write .i, " " if .i == 10 { writeln(); break } write random(0..19), " " }
http://rosettacode.org/wiki/Longest_common_subsequence
Longest common subsequence
Introduction Define a subsequence to be any output string obtained by deleting zero or more symbols from an input string. The Longest Common Subsequence (LCS) is a subsequence of maximum length common to two or more strings. Let A ≡ A[0]… A[m - 1] and B ≡ B[0]… B[n - 1], m < n be strings drawn from an alphabet Σ of ...
#Pascal
Pascal
Program LongestCommonSubsequence(output);   function lcs(a, b: string): string; var x, y: string; lenga, lengb: integer; begin lenga := length(a); lengb := length(b); lcs := ''; if (lenga > 0) and (lengb > 0) then if a[lenga] = b[lengb] then lcs := lcs(copy(a, 1, lenga-1), c...
http://rosettacode.org/wiki/Look-and-say_sequence
Look-and-say sequence
The   Look and say sequence   is a recursively defined sequence of numbers studied most notably by   John Conway. The   look-and-say sequence   is also known as the   Morris Number Sequence,   after cryptographer Robert Morris,   and the puzzle   What is the next number in the sequence 1,   11,   21,   1211,   11122...
#GAP
GAP
LookAndSay := function(s) local c, r, cur, ncur, v; v := "123"; r := ""; cur := 0; ncur := 0; for c in s do if c = cur then ncur := ncur + 1; else if ncur > 0 then Add(r, v[ncur]); Add(r, cur); fi; cur := c; ncur := 1; fi; od; Add(r, v[ncur]); ...
http://rosettacode.org/wiki/Longest_increasing_subsequence
Longest increasing subsequence
Calculate and show here a longest increasing subsequence of the list: { 3 , 2 , 6 , 4 , 5 , 1 } {\displaystyle \{3,2,6,4,5,1\}} And of the list: { 0 , 8 , 4 , 12 , 2 , 10 , 6 , 14 , 1 , 9 , 5 , 13 , 3 , 11 , 7 , 15 } {\displaystyle \{0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15\}} Note that a list may have m...
#Wren
Wren
var longestIncreasingSubsequence = Fn.new { |x| var n = x.count if (n == 0) return [] if (n == 1) return x var p = List.filled(n, 0) var m = List.filled(n+1, 0) var len = 0 for (i in 0...n) { var lo = 1 var hi = len while (lo <= hi) { var mid = ((lo + hi)/...
http://rosettacode.org/wiki/Loops/Continue
Loops/Continue
Task Show the following output using one loop. 1, 2, 3, 4, 5 6, 7, 8, 9, 10 Try to achieve the result by forcing the next iteration within the loop upon a specific condition, if your language allows it. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while...
#Oz
Oz
for I in 1..10 continue:C do {System.print I} if I mod 5 == 0 then {System.printInfo "\n"} {C} end {System.printInfo ", "} end
http://rosettacode.org/wiki/Loops/Continue
Loops/Continue
Task Show the following output using one loop. 1, 2, 3, 4, 5 6, 7, 8, 9, 10 Try to achieve the result by forcing the next iteration within the loop upon a specific condition, if your language allows it. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while...
#PARI.2FGP
PARI/GP
for(n=1,10, print1(n); if(n%5 == 0, print();continue); print1(", ") )
http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers
Luhn test of credit card numbers
The Luhn test is used by some credit card companies to distinguish valid credit card numbers from what could be a random selection of digits. Those companies using credit card numbers that can be validated by the Luhn test have numbers that pass the following test: Reverse the order of the digits in the number. Tak...
#Sidef
Sidef
func luhn (n) { static a = {|j| (2*j // 10) + (2*j % 10) }.map(^10)   var checksum = n.digits.map_kv {|i,j| i.is_odd ? a[j] : j }.sum   checksum % 10 == 0 }   for n in [49927398716, 49927398717, 1234567812345678, 1234567812345670] { say [n, luhn(n)] }
http://rosettacode.org/wiki/Loops/While
Loops/While
Task Start an integer value at   1024. Loop while it is greater than zero. Print the value (with a newline) and divide it by two each time through the loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For...
#UnixPipes
UnixPipes
(echo 1024>p.res;tail -f p.res) | while read a ; do test $a -gt 0 && (expr $a / 2 >> p.res ; echo $a) || exit 0 done
http://rosettacode.org/wiki/Loops/While
Loops/While
Task Start an integer value at   1024. Loop while it is greater than zero. Print the value (with a newline) and divide it by two each time through the loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For...
#Ursa
Ursa
decl int n set n 1024   while (> n 0) out n endl console set n (int (/ n 2)) end while
http://rosettacode.org/wiki/Loops/Do-while
Loops/Do-while
Start with a value at 0. Loop while value mod 6 is not equal to 0. Each time through the loop, add 1 to the value then print it. The loop must execute at least once. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loo...
#Sidef
Sidef
var value = 0; do { say ++value; } while (value % 6);
http://rosettacode.org/wiki/Loops/Do-while
Loops/Do-while
Start with a value at 0. Loop while value mod 6 is not equal to 0. Each time through the loop, add 1 to the value then print it. The loop must execute at least once. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loo...
#Slate
Slate
[| val | val: 0. [val: val + 1. print: val. val \\ 6 ~= 0] whileTrue ] do.
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. ...
#Lang5
Lang5
: cr "\n" . ;  : dip swap '_ set execute _ ; : nip swap drop ;  : last -1 extract nip ; : times swap iota '_ set do dup 'execute dip _ last 0 == if break then loop drop ;   : concat "" join ; '* 1 5 "2dup reshape concat . cr 1 +" times
http://rosettacode.org/wiki/Loops/For_with_a_specified_step
Loops/For with a specified step
Task Demonstrate a   for-loop   where the step-value is greater than one. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For with a specified step   Loops/Foreach   Loops/Increment loop index within loop b...
#Python
Python
for i in xrange(2, 9, 2): print "%d," % i, print "who do we appreciate?"
http://rosettacode.org/wiki/Long_multiplication
Long multiplication
Task Explicitly implement   long multiplication. This is one possible approach to arbitrary-precision integer algebra. For output, display the result of   264 * 264. Optionally, verify your result against builtin arbitrary precision support. The decimal representation of   264   is: 18,446,744,073,709,551,61...
#jq
jq
# multiply two decimal strings, which may be signed (+ or -) def long_multiply(num1; num2):   def stripsign: .[0:1] as $a | if $a == "-" then [ -1, .[1:]] elif $a == "+" then [ 1, .[1:]] else [1, .] end;   def adjustsign(sign): if sign == 1 then . else "-" + . end;   # mult/2 ass...
http://rosettacode.org/wiki/Literals/String
Literals/String
Task Show literal specification of characters and strings. If supported, show how the following work:   verbatim strings   (quotes where escape sequences are quoted literally)   here-strings   Also, discuss which quotes expand variables. Related tasks   Special characters   Here document Other tasks r...
#M2000_Interpreter
M2000 Interpreter
  Print "Hello {World}" Print {Hello "World"} Report {Multiline String 2nd line } Print """Hello There"""={"Hello There"} Print Quote$("Hello There")={"Hello There"}  
http://rosettacode.org/wiki/Literals/String
Literals/String
Task Show literal specification of characters and strings. If supported, show how the following work:   verbatim strings   (quotes where escape sequences are quoted literally)   here-strings   Also, discuss which quotes expand variables. Related tasks   Special characters   Here document Other tasks r...
#M4
M4
`this is quoted string'
http://rosettacode.org/wiki/Literals/String
Literals/String
Task Show literal specification of characters and strings. If supported, show how the following work:   verbatim strings   (quotes where escape sequences are quoted literally)   here-strings   Also, discuss which quotes expand variables. Related tasks   Special characters   Here document Other tasks r...
#Maple
Maple
  > "foobar"; "foobar"   > "foo\nbar"; # string with a newline "foo bar"   > "c"; # a character "c"  
http://rosettacode.org/wiki/Literals/Integer
Literals/Integer
Some programming languages have ways of expressing integer literals in bases other than the normal base ten. Task Show how integer literals can be expressed in as many bases as your language allows. Note:   this should not involve the calling of any functions/methods, but should be interpreted by the compiler or...
#Lua
Lua
  45, 0x45  
http://rosettacode.org/wiki/Literals/Integer
Literals/Integer
Some programming languages have ways of expressing integer literals in bases other than the normal base ten. Task Show how integer literals can be expressed in as many bases as your language allows. Note:   this should not involve the calling of any functions/methods, but should be interpreted by the compiler or...
#M2000_Interpreter
M2000 Interpreter
  Def ExpType$(x)=Type$(x) Print ExpType$(12345678912345#)="Currency", 12345678912345# Print ExpType$(123456789123456789123456@)="Decimal", 123456789123456789123456@ Print ExpType$(12&)="Long", 12&, 0xFFFFFFFF&=-1 Print ExpType$(12%)="Integer", 12%, 0xFFFF%=-1 \\ used for unsigned integers (but it is double) Print ExpT...
http://rosettacode.org/wiki/Logical_operations
Logical operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Genie
Genie
[indent=4] /* Logical operations in Genie valac logicals.gs ./logicals true false */   def logicals(a:bool, b:bool) print @"$a and $b is $(a and b)" print @"$a or $b is $(a or b)" print @"not $a is $(not a)"   init a:bool = bool.parse(args[1]) b:bool = bool.parse(args[2]) logicals(a, b)
http://rosettacode.org/wiki/Logical_operations
Logical operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Go
Go
func printLogic(a, b bool) { fmt.Println("a and b is", a && b) fmt.Println("a or b is", a || b) fmt.Println("not a is", !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: ...
#FreeBASIC
FreeBASIC
' FB 1.05.0 Win64   Function min(x As Integer, y As Integer) As Integer Return IIf(x < y, x, y) End Function   Dim arr1(1 To 3) As String = {"a", "b", "c"} Dim arr2(1 To 3) As String = {"A", "B", "C"} Dim arr3(1 To 3) As Integer = {1, 2, 3}   For i As Integer = 1 To 3 Print arr1(i) & arr2(i) & arr3(i) Next   Print ...
http://rosettacode.org/wiki/Loops/Break
Loops/Break
Task Show a loop which prints random numbers (each number newly generated each loop) from 0 to 19 (inclusive). If a number is 10, stop the loop after printing it, and do not generate any further numbers. Otherwise, generate and print a second random number before restarting the loop. If the number 10 is never gener...
#Lasso
Lasso
local(x = 0) while(#x != 10) => {^ #x = integer_random(19,0) #x #x == 10 ? loop_abort ', '+integer_random(19,0)+'\r' ^}
http://rosettacode.org/wiki/Loops/Break
Loops/Break
Task Show a loop which prints random numbers (each number newly generated each loop) from 0 to 19 (inclusive). If a number is 10, stop the loop after printing it, and do not generate any further numbers. Otherwise, generate and print a second random number before restarting the loop. If the number 10 is never gener...
#Liberty_BASIC
Liberty BASIC
while num<>10 num=rnd(1)*20 print num if num=10 then exit while print rnd(1)*20 wend  
http://rosettacode.org/wiki/Longest_common_subsequence
Longest common subsequence
Introduction Define a subsequence to be any output string obtained by deleting zero or more symbols from an input string. The Longest Common Subsequence (LCS) is a subsequence of maximum length common to two or more strings. Let A ≡ A[0]… A[m - 1] and B ≡ B[0]… B[n - 1], m < n be strings drawn from an alphabet Σ of ...
#Perl
Perl
sub lcs { my ($a, $b) = @_; if (!length($a) || !length($b)) { return ""; } if (substr($a, 0, 1) eq substr($b, 0, 1)) { return substr($a, 0, 1) . lcs(substr($a, 1), substr($b, 1)); } my $c = lcs(substr($a, 1), $b) || ""; my $d = lcs($a, substr($b, 1)) || ""; return length(...
http://rosettacode.org/wiki/Look-and-say_sequence
Look-and-say sequence
The   Look and say sequence   is a recursively defined sequence of numbers studied most notably by   John Conway. The   look-and-say sequence   is also known as the   Morris Number Sequence,   after cryptographer Robert Morris,   and the puzzle   What is the next number in the sequence 1,   11,   21,   1211,   11122...
#Go
Go
package main   import ( "fmt" "strconv" )   func lss(s string) (r string) { c := s[0] nc := 1 for i := 1; i < len(s); i++ { d := s[i] if d == c { nc++ continue } r += strconv.Itoa(nc) + string(c) c = d nc = 1 } return r ...
http://rosettacode.org/wiki/Longest_increasing_subsequence
Longest increasing subsequence
Calculate and show here a longest increasing subsequence of the list: { 3 , 2 , 6 , 4 , 5 , 1 } {\displaystyle \{3,2,6,4,5,1\}} And of the list: { 0 , 8 , 4 , 12 , 2 , 10 , 6 , 14 , 1 , 9 , 5 , 13 , 3 , 11 , 7 , 15 } {\displaystyle \{0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15\}} Note that a list may have m...
#zkl
zkl
fcn longestSequence(ns){ // based on Patience sorting piles:=L(); backPtr:='wrap(np){ return(np-1,if(np) piles[np-1].len()-1 else -1) }; // maybe (-1,-1) foreach n in (ns){ newPile:=True; // create list of sorted lists foreach e,p in (piles.enumerate()){ if(n<p[-1][0]){ p.del(1,-1) // only need...
http://rosettacode.org/wiki/Loops/Continue
Loops/Continue
Task Show the following output using one loop. 1, 2, 3, 4, 5 6, 7, 8, 9, 10 Try to achieve the result by forcing the next iteration within the loop upon a specific condition, if your language allows it. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while...
#Pascal
Pascal
foreach (1..10) { print $_; if ($_ % 5 == 0) { print "\n"; next; } print ', '; }
http://rosettacode.org/wiki/Loops/Continue
Loops/Continue
Task Show the following output using one loop. 1, 2, 3, 4, 5 6, 7, 8, 9, 10 Try to achieve the result by forcing the next iteration within the loop upon a specific condition, if your language allows it. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while...
#Perl
Perl
foreach (1..10) { print $_; if ($_ % 5 == 0) { print "\n"; next; } print ', '; }
http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers
Luhn test of credit card numbers
The Luhn test is used by some credit card companies to distinguish valid credit card numbers from what could be a random selection of digits. Those companies using credit card numbers that can be validated by the Luhn test have numbers that pass the following test: Reverse the order of the digits in the number. Tak...
#SNOBOL4
SNOBOL4
define('luhn(n)a,d,i,j,sum') :(luhn_end) luhn n = reverse(n); a = array('0:9') ln1 a<i> = (2 * i / 10) + remdr(2 * i,10) i = lt(i,9) i + 1 :s(ln1) ln2 n len(1) . d = :f(ln3) d = ne(remdr(j,2),0) a<d>; j = j + 1 sum = sum + d :(ln2) ln3 luhn = 0; luhn = eq(r...
http://rosettacode.org/wiki/Loops/While
Loops/While
Task Start an integer value at   1024. Loop while it is greater than zero. Print the value (with a newline) and divide it by two each time through the loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For...
#Ursala
Ursala
#import nat   g = ~&h-> ^C/half@h ~&   #show+   main = %nP*=tx g <1024>
http://rosettacode.org/wiki/Loops/While
Loops/While
Task Start an integer value at   1024. Loop while it is greater than zero. Print the value (with a newline) and divide it by two each time through the loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For...
#V
V
1024 [0 >] [ dup puts 2 / >int ] while
http://rosettacode.org/wiki/Loops/Do-while
Loops/Do-while
Start with a value at 0. Loop while value mod 6 is not equal to 0. Each time through the loop, add 1 to the value then print it. The loop must execute at least once. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loo...
#Smalltalk
Smalltalk
|val| val := 0. [ val := val + 1. val displayNl. ] doWhile: [ (val rem: 6) ~= 0 ]
http://rosettacode.org/wiki/Loops/Do-while
Loops/Do-while
Start with a value at 0. Loop while value mod 6 is not equal to 0. Each time through the loop, add 1 to the value then print it. The loop must execute at least once. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loo...
#Sparkling
Sparkling
var i = 0; do { print(++i); } while (i % 6 != 0);
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. ...
#langur
langur
for .i = 0; .i < 5; .i += 1 { for .j = 0; .j <= .i; .j += 1 { write "*" } writeln() }
http://rosettacode.org/wiki/Loops/For_with_a_specified_step
Loops/For with a specified step
Task Demonstrate a   for-loop   where the step-value is greater than one. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For with a specified step   Loops/Foreach   Loops/Increment loop index within loop b...
#Quackery
Quackery
  20 times [ i^ echo sp 2 step ] cr 1024 times [ i^ 1+ echo sp i^ 1+ step ] cr 1 56 times [ i^ echo sp i^ swap step ] cr
http://rosettacode.org/wiki/Loops/For_with_a_specified_step
Loops/For with a specified step
Task Demonstrate a   for-loop   where the step-value is greater than one. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For with a specified step   Loops/Foreach   Loops/Increment loop index within loop b...
#R
R
for(a in seq(2,8,2)) { cat(a, ", ") } cat("who do we appreciate?\n")
http://rosettacode.org/wiki/Long_multiplication
Long multiplication
Task Explicitly implement   long multiplication. This is one possible approach to arbitrary-precision integer algebra. For output, display the result of   264 * 264. Optionally, verify your result against builtin arbitrary precision support. The decimal representation of   264   is: 18,446,744,073,709,551,61...
#Julia
Julia
module LongMultiplication   using Compat   function addwithcarry!(r, addend, addendpos) while true pad = max(0, addendpos - lastindex(r)) append!(r, fill(0, pad)) addendrst = addend + r[addendpos] addend, r[addendpos] = divrem(addendrst, 10) iszero(addend) && break ad...
http://rosettacode.org/wiki/Literals/String
Literals/String
Task Show literal specification of characters and strings. If supported, show how the following work:   verbatim strings   (quotes where escape sequences are quoted literally)   here-strings   Also, discuss which quotes expand variables. Related tasks   Special characters   Here document Other tasks r...
#Mathematica.2FWolfram_Language
Mathematica/Wolfram Language
There is no character type in Mathematica, only string type. "c"; // String (result: "c") "\n"; // String (result: newline character)
http://rosettacode.org/wiki/Literals/String
Literals/String
Task Show literal specification of characters and strings. If supported, show how the following work:   verbatim strings   (quotes where escape sequences are quoted literally)   here-strings   Also, discuss which quotes expand variables. Related tasks   Special characters   Here document Other tasks r...
#MATLAB
MATLAB
  s1 = 'abcd' % simple string s2 = 'ab''cd' % string containing a single quote  
http://rosettacode.org/wiki/Literals/Integer
Literals/Integer
Some programming languages have ways of expressing integer literals in bases other than the normal base ten. Task Show how integer literals can be expressed in as many bases as your language allows. Note:   this should not involve the calling of any functions/methods, but should be interpreted by the compiler or...
#M4
M4
eval(10) # base 10 eval(010) # base 8 eval(0x10) # base 16
http://rosettacode.org/wiki/Literals/Integer
Literals/Integer
Some programming languages have ways of expressing integer literals in bases other than the normal base ten. Task Show how integer literals can be expressed in as many bases as your language allows. Note:   this should not involve the calling of any functions/methods, but should be interpreted by the compiler or...
#Mathematica.2FWolfram_Language
Mathematica/Wolfram Language
b^^nnnn is a valid number in base b (with b ranging from 2 to 36) : 2^^1011 -> 11   36^^1011 -> 46693
http://rosettacode.org/wiki/Logical_operations
Logical operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Groovy
Groovy
def logical = { a, b -> println """ a AND b = ${a} && ${b} = ${a & b} a OR b = ${a} || ${b} = ${a | b} NOT a = ! ${a} = ${! a} a XOR b = ${a} != ${b} = ${a != b} a EQV b = ${a} == ${b} = ${a == b} """ }
http://rosettacode.org/wiki/Logical_operations
Logical operations
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation |...
#Harbour
Harbour
PROCEDURE Foo( a, b ) // a and b was defined as .F. (false) or .T. (true) ? a .AND. b ? a .OR. b ? ! a, ! b RETURN
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: ...
#Frink
Frink
a1 = ["a", "b", "c"] a2 = ["A", "B", "C"] a3 = ["1", "2", "3"] m = [a1, a2, a3] for row = m.transpose[] println[join["",row]]
http://rosettacode.org/wiki/Loops/Break
Loops/Break
Task Show a loop which prints random numbers (each number newly generated each loop) from 0 to 19 (inclusive). If a number is 10, stop the loop after printing it, and do not generate any further numbers. Otherwise, generate and print a second random number before restarting the loop. If the number 10 is never gener...
#Lingo
Lingo
repeat while TRUE n = random(20)-1 put n if n = 10 then exit repeat put random(20)-1 end repeat
http://rosettacode.org/wiki/Loops/Break
Loops/Break
Task Show a loop which prints random numbers (each number newly generated each loop) from 0 to 19 (inclusive). If a number is 10, stop the loop after printing it, and do not generate any further numbers. Otherwise, generate and print a second random number before restarting the loop. If the number 10 is never gener...
#Lisaac
Lisaac
Section Header   + name := TEST_LOOP_BREAK;   Section Public   - main <- ( + a, b : INTEGER;   `srand(time(NULL))`; { a := `rand()`:INTEGER % 20; // not exactly uniformly distributed, but doesn't matter a.print; '\n'.print; a == 10 }.until_do { b := `rand()`:INTEGER % 20; // not exactly unif...
http://rosettacode.org/wiki/Longest_common_subsequence
Longest common subsequence
Introduction Define a subsequence to be any output string obtained by deleting zero or more symbols from an input string. The Longest Common Subsequence (LCS) is a subsequence of maximum length common to two or more strings. Let A ≡ A[0]… A[m - 1] and B ≡ B[0]… B[n - 1], m < n be strings drawn from an alphabet Σ of ...
#Phix
Phix
with javascript_semantics function lcs(sequence a, b) sequence res = "" if length(a) and length(b) then if a[$]=b[$] then res = lcs(a[1..-2],b[1..-2])&a[$] else sequence l = lcs(a,b[1..-2]), r = lcs(a[1..-2],b) res = iff(length(l)>length(r...
http://rosettacode.org/wiki/Look-and-say_sequence
Look-and-say sequence
The   Look and say sequence   is a recursively defined sequence of numbers studied most notably by   John Conway. The   look-and-say sequence   is also known as the   Morris Number Sequence,   after cryptographer Robert Morris,   and the puzzle   What is the next number in the sequence 1,   11,   21,   1211,   11122...
#Groovy
Groovy
def lookAndSay(sequence) { def encoded = new StringBuilder() (sequence.toString() =~ /(([0-9])\2*)/).each { matcher -> encoded.append(matcher[1].size()).append(matcher[2]) } encoded.toString() }
http://rosettacode.org/wiki/Loops/Continue
Loops/Continue
Task Show the following output using one loop. 1, 2, 3, 4, 5 6, 7, 8, 9, 10 Try to achieve the result by forcing the next iteration within the loop upon a specific condition, if your language allows it. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while...
#Phix
Phix
with javascript_semantics for i=1 to 10 do printf(1,"%d", i) if remainder(i,5)=0 then printf(1, "\n") continue end if printf(1,", ") end for
http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers
Luhn test of credit card numbers
The Luhn test is used by some credit card companies to distinguish valid credit card numbers from what could be a random selection of digits. Those companies using credit card numbers that can be validated by the Luhn test have numbers that pass the following test: Reverse the order of the digits in the number. Tak...
#SPARK
SPARK
with Spark_IO; --# inherit Spark_IO; --# main_program; procedure Luhn --# global in out Spark_IO.Outputs; --# derives Spark_IO.Outputs from *; is   function Luhn_Test (Num : String) return Boolean --# pre Num'Last <= 20; is Sum : Integer := 0; Od  : Boolean := True; Int : Integer; X  ...
http://rosettacode.org/wiki/Loops/While
Loops/While
Task Start an integer value at   1024. Loop while it is greater than zero. Print the value (with a newline) and divide it by two each time through the loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For...
#Vala
Vala
int i = 1024; while (i > 0) { stdout.printf("%d\n", i); i /= 2; }
http://rosettacode.org/wiki/Loops/While
Loops/While
Task Start an integer value at   1024. Loop while it is greater than zero. Print the value (with a newline) and divide it by two each time through the loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For...
#VBA
VBA
Public Sub LoopsWhile() Dim value As Integer value = 1024 Do While value > 0 Debug.Print value value = value / 2 Loop End Sub
http://rosettacode.org/wiki/Loops/Do-while
Loops/Do-while
Start with a value at 0. Loop while value mod 6 is not equal to 0. Each time through the loop, add 1 to the value then print it. The loop must execute at least once. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loo...
#Spin
Spin
con _clkmode = xtal1 + pll16x _clkfreq = 80_000_000   obj ser : "FullDuplexSerial.spin"   pub main | n ser.start(31, 30, 0, 115200)   n := 0 repeat n += 1 ser.dec(n) ser.tx(32) while n // 6   waitcnt(_clkfreq + cnt) ser.stop cogstop(0)
http://rosettacode.org/wiki/Loops/Do-while
Loops/Do-while
Start with a value at 0. Loop while value mod 6 is not equal to 0. Each time through the loop, add 1 to the value then print it. The loop must execute at least once. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loo...
#SPL
SPL
n = 0 > n += 1 #.output(n) < n%6
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. ...
#Lasso
Lasso
loop(5) => {^ loop(loop_count) => {^ '*' ^} '\r' ^}
http://rosettacode.org/wiki/Loops/For_with_a_specified_step
Loops/For with a specified step
Task Demonstrate a   for-loop   where the step-value is greater than one. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For with a specified step   Loops/Foreach   Loops/Increment loop index within loop b...
#Racket
Racket
  #lang racket   (for ([i (in-range 2 9 2)]) (printf "~a, " i)) (printf "who do we appreciate?~n")  
http://rosettacode.org/wiki/Loops/For_with_a_specified_step
Loops/For with a specified step
Task Demonstrate a   for-loop   where the step-value is greater than one. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for   Loops/For   Loops/For with a specified step   Loops/Foreach   Loops/Increment loop index within loop b...
#Raku
Raku
for 2, 4 ... 8 { print "$_, "; }   say 'whom do we appreciate?';
http://rosettacode.org/wiki/Long_multiplication
Long multiplication
Task Explicitly implement   long multiplication. This is one possible approach to arbitrary-precision integer algebra. For output, display the result of   264 * 264. Optionally, verify your result against builtin arbitrary precision support. The decimal representation of   264   is: 18,446,744,073,709,551,61...
#Kotlin
Kotlin
fun String.toDigits() = mapIndexed { i, c -> if (!c.isDigit()) throw IllegalArgumentException("Invalid digit $c found at position $i") c - '0' }.reversed()   operator fun String.times(n: String): String { val left = toDigits() val right = n.toDigits() val result = IntArray(left.size + right....
http://rosettacode.org/wiki/Literals/String
Literals/String
Task Show literal specification of characters and strings. If supported, show how the following work:   verbatim strings   (quotes where escape sequences are quoted literally)   here-strings   Also, discuss which quotes expand variables. Related tasks   Special characters   Here document Other tasks r...
#Maxima
Maxima
/* A string */ "The quick brown fox jumps over the lazy dog";   /* A character - just a one character string */ "a"