# parity of numbers

• Jan 15th 2013, 02:25 PM
andrec
parity of numbers
Hey!

Odd * Odd numbers are always odd correct??
Can you check the following numbers?

7^19,
9^17,
9^18,
9^19 ? I have a program that says they are all divisible by 2.

Isnt that peculiar?

andrec
• Jan 15th 2013, 02:35 PM
jakncoke
Re: parity of numbers
What? I don't know if you are trying to troll.
• Jan 15th 2013, 02:38 PM
emakarov
Re: parity of numbers
Can you post the program and the name of the programming language?
• Jan 15th 2013, 02:42 PM
andrec
Re: parity of numbers
public class logic {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
double eSquare=0;
int evenPlus1=0;
for (int even = 0; even < 10; even+=2) {

for (int n=0;n < 20; n+=1) {
evenPlus1 =even+1;
eSquare = Math.pow(evenPlus1,n); //(even+1)^n
if ((eSquare % 2) ==0)
System.out.println(evenPlus1+"^"+n+" "+eSquare+" is divisible by 2");
}
}
}
}

its java. :)

later,

andrec
• Jan 15th 2013, 02:44 PM
Deveno
Re: parity of numbers
since 2 is a prime and:

$7^{19} = 7(7^{18})$

either 2 divides 7 (clearly false) or 2 divides $7^{18}$.

rinse and repeat.
• Jan 15th 2013, 02:47 PM
andrec
Re: parity of numbers
sorry didnt understodd.. :(

9=7^18??!?!?

later,

andrec
• Jan 15th 2013, 03:05 PM
emakarov
Re: parity of numbers
This effect is probably due to overflow. Double numbers are represented as $m\cdot2^e$ where m is the mantissa and e is the exponent. Positive integers can only be represented precisely if they fit in the mantissa. According to this document, type double allots 53 bits to the mantissa. Interestingly, $\log_2\left(7^{19}\right)=19\log_27\approx53.3$, so $7^{19}$ requires 54 bits to be represented precisely. Therefore, as a double, it will be represented as $m\cdot 2^1$ for some m, which is an even number.
• Jan 15th 2013, 03:08 PM
andrec
Re: parity of numbers
yep, i just tested it on windows calculator and it works there

thanks.. :)

suprisingly the c code similiar to the java code has the same error

andrec
• Jan 15th 2013, 04:15 PM
topsquark
Re: parity of numbers
Never ever trust a calculator over your own mind!

-Dan
• Jan 15th 2013, 04:32 PM
emakarov
Re: parity of numbers
Quote:

Originally Posted by andrec
suprisingly the c code similiar to the java code has the same error

This must be because both C and Java implement the IEEE 754 standard for binary floating point numbers. This tutorial says, "This data type [float] should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead." Also, Java long type has 64 bits, so it can precisely represent positive integers up to 2^63 - 1, which is more than double can.
• Jan 15th 2013, 04:42 PM
Plato
Re: parity of numbers
Quote:

Originally Posted by topsquark
Never ever trust a calculator over your own mind!

That is the whole of this opinion piece.
It may be fifteen years old, but it is still true today.
• Jan 16th 2013, 07:27 AM
andrec
Re: parity of numbers
cheers,

thanks to all (bigdecimal works fine)

later,
andrec
• Jan 16th 2013, 10:55 AM
johng
Re: parity of numbers
Hi Andrec,

If you are going to use Java for big integers, use BigInteger, not BigDecimal. Here's code for computing 7^19 mod 2:
BigInteger seven = BigInteger.valueOf(7);
BigInteger nineteen = BigInteger.valueOf(19);
BigInteger two = BigInteger.valueOf(2);
BigInteger odd = seven.modPow(nineteen, two);
System.out.println(odd.toString());
As expected, output is 1.
A more interesting example is computing the last three digits of 151^192:
BigInteger onefiftyone = BigInteger.valueOf(151);
BigInteger oneninetytwo = BigInteger.valueOf(192);
BigInteger thousand = BigInteger.valueOf(1000);
BigInteger digits = onefiftyone.modPow(oneninetytwo, thousand);
System.out.println(digits.toString());
Output is 801
You can do this example by hand, but I wouldn't want to.
• Jan 16th 2013, 11:11 AM
andrec
Re: parity of numbers
thks! i started using BigInteger....

later,

andrec
• Jan 23rd 2013, 11:47 PM
cobemuathu
Re: parity of numbers
(2n+1) * (2n+1) with n is 1,...
= 4n(2) + 2(2n+1)(2) + 1
:)