1. ## 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

2. ## Re: parity of numbers

What? I don't know if you are trying to troll.

3. ## Re: parity of numbers

Can you post the program and the name of the programming language?

4. ## 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

5. ## 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.

6. ## Re: parity of numbers

sorry didnt understodd..

9=7^18??!?!?

later,

andrec

7. ## 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.

8. ## 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

9. ## Re: parity of numbers

Never ever trust a calculator over your own mind!

-Dan

10. ## Re: parity of numbers

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.

11. ## Re: parity of numbers

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.

12. ## Re: parity of numbers

cheers,

thanks to all (bigdecimal works fine)

later,
andrec

13. ## 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.

14. ## Re: parity of numbers

thks! i started using BigInteger....

later,

andrec

15. ## Re: parity of numbers

(2n+1) * (2n+1) with n is 1,...
= 4n(2) + 2(2n+1)(2) + 1