Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking odd/even numbers and changing outputs on number size

Tags:

python

numbers

I have a couple of problems to solve for an assignment, and am a bit stuck. The question is to write a program that gets the user to input an odd number (check it's odd), then print an upside down pyramid of stars based on the size of the input.

For example, if you enter 5, it comes up with

*****
 ***
  *

My problem is therefore two-fold.

1) How do I check if it's even or odd? I tried if number/2 == int in the hope that it might do something, and the internet tells me to do if number%2==0, but that doesn't work.

2) How do I change the asterisks in the middle of each line?

Any help with either problem is greatly appreciated.

like image 522
keirbtre Avatar asked Nov 29 '12 23:11

keirbtre


People also ask

How do you determine if an output is even or odd?

If a number is evenly divisible by 2 with no remainder, then it is even. You can calculate the remainder with the modulo operator % like this num % 2 == 0 . If a number divided by 2 leaves a remainder of 1, then the number is odd. You can check for this using num % 2 == 1 .

What are the rules for identifying odd and even numbers?

To identify a number as odd or even, we will look at its end number. If the number ends in a 0, 2, 4, 6, or 8, then it is even. If the number ends in a 1, 3, 5, 7, or 9, then it is odd. For example, the number 456 is an even number because it ends in a 6.

How do you create a program to check if a number is even or odd in Python?

num = int (input (“Enter any number to test whether it is odd or even: “) if (num % 2) == 0: print (“The number is even”) else: print (“The provided number is odd”) Output: Enter any number to test whether it is odd or even: 887 887 is odd. The program above only accepts integers as input.

How do you change odd numbers to even numbers?

An odd number can only be formed by the sum of an odd and even number (odd + even = odd, or even + odd = odd). An even number can only be formed by multiplication in three ways: even·odd, odd·even, and even·even.


4 Answers

Giving you the complete answer would have no point at all since this is homework, so here are a few pointers :

Even or Odd:

number % 2 == 0

definitely is a very good way to find whether your number is even.

In case you do not know %, this does modulo which is here the remainder of the division of number by 2. http://en.wikipedia.org/wiki/Modulo_operation

Printing the pyramid:

First advice: In order to print *****, you can do print "*" * 5.

Second advice: In order to center the asterisks, you need to find out how many spaces to write before the asterisks. Then you can print a bunch of spaces and asterisks with print " "*1 + "*"*3

like image 82
Julien Vivenot Avatar answered Oct 17 '22 00:10

Julien Vivenot


The modulo 2 solutions with %2 is good, but that requires a division and a subtraction. Because computers use binary arithmetic, a much more efficient solution is:

# This first solution does not produce a Boolean value. 
is_odd_if_zero = value & 1

# or

is_odd = (value & 1) == 1

# or

is_even = (value & 1) == 0
like image 31
Bill H Avatar answered Oct 17 '22 02:10

Bill H


A few of the solutions here reference the time taken for various "is even" operations, specifically n % 2 vs n & 1, without systematically checking how this varies with the size of n, which turns out to be predictive of speed.

The short answer is that if you're using reasonably sized numbers, normally < 1e9, it doesn't make much difference. If you're using larger numbers then you probably want to be using the bitwise operator.

Here's a plot to demonstrate what's going on (with Python 3.7.3, under Linux 5.1.2):

python3.7 benchmark

Basically as you hit "arbitrary precision" longs things get progressively slower for modulus, while remaining constant for the bitwise op. Also, note the 10**-7 multiplier on this, i.e. I can do ~30 million (small integer) checks per second.

Here's the same plot for Python 2.7.16:

python2.7 benchmark

which shows the optimisation that's gone into newer versions of Python.

I've only got these versions of Python on my machine, but could rerun for other versions of there's interest. There are 51 ns between 1 and 1e100 (evenly spaced on a log scale), for each point I do the equivalent of:

timeit('n % 2', f'n={n}', number=niter) 

where niter is calculated to make timeit take ~0.1 seconds, and this is repeated 5 times. The slightly awkward handling of n is to make sure we're not also benchmarking global variable lookup, which is slower than local variables. The mean of these values are used to draw the line, and the individual values are drawn as points.

like image 13
Sam Mason Avatar answered Oct 17 '22 01:10

Sam Mason


Simple but yet fast:

>>> def is_odd(a):
...     return bool(a - ((a>>1)<<1))
...
>>> print(is_odd(13))
True
>>> print(is_odd(12))
False
>>>

Or even simpler:

>>> def is_odd(a):
...   return bool(a & 1)
like image 9
Andrei Mironenko Avatar answered Oct 17 '22 01:10

Andrei Mironenko