Re: project euler #10: optimization with primes

2009-04-04 Thread Stewart Gordon
Tiago Carvalho wrote: I've done this a while ago. But if I remember correctly you only need to verify 2, 3, and after that all primes will be forms of 6k+1 or 6k-1. This made my code a lot faster at the time. Don't know if this is faster in this case since you have to store values in an arra

Re: project euler #10: optimization with primes

2009-04-04 Thread Stewart Gordon
Michael P. wrote: int sum = 2; You have a serious bug here. Can you see what it is? One thing that may help a little is that you need only loop from 2 to sqrt(n) in the isPrime function, otherwise you end up testing for factors > sqrt(n) twice. A factor over sqrt(n) must have a corr

Re: project euler #10: optimization with primes

2009-04-01 Thread Tiago Carvalho
"Michael P." wrote in message news:gqvksi$1r...@digitalmars.com... Spacen Jasset Wrote: Michael P. wrote: > Hey, I've started to do some of the problems on Project Euler to > practice my programming skills. I'm doing #10 right now, and I think > I've got a working solution. It's just that

Re: project euler #10: optimization with primes

2009-04-01 Thread Don
Michael P. wrote: Spacen Jasset Wrote: Michael P. wrote: Hey, I've started to do some of the problems on Project Euler to practice my programming skills. I'm doing #10 right now, and I think I've got a working solution. It's just that it's way too slow. Here's the link: http://projecteuler.n

Re: project euler #10: optimization with primes

2009-04-01 Thread Michael P.
Spacen Jasset Wrote: > Michael P. wrote: > > Hey, I've started to do some of the problems on Project Euler to practice > > my programming skills. I'm doing #10 right now, and I think I've got a > > working solution. It's just that it's way too slow. > > Here's the link: > > http://projecteuler.n

Re: project euler #10: optimization with primes

2009-04-01 Thread bearophile
Robert Fraser Wrote: > Yeah that's shorter (vertically; it's almost as long in characters), This is simpler and faster (runs in 2.19 seconds), and gives the same result, xtakeWhile isn't required because xprimes already stops nicely when the given max N is reached: import d.func, d.primes, d.st

Re: project euler #10: optimization with primes

2009-04-01 Thread Daniel Keep
Robert Fraser wrote: > bearophile wrote: >> import d.func, d.primes, d.string; >> void main() { >> const int N = 1_000_000_000; >> putr( sum(xtakeWhile((int i){ return i < N;}, xprimes(N))) ); >> } > > Yeah that's shorter (vertically; it's almost as long in characters), but > how much li

Re: project euler #10: optimization with primes

2009-04-01 Thread Robert Fraser
bearophile wrote: import d.func, d.primes, d.string; void main() { const int N = 1_000_000_000; putr( sum(xtakeWhile((int i){ return i < N;}, xprimes(N))) ); } Yeah that's shorter (vertically; it's almost as long in characters), but how much lisp do you have to smoke to understand it?

Re: project euler #10: optimization with primes

2009-04-01 Thread Spacen Jasset
Michael P. wrote: Hey, I've started to do some of the problems on Project Euler to practice my programming skills. I'm doing #10 right now, and I think I've got a working solution. It's just that it's way too slow. Here's the link: http://projecteuler.net/index.php?section=problems&id=10 The co

Re: project euler #10: optimization with primes

2009-03-31 Thread bearophile
Michael P. Wrote: > But for 2 million, it takes quite a while, and an FAQ on the page said most > problems are made with being run within a minute. Mine was gonna quite a bit > longer. This will not give you much practice in programming, but if you need primes quickly you may use my xprimes: h

Re: project euler #10: optimization with primes

2009-03-31 Thread Jarrett Billingsley
On Tue, Mar 31, 2009 at 9:11 PM, Michael P. wrote: > Hey, I've started to do some of the problems on Project Euler to practice my > programming skills. I'm doing #10 right now, and I think I've got a working > solution. It's just that it's way too slow. > Here's the link: > http://projecteuler.n

project euler #10: optimization with primes

2009-03-31 Thread Michael P.
Hey, I've started to do some of the problems on Project Euler to practice my programming skills. I'm doing #10 right now, and I think I've got a working solution. It's just that it's way too slow. Here's the link: http://projecteuler.net/index.php?section=problems&id=10 The code works fine with s