Skip to main content
summarized the challenge; edited tags
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Optimizing Python code for Project Euler #5 (LCM of all numbers from 1 to 20)

This is my solution for the 5th problem5th problem of Project Euler, which asks for the least common multiples of Project Eulerthe numbers from 1 to 20 (inclusive). Is there any way to improve the while loop conditional instead of using the sum?

table = range(1, 21)
result = 1
pf = 2
while sum(table) > len(table):
   flag = False
   for x, y in enumerate(table):
      if y % pf == 0:
         table[x] = y/pf 
         flag = True
   if flag:
      result *= pf
   else:
      pf += 1
print result

Optimizing Python code for Project Euler #5

This is my solution for the 5th problem of Project Euler. Is there any way to improve the while loop conditional instead of using the sum?

table = range(1, 21)
result = 1
pf = 2
while sum(table) > len(table):
   flag = False
   for x, y in enumerate(table):
      if y % pf == 0:
         table[x] = y/pf 
         flag = True
   if flag:
      result *= pf
   else:
      pf += 1
print result

Optimizing Python code for Project Euler #5 (LCM of all numbers from 1 to 20)

This is my solution for the 5th problem of Project Euler, which asks for the least common multiples of the numbers from 1 to 20 (inclusive). Is there any way to improve the while loop conditional instead of using the sum?

table = range(1, 21)
result = 1
pf = 2
while sum(table) > len(table):
   flag = False
   for x, y in enumerate(table):
      if y % pf == 0:
         table[x] = y/pf 
         flag = True
   if flag:
      result *= pf
   else:
      pf += 1
print result
added 2 characters in body
Source Link
Veritas
  • 245
  • 2
  • 9

This is my solution for the 5th problem of Project Euler. Is there any way to improve the while loop conditional instead of using the sum?

table = range(1, 21)
result = 1
pf = 2
while sum(table) > len(table):
   flag = False
   for x, y in enumerate(table):
      if y % pf == 0:
         table[x] = y/pf 
         flag = True
   if flag:
      result *= pf
   else:
      pf += 1
print result

This is my solution for the 5th problem of Project Euler. Is there any way to improve the while loop conditional instead of using the sum?

table = range(1,21)
result = 1
pf = 2
while sum(table) > len(table):
   flag = False
   for x,y in enumerate(table):
      if y % pf == 0:
         table[x] = y/pf 
         flag = True
   if flag:
      result *= pf
   else:
      pf += 1
print result

This is my solution for the 5th problem of Project Euler. Is there any way to improve the while loop conditional instead of using the sum?

table = range(1, 21)
result = 1
pf = 2
while sum(table) > len(table):
   flag = False
   for x, y in enumerate(table):
      if y % pf == 0:
         table[x] = y/pf 
         flag = True
   if flag:
      result *= pf
   else:
      pf += 1
print result
Source Link
Veritas
  • 245
  • 2
  • 9

Optimizing Python code for Project Euler #5

This is my solution for the 5th problem of Project Euler. Is there any way to improve the while loop conditional instead of using the sum?

table = range(1,21)
result = 1
pf = 2
while sum(table) > len(table):
   flag = False
   for x,y in enumerate(table):
      if y % pf == 0:
         table[x] = y/pf 
         flag = True
   if flag:
      result *= pf
   else:
      pf += 1
print result