Why this two codes give same output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
4 Answers
+ 6
If a number if divisible by 2, then that number raised to any integer exponent will be divisible by 2. If a number is not divisible by 2, then that number raised to any integer exponent will not be divisible by 2. So wether you're checking if that number is even, or that number raised to an exponent is even, you're checking the same thing. Example: 3 % 2 ==1; 3**2 % 2 = 1 4 % 2 == 0; 4**2 % 2 == 0
22nd Dec 2019, 3:26 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 4
That are pure maths k = r [2] Read it : k and r have the same remainder in the Euclidean division by 2 There is only two cases : - k is even : k = 0 [2] <=> k ** 2 = 0**2 [2] k ** 2 = 0 [2] - k is odd : k = 1 [2] <=> k ** 2 = 1**2 [2] k ** 2 = 1 [2]
22nd Dec 2019, 3:32 PM
Théophile
Théophile - avatar
+ 4
This has to do with math and the fact that the product of 2 numbers, whether the same or not, is even if any one, or both, are even
22nd Dec 2019, 5:12 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 3
The reason is because, anytime you raise a number to the power of 2 or any integer, the corresponding result will be: 1. Odd if the number u raised to the power of 2 is odd, and 2. Even if the number u raised to the power of 2 is even. That is, for x:(x%2==1) --> (x**2)%2==1 also for x:(x%2==0) --> (x**2)%2==0 so the result from your code will exclude all odd number from the operation.
23rd Dec 2019, 9:22 PM
Yusuf Shuaib Olalekan
Yusuf Shuaib Olalekan - avatar