PDA

View Full Version : Project Euler Problem 1



matthew
20-01-2009, 01:53
Has anyone heard of Project Euler (http://projecteuler.net/) before?

Earlier today I attempted to solve the first problem (http://projecteuler.net/index.php?section=problems&id=1) in thinBasic & I was successful. :D



uses "Console"

dim numbers, sums as long

dim timeTook as quad

console_cls

console_settitle ("Project Euler: Problem #01")

hirestimer_init

for numbers = 1 to 999

if not mod(numbers, 3) or not mod(numbers ,5) then
sums += numbers
endif

next

timeTook = hirestimer_get

console_printat ("Answer: " + sums, 1, 1)
console_printat ("Duration: " + format$(timeTook, 6), 1, 2)
console_waitkey

Petr Schreiber
20-01-2009, 12:25
Hi Matthew,

that is interesting challenge!
Now there is just remaining 227 problems to solve...

Regarding this problem - maybe some performance gain could be made by iterating through k=1..n, where we would sum 3*k and 5*k until some condition is met.

Thanks :)

matthew
20-01-2009, 13:32
Thanks for the comment, I've already started work on the Second problem. :)

ErosOlmi
20-01-2009, 13:37
matthew , thanks a lot for this effort. Very appreciated.

A little note on time measurement: I think starting time is missing. See example in hirestimer_init (http://www.thinbasic.com/public/products/thinBasic/help/html/hirestimer_init.htm)

Ciao
Eros

zak
21-01-2009, 11:12
thank you matthew, it is a big number of problems,
in the following site there are an experimental vb6 solutions for the first 55 problems, it may be of interest
http://obsolete-chaos.wikispaces.com/Project_Euler_0
but surely there are many ways to deal with the same problem

matthew
21-01-2009, 15:10
A little note on time measurement: I think starting time is missing. See example in hirestimer_init (http://www.thinbasic.com/public/products/thinBasic/help/html/hirestimer_init.htm)


When I originally wrote the program I looked at the hirestimer_init example, I assumed that hirestimer_init started the timer, so I could use hirestimer_get to tell me how much time had passed, instead of using hirestimer_get twice. Is the method used in the example more accurate?



thank you matthew, it is a big number of problems,
in the following site there are an experimental vb6 solutions for the first 55 problems, it may be of interest


Thanks for the link zak but I won't be clicking it just yet. :D

I'm going to try & solve as many of the problems myself before I start searching for solutions. :escribe:

Petr Schreiber
21-01-2009, 17:55
Hi matthew,

here is my solution for problem #2.
Could be optimized more, but as it takes 138 microseconds on my PC, the motivation to finish it well is ... low :P

I hope it is correct.
To not spoil surprise I attach it as script, so if you don't want to look, you don't have to

ErosOlmi
21-01-2009, 19:35
When I originally wrote the program I looked at the hirestimer_init example, I assumed that hirestimer_init started the timer, so I could use hirestimer_get to tell me how much time had passed, instead of using hirestimer_get twice. Is the method used in the example more accurate?


Hi matthew.

in reality there is no timer to start or to stop.
hirestimer_init check that the hardware of the computer supports the high resolution timer and return %TRUE or %FALSE

If hirestimer_init = %TRUE, you can use hirestimer_get to get the current timer when you need it.
Usually you should hirestimer_get just before the job to measure and hirestimer_get just after it.
Than compute the difference. Exactly like indicated in help example.

But your idea to have another way to measure it that is every hirestimer_get returns the difference between two calls is interesting. I will think about it.

Eros

Michael Clease
22-01-2009, 02:15
This is my solution for Problem 5.


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 number that is evenly divisible by all of the numbers from 1 to 20?

Couple of things

no point doing 1 - 10 again we know that is 2520
because 2520 is the smallest number that will work on 1-10 we increment by that number.

This is the brute force method not the clever way.

matthew
23-01-2009, 02:51
Ok, I've just finished Problem 2 & I've attached the solution.

As soon as I finished I took a look at your solution Petr, it never occurred to me to use the SWAP command. :P

Nice work on Problem 5 Mike, it gave the correct answer here.

Problem 3 involves Prime Numbers so it should be interesting. :eusadance:

ErosOlmi
23-01-2009, 08:57
matthew,

I've modified HiResTimer_Init and HiResTimer_Get in order to internally store last execution timer data.
Also developed HiResTimer_Delta that will be able to determine delta time between current time and last execution time of any of the HiResTimer... functions.

So in next thinBasic it will be possible to do something like:

'---...
HiResTimer_Init

'---Do something STEP 1
T1 = HiResTimer_Delta
'---Do something else STEP 2
T2 = HiResTimer_Delta
'---Do something else STEP 3
T3 = HiResTimer_Delta
'---...
T1, T2 and T3 will be the already calculated elapsed time in microseconds between successive calls (with a little overhead due to HiRes... functions calling itself).

Ciao
Eros

ErosOlmi
23-01-2009, 09:03
If you prefer, I can create a new subfosum dedicated to Euler challenge so you can create single posts for single Euler problems keeping things organized. Just le me know.

Ciao
Eros

zak
23-01-2009, 11:36
Hi
if i have to vote i vote for the Euler project to be a subsection of a more general section may be called "Math problems" or math ...
and in that general math section we will find links to what are available in the site for math:
such as Math Draw section (or Math Graphics):
1- FunctionPlotting:
http://community.thinbasic.com/index.php?topic=389.0
2- 3D surface function plotting :
http://community.thinbasic.com/index.php?topic=530.0
3- Fractals, Trees, Chaos
Big numbers section:
1- GMP library: free library for arbitrary precision arithmetic
http://community.thinbasic.com/index.php?topic=1410.0

and so on

matthew
02-02-2009, 13:57
@ Eros: Thanks for modifying the HiResTimer commands & starting the new sub-forum. :)