Project euler problem 10

For questions about problems on the Project Euler web site. No spoilers. Please include the question number in the subject line of your post.

Project euler problem 10

Postby Pandora-Box » Sat Dec 13, 2014 6:53 pm

Code: Select all
import time
start = time.clock()
def euler10():
    sum=10
    for num in range(3,100,2):
        for div in range(3,int(round(num**0.5))+1,2):
            print(num,div)
            if num % div == 0:
               sum=sum+0
            else:
                sum=sum+num
                print(sum)
                return
 
euler10()
endt=time.clock()   
print ((endt - start),"seconds")


I am trying to solve the problem 10 but I don't know what is the problem in my code :oops: ( Which is more dangerous)
It gives me mostly output as
if the number is greater than 6
Code: Select all
7 3
17



Any pointers will be helpful

I was trying to replicate this in another way
Code: Select all
def is_prime(x):
    print(x)
    if x<2:
        return False
    if x==2:
        return True
    if x%2==0:
        return False
    for i in range(3,int(round(x**0.5))+1,2):
        print(x,i)   
        if x%i==0:
           
            return False
    else:
        return True
   
s=2
for i in range(3,11,2):
   
    if is_prime(i)==True:
        s+=i

print(s)
Pandora-Box
 
Posts: 10
Joined: Sat Apr 05, 2014 8:44 am

Re: Project euler problem 10

Postby Mekire » Sun Dec 14, 2014 3:25 am

To generate all the primes below a given number you are going to need to look up sieves.

Don't try to just write one function called "problem 10".

First implement this:
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

After that the problem is simple.
-Mek
New Users, Read This
  • Use code tags when posting code.
  • Include any errors with your post (in code tags).
  • Describe your problem; not your chosen solution.
  • Make examples the minimum length to demonstrate your issue.
User avatar
Mekire
 
Posts: 1711
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona


Return to Project Euler

Who is online

Users browsing this forum: No registered users and 2 guests