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/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 ... | #QBasic | QBasic | FUNCTION min (a, b)
IF a < b THEN min = a ELSE min = b
END FUNCTION
FUNCTION Hamming (limit)
DIM h(limit)
h(0) = 1
x2 = 2
x3 = 3
x5 = 5
i = 0
j = 0
k = 0
FOR n = 1 TO limit
h(n) = min(x2, min(x3, x5))
IF x2 = h(n) THEN
i = i + 1
x2 = 2 ... |
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... | #SNUSP | SNUSP | /++.#
\>.--.++<..+ +\
/>+++++++.>-.+/
/ \ />++++++>++\
< + ?
< > \ -<<++++++/
$++++\ - + !
/++++/ > \++++++++++\
\+ %!/!\?/>>>,>>>>>>+++\/ !/?\<?\>>/
... |
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... | #Swift | Swift | import Cocoa
var found = false
let randomNum = Int(arc4random_uniform(10) + 1)
println("Guess a number between 1 and 10\n")
while (!found) {
var fh = NSFileHandle.fileHandleWithStandardInput()
println("Enter a number: ")
let data = fh.availableData
var str = NSString(data: data, encoding: NSUTF8St... |
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... | #NewLISP | NewLISP | ; guess-number-feedback.lsp
; oofoe 2012-01-19
; http://rosettacode.org/wiki/Guess_the_number/With_feedback
(seed (time-of-day)) ; Initialize random number generator from clock.
(setq low -5
high 62
number (+ low (rand (- high low)))
found nil
)
(print "I'm thinking of a number between " low " a... |
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... | #OCaml | OCaml | open Num
let step =
let rec aux s n =
if n =/ Int 0 then s else
let q = quo_num n (Int 10)
and r = mod_num n (Int 10)
in aux (s +/ (r */ r)) q
in aux (Int 0) ;;
let happy n =
let rec aux x y =
if x =/ y then x else aux (step x) (step (step y))
in (aux n (step n)) =/ Int 1 ;;
let first n =
let rec au... |
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
| #jq | jq | "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
| #Tosh | Tosh | when flag clicked
say "Goodbye, World!"
stop this script |
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
| #TXR | TXR | (with-dyn-lib "user32.dll"
(deffi messagebox "MessageBoxW" int (cptr wstr wstr uint)))
(messagebox cptr-null "Hello" "World" 0) ;; 0 is MB_OK |
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... | #VHDL | VHDL | LIBRARY ieee;
USE ieee.std_logic_1164.all;
entity b2g is
port( bin : in std_logic_vector (4 downto 0);
gray : out std_logic_vector (4 downto 0)
);
end b2g ;
architecture rtl of b2g is
constant N : integer := bin'high;
begin
gray <= bin(n) & ( bin(N-1 downto 0) xor bin(N downto 1));
end a... |
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... | #Elixir | Elixir |
x = 4
y = 5
{y,x} = {x,y}
y # => 4
x # => 5
[x,y] = [y,x]
x # => 4
y # => 5
|
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.
| #Erlang | Erlang | >lists:max([9,4,3,8,5]).
9 |
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... | #EasyLang | EasyLang | func gcd a b . res .
while b <> 0
h = b
b = a mod b
a = h
.
res = a
.
call gcd 120 35 r
print r |
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... | #Excel | Excel | In cell A1, place the starting number.
In cell A2 enter this formula =IF(MOD(A1,2)=0,A1/2,A1*3+1)
Drag and copy the formula down until 4, 2, 1
|
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 ... | #Qi | Qi | (define smerge
[X|Xs] [Y|Ys] -> [X | (freeze (smerge (thaw Xs) [Y|Ys]))] where (< X Y)
[X|Xs] [Y|Ys] -> [Y | (freeze (smerge [X|Xs] (thaw Ys)))] where (> X Y)
[X|Xs] [_|Ys] -> [X | (freeze (smerge (thaw Xs) (thaw Ys)))])
(define smerge3
Xs Ys Zs -> (smerge Xs (smerge Ys Zs)))
(define smap
F [S|Ss] -> [(F ... |
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... | #Tcl | Tcl | set target [expr {int(rand()*10 + 1)}]
puts "I have thought of a number."
puts "Try to guess it!"
while 1 {
puts -nonewline "Enter your guess: "
flush stdout
gets stdin guess
if {$guess == $target} {
break
}
puts "Your guess was wrong. Try again!"
}
puts "Well done! You guessed it." |
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... | #Nim | Nim | import random, strutils
randomize()
let iRange = 1..100
echo "Guess my target number that is between ", iRange.a, " and ", iRange.b, " (inclusive)."
let target = rand(iRange)
var answer, i = 0
while answer != target:
inc i
stdout.write "Your guess ", i, ": "
let txt = stdin.readLine()
try: answer = parseI... |
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... | #Oforth | Oforth | : isHappy(n)
| cycle |
ListBuffer new ->cycle
while(n 1 <>) [
cycle include(n) ifTrue: [ false return ]
cycle add(n)
0 n asString apply(#[ asDigit sq + ]) ->n
]
true ;
: happyNum(N)
| numbers |
ListBuffer new ->numbers
1 while(numbers size N <>) [ dup isHappy ifTrue: [ dup num... |
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
| #JSE | JSE | Print "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
| #UNIX_Shell | UNIX Shell |
whiptail --title 'Farewell' --msgbox 'Goodbye, World!' 7 20
|
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... | #Vlang | Vlang | fn enc(b int) int {
return b ^ b>>1
}
fn dec(gg int) int {
mut b := 0
mut g := gg
for ; g != 0; g >>= 1 {
b ^= g
}
return b
}
fn main() {
println("decimal binary gray decoded")
for b := 0; b < 32; b++ {
g := enc(b)
d := dec(g)
println(" ${b:2} ... |
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... | #Emacs_Lisp | Emacs Lisp | (defun swap (a-sym b-sym)
"Swap values of the variables given by A-SYM and B-SYM."
(let ((a-val (symbol-value a-sym)))
(set a-sym (symbol-value b-sym))
(set b-sym a-val)))
(swap 'a 'b) |
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.
| #ERRE | ERRE |
PROGRAM MAXLIST
!
! for rosettacode.org
!
! VAR L$,EL$,CH$,I%,MAX
BEGIN
PRINT(CHR$(12);) ! CLS
INPUT("Lista",L$)
L$=L$+CHR$(32)
MAX=-1.7E+38
FOR I%=1 TO LEN(L$) DO
CH$=MID$(L$,I%,1)
IF CH$<>CHR$(32) THEN ! blank is separator
EL$=EL$+CH$
ELSE
IF VAL(EL$)>MAX THEN MAX=VAL(EL$)... |
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.
| #Euler_Math_Toolbox | Euler Math Toolbox |
>v=random(1,100);
>max(v)
0.997492478596
|
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... | #EDSAC_order_code | EDSAC order code |
[Greatest common divisor for Rosetta Code.
Program for EDSAC, Initial Orders 2.]
[Library subroutine R2. Reads positive integers during input of orders,
and is then overwritten (so doesn't take up any memory).
Negative numbers can be input by adding 2^35.
Each integer is followed by 'F', except the last... |
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... | #Ezhil | Ezhil |
நிரல்பாகம் hailstone ( எண் )
பதிப்பி "=> ",எண் #hailstone seq
@( எண் == 1 ) ஆனால்
பின்கொடு எண்
முடி
@( (எண்%2) == 1 ) ஆனால்
hailstone( 3*எண் + 1)
இல்லை
hailstone( எண்/2 )
முடி
முடி
எண்கள் = [5,17,19,23,37]
@(எண... |
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 ... | #Quackery | Quackery | ' [ 2 3 5 ] smoothwith [ size 1000000 = ]
dup 20 split drop echo cr
dup 1690 peek echo cr
-1 peek echo
|
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... | #TUSCRIPT | TUSCRIPT |
$$ MODE TUSCRIPT
PRINT "Find the luckynumber (7 tries)!"
luckynumber=RANDOM_NUMBERS (1,10,1)
COMPILE
LOOP round=1,7
message=CONCAT ("[",round,"] Please insert a number")
ASK $message: n=""
IF (n!='digits') THEN
PRINT "wrong insert: ",n," Please insert a digit"
ELSEIF (n>10.or.n<1) THEN
PRINT "wrong insert: ",... |
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... | #UNIX_Shell | UNIX Shell | #!/bin/sh
# Guess the number
# This simplified program does not check the input is valid
# Use awk(1) to get a random number. If awk(1) not found, exit now.
number=`awk 'BEGIN{print int(rand()*10+1)}'` || exit
echo 'I have thought of a number. Try to guess it!'
echo 'Enter an integer from 1 to 10.'
until read guess... |
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... | #NS-HUBASIC | NS-HUBASIC | 10 NUMBER=RND(10)+1
20 INPUT "I'M THINKING OF A NUMBER BETWEEN 1 AND 10. WHAT IS IT? ",GUESS
30 IF GUESS>NUMBER THEN PRINT "MY NUMBER IS LOWER THAN THAT.": GOTO 20
40 IF GUESS<NUMBER THEN PRINT "MY NUMBER IS HIGHER THAN THAT.": GOTO 20
50 PRINT "THAT'S THE CORRECT NUMBER." |
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... | #Objeck | Objeck | use IO;
bundle Default {
class GuessNumber {
function : Main(args : String[]) ~ Nil {
Guess();
}
function : native : Guess() ~ Nil {
done := false;
"Guess the number which is between 1 and 10 or 'n' to quite: "->PrintLine();
rand_num := (Float->Random() * 10.0)... |
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... | #ooRexx | ooRexx |
count = 0
say "First 8 happy numbers are:"
loop i = 1 while count < 8
if happyNumber(i) then do
count += 1
say i
end
end
::routine happyNumber
use strict arg number
-- use to trace previous cycle results
previous = .set~new
loop forever
-- stop when we hit the target
if... |
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
| #Jsish | Jsish | puts("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
| #Ultimate.2B.2B | Ultimate++ |
#include <CtrlLib/CtrlLib.h>
// submitted by Aykayayciti (Earl Lamont Montgomery)
using namespace Upp;
class GoodbyeWorld : public TopWindow {
MenuBar menu;
StatusBar status;
void FileMenu(Bar& bar);
void MainMenu(Bar& bar);
void About();
public:
typedef GoodbyeWorld CLASSNAME;
GoodbyeWorld();
};
... |
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... | #Wren | Wren | import "/fmt" for Fmt
var toGray = Fn.new { |n| n ^ (n>>1) }
var fromGray = Fn.new { |g|
var b = 0
while (g != 0) {
b = b ^ g
g = g >> 1
}
return b
}
System.print("decimal binary gray decoded")
for (b in 0..31) {
System.write(" %(Fmt.d(2, b)) %(Fmt.bz(5, b))")
var... |
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... | #XBasic | XBasic | ' Gray code
PROGRAM "graycode"
VERSION "0.0001"
DECLARE FUNCTION Entry()
INTERNAL FUNCTION Encode(v&)
INTERNAL FUNCTION Decode(v&)
FUNCTION Entry()
PRINT "decimal binary gray decoded"
FOR i& = 0 TO 31
g& = Encode(i&)
d& = Decode(g&)
PRINT FORMAT$(" ##", i&); " "; BIN$(i&, 5); " "; BIN$(... |
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... | #Erlang | Erlang |
1> L = [a, 2].
[a,2]
2> lists:reverse(L).
[2,a]
|
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.
| #Euphoria | Euphoria | function aeval( sequence sArr, integer id )
for i = 1 to length( sArr ) do
sArr[ i ] = call_func( id, { sArr[ i ] } )
end for
return sArr
end function
object biggun
function biggest( object elem )
if compare(elem, biggun) > 0 then
biggun = elem
end if
return elem
end function
... |
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... | #Eiffel | Eiffel |
class
APPLICATION
create
make
feature -- Implementation
gcd (x: INTEGER y: INTEGER): INTEGER
do
if y = 0 then
Result := x
else
Result := gcd (y, x \\ y);
end
end
feature {NONE} -- Initialization
make
-- Run application.
do
print (gcd (15, 10))
print ("%N")
end
end
|
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... | #F.23 | F# | let rec hailstone n = seq {
match n with
| 1 -> yield 1
| n when n % 2 = 0 -> yield n; yield! hailstone (n / 2)
| n -> yield n; yield! hailstone (n * 3 + 1)
}
let hailstone27 = hailstone 27 |> Array.ofSeq
assert (Array.length hailstone27 = 112)
assert (hailstone27.[..3] = [|27;82... |
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 ... | #R | R | hamming=function(hamms,limit) {
tmp=hamms
for(h in c(2,3,5)) {
tmp=c(tmp,h*hamms)
}
tmp=unique(tmp[tmp<=limit])
if(length(tmp)>length(hamms)) {
hamms=hamming(tmp,limit)
}
hamms
}
h <- sort(hamming(1,limit=2^31-1))
print(h[1:20])
print(h[length(h)]) |
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... | #Ursa | Ursa | # Simple number guessing game
decl ursa.util.random random
decl int target guess
set target (int (+ 1 (random.getint 9)))
out "Guess a number between 1 and 10." endl console
while (not (= target guess))
set guess (in int console)
end while
out "That's right!" endl console |
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... | #Vala | Vala | int main() {
int x = Random.int_range(1, 10);
stdout.printf("Make a guess (1-10): ");
while(int.parse(stdin.read_line()) != x)
stdout.printf("Wrong! Try again: ");
stdout.printf("Got it!\n");
return 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... | #OCaml | OCaml | let rec _read_int() =
try read_int()
with _ ->
print_endline "Please give a cardinal numbers.";
(* TODO: what is the correct word? cipher, digit, figure or numeral? *)
_read_int() ;;
let () =
print_endline "Please give a set limits (two integers):";
let a = _read_int()
and b = _read_int() in
l... |
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... | #Oz | Oz | functor
import
System
define
fun {IsHappy N}
{IsHappy2 N nil}
end
fun {IsHappy2 N Seen}
if N == 1 then true
elseif {Member N Seen} then false
else
Next = {Sum {Map {Digits N} Square}}
in
{IsHappy2 Next N|Seen}
end
end
fun {Sum Xs}
{FoldL Xs Number.'+' 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
| #Julia | Julia | 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
| #Vala | Vala | #!/usr/local/bin/vala --pkg gtk+-3.0
using Gtk;
void main(string[] args) {
Gtk.init(ref args);
var window = new Window();
window.title = "Goodbye, world!";
window.border_width = 10;
window.window_position = WindowPosition.CENTER;
window.set_default_size(350, 70);
window.destroy.connect(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... | #XPL0 | XPL0 | include c:\cxpl\codes; \intrinsic 'code' declarations
func Gray2Bin(N); \Convert N from Gray code to binary
int N;
int S;
[S:= 1;
repeat N:= N>>S | N;
S:= S<<1;
until S=32;
return N;
]; \Gray2Bin
func Bin2Gray(N); \Convert N from binary to Gray code
int N;
return N>... |
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... | #zkl | zkl | fcn grayEncode(n){ n.bitXor(n.shiftRight(1)) }
fcn grayDecode(g){ b:=g; while(g/=2){ b=b.bitXor(g) } b } |
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... | #Euphoria | Euphoria |
include std/console.e -- for display
object x = 3.14159
object y = "Rosettacode"
{y,x} = {x,y}
display("x is now []",{x})
display("y is now []",{y})
|
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.
| #Excel | Excel |
=MAX(3;2;1;4;5;23;1;2)
|
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.
| #F.23 | F# |
let N = System.Random()
let G = List.init 10 (fun _->N.Next())
List.iter (printf "%d ") G
printfn "\nMax value of list is %d" (List.max G)
|
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... | #Elena | Elena | import system'math;
import extensions;
gcd(a,b)
{
var i := a;
var j := b;
while(j != 0)
{
var tmp := i;
i := j;
j := tmp.mod(j)
};
^ i
}
printGCD(a,b)
{
console.printLineFormatted("GCD of {0} and {1} is {2}", a, b, gcd(a,b))
}
public program()
{
printGCD(1... |
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... | #Factor | Factor | ! rosetta/hailstone/hailstone.factor
USING: arrays io kernel math math.ranges prettyprint sequences vectors ;
IN: rosetta.hailstone
: hailstone ( n -- seq )
[ 1vector ] keep
[ dup 1 number= ]
[
dup even? [ 2 / ] [ 3 * 1 + ] if
2dup swap push
] until
drop ;
<PRIVATE
: main ( -- )
... |
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 ... | #Racket | Racket | #lang racket
(require racket/stream)
(define first stream-first)
(define rest stream-rest)
(define (merge s1 s2)
(define x1 (first s1))
(define x2 (first s2))
(cond [(= x1 x2) (merge s1 (rest s2))]
[(< x1 x2) (stream-cons x1 (merge (rest s1) s2))]
[else (stream-cons x2 (merge s1 (rest s2))... |
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... | #VBA | VBA | Sub GuessTheNumber()
Dim NbComputer As Integer, NbPlayer As Integer
Randomize Timer
NbComputer = Int((Rnd * 10) + 1)
Do
NbPlayer = Application.InputBox("Choose a number between 1 and 10 : ", "Enter your guess", Type:=1)
Loop While NbComputer <> NbPlayer
MsgBox "Well guessed!"
End Sub |
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... | #VBScript | VBScript | randomize
MyNum=Int(rnd*10)+1
Do
x=x+1
YourGuess=InputBox("Enter A number from 1 to 10")
If not Isnumeric(YourGuess) then
msgbox YourGuess &" is not numeric. Try again."
ElseIf CInt(YourGuess)>10 or CInt(YourGuess)<1 then
msgbox YourGuess &" is not between 1 and 10. Try Again."
ElseIf CInt(YourGuess)=CInt(MyNu... |
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... | #Octave | Octave | function guess_a_number(low,high)
% Guess a number (with feedback)
% http://rosettacode.org/wiki/Guess_the_number/With_feedback
if nargin<1,
low=1;
end;
if nargin<2,
high=10;
end;
n = floor(rand(1)*(high-low+1))+low;
[guess,state] = str2num(input(sprintf('Guess a number between %i and %i: ',low,high),'s'));
whi... |
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... | #PARI.2FGP | PARI/GP | H=[1,7,10,13,19,23,28,31,32,44,49,68,70,79,82,86,91,94,97,100,103,109,129,130,133,139,167,176,188,190,192,193,203,208,219,226,230,236,239];
isHappy(n)={
if(n<262,
setsearch(H,n)>0
,
n=eval(Vec(Str(n)));
isHappy(sum(i=1,#n,n[i]^2))
)
};
select(isHappy, vector(31,i,i)) |
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
| #K | K |
"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
| #VBA | VBA | Public Sub hello_world_gui()
MsgBox "Goodbye, World!"
End Sub |
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
| #VBScript | VBScript |
MsgBox("Goodbye, World!")
|
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... | #F.23 | F# | let swap (a,b) = (b,a) |
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.
| #Factor | Factor | : supremum ( seq -- elt ) [ ] [ max ] map-reduce ; |
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.
| #Fancy | Fancy | [1,-2,2,4,6,-4,-1,5] max println # => 6 |
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... | #Elixir | Elixir | defmodule RC do
def gcd(a,0), do: abs(a)
def gcd(a,b), do: gcd(b, rem(a,b))
end
IO.puts RC.gcd(1071, 1029)
IO.puts RC.gcd(3528, 3780) |
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... | #Emacs_Lisp | Emacs Lisp | (defun gcd (a b)
(cond
((< a b) (gcd a (- b a)))
((> a b) (gcd (- a b) b))
(t a))) |
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... | #FALSE | FALSE | [$1&$[%3*1+0~]?~[2/]?]n:
[[$." "$1>][n;!]#%]s:
[1\[$1>][\1+\n;!]#%]c:
27s;! 27c;!."
"
0m:0f:
1[$100000\>][$c;!$m;>[m:$f:0]?%1+]#%
f;." has hailstone sequence length "m;. |
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 ... | #Raku | Raku | my $limit = 32;
sub powers_of ($radix) { 1, |[\*] $radix xx * }
my @hammings =
( powers_of(2)[^ $limit ] X*
powers_of(3)[^($limit * 2/3)] X*
powers_of(5)[^($limit * 1/2)]
).sort;
say @hammings[^20];
say @hammings[1690]; # zero indexed |
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... | #Visual_Basic_.NET | Visual Basic .NET | Module Guess_the_Number
Sub Main()
Dim random As New Random()
Dim secretNum As Integer = random.Next(10) + 1
Dim gameOver As Boolean = False
Console.WriteLine("I am thinking of a number from 1 to 10. Can you guess it?")
Do
Dim guessNum As Integer
Conso... |
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... | #Vlang | Vlang | import rand
import rand.seed
import os
fn main() {
seed_array := seed.time_seed_array(2)
rand.seed(seed_array)
num := rand.intn(10) + 1 // Random number 1-10
for {
print('Please guess a number from 1-10 and press <Enter>: ')
guess := os.get_line()
if guess.int() == num {
... |
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... | #Oforth | Oforth | import: console
: guessNumber(a, b)
| n g |
b a - rand a + 1- ->n
begin
"Guess a number between" . a . "and" . b . ":" .
while(System.Console askln asInteger dup -> g isNull) [ "Not a number " println ]
g n == ifTrue: [ "You found it !" .cr return ]
g n < ifTrue: [ "Less" ] else: [ "Gre... |
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... | #Ol | Ol |
(import (otus random!))
(define from 0)
(define to 100)
(define number (+ from (rand! (+ from to 1))))
(let loop ()
(for-each display `("Pick a number from " ,from " through " ,to ": "))
(let ((guess (read)))
(cond
((not (number? guess))
(print "Not a number!")
(loop... |
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... | #Pascal | Pascal | Program HappyNumbers (output);
uses
Math;
function find(n: integer; cache: array of integer): boolean;
var
i: integer;
begin
find := false;
for i := low(cache) to high(cache) do
if cache[i] = n then
find := true;
end;
function is_happy(n: integer): boolean;
var
cache: array... |
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
| #Kabap | Kabap | return = "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
| #Vedit_macro_language | Vedit macro language | Statline_Message("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
| #Visual_Basic | Visual Basic | Sub Main()
MsgBox "Goodbye, World!"
End Sub |
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... | #Factor | Factor | 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.
| #Fantom | Fantom |
class Greatest
{
public static Void main ()
{
Int[] values := [1,2,3,4,5,6,7,8,9]
Int greatest := values.max
echo (greatest)
}
}
|
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.
| #Forth | Forth | : array-max ( addr len -- max )
dup 0= if nip exit then
over @ rot cell+ rot 1-
cells bounds ?do i @ max cell +loop ;
: stack-max ( n ... m count -- max ) 1 ?do max 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... | #Erlang | Erlang | % Implemented by Arjun Sunel
-module(gcd).
-export([main/0]).
main() ->gcd(-36,4).
gcd(A, 0) -> A;
gcd(A, B) -> gcd(B, A rem B). |
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... | #Fermat | Fermat | Array g[2]
Func Collatz(n, d) =
{Runs the Collatz procedure for the number n and returns the number of steps.}
{If d is nonzero, prints the terms in the sequence.}
steps := 1;
while n>1 do
if n|2=0 then n:=n/2 else n:=3n+1 fi;
if d then !!n fi;
steps := steps + 1
od;
... |
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 ... | #Raven | Raven | define hamming use $limit
[ ] as $h
1 $h 0 set
2 as $x2 3 as $x3 5 as $x5
0 as $i 0 as $j 0 as $k
1 $limit 1 + 1 range each as $n
$x3 $x5 min $x2 min $h $n set
$h $n get $x2 = if
$i 1 + as $i
$h $i get 2 * as $x2
$h $n get... |
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... | #WebAssembly | WebAssembly | (module
(import "wasi_unstable" "fd_read"
(func $fd_read (param i32 i32 i32 i32) (result i32)))
(import "wasi_unstable" "fd_write"
(func $fd_write (param i32 i32 i32 i32) (result i32)))
(import "wasi_unstable" "random_get"
(func $random_get (param i32 i32) (result i32)))
(memory 1) (export "mem... |
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... | #Wee_Basic | Wee Basic | let mnnumber=1
let mxnumber=10
let number=mnnumber
let keycode=0
let guessed=0
print 1 "Press any key so the computer can generate a random number for you to guess."
while keycode=0
let number=number=1
let keycode=key()
if number=mxnumber+1
let number=mnnumber
endif
wend
print 1 "Guess the number. It is between "+mnnum... |
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... | #ooRexx | ooRexx | Select instead of a series of If's
simple comparison instead of strict
different indentations.
entering ? shows the number we are looking for
|
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... | #PARI.2FGP | PARI/GP | guess_the_number(N=10)={
a=random(N);
print("guess the number between 0 and "N);
for(x=1,N,
if(x>1,
if(b>a,
print("guess again lower")
,
print("guess again higher")
);
b=input();
if(b==a,break())
);
print("You guessed it correctly")
}; |
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... | #Perl | Perl | use List::Util qw(sum);
sub ishappy {
my $s = shift;
while ($s > 6 && $s != 89) {
$s = sum(map { $_*$_ } split(//,$s));
}
$s == 1;
}
my $n = 0;
print join(" ", map { 1 until ishappy(++$n); $n; } 1..8), "\n"; |
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
| #Kaya | Kaya | program hello;
Void main() {
// My first program!
putStrLn("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
| #Visual_Basic_.NET | Visual Basic .NET | Imports System.Windows.Forms
Module GoodbyeWorld
Sub Main()
Messagebox.Show("Goodbye, World!")
End Sub
End Module |
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
| #Visual_FoxPro | Visual FoxPro | * Version 1:
MESSAGEBOX("Goodbye, World!")
* Version 2:
? "Goodbye, World!" |
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... | #Falcon | Falcon |
a = 1
b = 2
a,b = arr = b,a
|
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.
| #Fortran | Fortran | program test_maxval
integer,dimension(5),parameter :: x = [10,100,7,1,2]
real,dimension(5),parameter :: y = [5.0,60.0,1.0,678.0,0.0]
write(*,'(I5)') maxval(x)
write(*,'(F5.1)') maxval(y)
end program test_maxval |
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... | #ERRE | ERRE |
PROGRAM EUCLIDE
! calculate G.C.D. between two integer numbers
! using Euclidean algorithm
!VAR J%,K%,MCD%,A%,B%
BEGIN
PRINT(CHR$(12);"Input two numbers : ";) !CHR$(147) in C-64 version
INPUT(J%,K%)
A%=J% B%=K%
WHILE A%<>B% DO
IF A%>B%
THEN
A%=A%-B%
ELSE
B%=B%-A%
E... |
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... | #Forth | Forth | : hail-next ( n -- n )
dup 1 and if 3 * 1+ else 2/ then ;
: .hail ( n -- )
begin dup . dup 1 > while hail-next repeat drop ;
: hail-len ( n -- n )
1 begin over 1 > while swap hail-next swap 1+ repeat nip ;
27 hail-len . cr
27 .hail cr
: longest-hail ( max -- )
0 0 rot 1+ 1 do ( n length )
i hail-len ... |
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 ... | #REXX | REXX | /*REXX program computes Hamming numbers: 1 ──► 20, # 1691, and the one millionth. */
numeric digits 100 /*ensure enough decimal digits. */
call hamming 1, 20 /*show the 1st ──► twentieth Hamming #s*/
call hamming 1691 ... |
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... | #Wortel | Wortel | @let {
num 10Wc
guess 0
[
@while != guess num
:guess !prompt "Guess the number between 1 and 10 inclusive"
!alert "Congratulations!\nThe number was {num}."
]
} |
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... | #Wren | Wren | import "io" for Stdin, Stdout
import "random" for Random
var rand = Random.new()
var n = rand.int(1, 11) // computer number from 1..10 inclusive
while (true) {
System.write("Your guess 1-10 : ")
Stdout.flush()
var guess = Num.fromString(Stdin.readLine())
if (n == guess) {
System.print("Well gu... |
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... | #Pascal | Pascal | sub prompt {
my $prompt = shift;
while (1) {
print "\n", $prompt, ": ";
# type ^D, q, quit, quagmire, etc to quit
defined($_ = <STDIN>) and !/^\s*q/ or exit;
return $_ if /^\s*\d+\s*$/s;
$prompt = "Please give a non-negative integer";
}
}
my $tgt = int(rand prompt("Hola! Please tell me the upper bound"... |
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... | #Phix | Phix | function is_happy(integer n)
sequence seen = {}
while n>1 do
seen &= n
integer k = 0
while n>0 do
k += power(remainder(n,10),2)
n = floor(n/10)
end while
n = k
if find(n,seen) then
return false
end if
end while
r... |
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
| #Kdf9_Usercode | Kdf9 Usercode |
V2; W0;
RESTART; J999; J999;
PROGRAM; (main program);
V0 = Q0/AV1/AV2;
V1 = B0750064554545700; ("Hello" in Flexowriter code);
V2 = B0767065762544477; ("World" in Flexowriter code);
V0; =Q9; POAQ9; (write "Hello World" to Flexowriter);
999; OUT;
FINISH;
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.