How to tell if a number is a multiple of n

  • Thread starter Thread starter Couperin
  • Start date Start date
  • Tags Tags
    Multiple
AI Thread Summary
The discussion focuses on writing a Python program to identify and delete numbers that are multiples of 2 from a list. Participants suggest using the modulo operator (%) and provide examples of list comprehensions and for loops to achieve this. One user shares a complete program that allows for the deletion of numbers divisible by any specified integer, enhancing its functionality for various mathematical riddles. The program successfully processes a range of numbers from 1 to 6000, demonstrating the ability to delete multiples of 2, 3, and 5, ultimately confirming that 1600 numbers remain that are not multiples of these values. The conversation highlights the collaborative effort to refine coding skills and solve mathematical problems through programming.
Couperin
Messages
59
Reaction score
0
I want to write a program that scans a list of numbers and checks if any of the numbers are multiples of 2, and then delete those numbers

Any ideas how to do this?

I use Python.
 
Technology news on Phys.org
Use the modulo operator (%).
 
newList = [ x for x in oldList if x % 2 != 0 ]

- Warren
 
Thank you.
This is what I have so far:

Code:
biglist = []

for i in range (1,6001):
    biglist.append(i)

This puts the numbers 1 to 6000 in a list as you know.
The modulo operator will definitely come in handy. My problem is with syntax (I'm a newbie). How would I write something to the effect of:

Code:
If (any number in the list) % 2 > 0:
   delete (number in the list)

Thanks for your help.
 
chroot said:
newList = [ x for x in oldList if x % 2 != 0 ]

- Warren

Thank you, I didn't see this. I'll see where I get.
 
Couperin:

The verbose C-derived way would be to use a for loop:

Code:
newList = []

for x in oldList:
  if x % 2 != 0:
    newList.append(x)

The list comprehension I gave above performs the same operation in a more efficient and more easily-readable way.

- Warren
 
Thank you very much chroot. You've allowed me to complete my program.

I got the idea when I was trying to solve the following maths problem that was once set in an Oxbridge interview for prospective maths students:

Of the numbers 1, 2, 3, ..., 6000, how many are not multiples of 2, 3 or 5?

I gave the question a go and after a bit of work (more than was necessary) with pen and paper, I got the answer 1600. But I didn't know whether or not this was the answer, and I couldn't find the answer on the net so I decided to write this program.

I've adapted the code so that you can crack similar riddles with any range of numbers (so long as they have a difference of 1, though I'm going to see if I can program something that allows you to work with different kinds of arithmetic progressions), and as many 'multiples' as you want.

Here's the code in case anyone's interested:

Code:
# riddle cracker, by Ben H, except for def inti

biglist = []
#
def inti(prompt):
    while 1:                                # loop infinitely
        x = raw_input('> ') # request a string
        try:
            new_x = int(x)                  # convert the input to a floater
            break                           # the conversion was successfull: break out of the loop
        except ValueError:                  # this block is accessed if the integer conversion fails
            print 'Please enter an integer. Not a string.'
    return new_x    
print "Riddle cracker, by Ben H"
#
def start():
    print "List of natural numbers (difference 1) from:"
    num1 = inti('')
    print "to:"
    num2 = inti('')

    for i in range (num1,num2 + 1):
        biglist.append(i)

    print "Numbers in list:"
    print len(biglist)

    deletenums()
#
def deletenums():
    print "Delete numbers divisible by:"
    diviz = inti('')
    print diviz
    for x in biglist:
        if x % diviz ==0:
            biglist.remove(x)
    print len(biglist), " numbers remain."

    choices()
#
def choices():
    print "Options:"
    print "(1) Delete more numbers?"
    print "(2) Start over again?"
    print "(3) Exit?"

    choice = inti('')

    if choice == 1:
        deletenums()
    elif choice == 2:
        del biglist[:]
        start()
    else:
        print "See you again..."

start()

And in case anyone's interested, here's the output when the above riddle is inputed:

Code:
Riddle cracker, by Ben H
List of natural numbers (difference 1) from:
> 1
to:
> 6000
Numbers in list:
6000
Delete numbers divisible by:
> 2
2
3000  numbers remain.
Options:
(1) Delete more numbers?
(2) Start over again?
(3) Exit?
> 1
Delete numbers divisible by:
> 3
3
2000  numbers remain.
Options:
(1) Delete more numbers?
(2) Start over again?
(3) Exit?
> 1
Delete numbers divisible by:
> 5
5
1600  numbers remain.
Options:
(1) Delete more numbers?
(2) Start over again?
(3) Exit?
> 1
Delete numbers divisible by:
> 5
5
1600  numbers remain.
Options:
(1) Delete more numbers?
(2) Start over again?
(3) Exit?
> 3
See you again...

1600? Nice one :smile:
 

Similar threads

Replies
10
Views
3K
Replies
3
Views
1K
Replies
36
Views
338
Replies
16
Views
2K
Replies
26
Views
3K
Replies
5
Views
2K
Replies
1
Views
1K
Replies
1
Views
2K
Back
Top