Talk:Even or odd: Difference between revisions

From Rosetta Code
Content added Content deleted
(Silly recursive solution)
 
m (→‎definition of even & odd numbers: added highlights and more whitespace.)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
== Silly recursive solution ==
== Silly recursive solution ==

I made this for Java, but maybe it's not ''that'' bad in languages with recursion optimizations:
I made this for Java, but maybe it's not ''that'' bad in languages with recursion optimizations:
<lang java>public static boolean isEven(int i){
<lang java>public static boolean isEven(int i){
Line 7: Line 6:
return !isEven(i - 1);
return !isEven(i - 1);
}</lang> --[[User:Mwn3d|Mwn3d]] 18:47, 1 December 2011 (UTC)
}</lang> --[[User:Mwn3d|Mwn3d]] 18:47, 1 December 2011 (UTC)
: I'd be quite surprised if anything optimized that very much, as it depends on applying an operation to the result of each recursive call which is usually a sign that the compiler ''won't'' be able to figure things out. A human could split that into a pair of functions that are the logical inverse of each other (i.e., isEven and notIsEven) which could then admit optimization, but I suspect that sort of analysis isn't done by compilers (on the grounds that it would so rarely lead to real optimizations in practice). –[[User:Dkf|Donal Fellows]] 09:48, 2 December 2011 (UTC)

== definition of even & odd numbers ==
This may be frivolous/trivial, but since the task is to determine if an integer is odd or even, a simple definition of an odd/even number could be in order.

The definition (below) defines an ODD NUMBER, and goes further than limiting the definition to an ODD INTEGER:


From MathWorld:

An &nbsp; ''odd number'' &nbsp; is an integer of the form &nbsp; '''n=2k+1''' &nbsp; where &nbsp; '''k''' &nbsp; is an integer.

Integers which are &nbsp; ''not odd'' &nbsp; are called &nbsp; ''even''.


The above definition has the advantage that it isn't dependent upon its (say, internal binary) representation (or any base, for that matter), although that is one method to determine evenness/oddness.

I've also seen the definition that an odd number is an integer, that when divided by two, the absolute value of the remainder is (positive) unity.
<br>For an even number ... the remainder is zero.

<br>[[User:Gerard Schildberger|Gerard Schildberger]] 00:46, 16 March 2012 (UTC)

== PL/I ==

This very short solution works only for Bin Fixed(n,0) variables.
Well i is Bin Fixed(15) by default.

consider also Dec Fixed, Float, and Pic variables
where mod(v,2)=0 -> v is even (if it's an integer)
:--[[User:Walterpachl|Walterpachl]] 06:21, 4 August 2012 (UTC)

Latest revision as of 15:46, 17 April 2016

Silly recursive solution

I made this for Java, but maybe it's not that bad in languages with recursion optimizations: <lang java>public static boolean isEven(int i){ if(i == 0) return true; if(i < 0) i = -i; return !isEven(i - 1); }</lang> --Mwn3d 18:47, 1 December 2011 (UTC)

I'd be quite surprised if anything optimized that very much, as it depends on applying an operation to the result of each recursive call which is usually a sign that the compiler won't be able to figure things out. A human could split that into a pair of functions that are the logical inverse of each other (i.e., isEven and notIsEven) which could then admit optimization, but I suspect that sort of analysis isn't done by compilers (on the grounds that it would so rarely lead to real optimizations in practice). –Donal Fellows 09:48, 2 December 2011 (UTC)

definition of even & odd numbers

This may be frivolous/trivial, but since the task is to determine if an integer is odd or even, a simple definition of an odd/even number could be in order.

The definition (below) defines an ODD NUMBER, and goes further than limiting the definition to an ODD INTEGER:


From MathWorld:

An   odd number   is an integer of the form   n=2k+1   where   k   is an integer.

Integers which are   not odd   are called   even.


The above definition has the advantage that it isn't dependent upon its (say, internal binary) representation (or any base, for that matter), although that is one method to determine evenness/oddness.

I've also seen the definition that an odd number is an integer, that when divided by two, the absolute value of the remainder is (positive) unity.
For an even number ... the remainder is zero.


Gerard Schildberger 00:46, 16 March 2012 (UTC)

PL/I

This very short solution works only for Bin Fixed(n,0) variables. Well i is Bin Fixed(15) by default.

consider also Dec Fixed, Float, and Pic variables where mod(v,2)=0 -> v is even (if it's an integer)

--Walterpachl 06:21, 4 August 2012 (UTC)