2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,3 +1,4 @@
|
|||
;Task:
|
||||
Test whether an integer is even or odd.
|
||||
|
||||
There is more than one way to solve this task:
|
||||
|
|
@ -8,3 +9,4 @@ There is more than one way to solve this task:
|
|||
* Use modular congruences:
|
||||
** ''i'' ≡ 0 (mod 2) iff ''i'' is even.
|
||||
** ''i'' ≡ 1 (mod 2) iff ''i'' is odd.
|
||||
<br><br>
|
||||
|
|
|
|||
4
Task/Even-or-odd/APL/even-or-odd.apl
Normal file
4
Task/Even-or-odd/APL/even-or-odd.apl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
2|28
|
||||
0
|
||||
2|37
|
||||
1
|
||||
1
Task/Even-or-odd/CoffeeScript/even-or-odd.coffee
Normal file
1
Task/Even-or-odd/CoffeeScript/even-or-odd.coffee
Normal file
|
|
@ -0,0 +1 @@
|
|||
isEven = (x) -> !(x%2)
|
||||
2
Task/Even-or-odd/Excel/even-or-odd-1.excel
Normal file
2
Task/Even-or-odd/Excel/even-or-odd-1.excel
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
=MOD(33;2)
|
||||
=MOD(18;2)
|
||||
2
Task/Even-or-odd/Excel/even-or-odd-2.excel
Normal file
2
Task/Even-or-odd/Excel/even-or-odd-2.excel
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
=ISEVEN(33)
|
||||
=ISEVEN(18)
|
||||
2
Task/Even-or-odd/Excel/even-or-odd-3.excel
Normal file
2
Task/Even-or-odd/Excel/even-or-odd-3.excel
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
=ISODD(33)
|
||||
=ISODD(18)
|
||||
|
|
@ -1,3 +1,8 @@
|
|||
function isEven( i ) {
|
||||
return i % 2 === 0;
|
||||
}
|
||||
|
||||
// Alternative
|
||||
function isEven( i ) {
|
||||
return !(i % 2);
|
||||
}
|
||||
|
|
|
|||
2
Task/Even-or-odd/JavaScript/even-or-odd-3.js
Normal file
2
Task/Even-or-odd/JavaScript/even-or-odd-3.js
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
// EMCAScript 6
|
||||
const isEven=x=>!(x%2)
|
||||
34
Task/Even-or-odd/MIPS-Assembly/even-or-odd.mips
Normal file
34
Task/Even-or-odd/MIPS-Assembly/even-or-odd.mips
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
.data
|
||||
even_str: .asciiz "Even"
|
||||
odd_str: .asciiz "Odd"
|
||||
|
||||
.text
|
||||
#set syscall to get integer from user
|
||||
li $v0,5
|
||||
syscall
|
||||
|
||||
#perform bitwise AND and store in $a0
|
||||
and $a0,$v0,1
|
||||
|
||||
#set syscall to print dytomh
|
||||
li $v0,4
|
||||
|
||||
#jump to odd if the result of the AND operation
|
||||
beq $a0,1,odd
|
||||
even:
|
||||
#load even_str message, and print
|
||||
la $a0,even_str
|
||||
syscall
|
||||
|
||||
#exit program
|
||||
li $v0,10
|
||||
syscall
|
||||
|
||||
odd:
|
||||
#load odd_str message, and print
|
||||
la $a0,odd_str
|
||||
syscall
|
||||
|
||||
#exit program
|
||||
li $v0,10
|
||||
syscall
|
||||
7
Task/Even-or-odd/Neko/even-or-odd.neko
Normal file
7
Task/Even-or-odd/Neko/even-or-odd.neko
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
var number = 6;
|
||||
|
||||
if(number % 2 == 0) {
|
||||
$print("Even");
|
||||
} else {
|
||||
$print("Odd");
|
||||
}
|
||||
21
Task/Even-or-odd/Oberon-2/even-or-odd.oberon-2
Normal file
21
Task/Even-or-odd/Oberon-2/even-or-odd.oberon-2
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
MODULE EvenOrOdd;
|
||||
IMPORT
|
||||
S := SYSTEM,
|
||||
Out;
|
||||
VAR
|
||||
x: INTEGER;
|
||||
s: SET;
|
||||
|
||||
BEGIN
|
||||
x := 10;Out.Int(x,0);
|
||||
IF ODD(x) THEN Out.String(" odd") ELSE Out.String(" even") END;
|
||||
Out.Ln;
|
||||
|
||||
x := 11;s := S.VAL(SET,LONG(x));Out.Int(x,0);
|
||||
IF 0 IN s THEN Out.String(" odd") ELSE Out.String(" even") END;
|
||||
Out.Ln;
|
||||
|
||||
x := 12;Out.Int(x,0);
|
||||
IF x MOD 2 # 0 THEN Out.String(" odd") ELSE Out.String(" even") END;
|
||||
Out.Ln
|
||||
END EvenOrOdd.
|
||||
2
Task/Even-or-odd/PowerShell/even-or-odd-1.psh
Normal file
2
Task/Even-or-odd/PowerShell/even-or-odd-1.psh
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
$IsOdd = -not ( [bigint]$N ).IsEven
|
||||
$IsEven = ( [bigint]$N ).IsEven
|
||||
2
Task/Even-or-odd/PowerShell/even-or-odd-2.psh
Normal file
2
Task/Even-or-odd/PowerShell/even-or-odd-2.psh
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
$IsOdd = [boolean]( $N -band 1 )
|
||||
$IsEven = [boolean]( $N -band 0 )
|
||||
2
Task/Even-or-odd/PowerShell/even-or-odd-3.psh
Normal file
2
Task/Even-or-odd/PowerShell/even-or-odd-3.psh
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
$IsOdd = $N % 2 -ne 0
|
||||
$IsEven = $N % 2 -eq 0
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
function parity($n) {
|
||||
if($n%2 -eq 0) {
|
||||
"$n is even"
|
||||
} else {
|
||||
"$n is odd"
|
||||
}
|
||||
}
|
||||
parity 0
|
||||
parity 1
|
||||
|
|
@ -1,85 +1,76 @@
|
|||
/*REXX program tests and displays if an integer is even or odd.*/
|
||||
!.=0; do j=0 by 2 to 8; !.j=1; end /*assign 0,2,4,6,8 to a "true" value.*/
|
||||
/* [↑] assigns even digits to "true".*/
|
||||
numeric digits 1000 /*handle most huge numbers from the CL.*/
|
||||
parse arg x _ . /*get an argument from the command line*/
|
||||
if x=='' then call terr "no input integer." /*error.*/
|
||||
if _\=='' | arg()\==1 then call terr "too many arguments: " _ arg(2) /*error.*/
|
||||
if \datatype(x,'N') then call terr x " isn't numeric." /*error.*/
|
||||
if \datatype(x,'W') then call terr x " isn't an integer." /*error.*/
|
||||
y=abs(x)/1 /*in case X is negative or malformed,*/
|
||||
/* [↑] remainder of neg # might be -1.*/
|
||||
/*malformed #s: 007 9.0 4.8e1 .21e2 */
|
||||
/*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒*/
|
||||
call sayr 'remainder method (oddness)'
|
||||
/*REXX program tests and displays if an integer is even or odd using different styles.*/
|
||||
!.=0; do j=0 by 2 to 8; !.j=1; end /*assign 0,2,4,6,8 to a "true" value.*/
|
||||
/* [↑] assigns even digits to "true".*/
|
||||
numeric digits 1000 /*handle most huge numbers from the CL.*/
|
||||
parse arg x _ . /*get an argument from the command line*/
|
||||
if x=='' then call terr "no integer input (argument)."
|
||||
if _\=='' | arg()\==1 then call terr "too many arguments: " _ arg(2)
|
||||
if \datatype(x, 'N') then call terr "argument isn't numeric: " x
|
||||
if \datatype(x, 'W') then call terr "argument isn't an integer: " x
|
||||
y=abs(x)/1 /*in case X is negative or malformed,*/
|
||||
/* [↑] remainder of neg # might be -1.*/
|
||||
/*malformed #s: 007 9.0 4.8e1 .21e2 */
|
||||
call tell 'remainder method (oddness)'
|
||||
if y//2 then say x 'is odd'
|
||||
else say x 'is even'
|
||||
/* [↑] uses division to get remainder.*/
|
||||
/* [↑] uses division to get remainder.*/
|
||||
|
||||
/*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒*/
|
||||
call sayr 'rightmost digit using BIF (not evenness)'
|
||||
_=right(y,1)
|
||||
if pos(_,86420)==0 then say x 'is odd'
|
||||
else say x 'is even'
|
||||
/* [↑] uses 2 BIF (built─in functions)*/
|
||||
call tell 'rightmost digit using BIF (not evenness)'
|
||||
_=right(y, 1)
|
||||
if pos(_, 86420)==0 then say x 'is odd'
|
||||
else say x 'is even'
|
||||
/* [↑] uses 2 BIF (built─in functions)*/
|
||||
|
||||
/*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒*/
|
||||
call sayr 'rightmost digit using BIF (evenness)'
|
||||
_=right(y,1)
|
||||
if pos(_,86420)\==0 then say x 'is even'
|
||||
else say x 'is odd'
|
||||
/* [↑] uses 2 BIF (built─in functions)*/
|
||||
call tell 'rightmost digit using BIF (evenness)'
|
||||
_=right(y, 1)
|
||||
if pos(_, 86420)\==0 then say x 'is even'
|
||||
else say x 'is odd'
|
||||
/* [↑] uses 2 BIF (built─in functions)*/
|
||||
|
||||
/*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒*/
|
||||
call sayr 'even rightmost digit using array (evenness)'
|
||||
_=right(y,1)
|
||||
call tell 'even rightmost digit using array (evenness)'
|
||||
_=right(y, 1)
|
||||
if !._ then say x 'is even'
|
||||
else say x 'is odd'
|
||||
/* [↑] uses a BIF (built─in function).*/
|
||||
/* [↑] uses a BIF (built─in function).*/
|
||||
|
||||
/*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒*/
|
||||
call sayr 'remainder of division via function invoke (evenness)'
|
||||
call tell 'remainder of division via function invoke (evenness)'
|
||||
if even(y) then say x 'is even'
|
||||
else say x 'is odd'
|
||||
/* [↑] uses (even) function invocation*/
|
||||
/* [↑] uses (even) function invocation*/
|
||||
|
||||
/*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒*/
|
||||
call sayr 'remainder of division via function invoke (oddness)'
|
||||
call tell 'remainder of division via function invoke (oddness)'
|
||||
if odd(y) then say x 'is odd'
|
||||
else say x 'is even'
|
||||
/* [↑] uses (odd) function invocation*/
|
||||
/* [↑] uses (odd) function invocation*/
|
||||
|
||||
/*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒*/
|
||||
call sayr 'rightmost digit using BIF (not oddness)'
|
||||
_=right(y,1)
|
||||
if pos(_,13579)==0 then say x 'is even'
|
||||
else say x 'is odd'
|
||||
/* [↑] uses 2 BIF (built─in functions)*/
|
||||
call tell 'rightmost digit using BIF (not oddness)'
|
||||
_=right(y, 1)
|
||||
if pos(_, 13579)==0 then say x 'is even'
|
||||
else say x 'is odd'
|
||||
/* [↑] uses 2 BIF (built─in functions)*/
|
||||
|
||||
/*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒*/
|
||||
call sayr 'rightmost (binary) bit (oddness)'
|
||||
if right(x2b(d2x(y)),1) then say x 'is odd'
|
||||
else say x 'is even'
|
||||
/* [↑] requires extra numeric digits. */
|
||||
call tell 'rightmost (binary) bit (oddness)'
|
||||
if right(x2b(d2x(y)), 1) then say x 'is odd'
|
||||
else say x 'is even'
|
||||
/* [↑] requires extra numeric digits. */
|
||||
|
||||
/*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒*/
|
||||
call sayr 'parse statement using BIF (not oddness)'
|
||||
parse var y '' -1 _ /*obtain last decimal digit of the Y #.*/
|
||||
if pos(_,02468)==0 then say x 'is odd'
|
||||
else say x 'is even'
|
||||
/* [↑] uses a BIF (built─in function).*/
|
||||
call tell 'parse statement using BIF (not oddness)'
|
||||
parse var y '' -1 _ /*obtain last decimal digit of the Y #.*/
|
||||
if pos(_, 02468)==0 then say x 'is odd'
|
||||
else say x 'is even'
|
||||
/* [↑] uses a BIF (built─in function).*/
|
||||
|
||||
/*▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒*/
|
||||
call sayr 'parse statement using array (evenness)'
|
||||
parse var y '' -1 _ /*obtain last decimal digit of the Y #.*/
|
||||
call tell 'parse statement using array (evenness)'
|
||||
parse var y '' -1 _ /*obtain last decimal digit of the Y #.*/
|
||||
if !._ then say x 'is even'
|
||||
else say x 'is odd'
|
||||
/* [↑] this is the fastest algorithm. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────one─liner subroutines─────────────────────*/
|
||||
even: return \ ( arg(1)//2 ) /*actual algorithm used can be varied. */
|
||||
even: return arg(1)//2 == 0 /* " " " " " " */
|
||||
even: parse arg '' -1 _; return !._ /* " " " " " " */
|
||||
odd: return arg(1)//2 /* " " " " " " */
|
||||
sayr: say; say center('using the' arg(1), 79, '═'); return
|
||||
terr: say; say '***error!***'; say; say arg(1); say; exit 13
|
||||
/* [↑] this is the fastest algorithm. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
even: return \( arg(1)//2 ) /*returns "evenness" of arg, version 1.*/
|
||||
even: return arg(1)//2==0 /* " " " " " 2.*/
|
||||
even: parse arg '' -1 _; return !._ /* " " " " " 3.*/
|
||||
/*last version shown is the fastest. */
|
||||
odd: return arg(1)//2 /*returns "oddness" of the argument. */
|
||||
tell: say; say center('using the' arg(1), 79, "═"); return
|
||||
terr: say; say '***error***'; say; say arg(1); say; exit 13
|
||||
|
|
|
|||
5
Task/Even-or-odd/SETL/even-or-odd.setl
Normal file
5
Task/Even-or-odd/SETL/even-or-odd.setl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
xs := {1..10};
|
||||
evens := {x in xs | even( x )};
|
||||
odds := {x in xs | odd( x )};
|
||||
print( evens );
|
||||
print( odds );
|
||||
10
Task/Even-or-odd/SNOBOL4/even-or-odd.sno
Normal file
10
Task/Even-or-odd/SNOBOL4/even-or-odd.sno
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
DEFINE('even(n)') :(even_end)
|
||||
even even = (EQ(REMDR(n, 2), 0) 'even', 'odd') :(RETURN)
|
||||
even_end
|
||||
|
||||
OUTPUT = "-2 is " even(-2)
|
||||
OUTPUT = "-1 is " even(-1)
|
||||
OUTPUT = "0 is " even(0)
|
||||
OUTPUT = "1 is " even(1)
|
||||
OUTPUT = "2 is " even(2)
|
||||
END
|
||||
13
Task/Even-or-odd/SQL/even-or-odd.sql
Normal file
13
Task/Even-or-odd/SQL/even-or-odd.sql
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
-- Setup a table with some integers
|
||||
create table ints(int integer);
|
||||
insert into ints values (-1);
|
||||
insert into ints values (0);
|
||||
insert into ints values (1);
|
||||
insert into ints values (2);
|
||||
|
||||
-- Are they even or odd?
|
||||
select
|
||||
int,
|
||||
case mod(int, 2) when 0 then 'Even' else 'Odd' end
|
||||
from
|
||||
ints;
|
||||
15
Task/Even-or-odd/Standard-ML/even-or-odd.ml
Normal file
15
Task/Even-or-odd/Standard-ML/even-or-odd.ml
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
fun even n =
|
||||
n mod 2 = 0;
|
||||
|
||||
fun odd n =
|
||||
n mod 2 <> 0;
|
||||
|
||||
(* bitwise and *)
|
||||
|
||||
type werd = Word.word;
|
||||
|
||||
fun evenbitw(w: werd) =
|
||||
Word.andb(w, 0w2) = 0w0;
|
||||
|
||||
fun oddbitw(w: werd) =
|
||||
Word.andb(w, 0w2) <> 0w0;
|
||||
6
Task/Even-or-odd/ZX-Spectrum-Basic/even-or-odd.zx
Normal file
6
Task/Even-or-odd/ZX-Spectrum-Basic/even-or-odd.zx
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
10 FOR n=-3 TO 4: GO SUB 30: NEXT n
|
||||
20 STOP
|
||||
30 LET odd=FN m(n,2)
|
||||
40 PRINT n;" is ";("Even" AND odd=0)+("Odd" AND odd=1)
|
||||
50 RETURN
|
||||
60 DEF FN m(a,b)=a-INT (a/b)*b
|
||||
Loading…
Add table
Add a link
Reference in a new issue