Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 My code is taking longer time to give result rajeshwin Unladen Swallow Posts: 2 Threads: 1 Joined: Feb 2019 Reputation: 0 Likes received: 0 #1 Feb-20-2019, 07:16 PM I am newbie to Python and tried to write a code to get smallest positive number that is evenly divisible by all of the numbers from 1 to 20. I used the script provided below. It is taking more time to get the result. How can I tweak this code to get the result sooner. ```a=1 while True: n=0 for y in range(1,21): if a%y==0: n=n+1 if n==20: print(a) break else: a=a+1 ``` woooee Minister of Silly Walks Posts: 437 Threads: 0 Joined: Feb 2018 Reputation: 32 Likes received: 36 #2 Feb-20-2019, 07:25 PM (This post was last modified: Feb-20-2019, 07:26 PM by woooee. Edited 2 times in total.) > that is evenly divisible by all of the numbers from 1 to 20 That would be 20 by definition. Look at your assignment again. You have read it wrong. rajeshwin Unladen Swallow Posts: 2 Threads: 1 Joined: Feb 2019 Reputation: 0 Likes received: 0 #3 Feb-20-2019, 07:30 PM 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? stranac A Beautiful Pony Posts: 254 Threads: 6 Joined: Nov 2016 Reputation: 12 Likes received: 108 #4 Feb-20-2019, 08:15 PM (This post was last modified: Feb-20-2019, 08:16 PM by stranac. Edited 1 time in total.) The problem here is that you're trying to brute-force the solution by testing every number. Since the solution is quite large, this will take some time. Project Euler problems (which this is, even if you found it elsewhere) often require some clever optimization or specific mathematical knowledge. In this case, you need to know how divisibility works, https://en.wikipedia.org/wiki/Divisibili...e_divisors should be of help. Once you do, you just need a single calculation to get a solution. ichabod801 Bunny Rabbit Posts: 3,623 Threads: 77 Joined: Sep 2016 Reputation: 210 Likes received: 1098 #5 Feb-20-2019, 08:18 PM Don't search for it, build it. Start with 1. 1 evenly divides into that. 2 does not, and there is no common divisor, so multiply by 2 to get 2. 3 does not evenly divide into 2, and there is no common divisor, so multiply by 3 to get 6. 4 does not divide into 6 evenly, but it has a common divisor of 2. So multiply 6 by (4 / 2) to get 12. Keep going up to 20. You would need to build a greatest common divisor function, (or use the one in the math or fractions module), but then you can build it piece by piece rather than searching for it, which should be faster. Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Post JSON from python to PHP don't give expected result pgagnebin 1 69 Sep-04-2019, 10:29 PM Last Post: micseydel Code scrape more than one time information Clnprof 5 118 Aug-26-2019, 09:26 AM Last Post: Clnprof Two lines of code at the same time? OTO1012 1 231 Mar-04-2019, 05:32 PM Last Post: ichabod801 Catching exceptions in embedded code no longer works on 3.7.2? FFMG 5 357 Feb-02-2019, 10:15 AM Last Post: Larz60+ Use a block of code only one time rlinux57 14 1,038 Sep-21-2018, 12:53 PM Last Post: rlinux57 python code (embedded in Redshift) to return result of the query Mel 0 502 Aug-24-2018, 06:12 PM Last Post: Mel How to generate more MP3 files at the same time in Amazon Polly using Python code? makiwara 2 969 Jul-02-2018, 08:43 PM Last Post: makiwara OSError: [Errno 22] Invalid argument - wasn't there last time I ran the code! meganhollie 2 2,010 Jun-11-2018, 06:01 PM Last Post: meganhollie how do i rewrite this code to give me 10 outputs BlackPimpernel 2 576 Mar-29-2018, 11:29 AM Last Post: BlackPimpernel Code issue with time remaining loop. Python3 deboerdn2000 11 3,190 May-04-2017, 04:53 PM Last Post: deboerdn2000

Forum Jump:

Users browsing this thread: 1 Guest(s)