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/Hello_world/Graphical
Hello world/Graphical
Task Display the string       Goodbye, World!       on a GUI object   (alert box, plain window, text area, etc.). Related task   Hello world/Text
#ScratchScript
ScratchScript
pos -100 70 print "Goodbye, World!"
http://rosettacode.org/wiki/Hello_world/Graphical
Hello world/Graphical
Task Display the string       Goodbye, World!       on a GUI object   (alert box, plain window, text area, etc.). Related task   Hello world/Text
#Seed7
Seed7
$ include "seed7_05.s7i"; include "draw.s7i"; include "keybd.s7i"; include "bitmapfont.s7i"; include "stdfont24.s7i"; include "pixmap_file.s7i";   const proc: main is func local var text: screen is STD_NULL; begin screen(400, 100); clear(curr_win, white); KEYBOARD := GRAPH_KEYBOARD; sc...
http://rosettacode.org/wiki/Gray_code
Gray code
Gray code Karnaugh maps Create functions to encode a number to and decode a number from Gray code. Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary). There are many possible Gray codes. The follow...
#Scala
Scala
def encode(n: Int) = (n ^ (n >>> 1)).toBinaryString def decode(s: String) = Integer.parseInt( s.scanLeft(0)(_ ^ _.asDigit).tail.mkString , 2)   println("decimal binary gray decoded") for (i <- 0 to 31; g = encode(i)) println("%7d  %6s  %5s  %7s".format(i, i.toBinaryString, g, decode(g)))  
http://rosettacode.org/wiki/Gray_code
Gray code
Gray code Karnaugh maps Create functions to encode a number to and decode a number from Gray code. Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary). There are many possible Gray codes. The follow...
#Scratch
Scratch
$ include "seed7_05.s7i"; include "bin32.s7i";   const func integer: grayEncode (in integer: n) is return ord(bin32(n) >< bin32(n >> 1));   const func integer: grayDecode (in var integer: n) is func result var integer: decoded is 0; begin decoded := n; while n > 1 do n >>:= 1; decoded :=...
http://rosettacode.org/wiki/Generic_swap
Generic swap
Task Write a generic swap function or operator which exchanges the values of two variables (or, more generally, any two storage places that can be assigned), regardless of their types. If your solution language is statically typed please describe the way your language provides genericity. If variables are typed in t...
#dc
dc
1 2 SaSbLaLb f =2 1
http://rosettacode.org/wiki/Greatest_element_of_a_list
Greatest element of a list
Task Create a function that returns the maximum value in a provided set of values, where the number of values may not be known until run-time.
#E
E
pragma.enable("accumulator") # non-finalized syntax feature   def max([first] + rest) { return accum first for x in rest { _.max(x) } }
http://rosettacode.org/wiki/Greatest_common_divisor
Greatest common divisor
Greatest common divisor You are encouraged to solve this task according to the task description, using any language you may know. Task Find the greatest common divisor   (GCD)   of two integers. Greatest common divisor   is also known as   greatest common factor (gcf)   and   greatest common measure. Related tas...
#D
D
import std.stdio, std.numeric;   long myGCD(in long x, in long y) pure nothrow @nogc { if (y == 0) return x; return myGCD(y, x % y); }   void main() { gcd(15, 10).writeln; // From Phobos. myGCD(15, 10).writeln; }
http://rosettacode.org/wiki/Hailstone_sequence
Hailstone sequence
The Hailstone sequence of numbers can be generated from a starting positive integer,   n   by:   If   n   is     1     then the sequence ends.   If   n   is   even then the next   n   of the sequence   = n/2   If   n   is   odd   then the next   n   of the sequence   = (3 * n) + 1 The (unproven) Collatz conje...
#Eiffel
Eiffel
  class APPLICATION   create make   feature   make local test: LINKED_LIST [INTEGER] count, number, te: INTEGER do create test.make test := hailstone_sequence (27) io.put_string ("There are " + test.count.out + " elements in the sequence for the number 27.") io.put_string ("%NThe first 4 elemen...
http://rosettacode.org/wiki/Hamming_numbers
Hamming numbers
Hamming numbers are numbers of the form   H = 2i × 3j × 5k where i, j, k ≥ 0 Hamming numbers   are also known as   ugly numbers   and also   5-smooth numbers   (numbers whose prime divisors are less or equal to 5). Task Generate the sequence of Hamming numbers, in increasing order.   In ...
#Picat
Picat
go => println([hamming(I) : I in 1..20]), time(println(hamming_1691=hamming(1691))), time(println(hamming_1000000=hamming(1000000))), nl.   hamming(1) = 1. hamming(2) = 2. hamming(3) = 3. hamming(N) = Hamming => A = new_array(N), [Next2, Next3, Next5] = [2,3,5], A[1] := Next2, A[2] := Next3, A[3] :...
http://rosettacode.org/wiki/Guess_the_number
Guess the number
Task Write a program where the program chooses a number between   1   and   10. A player is then prompted to enter a guess.   If the player guesses wrong,   then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will issue a   "Well guessed!"   message,   a...
#Ring
Ring
    ### Bert Mariani ### 2018-03-01 ### Guess_My_Number   myNumber = random(10) answer = 0   See "Guess my number between 1 and 10"+ nl   while answer != myNumber See "Your guess: " Give answer   if answer = myNumber See "Well done! You guessed it! "+ myNumber +nl else See "Try again"+ nl ok   if ...
http://rosettacode.org/wiki/Guess_the_number
Guess the number
Task Write a program where the program chooses a number between   1   and   10. A player is then prompted to enter a guess.   If the player guesses wrong,   then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will issue a   "Well guessed!"   message,   a...
#RPL
RPL
  DIR INITIALIZE << { C G R } PURGE RAND 10 * 1 + IP 'R' STO GUESSING >> GUESSING << "Pick a number between 1 and 10." "" INPUT OBJ-> 'G' STO IF G R == THEN CLLCD "You got it!" 1 DISP 7 FREEZE 0 WAIT CLLCD CONTINUE ELSE IF G R < THEN CLLCD "Try a la...
http://rosettacode.org/wiki/Greatest_subsequential_sum
Greatest subsequential sum
Task Given a sequence of integers, find a continuous subsequence which maximizes the sum of its elements, that is, the elements of no other single subsequence add up to a value larger than this one. An empty subsequence is considered to have the sum of   0;   thus if all elements are negative, the result must be th...
#Tcl
Tcl
package require Tcl 8.5 set a {-1 -2 3 5 6 -2 -1 4 -4 2 -1}   # from the Perl solution proc maxsumseq1 {a} { set len [llength $a] set maxsum 0   for {set start 0} {$start < $len} {incr start} { for {set end $start} {$end < $len} {incr end} { set sum 0 incr sum [expr [join [lr...
http://rosettacode.org/wiki/Guess_the_number/With_feedback
Guess the number/With feedback
Task Write a game (computer program) that follows the following rules: The computer chooses a number between given set limits. The player is asked for repeated guesses until the the target number is guessed correctly At each guess, the computer responds with whether the guess is: higher than the target, equal to...
#MATLAB_.2F_Octave
MATLAB / Octave
function guess_a_number(low, high)   if nargin < 1 || ~isnumeric(low) || length(low) > 1 || isnan(low) low = 1; end; if nargin < 2 || ~isnumeric(high) || length(high) > 1 || isnan(high) high = low+9; elseif low > high [low, high] = deal(high, low); end   n = floor(rand(1)*(high-low+1))+low; gs = input(sprin...
http://rosettacode.org/wiki/Happy_numbers
Happy numbers
From Wikipedia, the free encyclopedia: A happy number is defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals   1   (where it will stay),   or it loops endlessly in a cycle which does not inclu...
#ML
ML
(* A happy number is defined by the following process. Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ...
http://rosettacode.org/wiki/Hello_world/Text
Hello world/Text
Hello world/Text is part of Short Circuit's Console Program Basics selection. Task Display the string Hello world! on a text console. Related tasks   Hello world/Graphical   Hello world/Line Printer   Hello world/Newbie   Hello world/Newline omission   Hello world/Standard error   Hello world/Web server
#Jacquard_Loom
Jacquard Loom
+---------------+ | | | * * | |* * * * | |* * *| |* * *| |* * * | | * * * | | * | +---------------+   +---------------+ | | |* * * | |* * * | | * *| | * *| |* * * | |* * * ...
http://rosettacode.org/wiki/Hello_world/Graphical
Hello world/Graphical
Task Display the string       Goodbye, World!       on a GUI object   (alert box, plain window, text area, etc.). Related task   Hello world/Text
#SenseTalk
SenseTalk
Answer "Good Bye"
http://rosettacode.org/wiki/Hello_world/Graphical
Hello world/Graphical
Task Display the string       Goodbye, World!       on a GUI object   (alert box, plain window, text area, etc.). Related task   Hello world/Text
#Sidef
Sidef
var tk = require('Tk'); var main = %s'MainWindow'.new; main.Button( '-text' => 'Goodbye, World!', '-command' => 'exit', ).pack; tk.MainLoop;
http://rosettacode.org/wiki/Gray_code
Gray code
Gray code Karnaugh maps Create functions to encode a number to and decode a number from Gray code. Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary). There are many possible Gray codes. The follow...
#Seed7
Seed7
$ include "seed7_05.s7i"; include "bin32.s7i";   const func integer: grayEncode (in integer: n) is return ord(bin32(n) >< bin32(n >> 1));   const func integer: grayDecode (in var integer: n) is func result var integer: decoded is 0; begin decoded := n; while n > 1 do n >>:= 1; decoded :=...
http://rosettacode.org/wiki/Gray_code
Gray code
Gray code Karnaugh maps Create functions to encode a number to and decode a number from Gray code. Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary). There are many possible Gray codes. The follow...
#SenseTalk
SenseTalk
  function BinaryToGray param1 set theResult to "" repeat for each character in param1 if the counter is equal to 1 put it after theResult else if it is equal to previousCharacter put "0" after theResult else put "1" after theResult end if end if set previousCharacter to it end repeat re...
http://rosettacode.org/wiki/Generic_swap
Generic swap
Task Write a generic swap function or operator which exchanges the values of two variables (or, more generally, any two storage places that can be assigned), regardless of their types. If your solution language is statically typed please describe the way your language provides genericity. If variables are typed in t...
#DCL
DCL
$ a1 = 123 $ a2 = "hello" $ show symbol a* $ gosub swap $ show symbol a* $ exit $ $ swap: $ t = a1 $ a1 = a2 $ a2 = t $ return
http://rosettacode.org/wiki/Greatest_element_of_a_list
Greatest element of a list
Task Create a function that returns the maximum value in a provided set of values, where the number of values may not be known until run-time.
#EasyLang
EasyLang
a[] = [ 2 9 4 3 8 5 ] for e in a[] max = higher e max . print max
http://rosettacode.org/wiki/Greatest_element_of_a_list
Greatest element of a list
Task Create a function that returns the maximum value in a provided set of values, where the number of values may not be known until run-time.
#EchoLisp
EchoLisp
  ;; a random length list of random values (define L (map random (make-list (random 50) 100))) → L L → (24 60 83 8 24 60 31 97 96 65 9 41 64 24 22 57 73 17 6 28 77 58 18 13 27 22 41 69 85)   ;; find max (apply max L) → 97  
http://rosettacode.org/wiki/Greatest_common_divisor
Greatest common divisor
Greatest common divisor You are encouraged to solve this task according to the task description, using any language you may know. Task Find the greatest common divisor   (GCD)   of two integers. Greatest common divisor   is also known as   greatest common factor (gcf)   and   greatest common measure. Related tas...
#Dc
Dc
[dSa%Lard0<G]dsGx+
http://rosettacode.org/wiki/Hailstone_sequence
Hailstone sequence
The Hailstone sequence of numbers can be generated from a starting positive integer,   n   by:   If   n   is     1     then the sequence ends.   If   n   is   even then the next   n   of the sequence   = n/2   If   n   is   odd   then the next   n   of the sequence   = (3 * n) + 1 The (unproven) Collatz conje...
#Elena
Elena
import system'collections; import extensions;   const int maxNumber = 100000;   Hailstone(int n,Map<int,int> lengths) { if (n == 1) { ^ 1 };   while (true) { if (lengths.containsKey(n)) { ^ lengths[n] } else { if (n.isEven()) ...
http://rosettacode.org/wiki/Hamming_numbers
Hamming numbers
Hamming numbers are numbers of the form   H = 2i × 3j × 5k where i, j, k ≥ 0 Hamming numbers   are also known as   ugly numbers   and also   5-smooth numbers   (numbers whose prime divisors are less or equal to 5). Task Generate the sequence of Hamming numbers, in increasing order.   In ...
#PicoLisp
PicoLisp
(de hamming (N) (let (L (1) H) (do N (for (X L X (cadr X)) # Find smallest result (setq H (car X)) ) (idx 'L H NIL) # Remove it (for I (2 3 5) # Generate next results (idx 'L (* I H) T) ) ) H ) )   (println (make (for N 20 (...
http://rosettacode.org/wiki/Guess_the_number
Guess the number
Task Write a program where the program chooses a number between   1   and   10. A player is then prompted to enter a guess.   If the player guesses wrong,   then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will issue a   "Well guessed!"   message,   a...
#Ruby
Ruby
  n = rand(1..10) puts 'Guess the number: ' puts 'Wrong! Guess again: ' until gets.to_i == n puts 'Well guessed!'  
http://rosettacode.org/wiki/Guess_the_number
Guess the number
Task Write a program where the program chooses a number between   1   and   10. A player is then prompted to enter a guess.   If the player guesses wrong,   then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will issue a   "Well guessed!"   message,   a...
#Run_BASIC
Run BASIC
while 1 choose = int(RND(0) * 9) + 1 while guess <> choose print "Guess a number between 1 and 10: ";: input guess if guess = choose THEN print "You guessed!" else print "Sorry, try again" end if wend wend
http://rosettacode.org/wiki/Greatest_subsequential_sum
Greatest subsequential sum
Task Given a sequence of integers, find a continuous subsequence which maximizes the sum of its elements, that is, the elements of no other single subsequence add up to a value larger than this one. An empty subsequence is considered to have the sum of   0;   thus if all elements are negative, the result must be th...
#Ursala
Ursala
#import std #import int   max_subsequence = zleq$^l&r/&+ *aayK33PfatPRTaq ^/~& sum:-0   #cast %zL   example = max_subsequence <-1,-2,3,5,6,-2,-1,4,-4,2,-1>
http://rosettacode.org/wiki/Greatest_subsequential_sum
Greatest subsequential sum
Task Given a sequence of integers, find a continuous subsequence which maximizes the sum of its elements, that is, the elements of no other single subsequence add up to a value larger than this one. An empty subsequence is considered to have the sum of   0;   thus if all elements are negative, the result must be th...
#XPL0
XPL0
include c:\cxpl\codes; int Array, Size, Sum, Best, I, Lo, Hi, BLo, BHi;   [Array:= [-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1]; Size:= 11; Best:= -100000; for Lo:= 0 to Size-1 do for Hi:= Lo to Size-1 do [Sum:= 0; for I:= Lo to Hi do Sum:= Sum + Array(I); if Sum > Best then ...
http://rosettacode.org/wiki/Guess_the_number/With_feedback
Guess the number/With feedback
Task Write a game (computer program) that follows the following rules: The computer chooses a number between given set limits. The player is asked for repeated guesses until the the target number is guessed correctly At each guess, the computer responds with whether the guess is: higher than the target, equal to...
#MAXScript
MAXScript
  Range = [1,100] randomNumber = (random Range.x Range.y) as integer clearListener() while true do ( userVal = getKBValue prompt:("Enter a number between "+(range[1] as integer) as string+" and "+(range[2] as integer) as string+": ") if userVal == randomNumber do (format "\nWell guessed!\n"; exit with OK) case of (...
http://rosettacode.org/wiki/Guess_the_number/With_feedback
Guess the number/With feedback
Task Write a game (computer program) that follows the following rules: The computer chooses a number between given set limits. The player is asked for repeated guesses until the the target number is guessed correctly At each guess, the computer responds with whether the guess is: higher than the target, equal to...
#Mirah
Mirah
def getInput:int s = System.console.readLine() Integer.parseInt(s) end   number = int(Math.random() * 10 + 1)   puts "Guess the number between 1 and 10"   guessed = false   while !guessed do begin userNumber = getInput if userNumber == number guessed = true puts "You guessed it." elsif use...
http://rosettacode.org/wiki/Happy_numbers
Happy numbers
From Wikipedia, the free encyclopedia: A happy number is defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals   1   (where it will stay),   or it loops endlessly in a cycle which does not inclu...
#Modula-2
Modula-2
MODULE HappyNumbers; FROM InOut IMPORT WriteCard, WriteLn;   CONST Amount = 8; VAR seen, num: CARDINAL;   PROCEDURE SumDigitSquares(n: CARDINAL): CARDINAL; VAR sum, digit: CARDINAL; BEGIN sum := 0; WHILE n>0 DO digit := n MOD 10; n := n DIV 10; sum := sum + digit * digit; END; RE...
http://rosettacode.org/wiki/Hello_world/Text
Hello world/Text
Hello world/Text is part of Short Circuit's Console Program Basics selection. Task Display the string Hello world! on a text console. Related tasks   Hello world/Graphical   Hello world/Line Printer   Hello world/Newbie   Hello world/Newline omission   Hello world/Standard error   Hello world/Web server
#Java
Java
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world!"); } }
http://rosettacode.org/wiki/Hello_world/Graphical
Hello world/Graphical
Task Display the string       Goodbye, World!       on a GUI object   (alert box, plain window, text area, etc.). Related task   Hello world/Text
#Smalltalk
Smalltalk
MessageBox show: 'Goodbye, world.'
http://rosettacode.org/wiki/Hello_world/Graphical
Hello world/Graphical
Task Display the string       Goodbye, World!       on a GUI object   (alert box, plain window, text area, etc.). Related task   Hello world/Text
#SmileBASIC
SmileBASIC
DIALOG "Goodbye, world."
http://rosettacode.org/wiki/Gray_code
Gray code
Gray code Karnaugh maps Create functions to encode a number to and decode a number from Gray code. Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary). There are many possible Gray codes. The follow...
#Sidef
Sidef
func bin2gray(n) { n ^ (n >> 1) }   func gray2bin(num) { var bin = num while (num >>= 1) { bin ^= num } return bin }   { |i| var gr = bin2gray(i) printf("%d\t%b\t%b\t%b\n", i, i, gr, gray2bin(gr)) } << ^32
http://rosettacode.org/wiki/Gray_code
Gray code
Gray code Karnaugh maps Create functions to encode a number to and decode a number from Gray code. Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary). There are many possible Gray codes. The follow...
#SQL
SQL
  DECLARE @BINARY AS NVARCHAR(MAX) = '001010111' DECLARE @gray AS NVARCHAR(MAX) = ''   --Encoder SET @gray = LEFT(@BINARY, 1)   WHILE LEN(@BINARY) > 1 BEGIN IF LEFT(@BINARY, 1) != SUBSTRING(@BINARY, 2, 1) SET @gray = @gray + '1' ELSE SET @gray = @gray + '0'   SET @BINARY = ...
http://rosettacode.org/wiki/Generic_swap
Generic swap
Task Write a generic swap function or operator which exchanges the values of two variables (or, more generally, any two storage places that can be assigned), regardless of their types. If your solution language is statically typed please describe the way your language provides genericity. If variables are typed in t...
#Delphi
Delphi
  procedure Swap_T(var a, b: T); var temp: T; begin temp := a; a := b; b := temp; end;  
http://rosettacode.org/wiki/Greatest_element_of_a_list
Greatest element of a list
Task Create a function that returns the maximum value in a provided set of values, where the number of values may not be known until run-time.
#ECL
ECL
  MaxVal(SET OF INTEGER s) := MAX(s);   //example usage   SetVals := [4,8,16,2,1]; MaxVal(SetVals) //returns 16;  
http://rosettacode.org/wiki/Greatest_element_of_a_list
Greatest element of a list
Task Create a function that returns the maximum value in a provided set of values, where the number of values may not be known until run-time.
#Efene
Efene
list_max = fn ([Head:Rest]) { list_max(Rest, Head) }   list_max = fn ([], Res) { Res } fn ([Head:Rest], Max) when Head > Max { list_max(Rest, Head) } fn ([_Head:Rest], Max) { list_max(Rest, Max) }   list_max1 = fn ([H:T]) { lists.foldl(fn erlang.max:2, H, T) }   @public run = fn () { io.format("~p~n", [...
http://rosettacode.org/wiki/Greatest_common_divisor
Greatest common divisor
Greatest common divisor You are encouraged to solve this task according to the task description, using any language you may know. Task Find the greatest common divisor   (GCD)   of two integers. Greatest common divisor   is also known as   greatest common factor (gcf)   and   greatest common measure. Related tas...
#Delphi
Delphi
proc nonrec gcd(word m, n) word: word t; while n ~= 0 do t := m; m := n; n := t % n od; m corp   proc nonrec show(word m, n) void: writeln("gcd(", m, ", ", n, ") = ", gcd(m, n)) corp   proc nonrec main() void: show(18, 12); show(1071, 1029); show(3528, 3780) corp
http://rosettacode.org/wiki/Hailstone_sequence
Hailstone sequence
The Hailstone sequence of numbers can be generated from a starting positive integer,   n   by:   If   n   is     1     then the sequence ends.   If   n   is   even then the next   n   of the sequence   = n/2   If   n   is   odd   then the next   n   of the sequence   = (3 * n) + 1 The (unproven) Collatz conje...
#Elixir
Elixir
defmodule Hailstone do require Integer   def step(1) , do: 0 def step(n) when Integer.is_even(n), do: div(n,2) def step(n) , do: n*3 + 1   def sequence(n) do Stream.iterate(n, &step/1) |> Stream.take_while(&(&1 > 0)) |> Enum.to_list end   def run do se...
http://rosettacode.org/wiki/Hamming_numbers
Hamming numbers
Hamming numbers are numbers of the form   H = 2i × 3j × 5k where i, j, k ≥ 0 Hamming numbers   are also known as   ugly numbers   and also   5-smooth numbers   (numbers whose prime divisors are less or equal to 5). Task Generate the sequence of Hamming numbers, in increasing order.   In ...
#PL.2FI
PL/I
(subscriptrange): Hamming: procedure options (main); /* 14 November 2013 with fixes 2021 */ declare (H(2000), p2, p3, p5, twoTo31, Hm, tenP(11)) decimal(12)fixed; declare (i, j, k, m, d, w) fixed binary;   /* Quicksorts in-place the array of integers H, from lb to ub */ quicksortH: procedure( lb, ub ) recur...
http://rosettacode.org/wiki/Guess_the_number
Guess the number
Task Write a program where the program chooses a number between   1   and   10. A player is then prompted to enter a guess.   If the player guesses wrong,   then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will issue a   "Well guessed!"   message,   a...
#Rust
Rust
extern crate rand;   fn main() { println!("Type in an integer between 1 and 10 and press enter.");   let n = rand::random::<u32>() % 10 + 1; loop { let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); let option: Result<u32,_> = line.trim().parse(); m...
http://rosettacode.org/wiki/Guess_the_number
Guess the number
Task Write a program where the program chooses a number between   1   and   10. A player is then prompted to enter a guess.   If the player guesses wrong,   then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will issue a   "Well guessed!"   message,   a...
#Scala
Scala
  val n = (math.random * 10 + 1).toInt print("Guess the number: ") while(readInt != n) print("Wrong! Guess again: ") println("Well guessed!")  
http://rosettacode.org/wiki/Greatest_subsequential_sum
Greatest subsequential sum
Task Given a sequence of integers, find a continuous subsequence which maximizes the sum of its elements, that is, the elements of no other single subsequence add up to a value larger than this one. An empty subsequence is considered to have the sum of   0;   thus if all elements are negative, the result must be th...
#Wren
Wren
var gss = Fn.new { |s| var best = 0 var start = 0 var end = 0 var sum = 0 var sumStart = 0 var i = 0 for (x in s) { sum = sum + x if (sum > best) { best = sum start = sumStart end = i + 1 } else if (sum < 0) { sum = 0 ...
http://rosettacode.org/wiki/Guess_the_number/With_feedback
Guess the number/With feedback
Task Write a game (computer program) that follows the following rules: The computer chooses a number between given set limits. The player is asked for repeated guesses until the the target number is guessed correctly At each guess, the computer responds with whether the guess is: higher than the target, equal to...
#Modula-2
Modula-2
MODULE guessf;   IMPORT InOut, Random, NumConv, Strings;   VAR number, guess : CARDINAL; input : Strings.String; OK, Done : BOOLEAN;   BEGIN number := Random.nr (1000); InOut.WriteString ("I have chosen a number below 1000; please try to guess it."); ...
http://rosettacode.org/wiki/Happy_numbers
Happy numbers
From Wikipedia, the free encyclopedia: A happy number is defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals   1   (where it will stay),   or it loops endlessly in a cycle which does not inclu...
#MUMPS
MUMPS
ISHAPPY(N)  ;Determines if a number N is a happy number  ;Note that the returned strings do not have a leading digit unless it is a happy number IF (N'=N\1)!(N<0) QUIT "Not a positive integer" NEW SUM,I  ;SUM is the sum of the square of each digit  ;I is a loop variable  ;SEQ is the sequence of previously checked SUM...
http://rosettacode.org/wiki/Hello_world/Text
Hello world/Text
Hello world/Text is part of Short Circuit's Console Program Basics selection. Task Display the string Hello world! on a text console. Related tasks   Hello world/Graphical   Hello world/Line Printer   Hello world/Newbie   Hello world/Newline omission   Hello world/Standard error   Hello world/Web server
#JavaScript
JavaScript
document.write("Hello world!");
http://rosettacode.org/wiki/Hello_world/Graphical
Hello world/Graphical
Task Display the string       Goodbye, World!       on a GUI object   (alert box, plain window, text area, etc.). Related task   Hello world/Text
#SSEM
SSEM
01100000000001110000000000000000 10000000000001010000000000000000 10011101110111011101010111000000 10010101010101010101010101000000 10010101010101010101010111000000 10011101110111011100110100000010 10000000000000000000010011000010 10011000000000000000100000000100 01101000000000000001000000000000 00000000000000000000000...
http://rosettacode.org/wiki/Hello_world/Graphical
Hello world/Graphical
Task Display the string       Goodbye, World!       on a GUI object   (alert box, plain window, text area, etc.). Related task   Hello world/Text
#Standard_ML
Standard ML
open XWindows ; open Motif ;   val helloWindow = fn () =>   let val shell = XtAppInitialise "" "demo" "top" [] [XmNwidth 400, XmNheight 300 ] ; val main = XmCreateMainWindow shell "main" [XmNmappedWhenManaged true ] ; val text = XmCreateLabel main "show" [ XmNlabelString "Hello World!"] in   ( XtMan...
http://rosettacode.org/wiki/Gray_code
Gray code
Gray code Karnaugh maps Create functions to encode a number to and decode a number from Gray code. Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary). There are many possible Gray codes. The follow...
#Standard_ML
Standard ML
fun gray_encode b = Word.xorb (b, Word.>> (b, 0w1))   fun gray_decode n = let fun aux (p, n) = if n = 0w0 then p else aux (Word.xorb (p, n), Word.>> (n, 0w1)) in aux (n, Word.>> (n, 0w1)) end;   val s = Word.fmt StringCvt.BIN; fun aux i = if i = 0w32 then () else let val g ...
http://rosettacode.org/wiki/Gray_code
Gray code
Gray code Karnaugh maps Create functions to encode a number to and decode a number from Gray code. Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary). There are many possible Gray codes. The follow...
#Swift
Swift
func grayEncode(_ i: Int) -> Int { return (i >> 1) ^ i }   func grayDecode(_ i: Int) -> Int { switch i { case 0: return 0 case _: return i ^ grayDecode(i >> 1) } }   for i in 0..<32 { let iStr = String(i, radix: 2) let encode = grayEncode(i) let encodeStr = String(encode, radix: 2) let decode ...
http://rosettacode.org/wiki/Generic_swap
Generic swap
Task Write a generic swap function or operator which exchanges the values of two variables (or, more generally, any two storage places that can be assigned), regardless of their types. If your solution language is statically typed please describe the way your language provides genericity. If variables are typed in t...
#D.C3.A9j.C3.A0_Vu
Déjà Vu
swap
http://rosettacode.org/wiki/Greatest_element_of_a_list
Greatest element of a list
Task Create a function that returns the maximum value in a provided set of values, where the number of values may not be known until run-time.
#Eiffel
Eiffel
  class GREATEST_ELEMENT [G -> COMPARABLE]   create make   feature {NONE} --Implementation   is_max (element: G maximum: G): BOOLEAN do Result := maximum >= element end   max (list: ARRAY [G]): G require not_empty: not list.is_empty do Result := list [list.lower] across list as i loop ...
http://rosettacode.org/wiki/Greatest_common_divisor
Greatest common divisor
Greatest common divisor You are encouraged to solve this task according to the task description, using any language you may know. Task Find the greatest common divisor   (GCD)   of two integers. Greatest common divisor   is also known as   greatest common factor (gcf)   and   greatest common measure. Related tas...
#Draco
Draco
proc nonrec gcd(word m, n) word: word t; while n ~= 0 do t := m; m := n; n := t % n od; m corp   proc nonrec show(word m, n) void: writeln("gcd(", m, ", ", n, ") = ", gcd(m, n)) corp   proc nonrec main() void: show(18, 12); show(1071, 1029); show(3528, 3780) corp
http://rosettacode.org/wiki/Hailstone_sequence
Hailstone sequence
The Hailstone sequence of numbers can be generated from a starting positive integer,   n   by:   If   n   is     1     then the sequence ends.   If   n   is   even then the next   n   of the sequence   = n/2   If   n   is   odd   then the next   n   of the sequence   = (3 * n) + 1 The (unproven) Collatz conje...
#Erlang
Erlang
-module(hailstone). -import(io). -export([main/0]).   hailstone(1) -> [1]; hailstone(N) when N band 1 == 1 -> [N|hailstone(N * 3 + 1)]; hailstone(N) when N band 1 == 0 -> [N|hailstone(N div 2)].   max_length(Start, Stop) -> F = fun (N) -> {length(hailstone(N)), N} end, Lengths = lists:map(F, lists:seq(Start, St...
http://rosettacode.org/wiki/Hamming_numbers
Hamming numbers
Hamming numbers are numbers of the form   H = 2i × 3j × 5k where i, j, k ≥ 0 Hamming numbers   are also known as   ugly numbers   and also   5-smooth numbers   (numbers whose prime divisors are less or equal to 5). Task Generate the sequence of Hamming numbers, in increasing order.   In ...
#Prolog
Prolog
%% collect N elements produced by a generator in a row   take( 0, Next, Z-Z, Next). take( N, Next, [A|B]-Z, NZ):- N>0, !, next(Next,A,Next1), N1 is N-1, take(N1,Next1,B-Z,NZ).   %% a generator provides specific {next} implementation   next( hamm( A2,B,C3,D,E5,F,[H|G] ), H, hamm(X,U,Y,V,Z,W,G) ):- H is min(A2, mi...
http://rosettacode.org/wiki/Guess_the_number
Guess the number
Task Write a program where the program chooses a number between   1   and   10. A player is then prompted to enter a guess.   If the player guesses wrong,   then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will issue a   "Well guessed!"   message,   a...
#Scheme
Scheme
(define (guess) (define number (random 11)) (display "Pick a number from 1 through 10.\n> ") (do ((guess (read) (read))) ((= guess number) (display "Well guessed!\n")) (display "Guess again.\n")))
http://rosettacode.org/wiki/Guess_the_number
Guess the number
Task Write a program where the program chooses a number between   1   and   10. A player is then prompted to enter a guess.   If the player guesses wrong,   then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will issue a   "Well guessed!"   message,   a...
#Seed7
Seed7
$ include "seed7_05.s7i";   const proc: main is func local var integer: number is 0; var integer: guess is 0; begin number := rand(1, 10); writeln("I'm thinking of a number between 1 and 10."); writeln("Try to guess it!"); readln(guess); while guess <> number do writeln("That's not...
http://rosettacode.org/wiki/Greatest_subsequential_sum
Greatest subsequential sum
Task Given a sequence of integers, find a continuous subsequence which maximizes the sum of its elements, that is, the elements of no other single subsequence add up to a value larger than this one. An empty subsequence is considered to have the sum of   0;   thus if all elements are negative, the result must be th...
#zkl
zkl
fcn maxsubseq(s){ s.reduce(fcn([(sum, seq, maxsum, maxseq)], x){ sum=sum+x; seq=T(x).extend(seq); if(sum < 0) return(0,T,maxsum,maxseq); if (sum>maxsum) return(sum, seq, sum, seq); return(sum, seq, maxsum, maxseq); }, T(0,T,0,T))[3].reverse(); // -->maxseq.reverse() }
http://rosettacode.org/wiki/Greatest_subsequential_sum
Greatest subsequential sum
Task Given a sequence of integers, find a continuous subsequence which maximizes the sum of its elements, that is, the elements of no other single subsequence add up to a value larger than this one. An empty subsequence is considered to have the sum of   0;   thus if all elements are negative, the result must be th...
#ZX_Spectrum_Basic
ZX Spectrum Basic
10 DATA 12,0,1,2,-3,3,-1,0,-4,0,-1,-4,2 20 DATA 11,-1,-2,3,5,6,-2,-1,4,-4,2,-1 30 DATA 5,-1,-2,-3,-4,-5 40 FOR n=1 TO 3 50 READ l 60 DIM a(l) 70 FOR i=1 TO l 80 READ a(i) 90 PRINT a(i); 100 IF i<l THEN PRINT ", "; 110 NEXT i 120 PRINT 130 LET a=1: LET m=0: LET b=0 140 FOR i=1 TO l 150 LET s=0 160 FOR j=i TO l 170 LET ...
http://rosettacode.org/wiki/Guess_the_number/With_feedback
Guess the number/With feedback
Task Write a game (computer program) that follows the following rules: The computer chooses a number between given set limits. The player is asked for repeated guesses until the the target number is guessed correctly At each guess, the computer responds with whether the guess is: higher than the target, equal to...
#Nanoquery
Nanoquery
import Nanoquery.Util   random = new(Random) inclusive_range = {1, 100}   print format("Guess my target number that is between %d and %d (inclusive).\n",\ inclusive_range[0], inclusive_range[1]) target = random.getInt(inclusive_range[1]) + inclusive_range[0] answer = 0 i = 0 while answer != target i += 1 ...
http://rosettacode.org/wiki/Happy_numbers
Happy numbers
From Wikipedia, the free encyclopedia: A happy number is defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals   1   (where it will stay),   or it loops endlessly in a cycle which does not inclu...
#NetRexx
NetRexx
/*NetRexx program to display the 1st 8 (or specified arg) happy numbers*/ limit = arg[0] /*get argument for LIMIT. */ say limit if limit = null, limit ='' then limit=8 /*if not specified, set LIMIT to 8*/ haps = 0 /*count of happy numbers so far. */   loop ...
http://rosettacode.org/wiki/Hello_world/Text
Hello world/Text
Hello world/Text is part of Short Circuit's Console Program Basics selection. Task Display the string Hello world! on a text console. Related tasks   Hello world/Graphical   Hello world/Line Printer   Hello world/Newbie   Hello world/Newline omission   Hello world/Standard error   Hello world/Web server
#JCL
JCL
/*MESSAGE Hello world!
http://rosettacode.org/wiki/Hello_world/Graphical
Hello world/Graphical
Task Display the string       Goodbye, World!       on a GUI object   (alert box, plain window, text area, etc.). Related task   Hello world/Text
#Stata
Stata
window stopbox note "Goodbye, World!"
http://rosettacode.org/wiki/Hello_world/Graphical
Hello world/Graphical
Task Display the string       Goodbye, World!       on a GUI object   (alert box, plain window, text area, etc.). Related task   Hello world/Text
#Supernova
Supernova
I want window and the window title is "Goodbye, World".
http://rosettacode.org/wiki/Gray_code
Gray code
Gray code Karnaugh maps Create functions to encode a number to and decode a number from Gray code. Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary). There are many possible Gray codes. The follow...
#Tcl
Tcl
namespace eval gray { proc encode n { expr {$n ^ $n >> 1} } proc decode n { # Compute some bit at least as large as MSB set i [expr {2**int(ceil(log($n+1)/log(2)))}] set b [set bprev [expr {$n & $i}]] while {[set i [expr {$i >> 1}]]} { set b [expr {$b | [set bprev [expr {$n & $i ^ $bprev >> 1}]]}]...
http://rosettacode.org/wiki/Gray_code
Gray code
Gray code Karnaugh maps Create functions to encode a number to and decode a number from Gray code. Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary). There are many possible Gray codes. The follow...
#TypeScript
TypeScript
// Gray code   function encode(v: number): number { return v ^ (v >> 1); }   function decode(v: number): number { var result = 0; while (v > 0) { result ^= v; v >>= 1; } return result; }   console.log("decimal binary gray decoded"); for (var i = 0; i <= 31; i++) { var g = encode(i); var d = ...
http://rosettacode.org/wiki/Generic_swap
Generic swap
Task Write a generic swap function or operator which exchanges the values of two variables (or, more generally, any two storage places that can be assigned), regardless of their types. If your solution language is statically typed please describe the way your language provides genericity. If variables are typed in t...
#E
E
def swap(&left, &right) { def t := left left := right right := t }
http://rosettacode.org/wiki/Greatest_element_of_a_list
Greatest element of a list
Task Create a function that returns the maximum value in a provided set of values, where the number of values may not be known until run-time.
#Ela
Ela
open list   findBy p (x::xs) = foldl (\x y | p x y -> x | else -> y) x xs maximum = findBy (>)   maximum [1..10]
http://rosettacode.org/wiki/Greatest_common_divisor
Greatest common divisor
Greatest common divisor You are encouraged to solve this task according to the task description, using any language you may know. Task Find the greatest common divisor   (GCD)   of two integers. Greatest common divisor   is also known as   greatest common factor (gcf)   and   greatest common measure. Related tas...
#DWScript
DWScript
PrintLn(Gcd(231, 210));
http://rosettacode.org/wiki/Hailstone_sequence
Hailstone sequence
The Hailstone sequence of numbers can be generated from a starting positive integer,   n   by:   If   n   is     1     then the sequence ends.   If   n   is   even then the next   n   of the sequence   = n/2   If   n   is   odd   then the next   n   of the sequence   = (3 * n) + 1 The (unproven) Collatz conje...
#ERRE
ERRE
  PROGRAM ULAM   !$DOUBLE   PROCEDURE HAILSTONE(X,PRT%->COUNT) COUNT=1 IF PRT% THEN PRINT(X,) END IF REPEAT IF X/2<>INT(X/2) THEN X=X*3+1 ELSE X=X/2 END IF IF PRT% THEN PRINT(X,) END IF COUNT=COUNT+1 UNTIL X=1 IF PRT% THEN PRINT END IF END PROCEDURE   B...
http://rosettacode.org/wiki/Hamming_numbers
Hamming numbers
Hamming numbers are numbers of the form   H = 2i × 3j × 5k where i, j, k ≥ 0 Hamming numbers   are also known as   ugly numbers   and also   5-smooth numbers   (numbers whose prime divisors are less or equal to 5). Task Generate the sequence of Hamming numbers, in increasing order.   In ...
#PureBasic
PureBasic
#X2 = 2 #X3 = 3 #X5 = 5   Macro Ham(w) PrintN("H("+Str(w)+") = "+Str(Hamming(w))) EndMacro   Procedure.i Hamming(l.i) Define.i i,j,k,n,m,x=#X2,y=#X3,z=#X5 Dim h.i(l) : h(0)=1 For n=1 To l-1 m=x If m>y : m=y : EndIf If m>z : m=z : EndIf h(n)=m If m=x : i+1 : x=#X2*h(i) : EndIf If m=y : j+...
http://rosettacode.org/wiki/Guess_the_number
Guess the number
Task Write a program where the program chooses a number between   1   and   10. A player is then prompted to enter a guess.   If the player guesses wrong,   then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will issue a   "Well guessed!"   message,   a...
#Self
Self
(| parent* = traits clonable. copy = (resend.copy secretNumber: random integerBetween: 1 And: 10). secretNumber.   ask = ((userQuery askString: 'Guess the Number: ') asInteger). reportSuccess = (userQuery report: 'You got it!'). reportFailure = (userQuery report: 'Nope. Guess again.'). sayIntroduction ...
http://rosettacode.org/wiki/Guess_the_number/With_feedback
Guess the number/With feedback
Task Write a game (computer program) that follows the following rules: The computer chooses a number between given set limits. The player is asked for repeated guesses until the the target number is guessed correctly At each guess, the computer responds with whether the guess is: higher than the target, equal to...
#Nemerle
Nemerle
using System; using System.Console;   module GuessHints { Main() : void { def rand = Random(); def secret = rand.Next(1, 101); mutable guess = 0;   def GetGuess() : int {Int32.Parse(ReadLine())}   WriteLine("Guess a number between 1 and 100:");   do ...
http://rosettacode.org/wiki/Happy_numbers
Happy numbers
From Wikipedia, the free encyclopedia: A happy number is defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals   1   (where it will stay),   or it loops endlessly in a cycle which does not inclu...
#Nim
Nim
import intsets   proc happy(n: int): bool = var n = n past = initIntSet() while n != 1: let s = $n n = 0 for c in s: let i = ord(c) - ord('0') n += i * i if n in past: return false past.incl(n) return true   for x in 0..31: if happy(x): echo x
http://rosettacode.org/wiki/Hello_world/Text
Hello world/Text
Hello world/Text is part of Short Circuit's Console Program Basics selection. Task Display the string Hello world! on a text console. Related tasks   Hello world/Graphical   Hello world/Line Printer   Hello world/Newbie   Hello world/Newline omission   Hello world/Standard error   Hello world/Web server
#Jinja
Jinja
  from jinja2 import Template print(Template("Hello World!").render())  
http://rosettacode.org/wiki/Hello_world/Graphical
Hello world/Graphical
Task Display the string       Goodbye, World!       on a GUI object   (alert box, plain window, text area, etc.). Related task   Hello world/Text
#Swift
Swift
import Cocoa   let alert = NSAlert() alert.messageText = "Goodbye, World!" alert.runModal()
http://rosettacode.org/wiki/Hello_world/Graphical
Hello world/Graphical
Task Display the string       Goodbye, World!       on a GUI object   (alert box, plain window, text area, etc.). Related task   Hello world/Text
#Tcl
Tcl
pack [label .l -text "Goodbye, World"]
http://rosettacode.org/wiki/Gray_code
Gray code
Gray code Karnaugh maps Create functions to encode a number to and decode a number from Gray code. Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary). There are many possible Gray codes. The follow...
#Ursala
Ursala
#import std #import nat   xor = ~&Y&& not ~&B # either and not both   btog = xor*+ zipp0@iitBX # map xor over the argument zipped with its shift   gtob = ~&y+ =><0> ^C/xor@lrhPX ~&r # fold xor over the next input with previous output   #show+   test = mat` * 2-$'01'***K7xSS pad0*K7 <.~&,b...
http://rosettacode.org/wiki/Gray_code
Gray code
Gray code Karnaugh maps Create functions to encode a number to and decode a number from Gray code. Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary). There are many possible Gray codes. The follow...
#VBScript
VBScript
Function Encoder(ByVal n) Encoder = n Xor (n \ 2) End Function   Function Decoder(ByVal n) Dim g : g = 0 Do While n > 0 g = g Xor n n = n \ 2 Loop Decoder = g End Function   ' Decimal to Binary Function Dec2bin(ByVal n, ByVal length) Dim i, strbin : strbin = "" For i = 1 to 5 ...
http://rosettacode.org/wiki/Generic_swap
Generic swap
Task Write a generic swap function or operator which exchanges the values of two variables (or, more generally, any two storage places that can be assigned), regardless of their types. If your solution language is statically typed please describe the way your language provides genericity. If variables are typed in t...
#EchoLisp
EchoLisp
  ;; 1) ;; a macro will do it, as shown in Racket (same syntax) (define-syntax-rule (swap a b) (let ([tmp a]) (set! a b) (set! b tmp)))   (define A 666) (define B "simon") (swap A B) A → "simon" B → 666   ;; 2) ;; The list-swap! function allows to swap two items inside a list, regardless of their types ;; ...
http://rosettacode.org/wiki/Greatest_element_of_a_list
Greatest element of a list
Task Create a function that returns the maximum value in a provided set of values, where the number of values may not be known until run-time.
#Elena
Elena
import extensions;   extension op { get Maximal() { auto en := cast Enumerator(self.enumerator());   object maximal := nil; while (en.next()) { var item := en.get(); if (nil == maximal) { maximal := item } ...
http://rosettacode.org/wiki/Greatest_common_divisor
Greatest common divisor
Greatest common divisor You are encouraged to solve this task according to the task description, using any language you may know. Task Find the greatest common divisor   (GCD)   of two integers. Greatest common divisor   is also known as   greatest common factor (gcf)   and   greatest common measure. Related tas...
#Dyalect
Dyalect
func gcd(a, b) { func bgcd(a, b, res) { if a == b { return res * a } else if a % 2 == 0 && b % 2 == 0 { return bgcd(a/2, b/2, 2*res) } else if a % 2 == 0 { return bgcd(a/2, b, res) } else if b % 2 == 0 { return bgcd(a, b/2, res) ...
http://rosettacode.org/wiki/Hailstone_sequence
Hailstone sequence
The Hailstone sequence of numbers can be generated from a starting positive integer,   n   by:   If   n   is     1     then the sequence ends.   If   n   is   even then the next   n   of the sequence   = n/2   If   n   is   odd   then the next   n   of the sequence   = (3 * n) + 1 The (unproven) Collatz conje...
#Euler_Math_Toolbox
Euler Math Toolbox
  >function hailstone (n) ... $ v=[n]; $ repeat $ if mod(n,2) then n=3*n+1; $ else n=n/2; $ endif; $ v=v|n; $ until n==1; $ end; $ return v; $ endfunction >hailstone(27), length(%) [ 27 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 ...
http://rosettacode.org/wiki/Hamming_numbers
Hamming numbers
Hamming numbers are numbers of the form   H = 2i × 3j × 5k where i, j, k ≥ 0 Hamming numbers   are also known as   ugly numbers   and also   5-smooth numbers   (numbers whose prime divisors are less or equal to 5). Task Generate the sequence of Hamming numbers, in increasing order.   In ...
#Python
Python
from itertools import islice   def hamming2(): '''\ This version is based on a snippet from: https://web.archive.org/web/20081219014725/http://dobbscodetalk.com:80 /index.php?option=com_content&task=view&id=913&Itemid=85 http://www.drdobbs.com/architecture-and-design/ham...
http://rosettacode.org/wiki/Guess_the_number
Guess the number
Task Write a program where the program chooses a number between   1   and   10. A player is then prompted to enter a guess.   If the player guesses wrong,   then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will issue a   "Well guessed!"   message,   a...
#Sidef
Sidef
var n = irand(1, 10) var msg = 'Guess the number: ' while (n != read(msg, Number)) { msg = 'Wrong! Guess again: ' } say 'Well guessed!'
http://rosettacode.org/wiki/Guess_the_number
Guess the number
Task Write a program where the program chooses a number between   1   and   10. A player is then prompted to enter a guess.   If the player guesses wrong,   then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will issue a   "Well guessed!"   message,   a...
#Small_Basic
Small Basic
number=Math.GetRandomNumber(10) TextWindow.WriteLine("I just thought of a number between 1 and 10. What is it?") While guess<>number guess=TextWindow.ReadNumber() TextWindow.WriteLine("Guess again! ") EndWhile TextWindow.WriteLine("You win!")
http://rosettacode.org/wiki/Guess_the_number/With_feedback
Guess the number/With feedback
Task Write a game (computer program) that follows the following rules: The computer chooses a number between given set limits. The player is asked for repeated guesses until the the target number is guessed correctly At each guess, the computer responds with whether the guess is: higher than the target, equal to...
#NetRexx
NetRexx
/* NetRexx */ options replace format comments java crossref symbols nobinary   parse arg lo hi . if lo = '' | lo = '.' then lo = 1 if hi = '' | hi = '.' then hi = 100 if lo > hi then parse (hi lo) lo hi -- make sure lo is < hi rg = Random() tries = 0 guessThis = rg.nextInt(hi - lo) + lo   say say 'Rules: Guess a numbe...
http://rosettacode.org/wiki/Happy_numbers
Happy numbers
From Wikipedia, the free encyclopedia: A happy number is defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals   1   (where it will stay),   or it loops endlessly in a cycle which does not inclu...
#Objeck
Objeck
use IO; use Structure;   bundle Default { class HappyNumbers { function : native : IsHappy(n : Int) ~ Bool { cache := IntVector->New(); sum := 0; while(n <> 1) { if(cache->Has(n)) { return false; };   cache->AddBack(n); while(n <> 0) { ...
http://rosettacode.org/wiki/Hello_world/Text
Hello world/Text
Hello world/Text is part of Short Circuit's Console Program Basics selection. Task Display the string Hello world! on a text console. Related tasks   Hello world/Graphical   Hello world/Line Printer   Hello world/Newbie   Hello world/Newline omission   Hello world/Standard error   Hello world/Web server
#Joy
Joy
"Hello world!" putchars.
http://rosettacode.org/wiki/Hello_world/Graphical
Hello world/Graphical
Task Display the string       Goodbye, World!       on a GUI object   (alert box, plain window, text area, etc.). Related task   Hello world/Text
#TI-83_BASIC
TI-83 BASIC
PROGRAM:GUIHELLO :Text(0,0,"GOODBYE, WORLD!")
http://rosettacode.org/wiki/Hello_world/Graphical
Hello world/Graphical
Task Display the string       Goodbye, World!       on a GUI object   (alert box, plain window, text area, etc.). Related task   Hello world/Text
#TI-89_BASIC
TI-89 BASIC
Dialog Text "Goodbye, World!" EndDlog
http://rosettacode.org/wiki/Gray_code
Gray code
Gray code Karnaugh maps Create functions to encode a number to and decode a number from Gray code. Display the normal binary representations, Gray code representations, and decoded Gray code values for all 5-bit binary numbers (0-31 inclusive, leading 0's not necessary). There are many possible Gray codes. The follow...
#Verilog
Verilog
  `timescale 1ns/10ps `default_nettype wire   module graytestbench;   localparam aw = 8;   function [aw:0] binn_to_gray; input [aw:0] binn; begin :b2g binn_to_gray = binn ^ (binn >> 1); end endfunction   function [aw:0] gray_to_binn; input [aw:0] gray; begin :g2b reg [aw:0] binn; integer i;...
http://rosettacode.org/wiki/Generic_swap
Generic swap
Task Write a generic swap function or operator which exchanges the values of two variables (or, more generally, any two storage places that can be assigned), regardless of their types. If your solution language is statically typed please describe the way your language provides genericity. If variables are typed in t...
#Elena
Elena
import extensions;   swap(ref object v1, ref object v2) { var tmp := v1;   v1 := v2; v2 := tmp }   public program() { var n := 2; var s := "abc";   console.printLine(n," ",s);   swap(ref n, ref s);   console.printLine(n," ",s) }
http://rosettacode.org/wiki/Greatest_element_of_a_list
Greatest element of a list
Task Create a function that returns the maximum value in a provided set of values, where the number of values may not be known until run-time.
#Elixir
Elixir
iex(1)> Enum.max([3,1,4,1,5,9,2,6,5,3]) 9
http://rosettacode.org/wiki/Greatest_element_of_a_list
Greatest element of a list
Task Create a function that returns the maximum value in a provided set of values, where the number of values may not be known until run-time.
#Emacs_Lisp
Emacs Lisp
(defun find-maximum (items) (let (max) (dolist (item items) (when (or (not max) (> item max)) (setq max item))) max))   (find-maximum '(2 7 5)) ;=> 7
http://rosettacode.org/wiki/Greatest_common_divisor
Greatest common divisor
Greatest common divisor You are encouraged to solve this task according to the task description, using any language you may know. Task Find the greatest common divisor   (GCD)   of two integers. Greatest common divisor   is also known as   greatest common factor (gcf)   and   greatest common measure. Related tas...
#E
E
def gcd(var u :int, var v :int) { while (v != 0) { def r := u %% v u := v v := r } return u.abs() }
http://rosettacode.org/wiki/Hailstone_sequence
Hailstone sequence
The Hailstone sequence of numbers can be generated from a starting positive integer,   n   by:   If   n   is     1     then the sequence ends.   If   n   is   even then the next   n   of the sequence   = n/2   If   n   is   odd   then the next   n   of the sequence   = (3 * n) + 1 The (unproven) Collatz conje...
#Euphoria
Euphoria
function hailstone(atom n) sequence s s = {n} while n != 1 do if remainder(n,2)=0 then n /= 2 else n = 3*n + 1 end if s &= n end while return s end function   function hailstone_count(atom n) integer count count = 1 while n != 1 do ...