Even or odd: Difference between revisions
Content deleted Content added
Add ed example |
|||
(12 intermediate revisions by 9 users not shown) | |||
Line 1,004:
7 is odd
10 is even</pre>
==={{header|SmallBASIC}}===
<syntaxhighlight lang="qbasic">
FOR i = 1 to 10
IF(i % 2 == 0)
PRINT i; " is even"
ELSE
PRINT i; " is odd"
ENDIF
NEXT
</syntaxhighlight>
==={{header|TI-83 BASIC}}===
Line 1,253 ⟶ 1,264:
Outputs E if even, O if odd.
=={{header|Binary Lambda Calculus}}==
In lambda calculus, the oddness of a given church numeral n can be computed as n applications of <code>not</code> to <code>false</code>: <code>\n. n (\b\x\y. b y x) (\x\y.y)</code>, which in BLC is
<pre>00 01 01 10 0000000101111010110 000010</pre>
To compute the evenness, one need only replace <code>false</code> by <code>true</code>, i.e. replace the final 0 bit by 10.
=={{header|BQN}}==
Line 1,766 ⟶ 1,784:
Modulo:
<syntaxhighlight lang="delphi">var isOdd := (i mod 2)=1;</syntaxhighlight>
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
a = 13
if a mod 2 = 0
print a & " is even"
else
print a & " is odd"
.
</syntaxhighlight>
=={{header|Ed}}==
Checks the least significant digit of a line.
<syntaxhighlight lang="sed">
H
g/(.*[13579])$/s//\1 is odd/
v/odd/s/(.*)/\1 is even/
,p
Q
</syntaxhighlight>
{{out}}
<pre>$ cat even-odd.ed | ed -GlEs even-odd.input
Newline appended
1 is odd
3 is odd
5 is odd
9 is odd
23 is odd
9 is odd
0 is even
83 is odd
3 is odd
54 is even
6 is even
2 is even
7 is odd
433 is odd
78 is even</pre>
=={{header|EDSAC order code}}==
Line 2,184 ⟶ 2,244:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Even_or_odd}}
'''Solutions'''
'''Case 1.''' Intrinsic expressions:
[[File:Fōrmulæ - Even or odd 01.png]]
[[File:Fōrmulæ - Even or odd 02.png]]
'''Case 2.''' Using the Divides and DoesNotDivide expressions:
[[File:Fōrmulæ - Even or odd 03.png]]
[[File:Fōrmulæ - Even or odd 04.png]]
'''Case 3.''' Using modular congruences
[[File:Fōrmulæ - Even or odd 05.png]]
[[File:Fōrmulæ - Even or odd 06.png]]
'''Case 4.''' Using bitwise operations
[[File:Fōrmulæ - Even or odd 07.png]]
[[File:Fōrmulæ - Even or odd 08.png]]
'''Case 5.''' Using IsRational
[[File:Fōrmulæ - Even or odd 09.png]]
[[File:Fōrmulæ - Even or odd 10.png]]
=={{header|GAP}}==
Line 2,333 ⟶ 2,422:
return n%2 = 0
end</syntaxhighlight>
=={{header|Insitux}}==
Exactly the same as [[Even_or_odd#Clojure|Clojure]], these are built-in predicates.
<syntaxhighlight lang="insitux">(if (even? some-var) (do-even-stuff))
(if (odd? some-var) (do-odd-stuff))</syntaxhighlight>
=={{header|J}}==
Line 3,282 ⟶ 3,376:
<syntaxhighlight lang="parigp">odd(n)=bitand(n,1);</syntaxhighlight>
PARI can use the same method as [[#C|C]] for testing individual words. For multiprecision integers (t_INT), use <code>mpodd</code>. If the number is known to be nonzero, <code>mod2</code> is (insignificantly) faster.
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
function IsOddRemainder(x: integer) := x mod 2 <> 0;
function IsEvenRemainder(x: integer) := x mod 2 = 0;
function IsOddBitwise(x: integer) := x and 1 = 1;
function IsEvenBitwise(x: integer) := x and 1 = 0;
begin
var x := 3;
Println(x.IsEven,x.IsOdd); // Standard Predicates
Println(IsEvenRemainder(x),IsOddRemainder(x));
Println(IsEvenBitwise(x),IsOddBitwise(x));
end.
</syntaxhighlight>
=={{header|Pascal}}==
Line 4,144 ⟶ 4,257:
return true
}</syntaxhighlight>
=={{header|Swift}}==
<syntaxhighlight lang="swift">
// Swift has Int.isMultiple(of:Int) -> Bool
var isEven: (_:Int) -> Bool = {$0.isMultiple(of: 2)}
</syntaxhighlight>
=={{header|Symsyn}}==
Line 4,327 ⟶ 4,446:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
var isEven1 = Fn.new { |i| i & 1 == 0 }
|