How to tell if a number is a multiple of n

  • Thread starter Thread starter Couperin
  • Start date Start date
  • Tags Tags
    Multiple
Click For Summary

Discussion Overview

The discussion revolves around writing a program in Python to determine if numbers in a list are multiples of a given number, specifically focusing on multiples of 2, 3, and 5. Participants share code snippets, seek help with syntax, and discuss the logic behind their implementations.

Discussion Character

  • Technical explanation
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant seeks advice on how to check for multiples of 2 and delete those numbers from a list.
  • Another participant suggests using the modulo operator (%) for this task.
  • A code snippet is provided that uses list comprehension to filter out multiples of 2.
  • A participant shares a more verbose approach using a for loop to achieve the same result, emphasizing readability.
  • Further, a participant describes their program that not only checks for multiples but also allows for user-defined ranges and multiple divisors.
  • The program's output is shared, demonstrating how many numbers remain after filtering out multiples of 2, 3, and 5, with a specific result of 1600 remaining numbers.

Areas of Agreement / Disagreement

Participants generally agree on the use of the modulo operator and the effectiveness of list comprehension versus traditional loops. However, there is no consensus on the correctness of the final answer to the math problem posed, as it is based on individual calculations and interpretations.

Contextual Notes

Some participants express uncertainty about the correctness of their results, particularly regarding the math problem related to multiples of 2, 3, and 5. There are also discussions about syntax and programming practices, indicating varying levels of experience among participants.

Who May Find This Useful

Individuals interested in Python programming, particularly those learning about list manipulation and mathematical operations in coding, may find this discussion beneficial.

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 ·
Replies
10
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 36 ·
2
Replies
36
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 26 ·
Replies
26
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K