Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Nth Term
- ShieldOS
- Scratcher
30 posts
Nth Term
I have recently tried to make a Custom Block that tells you the Nth term of a sequence although I can't manage to get it right, any suggestions on how to go about this? Or better still a link to a project containing it. Thx! ~ShieldOS
- ShieldOS
- Scratcher
30 posts
Nth Term
Thats what I thought but what if lets say N=12-N Then it doesn't work, anyway thx for trying to help!
- DanloVorje
- Scratcher
100+ posts
Nth Term
Is there a specific sequence you want to use, or are you trying to make a custom block that can have a formula input?
Here are two examples…
For some sequences, there is a formula for the nth term that only requires n as input. Triangular numbers:
Others require a recursive formula. Here's a script that returns the nth term of the Fibonacci sequence:
*Note that saying “nth term” is a bit messy when referring to the Fibonacci sequence, since some people start with F(0)=0 and others start with F(0)=1. The above uses F(0)=1.
I hope this helps. I'll be watching this thread in case you have more questions/clarifications.
–Danlo
Here are two examples…
For some sequences, there is a formula for the nth term that only requires n as input. Triangular numbers:
set [F(n) v] to (((n) * ((n) + (1))) / (2))
Others require a recursive formula. Here's a script that returns the nth term of the Fibonacci sequence:
ask [choose n] and wait
set [Fn v] to [1]
set [Fn-2 v] to [0]
set [Fn-1 v] to [1]
repeat ((answer)-(1))
set [Fn v] to ((Fn-2) + (Fn-1))
set [Fn-2 v] to (Fn-1)
set [Fn-1 v] to (Fn)
end
say (Fn)
*Note that saying “nth term” is a bit messy when referring to the Fibonacci sequence, since some people start with F(0)=0 and others start with F(0)=1. The above uses F(0)=1.
I hope this helps. I'll be watching this thread in case you have more questions/clarifications.
–Danlo
What I'm working on right now: Isle of Dolor
- ShieldOS
- Scratcher
30 posts
Nth Term
What I am trying to make is something that once told the formula through custom blocks will be able to make the Nth term I matter what the formula was
- DanloVorje
- Scratcher
100+ posts
Nth Term
What I am trying to make is something that once told the formula through custom blocks will be able to make the Nth term I matter what the formula was
Hmm… so you want to be able to enter a formula as a string? You'd need to start out with a custom block that parses the formula, which could be tough, but I'm sure it's entirely doable. I threw together a basic parser in the project below. Take a look at it to get some ideas.
http://scratch.mit.edu/projects/23037552/
If you have questions about how it works that aren't answered in the in-project notations, just ask.
–Danlo
What I'm working on right now: Isle of Dolor
- DanloVorje
- Scratcher
100+ posts
Nth Term
: P
Just saw that @griffpatch already made something far more sophisticated than my parser.
Good luck with your project!
–Danlo
Just saw that @griffpatch already made something far more sophisticated than my parser.
Good luck with your project!
–Danlo
What I'm working on right now: Isle of Dolor
- Discussion Forums
- » Help with Scripts
- » Nth Term