Even or odd: Difference between revisions

1,212 bytes added ,  2 months ago
m
(Added Z80 shift instructions)
 
(14 intermediate revisions by 10 users not shown)
Line 382:
 
=={{header|Agda}}==
<syntaxhighlight lang="agda">even : ℕ → Bool
module EvenOrOdd where
 
open import Data.Bool using (Bool; false; true)
open import Data.Nat using (ℕ; zero; suc)
 
even : ℕ → Bool
odd : ℕ → Bool
 
Line 389 ⟶ 395:
 
odd zero = false
odd (suc n) = even n</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Aime}}==
Line 886 ⟶ 893:
 
==={{header|Minimal BASIC}}===
{{works with|IS-BASIC}}
<syntaxhighlight lang="gwbasic">10 REM Even or odd
20 PRINT "Enter an integer number";
Line 1,245 ⟶ 1,253:
 
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,758 ⟶ 1,773:
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|EDSAC order code}}==
Line 2,176 ⟶ 2,201:
 
=={{header|Fōrmulæ}}==
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Even_or_odd}}
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
'''Solutions'''
In '''[https://formulae.org/?example=Even_or_odd this]''' page you can see the program(s) related to this task and their results.
 
'''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,325 ⟶ 2,379:
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 2,346 ⟶ 2,405:
0 1 1 1</syntaxhighlight>
Note: as a general rule, the simplest expressions in J should be preferred over more complex approaches.
 
=={{header|Jakt}}==
<syntaxhighlight lang="jakt">
fn is_even<T>(anon n: T) -> bool => 0 == (n & 1)
 
fn is_odd<T>(anon n: T) -> bool => 0 != (n & 1)
 
fn main() {
for i in 0..11 {
println("{} {} {}", i, is_even(i), is_odd(i))
}
}
</syntaxhighlight>
 
=={{header|Java}}==
Line 4,123 ⟶ 4,195:
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,150 ⟶ 4,228:
49:1,0
</pre>
 
=={{header|TI-57}}==
This routine returns the remainder of the division by 2 of the number in the display register. It is therefore a kind of is_odd(x) function.
Lbl 9
/
2
-
CE
Int
=
×
2
=
INV SBR
 
=={{header|TUSCRIPT}}==
Line 4,292 ⟶ 4,384:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var isEven1 = Fn.new { |i| i & 1 == 0 }
56

edits