3

So I was taking a break from my PvZ project in Scratch and trying to see what I could do in Scratch. So here's the thing: I sort of wanted to see if I could make a converter from say, sin(x°) to sin(xradians), which shouldn't be too hard, except here's the thing: sin(xradians) is sin(πx°/180) Now here's where problems arise, because here is a representation of π:

<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  extensions: ["tex2jax.js"],
  tex2jax: {inlineMath: [["$","$"]]},
  CommonHTML: {
    scale: 120
  }
});
</script>
<script type="text/javascript" async src=
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=
TeX-MML-AM_CHTML"></script>
$\sum_{k=0}^\infty\dfrac{50k-6}{2^k\binom{3k}k}$
Which in turn simplifies to
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
  extensions: ["tex2jax.js"],
  tex2jax: {inlineMath: [["$","$"]]},
  CommonHTML: {
    scale: 120
  }
});
</script>
<script type="text/javascript" async src=
"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=
TeX-MML-AM_CHTML"></script>
$\sum_{k=0}^\infty\dfrac{k!(2k)!(50k-6)}{2^k(3k)!}$



Now here's my problem:



How would I format this in Scratch, since there's no code block for Scratch that would allow me to do this. The only workaround that I could possibly see happening is if somehow, there was a way to create summation and products in Scratch. Even in my first question on this site where I was wondering what I would be able to do since there wasn't a way to represent summation, it just turns out that the best that I could do would be something like this:


Raising stuff to a power code:

[scratchblocks]
define pow(base)(power)
set [result v] to [1]
set [base v] to (base)
set [power v] to (round (power))
set [done v] to [0]
if <(power) < [0]> then
set [base v] to [((1) / (base))]
set [power v] to [-1(() *(round (power))]
end
repeat until <[done v] = [1]>
if <(([power v]) mod (2)) = [0]> then
set [result v] to (([result v]) * ([base v]))
change [power v] by (-1)
end
if <(([power v]) mod (2)) > [0]> then
set [done v] to [1]
end
if <<not <<[done v] = [1]>>>
set [base v] to (([base v]) * ([base v]))
set [power v] to (([power v]) / (2))
end
end
[/scratchblocks]

And even then, if I want to write summation, I have to write the exponentiation individually, which honestly is just extremely annoying in my opinion. In fact, if you do want to see how it's written out, here is the link to the Scratch project that I asked about in my first ever question.

And just so people don't get angry and say that it's annoyingly difficult to find, a quick way to find it is by clicking on the "Buy 10 (cursorSprite1" sprite (which this isn't even a typo, this is actually how the sprite name would be typed out)


In fact, the only way I can think of even attempting to write this out would be like how the summation is written out: Looks simple, but has to be extremely complex just for it to work in a simple way. There would have to be a lot of variables/messages going out at the same time, and that would cause a great deal of lag. And I'm not even needing that much precision (only a sum from k=0 to 13), so it seems that it would only be trying to get factorials to work being an honest pain in the butt.



My question



Is there a way that I could create factorials in Scratch, or if not, why and how?

2 Answers 2

2

TL;DR No, there is no built-in factorial function. But even if there were such a function, why would you need it here?

First of all, when you need π in an application (as in your case, to convert between degrees and radials), you are not going to calculate π. Instead, you just define a constant with the hardcoded value 3.141592653589793. This has three important advantages:

  1. Performance: hardcoding π is a lot more efficient than calculating π.
  2. Accuracy: rounding errors will accumulate during your calculation, given the limited accuracy of the floating-point numbers used in almost every programming language, including Scratch.
  3. Simplicity: in application code, this calculation is basically just clutter.

Even if you would insist on having π calculated, it makes more sense to use the built-in arctan function, for the same three reasons: performance, accuracy, simplicity.

pi=4*arctan(1)

If you insist on doing the calculation using only very basic math operations, this wiki article suggests to use the Leibniz formula:

https://en.scratch-wiki.info/wiki/Calculating_Pi

Of course, it makes total sense to write your own calculation, but only out of mathematical curiosity or as a coding exercise. Please keep in mind that n! will overflow for n greater than 170. Consequently, this will overflow for k > 56:

naive implementation

Whereas this will take much longer to overflow.

efficient implementation

You can even combine this with taking the power of 2.

combined with power

In pseudo code:

denominator = 1
a = 6 * k
b = k
do k times
    denominator = denominator * (a / b)
    a = a - 2
    b = b - 1
Sign up to request clarification or add additional context in comments.

1 Comment

So in other words, if I did want to calculate π, I should use 4*arctan(1) since according to the formula, arctan(1)=π/4?
1

Here's one way to calculate factorials in Scratch.

Think about how factorials are really calculated. You start at n and go down by one until you hit one, each time multiplying the your new number (eg: 3!=3x2x1)

This can be replicated in Scratch quite easily:

Scratchblocks format:

[scratchblocks]
when green flag clicked
ask [enter value for n] and wait
return=(answer)!
say (join (join (answer) [!=]) (return))
stop [all v]
[/scratchblocks]
 
[scratchblocks]
define return=(n)!
set [index v] to (n)
set [return v] to [1]
repeat ((n))
set [return v] to (([return]) * ([index]))
change [index v] by (-1)
end
[/scratchblocks]

https://scratch.mit.edu/projects/939563834/

4 Comments

Well, this is fascinating! I'll edit in the [scratchblocks] for you. Edit: when the pending edits queue is not full lol
actually here's the scratchblocks format if you would want to edit this in
Thanks! Is there anywhere where I could learn [scratchblocks]?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.