Python Forum
I code a program to solve puzzle but i can't make it more dynamic.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I code a program to solve puzzle but i can't make it more dynamic.
#1
# find the first number that is divided by 1 to 9 with 0 remainder
for i in range (10000):
	x = i % 3
	y = i % 4
	z = i % 7
	a = i % 5
	b = i % 6
	c = i % 8
	d = i % 9
	if x==y==z==a==b==c==d==0:
	 	print (i)
As it mention, i wanted to find numbers which are divided by 1 to 9 numbers with 0 remainders.
I tried to make it dynamic like instead of 1 to 9 what if i search numbers which are divided by 1 to 20 numbers.
I have already repeated lots of code line for 1 to 9 numbers and i don't want to do that for more numbers.
I tried to make a list of 1 to 20 numbers and with loop tried to iterate but failed.
Thanks in advance.
Professional Dentist(32years) fell in love with Python during COVID-19 Lockdown.

"Nothing can stop you from learning new things except your own will"

Reply
#2
Maybe run two loops: one for the number to check, one for the division test.

max_check = 10000
dividends = [3, 4, 7, 5, 6, 8, 9]
for n in range(1,max_check):
    for dividend in dividends:
        if n % dividend:
            break
    else:
        print(f"{n} is evenly divided by {dividends}")
        break
else:
    print(f"No numbers found evenly divisible by {dividends} that are less than {max_check}")
Reply
#3
There is no reason to test every value. If the result must be divisible for all numbers 1 through 9, the number will be divisible by 9. There is no reason to test 17 or 33 or 121.
def find_number(first, last):
    """Find first number that is divisible by all numbers in range first..last"""
    num = last
    while True:
        for divisor in range(first, last):
            if num % divisor != 0: # Failed test.  Try next num
                num += last
                break # break out of for loop
        else: # Gets called if for loop reaches end
            print(num)
            break;  # Break out of while loop

find_number(5, 13)
Reply
#4
Great! Thank you so much.

The second nested 'for' loop with 'else' was the key that i missed.
And of course learned that i should use 0 as a boolean even i was working with arithmetic. It optimized code so well.
Thank you
Professional Dentist(32years) fell in love with Python during COVID-19 Lockdown.

"Nothing can stop you from learning new things except your own will"

Reply
#5
you can use any()
for number in range (1, 10000):
    if not any(number % divisor for divisor in range(3, 10)):
        print(number)
        break # break out of loop early
else:
    print('no such number found')
note that probably you need to find more elegant (from math perspective) solution, not trying to brute-force it
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Wow!
TIL any() function.
Thank you so much.
Professional Dentist(32years) fell in love with Python during COVID-19 Lockdown.

"Nothing can stop you from learning new things except your own will"

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Frog Puzzle Random To Custom MoreMoney 4 290 Mar-26-2024, 08:38 AM
Last Post: MoreMoney
  hi need help to make this code work correctly atulkul1985 5 701 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 626 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Cleaning my code to make it more efficient BSDevo 13 1,275 Sep-27-2023, 10:39 PM
Last Post: BSDevo
  Code challenge I need to solve for a Bootcamp ramonrocha 1 463 Sep-05-2023, 03:41 PM
Last Post: deanhystad
  how to make bot that sends instagram auto password reset code kraixx 2 1,282 Mar-04-2023, 09:59 PM
Last Post: jefsummers
  Make code non-blocking? Extra 0 1,099 Dec-03-2022, 10:07 PM
Last Post: Extra
  Make the code shorter quest 2 1,476 Mar-14-2022, 04:28 PM
Last Post: deanhystad
  Is it possible to make a program recognize how many clicks it has had on the monitor? jao 0 1,129 Feb-25-2022, 06:31 PM
Last Post: jao
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,751 Feb-21-2022, 10:52 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020