1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to tell if a number is a multiple of n

  1. May 29, 2007 #1
    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.
     
  2. jcsd
  3. May 29, 2007 #2

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Use the modulo operator (%).
     
  4. May 29, 2007 #3

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    newList = [ x for x in oldList if x % 2 != 0 ]

    - Warren
     
  5. May 29, 2007 #4
    Thank you.
    This is what I have so far:

    Code (Text):
    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 (Text):
    If (any number in the list) % 2 > 0:
       delete (number in the list)
     
    Thanks for your help.
     
  6. May 29, 2007 #5
    Thank you, I didn't see this. I'll see where I get.
     
  7. May 29, 2007 #6

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Couperin:

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

    Code (Text):

    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
     
  8. May 29, 2007 #7
    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 (Text):
    # 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 (Text):
    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 :rofl:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: How to tell if a number is a multiple of n
  1. Number of digits in n! (Replies: 8)

  2. C++ power of n number (Replies: 13)

Loading...