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.
#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


Messages In This Thread
RE: I code a program to solve puzzle but i can't make it more dynamic. - by deanhystad - Apr-16-2020, 09:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Frog Puzzle Random To Custom MoreMoney 4 586 Mar-26-2024, 08:38 AM
Last Post: MoreMoney
  hi need help to make this code work correctly atulkul1985 5 892 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 750 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Cleaning my code to make it more efficient BSDevo 13 1,543 Sep-27-2023, 10:39 PM
Last Post: BSDevo
  Code challenge I need to solve for a Bootcamp ramonrocha 1 531 Sep-05-2023, 03:41 PM
Last Post: deanhystad
  how to make bot that sends instagram auto password reset code kraixx 2 1,472 Mar-04-2023, 09:59 PM
Last Post: jefsummers
  Make code non-blocking? Extra 0 1,186 Dec-03-2022, 10:07 PM
Last Post: Extra
  Make the code shorter quest 2 1,568 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,187 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,863 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