Discuss Scratch

IcyCoder
Scratcher
1000+ posts

AMC #11? - The comeback

The math challenge for advanced topics is back!

What is the challenge?
To make a program that graphs 2D equations (most of them will be in slope-intercept form but not all) with a pen.
The winner (assuming that >1 people finish the challenge) is the one that program is the fastest at rendering all of the given equations.

Rules:
You must use the provided template by cloning it.
You cannot remove anything from init but you can add to init otherwise that you cannot change the green flag clicked code.
Hacked blocks are allowed.
Turbo mode may be requested to be used during judging.
The program will be run once.
Everything will be judged on my Chromebook.
Failing to do any required equation is the time it takes to fail plus 0.1s
Any extra challenges will not be based on speed but accuracy. For every bonus, you successfully do it will remove 0.1s from your total time.
The winner can nominate the next person to do a challenge or they can nominate themselves.

Equations:
y=2x+10
y=x
y=0.45x-1
y= -x+2 (spaces are on purpose)
y= 2x+2*x
x-2=y+0

Bonus Equations:
y=x^2
y=x^2 + 1.2x -0.2
y= (2x)(1.2x)
2y=1x-2

Good Luck!

Studio

Template

Last edited by IcyCoder (Nov. 16, 2017 20:14:18)


Because JS is the future (echos) future future futur futu fut fu f
TheUltimatum
Scratcher
1000+ posts

AMC #11? - The comeback

Your base project never uses the last equation. Move the change x by block to the beginning of the loop instead of the end.

Last edited by TheUltimatum (Nov. 14, 2017 04:59:41)

gtoal
Scratcher
1000+ posts

AMC #11? - The comeback

I wish I had time to try this - I'ld like to try some ideas I had about autoscaling and finding the zeroes of a function to align the graph. (eg if the function has two zeroes, you want them both to be within the x range of the display but not right up against the edges of the displayed graph).

You should support a Sigma function ( http://www.gtoal.com/languages/algol60/TESTS/jensen.a60.html ) or some other way of evaluating convergent infinite series and not just rely on Scratch's built-in sin etc.

I don't suppose you guys have ever heard of “GPS” by any chance? ( http://www.gtoal.com/languages/algol60/TESTS/gps.a60.html )

G
IcyCoder
Scratcher
1000+ posts

AMC #11? - The comeback

TheUltimatum wrote:

Your base project never uses the last equation. Move the change x by block to the beginning of the loop instead of the end.
Thanks…

gtoal wrote:

I wish I had time to try this - I'ld like to try some ideas I had about autoscaling and finding the zeroes of a function to align the graph. (eg if the function has two zeroes, you want them both to be within the x range of the display but not right up against the edges of the displayed graph).

You should support a Sigma function ( http://www.gtoal.com/languages/algol60/TESTS/jensen.a60.html ) or some other way of evaluating convergent infinite series and not just rely on Scratch's built-in sin etc.

I don't suppose you guys have ever heard of “GPS” by any chance? ( http://www.gtoal.com/languages/algol60/TESTS/gps.a60.html )

G

Not sure…

Because JS is the future (echos) future future futur futu fut fu f
PutneyCat
Scratcher
500+ posts

AMC #11? - The comeback

IcyCoder
Scratcher
1000+ posts

AMC #11? - The comeback

PutneyCat wrote:

My cheap and cheerful entry.
Nice entry!

Because JS is the future (echos) future future futur futu fut fu f
gtoal
Scratcher
1000+ posts

AMC #11? - The comeback

btw parsing and generating internal code is likely to be fast compared to 480 evaluations of f(x).

So if the equation is linear, why not just calculate the slope and 0-intercept, and draw it as a single line rather than evaluating it for every x :-)

G
TheUltimatum
Scratcher
1000+ posts

AMC #11? - The comeback

gtoal wrote:

btw parsing and generating internal code is likely to be fast compared to 480 evaluations of f(x).

So if the equation is linear, why not just calculate the slope and 0-intercept, and draw it as a single line rather than evaluating it for every x :-)

G
That would be great. Another optimization that's possible is storing a table of slopes and then checking if you've already calculated the slope for that function or not.
PutneyCat
Scratcher
500+ posts

AMC #11? - The comeback

gtoal wrote:

btw parsing and generating internal code is likely to be fast compared to 480 evaluations of f(x).

So if the equation is linear, why not just calculate the slope and 0-intercept, and draw it as a single line rather than evaluating it for every x :-)

G

Good point. I did think of drawing just a single line but not all the equations in the challenge are straight lines (see bonus ones) and for a general solver (which originally aiming at) straight lines might be exception rather than rule. Actually it doesn't seem much slower to do 240 calculations (I change x value by 2) - because of the “clear” the project takes about 20 secs even without any added code, and only about the same with my added code.
IcyCoder
Scratcher
1000+ posts

AMC #11? - The comeback

bump

Because JS is the future (echos) future future futur futu fut fu f
A-KouZ1
Scratcher
100+ posts

AMC #11? - The comeback

Hey, this is good challenge!
Insha Allah, I'll try my best

Somewhat, in my thought for balance of penformace. Is to make algorithm as simple as it can.
And I'll just let Y is dependent, meaning formula result is always based on right part of equation.

For example, wrote:

a) for “2y=1x-2”, I let this simplified to “y= (x-2)/2”.
b) for “x-2=y+0”, I let this simplified through boring algebraic manipulation:
“x-2=y”; “x=2+y”; “0=2+y-x”; “-y=2-x”; final “y=x-2”.
c) for “y=x^2”, I let this simplified as “y=xx” or y=x^2

However, implementing this function (algebra manipulator) seems an insane work.
Unless for simple equation like above. Otherwise, see in below:

