hey there! lately I had to code 2 out of the three problems I wrote in the subject, nothing out of this world anyway.
I think you probably have done them at least once in your life, and if so, did you write your own code from scratch or you just used someone else's algorithm?
Awesoken at
It just so happens I played around with some fibonacci code recently. Here's the EVALDRAW program I wrote for it:
() cls(0,0,0);
a = 0; b = 1; phi = (sqrt(5)+1)/2;
for(i=0;i<yres/10;i++)
{
moveto(200,i*10); printnum(a);
c = a; a += b; b = c;
My code (the 2nd and 3rd printnum's) are based directly off of some equations I found online.
Sorry, I'm not familiar with the other puzzles.
counting_pine at
8 Queens: Is that the problem where you have to put eight queens on a chess board, so that they're all in diferent ranks/files/diagonals?
I had a go at that many years ago in QB.
DEFINT A-Z
FOR A=1 TO 8
FOR B=1 TO 8
FOR C=1 TO 8
IF B=A THEN EXIT FOR
IF B+1=A THEN EXIT FOR
IF B=A+1 THEN EXIT FOR
FOR D=1 TO 8
IF C=A OR C=B THEN EXIT FOR
IF C+2=A OR C+1=B THEN EXIT FOR
IF C=A+2 OR C=B+1 THEN EXIT FOR
FOR E=1 TO 8
IF D=A OR D=B OR D=C THEN EXIT FOR
IF D+3=A OR D+2=B OR D+1=C THEN EXIT FOR
IF D=A+3 OR D=B+2 OR D=C+1 THEN EXIT FOR
FOR F=1 TO 8
IF E=A OR E=B OR E=C OR E=D THEN EXIT FOR
IF E+4=A OR E+3=B OR E+2=C OR E+1=D THEN EXIT FOR
IF E=A+4 OR E=B+3 OR E=C+2 OR E=D+1 THEN EXIT FOR
FOR G=1 TO 8
IF F=A OR F=B OR F=C OR F=D OR F=E THEN EXIT FOR
IF F+5=A OR F+4=B OR F+3=C OR F+2=D OR F+1=E THEN EXIT FOR
IF F=A+5 OR F=B+4 OR F=C+3 OR F=D+2 OR F=E+1 THEN EXIT FOR
FOR H=1 TO 8
IF G=A OR G=B OR G=C OR G=D OR G=E OR G=F THEN EXIT FOR
IF G+6=A OR G+5=B OR G+4=C OR G+3=D OR G+2=E OR G+1=F THEN EXIT FOR
IF G=A+6 OR G=B+5 OR G=C+4 OR G=D+3 OR G=E+2 OR G=F+1 THEN EXIT FOR
IF NOT (H=A OR H=B OR H=C OR H=D OR H=E OR H=F OR H=G) THEN
IF NOT (H+7=A OR H+6=B OR H+5=C OR H+4=D OR H+3=E OR H+2=F OR H+1=G) THEN
IF NOT (H=A+7 OR H=B+6 OR H=C+5 OR H=D+4 OR H=E+3 OR H=F+2 OR H=G+1) THEN
ENDIF
ENDIF
ENDIF
NEXT H:NEXT G:NEXT F:NEXT E:NEXT D:NEXT C:NEXT B:NEXT A
Towers of Hanoi: There's a website here which talks about it. It gives a nice, simple, recursive function for doing it, which should be easy to implement.
Awesoken at
Ah, so that's what the 8 queens problem is. I came up with a much shorter solution just now:
DEFINT A-Z: DIM q(8)
FOR i& = 0 TO 40319
FOR j = 0 TO 7: q(j) = j: NEXT: k& = i&
FOR j = 8 TO 1 STEP -1: SWAP q(j - 1), q(k& MOD j): k& = k& \ j: NEXT
a = 0: b = 0
FOR j = 7 TO 0 STEP -1
v = 2 ^ (q(j) + j): IF a AND v THEN EXIT FOR
a = a + v
v = 2 ^ (q(j) - j + 7): IF b AND v THEN EXIT FOR
b = b + v
NEXT
IF j < 0 THEN
c = c + 1: PRINT c; ":";
FOR j = 0 TO 7: PRINT STR$(q(j)); : NEXT: PRINT
END IF
NEXT
I leave it up to you to decipher that mess : P
counting_pine at
OK, OK, you win :P
By the way, I found another formula for the Fibonacci numbers. This one uses integer math and is something like O(log n). It's recursive, though, so I'm not sure how easy an EvalDraw implementation would be. I had a quick go in QB and managed to get it working, but currently my program is O(n and then some).
Well anyway, you can read about the formula here (PDF).
UPDATE:
Here's a recursive QB routine. It returns f1 as the nth fibonacci number, f2 as the n+1th fibonacci number
SUB fibonacci (n AS INTEGER, f1 AS DOUBLE, f2 AS DOUBLE)
DIM g1 AS DOUBLE, g2 AS DOUBLE
IF n THEN
CALL fibonacci(n \ 2, g1, g2)
IF n AND 1 THEN
f1 = g1 * g1 + g2 * g2
f2 = (2 * g1 + g2) * g2
ELSE
f1 = (2 * g2 - g1) * g1
f2 = g1 * g1 + g2 * g2
END IF
ELSE
f1 = 0: f2 = 1
END IF
END SUB
Ah, so that's what the 8 queens problem is. I came up with a much shorter solution just now:
DEFINT A-Z: DIM q(8)
FOR i& = 0 TO 40319
FOR j = 0 TO 7: q(j) = j: NEXT: k& = i&
FOR j = 8 TO 1 STEP -1: SWAP q(j - 1), q(k& MOD j): k& = k& \ j: NEXT
a = 0: b = 0
FOR j = 7 TO 0 STEP -1
v = 2 ^ (q(j) + j): IF a AND v THEN EXIT FOR
a = a + v
v = 2 ^ (q(j) - j + 7): IF b AND v THEN EXIT FOR
b = b + v
NEXT
IF j < 0 THEN
c = c + 1: PRINT c; ":";
FOR j = 0 TO 7: PRINT STR$(q(j)); : NEXT: PRINT
END IF
NEXT
I leave it up to you to decipher that mess : P
Now I see you write your own code for everything :D ... I thought there was no other way to solve the 8 queens problem until you did it jejejeje nice.
counting_pine at
Since the 8 Queens problem works so well graphically, I had a go at getting it to work in EvalDraw. The result is based partly on my QBASIC code, but I managed to get it to try various different values of board size (which was probably more trouble than it's worth). In any case, it's given me a chance to experiment with the new static variables feature a bit.
Nice code! I can see you have a deep understanding of Evaldraw. I have a few small suggestions:
* You can add a "break;" after the "valid = 0;" to exit the inner loop early.
* You can replace "u=floor(...);if(u>n-1)u=n-1;" with "u=min(floor(...),n-1);".
* I usually use {} instead of , to group statements. I use commas only when optimizing code size : )
Now that you're experienced, perhaps you have suggestions on how to improve Evaldraw? I'd listen : )
counting_pine at
Thanks for the tips:) I removed some of the brackets from my code to try and find a good balance between readability and post length. IMO with good indentation, the readability isn't too badly affected.
EvalDraw is really great program, the main suggestion I'd make is that I think it would be better if EvalDraw could give priority of the CPU to the interface part of the program. This would make it easier to edit the code, and also allow editing/saving/graceful exiting if it gets stuck in a loop (this happened occasionally while I was writing the Queens program:).
And, since you asked for it, couple of trivial things I thought I'd mention as well:)
The mouse is slower in EvalDraw than in Windows, although maybe that's deliberate, or hard to fix. Also, it would be helpful if there was an option for EvalDraw to display a pointer all the time.
Closing EvalDraw with the Cross button doesn't bring up the save prompt. (Also, this is a really small thing, but a Cancel button would be nice to have on there:)
This is probably a personal preference, but I'd be happier if typing/pasting in the code window could replace selected text.
I thought of a couple of things that might allow EvalDraw to be more efficient. Maybe they're not really feasible, or maybe you've already considered them, but thought I'd mention them anyway, see what you thought.
Would you get much speedup if you were able to use integers for some things? In some situations, double precision feels like a bit of an overkill.
Also, how hard would it be to get the compiler to detect parts of the function that it can precache in 2/3D modes? E.g. if a function uses sin(t), get EvalDraw to only calculate it when it changes t.
Awesoken at
I just updated EVALDRAW with the following fixes:
* Fixed mouse speed using a little reverse-engineering : )
* The mouse pointer is now visible in all graphing modes.
* Closing with the cross button now prompts a save dialog if necessary.
* Typing / pasting now replaces selected text.
I will save the following suggestions for another day/year:
* CPU priority / freeze protection
Currently, the general-purpose mode (0D / "()") already supports infinite loop protection. I plan to add this feature to the rest of the modes. The reason I haven't done it yet is because the other modes already make use of multithreading in a different way - for HyperThreading - and I haven't thought of the best way to combine these 2 features.
* support integers ...
Yeah, that would be wonderful. Unfortunately, implementing this would be a huge project - something I don't want to do for a while.
* precalculate constants based on known inputs. Ex: calculate sin(t) only when t changes.
A more practical solution would be extend the 1D / 2D / 3D graphing modes with additional initialization functions that are called once per frame when t changes, once per line when y or z changes, etc...
counting_pine at
Cool, thanks for making those changes:)
Compile/execute/edit priorities:
I think the keystroke compile / constant execution features work best when the processor can keep up, and the code editing doesn't suffer.
Since the priority thing is going to be difficult to implement, maybe an easier short term solution would be to add an option to temporarily disable keystroke compiling or to pause the code execution.
Sometimes, if code takes too long to execute, I tend to do this the quick&dirty way by putting a deliberate mistake in the code so it doesn't compile:)
(Another couple of small things I've just found:
Cancel acts like No in the Alt-F-X save prompt
EvalDraw doesn't update the filename when you save in a new file)
masterlee at
Another workaround is: edit in notepad and than copy&paste.
Awesoken at
I just updated EVALDRAW again (06/26/2005):
* Added option to compile on request (Ctrl+Enter) instead of every change.
* Fixed cancel button in save prompt while quitting.
* Fixed handling of current filename.
Another workaround is: edit in notepad and than copy&paste.
That takes all the fun out of it though. You might as well use a standard compiler if you're going to do that. : / Things will be more stable in a future version - so that compiling on every keystroke will be safe for all modes, not just "()" mode.
masterlee at
Awesoken said
Another workaround is: edit in notepad and than copy&paste.
That takes all the fun out of it though. You might as well use a standard compiler if you're going to do that. : / Things will be more stable in a future version - so that compiling on every keystroke will be safe for all modes, not just "()" mode.
Yep i know but i most times i do the basic work on notepad and then going for optimize in EVALDRAW. Where optimizing is most times for more colours then for speed.