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/Loops/Foreach
Loops/Foreach
Loop through and print each element in a collection in order. Use your language's "for each" loop if it has one, otherwise iterate through the collection in order with some other loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for...
#D
D
import std.stdio: writeln;   void main() { auto collection1 = "ABC"; foreach (element; collection1) writeln(element);   auto collection2 = [1, 2, 3]; foreach (element; collection1) writeln(element);   auto collection3 = [1:10, 2:20, 3:30]; foreach (element; collection3) ...
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...
#Bracmat
Bracmat
( luhn = sum odd even . 0:?sum & rev$!arg:?arg & whl ' ( @( !arg  :  %?odd ( %?even ?arg | :?arg&0:?even ) ) & !odd+mod$(2*!even.10)+div$(!even.5)+!sum:?sum ) & mod$(!sum.10):0 ) & ...
http://rosettacode.org/wiki/Lucas-Lehmer_test
Lucas-Lehmer test
Lucas-Lehmer Test: for p {\displaystyle p} an odd prime, the Mersenne number 2 p − 1 {\displaystyle 2^{p}-1} is prime if and only if 2 p − 1 {\displaystyle 2^{p}-1} divides S ( p − 1 ) {\displaystyle S(p-1)} where S ( n + 1 ) = ( S ( n ) ) 2 − 2 {\displaystyle S(n+1)=(...
#Fortran
Fortran
PROGRAM LUCAS_LEHMER IMPLICIT NONE   INTEGER, PARAMETER :: i64 = SELECTED_INT_KIND(18) INTEGER(i64) :: s, n INTEGER :: i, exponent   DO exponent = 2, 31 IF (exponent == 2) THEN s = 0 ELSE s = 4 END IF n = 2_i64**exponent - 1 DO i = 1, exponent-2 s = MOD(s*s - 2...
http://rosettacode.org/wiki/LZW_compression
LZW compression
The Lempel-Ziv-Welch (LZW) algorithm provides loss-less data compression. You can read a complete description of it in the   Wikipedia article   on the subject.   It was patented, but it entered the public domain in 2004.
#Liberty_BASIC
Liberty BASIC
DIM LZW(1, 1) DIM JDlzw(1) DIM JDch$(1) LET maxBits = 20 ' maximum bit width of the dictionary: minimum=12; maximum=21 LET resetDictionary = 1 ' flag to reset the dictionary when it gets full: 1=TRUE; 0=FALSE LET printDictionary = 0 ' output encoding and decoding dictionaries to files LET maxChunkSi...
http://rosettacode.org/wiki/LU_decomposition
LU decomposition
Every square matrix A {\displaystyle A} can be decomposed into a product of a lower triangular matrix L {\displaystyle L} and a upper triangular matrix U {\displaystyle U} , as described in LU decomposition. A = L U {\displaystyle A=LU} It is a modified form of Gaussian elimination. Whil...
#PL.2FI
PL/I
(subscriptrange, fofl, size): /* 2 Nov. 2013 */ LU_Decomposition: procedure options (main); declare a1(3,3) float (18) initial ( 1, 3, 5, 2, 4, 7, 1, 1, 0); declare a2(4,4) float (18) initial (11, 9, 24,...
http://rosettacode.org/wiki/Lychrel_numbers
Lychrel numbers
  Take an integer n, greater than zero.   Form the next n of its series by reversing the digits of the current n and adding the result to the current n.   Stop when n becomes palindromic - i.e. the digits of n in reverse order == n. The above recurrence relation when applied to most starting numbers n = 1, 2, ... ...
#Sidef
Sidef
var ( lychrels = [], palindromes = [], seeds = [], max = 500, )   for i in (1 .. 10_000) { var (test = [], count = 0)   func lychrel(l) { count++ > max && return true test << (var m = (l + Num(Str(l).flip))) Str(m).is_palindrome && return false lychrel(m) }   ...
http://rosettacode.org/wiki/Mad_Libs
Mad Libs
This page uses content from Wikipedia. The original article was at Mad Libs. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) Mad Libs is a phrasal template word game where one player prompts another...
#PowerShell
PowerShell
  function New-MadLibs { [CmdletBinding(DefaultParameterSetName='None')] [OutputType([string])] Param ( [Parameter(Mandatory=$false)] [AllowEmptyString()] [string] $Name = "",   [Parameter(Mandatory=$false, ParameterSetName='Male')] [switch] $Male,...
http://rosettacode.org/wiki/Loops/Increment_loop_index_within_loop_body
Loops/Increment loop index within loop body
Sometimes, one may need   (or want)   a loop which its   iterator   (the index variable)   is modified within the loop body   in addition to the normal incrementation by the   (do)   loop structure index. Goal Demonstrate the best way to accomplish this. Task Write a loop which:   starts the index (variable...
#Haxe
Haxe
using StringTools; import haxe.Int64;   class PrimeNumberLoops { private static var limit = 42;   static function isPrime(i:Int64):Bool { if (i == 2 || i == 3) { return true; } else if (i % 2 == 0 || i % 3 ==0) { return false; } var idx:haxe.Int64 = 5; while (idx * idx <= i) { ...
http://rosettacode.org/wiki/Loops/Increment_loop_index_within_loop_body
Loops/Increment loop index within loop body
Sometimes, one may need   (or want)   a loop which its   iterator   (the index variable)   is modified within the loop body   in addition to the normal incrementation by the   (do)   loop structure index. Goal Demonstrate the best way to accomplish this. Task Write a loop which:   starts the index (variable...
#J
J
  tacit_loop =: _1&(>:@:{`[`]})@:(, (1&p: # _1 2&p.)@:{:)@:]^:(0 ~: (>: #))^:_ x:  
http://rosettacode.org/wiki/Loops/Infinite
Loops/Infinite
Task Print out       SPAM       followed by a   newline   in an infinite loop. 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 l...
#Befunge
Befunge
55+"MAPS",,,,,
http://rosettacode.org/wiki/Loops/Infinite
Loops/Infinite
Task Print out       SPAM       followed by a   newline   in an infinite loop. 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 l...
#blz
blz
while true print("SPAM") end
http://rosettacode.org/wiki/Loops/With_multiple_ranges
Loops/With multiple ranges
Loops/With multiple ranges You are encouraged to solve this task according to the task description, using any language you may know. Some languages allow multiple loop ranges, such as the PL/I example (snippet) below. /* all variables are DECLARED as integers. */ prod=...
#Phix
Phix
integer prod = 1, total = 0, -- (renamed as sum is a Phix builtin) x = +5, y = -5, z = -2, one = 1, three = 3, seven = 7 sequence loopset = {{ -three, power(3,3), three }, { -seven, +seven, 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...
#C
C
int i = 1024; while(i > 0) { 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...
#C.23
C#
int i = 1024; while(i > 0){ System.Console.WriteLine(i); i /= 2; }
http://rosettacode.org/wiki/Loops/Downward_for
Loops/Downward for
Task Write a   for   loop which writes a countdown from   10   to   0. 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 body...
#Arturo
Arturo
loop 10..0 'i [ print i ]
http://rosettacode.org/wiki/Loops/Downward_for
Loops/Downward for
Task Write a   for   loop which writes a countdown from   10   to   0. 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 body...
#Asymptote
Asymptote
for(int i = 10; i >=0; --i) { write(i); }
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...
#ALGOL_60
ALGOL 60
'BEGIN' 'COMMENT' Loops DoWhile - Algol60 - 22/06/2018; 'INTEGER' I; I:=0; LOOP: I:=I+1; OUTINTEGER(1,I); 'IF' I=I'/'6*6 'THEN' 'GOTO' ENDLOOP; 'GOTO' LOOP; ENDLOOP: 'END'
http://rosettacode.org/wiki/Loops/For
Loops/For
“For”   loops are used to make some block of code be iterated a number of times, setting a variable or parameter to a monotonically increasing integer value for each execution of the block of code. Common extensions of this allow other counting patterns or iterating over abstract structures other than the integers. ...
#ALGOL_W
ALGOL W
begin for i := 1 until 5 do begin write( "*" ); for j := 2 until i do begin writeon( "*" ) end j end i end.
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...
#Arturo
Arturo
loop 0..10 .step:2 'i [ print i ]
http://rosettacode.org/wiki/Ludic_numbers
Ludic numbers
Ludic numbers   are related to prime numbers as they are generated by a sieve quite like the Sieve of Eratosthenes is used to generate prime numbers. The first ludic number is   1. To generate succeeding ludic numbers create an array of increasing integers starting from   2. 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1...
#Prolog
Prolog
  % John Devou: 26-Nov-2021   d(_,_,[],[]). d(N,N,[_|Xs],Rs):- d(N,1,Xs,Rs). d(N,M,[X|Xs],[X|Rs]):- M < N, M_ is M+1, d(N,M_,Xs,Rs).   l([],[]). l([X|Xs],[X|Rs]):- d(X,1,Xs,Ys), l(Ys,Rs).   % g(N,L):- generate in L a list with Ludic numbers up to N   g(N,[1|X]):- numlist(2,N,L), l(L,X).   s(0,Xs,[],Xs). s(N,[X|Xs],[X|L...
http://rosettacode.org/wiki/Loops/N_plus_one_half
Loops/N plus one half
Quite often one needs loops which, in the last iteration, execute only part of the loop body. Goal Demonstrate the best way to do this. Task Write a loop which writes the comma-separated list 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 using separate output statements for the number and the comma from within the body of th...
#CoffeeScript
CoffeeScript
  # Loop plus half. This code shows how to break out of a loop early # on the last iteration. For the contrived example, there are better # ways to generate a comma-separated list, of course. start = 1 end = 10 s = '' for i in [start..end] # the top half of the loop executes every time s += i break if i == end ...
http://rosettacode.org/wiki/Loops/N_plus_one_half
Loops/N plus one half
Quite often one needs loops which, in the last iteration, execute only part of the loop body. Goal Demonstrate the best way to do this. Task Write a loop which writes the comma-separated list 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 using separate output statements for the number and the comma from within the body of th...
#ColdFusion
ColdFusion
<cfloop index = "i" from = "1" to = "10"> #i# <cfif i EQ 10> <cfbreak /> </cfif> , </cfloop>
http://rosettacode.org/wiki/Loops/Nested
Loops/Nested
Show a nested loop which searches a two-dimensional array filled with random numbers uniformly distributed over [ 1 , … , 20 ] {\displaystyle [1,\ldots ,20]} . The loops iterate rows and columns of the array printing the elements until the value 20 {\displaystyle 20} is met. Specifically, this task als...
#Clojure
Clojure
(ns nested)   (defn create-matrix [width height] (for [_ (range width)] (for [_ (range height)] (inc (rand-int 20)))))   (defn print-matrix [matrix] (loop [[row & rs] matrix] (when (= (loop [[x & xs] row] (println x) (cond (= x 20) :stop xs (recur xs)...
http://rosettacode.org/wiki/Loops/Foreach
Loops/Foreach
Loop through and print each element in a collection in order. Use your language's "for each" loop if it has one, otherwise iterate through the collection in order with some other loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for...
#Dao
Dao
items = { 1, 2, 3 } for( item in items ) io.writeln( item )
http://rosettacode.org/wiki/Loops/Foreach
Loops/Foreach
Loop through and print each element in a collection in order. Use your language's "for each" loop if it has one, otherwise iterate through the collection in order with some other loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for...
#Delphi
Delphi
program LoopForEach;   {$APPTYPE CONSOLE}   var s: string; begin for s in 'Hello' do Writeln(s); end.
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...
#Brainf.2A.2A.2A
Brainf***
>>>>>>>>>+>,----------[ READ CHARACTERS UNTIL \N AND >++++++++[<----->-]<++>>>>>>+>,----------] SUBTRACT ASCII 0 FROM EACH <-<<<<<<< GO TO LAST DIGIT [ WHILE THERE ARE DIGITS >>>>>>>>[-<<<<<<<+>>>>>>>]<<<<<<<<...
http://rosettacode.org/wiki/Lucas-Lehmer_test
Lucas-Lehmer test
Lucas-Lehmer Test: for p {\displaystyle p} an odd prime, the Mersenne number 2 p − 1 {\displaystyle 2^{p}-1} is prime if and only if 2 p − 1 {\displaystyle 2^{p}-1} divides S ( p − 1 ) {\displaystyle S(p-1)} where S ( n + 1 ) = ( S ( n ) ) 2 − 2 {\displaystyle S(n+1)=(...
#FreeBASIC
FreeBASIC
' version 18-09-2015 ' compile with: fbc -s console   #Ifndef TRUE ' define true and false for older freebasic versions #Define FALSE 0 #Define TRUE Not FALSE #EndIf   Function mul_mod(a As ULongInt, b As ULongInt, modulus As ULongInt) As ULongInt ' returns a * b mod modulus   Dim As ULongInt x ,...
http://rosettacode.org/wiki/LZW_compression
LZW compression
The Lempel-Ziv-Welch (LZW) algorithm provides loss-less data compression. You can read a complete description of it in the   Wikipedia article   on the subject.   It was patented, but it entered the public domain in 2004.
#Lua
Lua
local function compress(uncompressed) -- string local dictionary, result, dictSize, w, c = {}, {}, 255, "" for i = 0, 255 do dictionary[string.char(i)] = i end for i = 1, #uncompressed do c = string.sub(uncompressed, i, i) if dictionary[w .. c] then w = w .. c else table.insert(resul...
http://rosettacode.org/wiki/LU_decomposition
LU decomposition
Every square matrix A {\displaystyle A} can be decomposed into a product of a lower triangular matrix L {\displaystyle L} and a upper triangular matrix U {\displaystyle U} , as described in LU decomposition. A = L U {\displaystyle A=LU} It is a modified form of Gaussian elimination. Whil...
#Python
Python
from pprint import pprint   def matrixMul(A, B): TB = zip(*B) return [[sum(ea*eb for ea,eb in zip(a,b)) for b in TB] for a in A]   def pivotize(m): """Creates the pivoting matrix for m.""" n = len(m) ID = [[float(i == j) for i in xrange(n)] for j in xrange(n)] for j in xrange(n): row = m...
http://rosettacode.org/wiki/Lychrel_numbers
Lychrel numbers
  Take an integer n, greater than zero.   Form the next n of its series by reversing the digits of the current n and adding the result to the current n.   Stop when n becomes palindromic - i.e. the digits of n in reverse order == n. The above recurrence relation when applied to most starting numbers n = 1, 2, ... ...
#Swift
Swift
import BigInt   public struct Lychrel<T: ReversibleNumeric & CustomStringConvertible>: Sequence, IteratorProtocol { @usableFromInline let seed: T   @usableFromInline var done = false   @usableFromInline var n: T   @usableFromInline var iterations: T   @inlinable public init(seed: T, iterations: T = ...
http://rosettacode.org/wiki/Mad_Libs
Mad Libs
This page uses content from Wikipedia. The original article was at Mad Libs. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) Mad Libs is a phrasal template word game where one player prompts another...
#PureBasic
PureBasic
  If OpenConsole()   cadena$ = "<name> went for a walk in the park. <he or she> found a <noun>. <name> decided to take it home." k = FindString(cadena$, "<")   PrintN("La historia: ") Print(cadena$ + Chr(10)) While k reemplaza$ = Mid(cadena$, k, FindString(cadena$, ">") - k + 1) Print(Chr(10) + "What ...
http://rosettacode.org/wiki/Loops/Increment_loop_index_within_loop_body
Loops/Increment loop index within loop body
Sometimes, one may need   (or want)   a loop which its   iterator   (the index variable)   is modified within the loop body   in addition to the normal incrementation by the   (do)   loop structure index. Goal Demonstrate the best way to accomplish this. Task Write a loop which:   starts the index (variable...
#Java
Java
public class LoopIncrementWithinBody {   static final int LIMIT = 42;   static boolean isPrime(long n) { if (n % 2 == 0) return n == 2; if (n % 3 == 0) return n == 3; long d = 5; while (d * d <= n) { if (n % d == 0) return false; d += 2; if (n ...
http://rosettacode.org/wiki/Loops/Infinite
Loops/Infinite
Task Print out       SPAM       followed by a   newline   in an infinite loop. 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 l...
#bootBASIC
bootBASIC
10 print "SPAM" 20 goto 10
http://rosettacode.org/wiki/Loops/Infinite
Loops/Infinite
Task Print out       SPAM       followed by a   newline   in an infinite loop. 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 l...
#BQN
BQN
{𝕊 •Out 𝕩}"SPAM"
http://rosettacode.org/wiki/Loops/With_multiple_ranges
Loops/With multiple ranges
Loops/With multiple ranges You are encouraged to solve this task according to the task description, using any language you may know. Some languages allow multiple loop ranges, such as the PL/I example (snippet) below. /* all variables are DECLARED as integers. */ prod=...
#Picat
Picat
foreach(I in 1..3, J in 4..6, K in 7..9)  % ... end
http://rosettacode.org/wiki/Loops/With_multiple_ranges
Loops/With multiple ranges
Loops/With multiple ranges You are encouraged to solve this task according to the task description, using any language you may know. Some languages allow multiple loop ranges, such as the PL/I example (snippet) below. /* all variables are DECLARED as integers. */ prod=...
#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). for(Hi,Lo,Step,Hi) :- Step<0, Lo=<Hi. for(Hi,Lo,Step,Val) :- Step<0, plus(Hi,Step,V), Lo=<V, !, for(V,Lo,Step,Val).   sym(x,5). % symbolic lookups for values sym(y,-5). sym(z,-2). sym(on...
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...
#C.2B.2B
C++
int i = 1024; while(i > 0){ std::cout << i << std::endl; 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...
#Cach.C3.A9_ObjectScript
Caché ObjectScript
WHILELOOP set x = 1024 while (x > 0) { write x,! set x = (x \ 2)  ; using non-integer division will never get to 0 }   quit
http://rosettacode.org/wiki/Loops/Downward_for
Loops/Downward for
Task Write a   for   loop which writes a countdown from   10   to   0. 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 body...
#AutoHotkey
AutoHotkey
x := 10 While (x >= 0) { output .= "`n" . x x-- } MsgBox % output  
http://rosettacode.org/wiki/Loops/Downward_for
Loops/Downward for
Task Write a   for   loop which writes a countdown from   10   to   0. 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 body...
#Avail
Avail
For each i from 10 to 0 by -1 do [Print: “i” ++ "\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...
#ALGOL_68
ALGOL 68
FOR value WHILE print(value); # WHILE # value MOD 6 /= 0 DO SKIP OD
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. ...
#ALGOL-M
ALGOL-M
BEGIN INTEGER I, J; FOR I := 1 STEP 1 UNTIL 5 DO BEGIN WRITE( "" ); FOR J := 1 STEP 1 UNTIL I DO WRITEON( "*" ); END; END
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...
#AutoHotkey
AutoHotkey
SetBatchLines, -1 iterations := 5 step := 10 iterations *= step Loop,  % iterations { If Mod(A_Index, step) Continue MsgBox, % A_Index } ExitApp
http://rosettacode.org/wiki/Ludic_numbers
Ludic numbers
Ludic numbers   are related to prime numbers as they are generated by a sieve quite like the Sieve of Eratosthenes is used to generate prime numbers. The first ludic number is   1. To generate succeeding ludic numbers create an array of increasing integers starting from   2. 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1...
#PureBasic
PureBasic
EnableExplicit If Not OpenConsole() : End 1 : EndIf   #NMAX=25000   Dim ludic.b(#NMAX) FillMemory(@ludic(0),SizeOf(Byte)*#NMAX,#True,#PB_Byte)   Define.i i,j,n,c1,c2 Define r1$,r2$,r3$,r4$   For i=2 To Int(#NMAX/2) If ludic(i) n=0 For j=i+1 To #NMAX If ludic(j) : n+1 : EndIf If n=i : ludic(j)=...
http://rosettacode.org/wiki/Loops/N_plus_one_half
Loops/N plus one half
Quite often one needs loops which, in the last iteration, execute only part of the loop body. Goal Demonstrate the best way to do this. Task Write a loop which writes the comma-separated list 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 using separate output statements for the number and the comma from within the body of th...
#Common_Lisp
Common Lisp
  (loop for i from 1 below 10 do (princ i) (princ ", ") finally (princ i))  
http://rosettacode.org/wiki/Loops/N_plus_one_half
Loops/N plus one half
Quite often one needs loops which, in the last iteration, execute only part of the loop body. Goal Demonstrate the best way to do this. Task Write a loop which writes the comma-separated list 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 using separate output statements for the number and the comma from within the body of th...
#Cowgol
Cowgol
include "cowgol.coh";   var i: uint8 := 1; loop print_i8(i); if i == 10 then break; end if; print(", "); i := i + 1; end loop; print_nl();
http://rosettacode.org/wiki/Loops/Nested
Loops/Nested
Show a nested loop which searches a two-dimensional array filled with random numbers uniformly distributed over [ 1 , … , 20 ] {\displaystyle [1,\ldots ,20]} . The loops iterate rows and columns of the array printing the elements until the value 20 {\displaystyle 20} is met. Specifically, this task als...
#COBOL
COBOL
IDENTIFICATION DIVISION. PROGRAM-ID. Nested-Loop.   DATA DIVISION. LOCAL-STORAGE SECTION. 78 Table-Size VALUE 10. 01 Table-Area. 03 Table-Row OCCURS Table-Size TIMES INDEXED BY Row-Index. 05 Table-Element PIC 99 OCCURS Table-Size...
http://rosettacode.org/wiki/Loops/Foreach
Loops/Foreach
Loop through and print each element in a collection in order. Use your language's "for each" loop if it has one, otherwise iterate through the collection in order with some other loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for...
#Dragon
Dragon
  for value : array { showln value }  
http://rosettacode.org/wiki/Loops/Foreach
Loops/Foreach
Loop through and print each element in a collection in order. Use your language's "for each" loop if it has one, otherwise iterate through the collection in order with some other loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for...
#Dyalect
Dyalect
for i in [1,2,3] { print(i) }
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...
#Burlesque
Burlesque
  tt "Remove whitespace"vv pe "Eval to number"vv <- "Reverse digits"vv XX "Split number into digits"vv   { { "Odd digits"vv 2EN }   { "Even digits"vv 2en { 2.* "Double"vv ^^ 9.> "<test>=Duplicate greater than 9"vv { XX++ "Sum digits"vv }if "If <test>"...
http://rosettacode.org/wiki/Lucas-Lehmer_test
Lucas-Lehmer test
Lucas-Lehmer Test: for p {\displaystyle p} an odd prime, the Mersenne number 2 p − 1 {\displaystyle 2^{p}-1} is prime if and only if 2 p − 1 {\displaystyle 2^{p}-1} divides S ( p − 1 ) {\displaystyle S(p-1)} where S ( n + 1 ) = ( S ( n ) ) 2 − 2 {\displaystyle S(n+1)=(...
#Frink
Frink
  for n = primes[] if isPrime[2^n-1] println[n]  
http://rosettacode.org/wiki/LZW_compression
LZW compression
The Lempel-Ziv-Welch (LZW) algorithm provides loss-less data compression. You can read a complete description of it in the   Wikipedia article   on the subject.   It was patented, but it entered the public domain in 2004.
#M2000_Interpreter
M2000 Interpreter
  Module BBCtrans { \\ LZW compression plaintext$="TOBEORNOTTOBEORTOBEORNOT" Function encodeLZW$(i$) { Def long c, d, i, l, o$, w$ DIM dict$(0 to 4095) FOR i = 0 TO 255 : dict$(i) = CHR$(i) : NEXT i l = i i = 1 ...
http://rosettacode.org/wiki/LU_decomposition
LU decomposition
Every square matrix A {\displaystyle A} can be decomposed into a product of a lower triangular matrix L {\displaystyle L} and a upper triangular matrix U {\displaystyle U} , as described in LU decomposition. A = L U {\displaystyle A=LU} It is a modified form of Gaussian elimination. Whil...
#R
R
library(Matrix) A <- matrix(c(1, 3, 5, 2, 4, 7, 1, 1, 0), 3, 3, byrow=T) dim(A) <- c(3, 3) expand(lu(A))
http://rosettacode.org/wiki/Lychrel_numbers
Lychrel numbers
  Take an integer n, greater than zero.   Form the next n of its series by reversing the digits of the current n and adding the result to the current n.   Stop when n becomes palindromic - i.e. the digits of n in reverse order == n. The above recurrence relation when applied to most starting numbers n = 1, 2, ... ...
#Tcl
Tcl
proc pal? {n} { expr {$n eq [string reverse $n]} }   proc step {n} { set n_ [string reverse $n] set n_ [string trimleft $n_ 0] expr {$n + $n_} }   proc lychrels {{max 10000} {iters 500}} {   set lychrels {} ;# true Lychrels set visited {} ;# visited numbers for Related test set nRel...
http://rosettacode.org/wiki/Mad_Libs
Mad Libs
This page uses content from Wikipedia. The original article was at Mad Libs. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) Mad Libs is a phrasal template word game where one player prompts another...
#Python
Python
import re   # Optional Python 2.x compatibility #try: input = raw_input #except: pass   template = '''<name> went for a walk in the park. <he or she> found a <noun>. <name> decided to take it home.'''   def madlibs(template): print('The story template is:\n' + template) fields = sorted(set( re.findall('<[^>]+>'...
http://rosettacode.org/wiki/Loops/Increment_loop_index_within_loop_body
Loops/Increment loop index within loop body
Sometimes, one may need   (or want)   a loop which its   iterator   (the index variable)   is modified within the loop body   in addition to the normal incrementation by the   (do)   loop structure index. Goal Demonstrate the best way to accomplish this. Task Write a loop which:   starts the index (variable...
#jq
jq
{i:42, count:0} | while( .count <= 42; .emit = null | .i += 1 | if .i|is_prime then .count += 1 | .emit = "count at \(.i) is \(.count)" | .i = .i + .i - 1 else . end ) | select(.emit).emit
http://rosettacode.org/wiki/Loops/Increment_loop_index_within_loop_body
Loops/Increment loop index within loop body
Sometimes, one may need   (or want)   a loop which its   iterator   (the index variable)   is modified within the loop body   in addition to the normal incrementation by the   (do)   loop structure index. Goal Demonstrate the best way to accomplish this. Task Write a loop which:   starts the index (variable...
#Julia
Julia
using Primes, Formatting   function doublemyindex(n=42) shown = 0 i = BigInt(n) while shown < n if isprime(i + 1) shown += 1 println("The index is ", format(shown, commas=true), " and ", format(i + 1, commas=true), " is prime.") ...
http://rosettacode.org/wiki/Loops/Infinite
Loops/Infinite
Task Print out       SPAM       followed by a   newline   in an infinite loop. 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 l...
#Bracmat
Bracmat
whl'out$SPAM
http://rosettacode.org/wiki/Loops/Infinite
Loops/Infinite
Task Print out       SPAM       followed by a   newline   in an infinite loop. 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 l...
#Brainf.2A.2A.2A
Brainf***
++++++++++[->++++++>++++++++>+<<<]>+++++> [+++.---.<.>---.+++>.<]
http://rosettacode.org/wiki/Loops/With_multiple_ranges
Loops/With multiple ranges
Loops/With multiple ranges You are encouraged to solve this task according to the task description, using any language you may know. Some languages allow multiple loop ranges, such as the PL/I example (snippet) below. /* all variables are DECLARED as integers. */ prod=...
#PureBasic
PureBasic
#X = 5 : #Y = -5 : #Z = -2 #ONE = 1 : #THREE = 3 : #SEVEN = 7 Define j.i Global prod.i = 1, sum.i = 0   Macro ipow(n, e) Int(Pow(n, e)) EndMacro   Macro ifn(x) FormatNumber(x,0,".",",") EndMacro   Macro loop_for(start, stop, step_for=1) For j = start To stop Step step_for proc(j) Next EndMacro   Procedure...
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...
#Chapel
Chapel
var val = 1024; while val > 0 { writeln(val); val /= 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...
#ChucK
ChucK
  1024 => int value;   while(value > 0) { <<<value>>>; value / 2 => value; }  
http://rosettacode.org/wiki/Loops/Downward_for
Loops/Downward for
Task Write a   for   loop which writes a countdown from   10   to   0. 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 body...
#AWK
AWK
BEGIN { for(i=10; i>=0; i--) { print i } }
http://rosettacode.org/wiki/Loops/Downward_for
Loops/Downward for
Task Write a   for   loop which writes a countdown from   10   to   0. 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 body...
#Axe
Axe
For(I,0,10) Disp 10-I▶Dec,i End
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...
#ALGOL_W
ALGOL W
begin integer i; i := 0; while begin i := i + 1; write( i ); ( i rem 6 ) not = 0 end do begin end end.
http://rosettacode.org/wiki/Loops/For
Loops/For
“For”   loops are used to make some block of code be iterated a number of times, setting a variable or parameter to a monotonically increasing integer value for each execution of the block of code. Common extensions of this allow other counting patterns or iterating over abstract structures other than the integers. ...
#Alore
Alore
for i in 0 to 6 for j in 0 to i Write('*') end WriteLn() end  
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...
#Avail
Avail
For each i from 0 to 100 by 7 do [Print: “i” ++ " is a multiple of 7!\n";];
http://rosettacode.org/wiki/Ludic_numbers
Ludic numbers
Ludic numbers   are related to prime numbers as they are generated by a sieve quite like the Sieve of Eratosthenes is used to generate prime numbers. The first ludic number is   1. To generate succeeding ludic numbers create an array of increasing integers starting from   2. 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1...
#Python
Python
def ludic(nmax=100000): yield 1 lst = list(range(2, nmax + 1)) while lst: yield lst[0] del lst[::lst[0]]   ludics = [l for l in ludic()]   print('First 25 ludic primes:') print(ludics[:25]) print("\nThere are %i ludic numbers <= 1000"  % sum(1 for l in ludics if l <= 1000)) print("\n20...
http://rosettacode.org/wiki/Loops/N_plus_one_half
Loops/N plus one half
Quite often one needs loops which, in the last iteration, execute only part of the loop body. Goal Demonstrate the best way to do this. Task Write a loop which writes the comma-separated list 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 using separate output statements for the number and the comma from within the body of th...
#D
D
import std.stdio;   void main() { for (int i = 1; ; i++) { write(i); if (i >= 10) break; write(", "); }   writeln(); }
http://rosettacode.org/wiki/Loops/N_plus_one_half
Loops/N plus one half
Quite often one needs loops which, in the last iteration, execute only part of the loop body. Goal Demonstrate the best way to do this. Task Write a loop which writes the comma-separated list 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 using separate output statements for the number and the comma from within the body of th...
#Dart
Dart
String loopPlusHalf(start, end) { var result = ''; for(int i = start; i <= end; i++) { result += '$i'; if(i == end) { break; } result += ', '; } return result; }   void main() { print(loopPlusHalf(1, 10)); }
http://rosettacode.org/wiki/Loops/Nested
Loops/Nested
Show a nested loop which searches a two-dimensional array filled with random numbers uniformly distributed over [ 1 , … , 20 ] {\displaystyle [1,\ldots ,20]} . The loops iterate rows and columns of the array printing the elements until the value 20 {\displaystyle 20} is met. Specifically, this task als...
#ColdFusion
ColdFusion
  <Cfset RandNum = 0> <Cfloop condition="randNum neq 20"> <Cfloop from="1" to="5" index="i"> <Cfset randNum = RandRange(1, 20)> <Cfoutput>#randNum# </Cfoutput> <Cfif RandNum eq 20><cfbreak></Cfif> </Cfloop> <br> </Cfloop>  
http://rosettacode.org/wiki/Loops/Foreach
Loops/Foreach
Loop through and print each element in a collection in order. Use your language's "for each" loop if it has one, otherwise iterate through the collection in order with some other loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for...
#E
E
for e in theCollection { println(e) }
http://rosettacode.org/wiki/Loops/Foreach
Loops/Foreach
Loop through and print each element in a collection in order. Use your language's "for each" loop if it has one, otherwise iterate through the collection in order with some other loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for...
#EchoLisp
EchoLisp
  (define my-list '( albert simon antoinette)) (for ((h my-list)) (write h)) albert simon antoinette   (define my-vector #(55 66 soixante-dix-sept)) (for (( u my-vector)) (write u)) 55 66 soixante-dix-sept   (define my-string "Longtemps") (for ((une-lettre my-string)) (write une-lettre)) "L" "o" "n" "g" "t"...
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...
#C
C
#include <string.h> #include <stdio.h>   int luhn(const char* cc) { const int m[] = {0,2,4,6,8,1,3,5,7,9}; // mapping for rule 3 int i, odd = 1, sum = 0;   for (i = strlen(cc); i--; odd = !odd) { int digit = cc[i] - '0'; sum += odd ? digit : m[digit]; }   return sum % 10 == 0; }   int main() { const char* cc[...
http://rosettacode.org/wiki/Lucas-Lehmer_test
Lucas-Lehmer test
Lucas-Lehmer Test: for p {\displaystyle p} an odd prime, the Mersenne number 2 p − 1 {\displaystyle 2^{p}-1} is prime if and only if 2 p − 1 {\displaystyle 2^{p}-1} divides S ( p − 1 ) {\displaystyle S(p-1)} where S ( n + 1 ) = ( S ( n ) ) 2 − 2 {\displaystyle S(n+1)=(...
#FunL
FunL
def mersenne( p ) = if p == 2 then return true   var s = 4 var M = 2^p - 1   repeat p - 2 s = (s*s - 2) mod M   s == 0   import integers.primes   for p <- primes().filter( mersenne ).take( 20 ) println( 'M' + p )
http://rosettacode.org/wiki/LZW_compression
LZW compression
The Lempel-Ziv-Welch (LZW) algorithm provides loss-less data compression. You can read a complete description of it in the   Wikipedia article   on the subject.   It was patented, but it entered the public domain in 2004.
#Mathematica.2FWolfram_Language
Mathematica/Wolfram Language
compress[uncompressed_] := Module[{dictsize, dictionary, w, result, wc}, dictsize = 256; dictionary = # -> # & /@ FromCharacterCode /@ Range@dictsize; w = ""; result = {}; Do[wc = w <> c; If[MemberQ[dictionary[[All, 1]], wc], w = wc, AppendTo[result, w /. dictionary]; AppendTo[dictio...
http://rosettacode.org/wiki/LU_decomposition
LU decomposition
Every square matrix A {\displaystyle A} can be decomposed into a product of a lower triangular matrix L {\displaystyle L} and a upper triangular matrix U {\displaystyle U} , as described in LU decomposition. A = L U {\displaystyle A=LU} It is a modified form of Gaussian elimination. Whil...
#Racket
Racket
  #lang racket (require math) (define A (matrix [[1 3 5] [2 4 7] [1 1 0]]))   (matrix-lu A) ; result: ; (mutable-array #[#[1 0 0] ; #[2 1 0] ; #[1 1 1]]) ; (mutable-array #[#[1 3 5] ; #[0 -2 -3] ; #[0 ...
http://rosettacode.org/wiki/Lychrel_numbers
Lychrel numbers
  Take an integer n, greater than zero.   Form the next n of its series by reversing the digits of the current n and adding the result to the current n.   Stop when n becomes palindromic - i.e. the digits of n in reverse order == n. The above recurrence relation when applied to most starting numbers n = 1, 2, ... ...
#Wren
Wren
import "/big" for BigInt import "/set" for Set   var iterations = 500 var limit = 10000 var bigLimit = BigInt.new(limit)   // In the sieve, 0 = not Lychrel, 1 = Seed Lychrel, 2 = Related Lychrel var lychrelSieve = List.filled(limit + 1, 0) var seedLychrels = [] var relatedLychrels = Set.new()   var isPalindrome ...
http://rosettacode.org/wiki/Mad_Libs
Mad Libs
This page uses content from Wikipedia. The original article was at Mad Libs. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) Mad Libs is a phrasal template word game where one player prompts another...
#Racket
Racket
(define (get-mad-libs file) (with-input-from-file file (lambda () (for/fold ((text "")) ((line (in-lines))) (string-append text line "\n")))))     (define (replace-context mad-libs) (define matches (regexp-match* #rx"<[a-zA-Z0-9 ]*>" mad-libs)) (map (lambda (context) (displa...
http://rosettacode.org/wiki/Loops/Increment_loop_index_within_loop_body
Loops/Increment loop index within loop body
Sometimes, one may need   (or want)   a loop which its   iterator   (the index variable)   is modified within the loop body   in addition to the normal incrementation by the   (do)   loop structure index. Goal Demonstrate the best way to accomplish this. Task Write a loop which:   starts the index (variable...
#Kotlin
Kotlin
// version 1.2.60   fun isPrime(n: Long): Boolean { if (n % 2L == 0L) return n == 2L if (n % 3L == 0L) return n == 3L var d = 5L while (d * d <= n) { if (n % d == 0L) return false d += 2L if (n % d == 0L) return false d += 4L } return true }   fun main(args: Array...
http://rosettacode.org/wiki/Loops/Infinite
Loops/Infinite
Task Print out       SPAM       followed by a   newline   in an infinite loop. 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 l...
#Brat
Brat
loop { p "SPAM" }
http://rosettacode.org/wiki/Loops/Infinite
Loops/Infinite
Task Print out       SPAM       followed by a   newline   in an infinite loop. 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 l...
#C
C
while(1) puts("SPAM");
http://rosettacode.org/wiki/Loops/With_multiple_ranges
Loops/With multiple ranges
Loops/With multiple ranges You are encouraged to solve this task according to the task description, using any language you may know. Some languages allow multiple loop ranges, such as the PL/I example (snippet) below. /* all variables are DECLARED as integers. */ prod=...
#Python
Python
from itertools import chain   prod, sum_, x, y, z, one,three,seven = 1, 0, 5, -5, -2, 1, 3, 7   def _range(x, y, z=1): return range(x, y + (1 if z > 0 else -1), z)   print(f'list(_range(x, y, z)) = {list(_range(x, y, z))}') print(f'list(_range(-seven, seven, x)) = {list(_range(-seven, seven, x))}')   for j in chain...
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...
#Clojure
Clojure
(def i (ref 1024))   (while (> @i 0) (println @i) (dosync (ref-set i (quot @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...
#CLU
CLU
start_up = proc () po: stream := stream$primary_output() n: int := 1024 while n>0 do stream$putl(po, int$unparse(n)) n := n/2 end end start_up
http://rosettacode.org/wiki/Loops/Downward_for
Loops/Downward for
Task Write a   for   loop which writes a countdown from   10   to   0. 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 body...
#BASIC
BASIC
FOR I = 10 TO 0 STEP -1 : PRINT I : NEXT I
http://rosettacode.org/wiki/Loops/Downward_for
Loops/Downward for
Task Write a   for   loop which writes a countdown from   10   to   0. 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 body...
#Batch_File
Batch File
@echo off for /l %%D in (10,-1,0) do echo %%D
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...
#AmigaE
AmigaE
PROC main() DEF i = 0 REPEAT i := i + 1 WriteF('\d\n', i) UNTIL Mod(i, 6) = 0 ENDPROC
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. ...
#AmigaE
AmigaE
PROC main() DEF i, j FOR i := 1 TO 5 FOR j := 1 TO i DO WriteF('*') WriteF('\n') ENDFOR ENDPROC
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...
#AWK
AWK
BEGIN { for (i= 2; i <= 8; i = i + 2) { print i } print "Ain't never too late!" }
http://rosettacode.org/wiki/Ludic_numbers
Ludic numbers
Ludic numbers   are related to prime numbers as they are generated by a sieve quite like the Sieve of Eratosthenes is used to generate prime numbers. The first ludic number is   1. To generate succeeding ludic numbers create an array of increasing integers starting from   2. 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1...
#Racket
Racket
#lang racket (define lucid-sieve-size 25000) ; this should be enough to do me! (define lucid? (let ((lucid-bytes-sieve (delay (define sieve-bytes (make-bytes lucid-sieve-size 1)) (bytes-set! sieve-bytes 0 0) ; not a lucid number (define (sieve-pass L) (let...
http://rosettacode.org/wiki/Loops/N_plus_one_half
Loops/N plus one half
Quite often one needs loops which, in the last iteration, execute only part of the loop body. Goal Demonstrate the best way to do this. Task Write a loop which writes the comma-separated list 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 using separate output statements for the number and the comma from within the body of th...
#Delphi
Delphi
program LoopsNPlusOneHalf;   {$APPTYPE CONSOLE}   var i: integer; const MAXVAL = 10; begin for i := 1 to MAXVAL do begin Write(i); if i < MAXVAL then Write(', '); end; Writeln; end.
http://rosettacode.org/wiki/Loops/N_plus_one_half
Loops/N plus one half
Quite often one needs loops which, in the last iteration, execute only part of the loop body. Goal Demonstrate the best way to do this. Task Write a loop which writes the comma-separated list 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 using separate output statements for the number and the comma from within the body of th...
#Draco
Draco
proc nonrec main() void: byte i; i := 1; while write(i); i := i + 1; i <= 10 do write(", ") od corp
http://rosettacode.org/wiki/Loops/Nested
Loops/Nested
Show a nested loop which searches a two-dimensional array filled with random numbers uniformly distributed over [ 1 , … , 20 ] {\displaystyle [1,\ldots ,20]} . The loops iterate rows and columns of the array printing the elements until the value 20 {\displaystyle 20} is met. Specifically, this task als...
#Common_Lisp
Common Lisp
(let ((a (make-array '(10 10)))) (dotimes (i 10) (dotimes (j 10) (setf (aref a i j) (1+ (random 20)))))   (block outer (dotimes (i 10) (dotimes (j 10) (princ " ") (princ (aref a i j)) (if (= 20 (aref a i j)) (return-from outer))) (terpri)) (terpri)))
http://rosettacode.org/wiki/Loops/Foreach
Loops/Foreach
Loop through and print each element in a collection in order. Use your language's "for each" loop if it has one, otherwise iterate through the collection in order with some other loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for...
#Efene
Efene
io.format("~p~n", [Collection])
http://rosettacode.org/wiki/Loops/Foreach
Loops/Foreach
Loop through and print each element in a collection in order. Use your language's "for each" loop if it has one, otherwise iterate through the collection in order with some other loop. Related tasks   Loop over multiple arrays simultaneously   Loops/Break   Loops/Continue   Loops/Do-while   Loops/Downward for...
#Eiffel
Eiffel
across my_list as ic loop print (ic.item) end
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...
#C.23
C#
  public static class Luhn { public static bool LuhnCheck(this string cardNumber) { return LuhnCheck(cardNumber.Select(c => c - '0').ToArray()); }   private static bool LuhnCheck(this int[] digits) { return GetCheckValue(digits) == 0; }   ...
http://rosettacode.org/wiki/Lucas-Lehmer_test
Lucas-Lehmer test
Lucas-Lehmer Test: for p {\displaystyle p} an odd prime, the Mersenne number 2 p − 1 {\displaystyle 2^{p}-1} is prime if and only if 2 p − 1 {\displaystyle 2^{p}-1} divides S ( p − 1 ) {\displaystyle S(p-1)} where S ( n + 1 ) = ( S ( n ) ) 2 − 2 {\displaystyle S(n+1)=(...
#GAP
GAP
LucasLehmer := function(n) local i, m, s; if n = 2 then return true; elif not IsPrime(n) then return false; else m := 2^n - 1; s := 4; for i in [3 .. n] do s := RemInt(s*s, m) - 2; od; return s = 0; fi; end;   Filtered([1 .. 2000], ...
http://rosettacode.org/wiki/LZW_compression
LZW compression
The Lempel-Ziv-Welch (LZW) algorithm provides loss-less data compression. You can read a complete description of it in the   Wikipedia article   on the subject.   It was patented, but it entered the public domain in 2004.
#Nim
Nim
import tables   proc compress*(uncompressed: string): seq[int] =   # Build the dictionary. var dictionary: Table[string, int] for i in 0..255: dictionary[$chr(i)] = i   var w = "" for c in uncompressed: var wc = w & c if wc in dictionary: w = wc else: # Writes "w" to output. resu...
http://rosettacode.org/wiki/LU_decomposition
LU decomposition
Every square matrix A {\displaystyle A} can be decomposed into a product of a lower triangular matrix L {\displaystyle L} and a upper triangular matrix U {\displaystyle U} , as described in LU decomposition. A = L U {\displaystyle A=LU} It is a modified form of Gaussian elimination. Whil...
#Raku
Raku
for ( [1, 3, 5], # Test Matrices [2, 4, 7], [1, 1, 0] ), ( [11, 9, 24, 2], [ 1, 5, 2, 6], [ 3, 17, 18, 1], [ 2, 5, 7, 1] ) -> @test { say-it 'A Matrix', @test; say-it( $_[0], @($_[1]) ) for 'P Matrix', 'Aʼ Matrix', 'L Matrix', 'U Matrix' Z, lu @test;...