For example, wrote:

a) for “y=x/y”, don't ya know what? how to do this? let's see:
“y=x/y”; multiply each by y: “y^2=x”; final, square root each: “y=√x”.
b) for “y= (x-1)^2”, simplify: “(x-1)^2” also equal to “(x-1)(x-1)” so “y= (x-1)(x-1)”;
then multiply (x-1) each other then we get: “y=xx-x+1” or “y=x^2-x+1”.

handling x^n which n is fraction, couldn't been simplified.


Also, In my thought, a way to parse input is simply put every element to equation list

For example wrote:

a) “y = x+1” in list as “+y, =, +x, +1”
b) “y-(x+1) = 1: in list as ”+y, -(, +x, +1, ), =, +1"
Which easier to be proccessed

Last edited by A-KouZ1 (Nov. 19, 2017 14:45:13)


Somehow, I just need some of time and encourage…
PutneyCat
Scratcher
500+ posts

AMC #11? - The comeback

A-KouZ1 wrote:

Also, In my thought, a way to parse input is simply put every element to equation list
That's more or less what I did - turn the equation into a list of values and operators, substitute an actual value for each x, then perform the operations in the right order.
IcyCoder
Scratcher
1000+ posts

AMC #11? - The comeback

A-KouZ1 wrote:

Hey, this is good challenge!
Insha Allah, I'll try my best

Thanks for thinking that Good luck

Because JS is the future (echos) future future futur futu fut fu f
IcyCoder
Scratcher
1000+ posts

AMC #11? - The comeback

bump/anyone want more bonus equations?

Because JS is the future (echos) future future futur futu fut fu f
A-KouZ1
Scratcher
100+ posts

AMC #11? - The comeback

IcyCoder wrote:

bump/anyone want more bonus equations?
Sure, but it should be something optional.

I wrote few of equations for testing or just an experiment how does it will be parsed:
x^2+y^2=625; which simplified as “y=√(625-x^2)”
2(y+16)=2/x; which simplified as “y= (1/x)-16”
2x=2y+(y-1)/(x+1)
I don't think this is would be simple, so just let them away if resisted.

PutneyCat wrote:

That's more or less what I did - turn the equation into a list of values and operators, substitute an actual value for each x, then perform the operations in the right order.
I tried your project, and it is good…

Operation performing of what I seen in your project workaround as,
“Find the operator, let perform operation of both numbers on back and fourth of operator.”
Urm, I sure there's such a better method, I don't had a rule of calculation order there, though.
But, whatever that's a good try.


EDIT:
Method of parsing (for the sake of simplicity algebraic manipulation )
Simple addition operator
2 + 3 - 2 + 3 2 + - 3
/| |\ \ | | |\ \ /| | | |
+ 2 + + 3 - 2 + + 3 + 2 + - 3
With multiplication
- 2 * - 3 - 3 * 2 3 * 2
| | | | | | | | |\ /| | |\
- 2 * - 3 - 3 * + 2 + 3 * + 2
Little explanation, an output is consist with three kind of lists, which called: sign, value, and operator of each element.
	With variable parse:
y+1-2+2x
Should be parsed as
[s,v,o,s,v,o,s,v,o,s,v,o,s,v]
[+,y,+,+,1,+,-,2,+,+,2,*,+,x]
Separated form:
s[+,+,-,+,+]
v[y,1,2,2,x]
o[,+,+,+,*,]

Other example:
2x(x+1)=2y(y+1)/y-1
Parsed as
[s,v,o,s,v,o,s, v ,o,s,v,o,s,v,o,s, v ,o,s,v,o,s,v]
[+,2,*,+,x,*,+,(+x+1),=,+,2,*,+,y,*,+,(+y+1),/,+,y,+,-,1]
Separated form
s[+,+, + ,+,+, + ,+,-]
v[2,x,(+x+1),2,y,(+y+1),y,1]
o[,*,* =,*,* /,+,]
In third element of first example, which is “-2” denoted as negative number, but also it did addition with previous element. So, let's say now then “+1 + (-2)”. (there's no 'operator subtraction, but addition with negative value.)

In second example, pointed to such of (+x+1) or (+y+1) which treated as like single value,
which will be unpacked later when the equation is being simplified.

Last edited by A-KouZ1 (Nov. 21, 2017 22:59:46)


Somehow, I just need some of time and encourage…
IcyCoder
Scratcher
1000+ posts

AMC #11? - The comeback

A-KouZ1 wrote:

IcyCoder wrote:

bump/anyone want more bonus equations?
Sure, but it should be something optional.
of course, all bonus equations are optional…

As for the other things you said I have no clue what you are talking about

Because JS is the future (echos) future future futur futu fut fu f
IcyCoder
Scratcher
1000+ posts

AMC #11? - The comeback

Results on monday… unless someone wants an extension

Because JS is the future (echos) future future futur futu fut fu f
TheUltimatum
Scratcher
1000+ posts

AMC #11? - The comeback

IcyCoder wrote:

Results on monday… unless someone wants an extension
Sorry I've been gone for a while. I'll probably post mine after the deadline.
IcyCoder
Scratcher
1000+ posts

AMC #11? - The comeback

TheUltimatum wrote:

IcyCoder wrote:

Results on monday… unless someone wants an extension
Sorry I've been gone for a while. I'll probably post mine after the deadline.
Don't worry that is a very soft due date… I can wait

Because JS is the future (echos) future future futur futu fut fu f
IcyCoder
Scratcher
1000+ posts

AMC #11? - The comeback

bump hard end date is on Saturday… if you want to join in or finish try doing it this week.

Because JS is the future (echos) future future futur futu fut fu f

Powered by DjangoBB