Discuss Scratch
- Discussion Forums
- » Advanced Topics
- » What are some programming problems like "Hello, world" and "FizzBuzz"?
- TheUltimatum
- Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Polyglot: Write a program that works in a language and also works in another language.
- badatprogrammingibe
- Scratcher
500+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
when green flag clickedI believe this is the shortest primality test (least amount of blocks) written in scratch.
repeat ((number) - (2))
change [iter v] by (1)
if <((number) mod (iter)) = [0]> then
say [comp]
stop [this script v]
end
end
say [prime]
Iter starts at 1, 13 blocks.
Last edited by badatprogrammingibe (Sept. 21, 2018 02:58:45)
- onlyNones
- Scratcher
61 posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Ok, added that it needs to printPolyglot: Write a program that works in a language and also works in another language.works in any language that supports interpreting or compiling an empty program.
Last edited by onlyNones (Sept. 25, 2018 12:25:14)
########################## E ^ ^ ^
| | v v |v| |||v | N | | ^ | ^
v | | | | v ||v v C | ^ | ^ | |
| | | | || v O ^ | | | | ^ |
| | v | || D | | | ^| | | |
| v v || E | | ^ || | | |
v vv D ##########################
- ShinigamiBlacky
- Scratcher
100+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
The shortest way to find whether a number is a prime needs only 5 Blocks
I cheated around needing to reset the itterator by using custom Blocks :D
I cheated around needing to reset the itterator by using custom Blocks :D
define Is (number1) prime? || counter (number2)
if <<(number1)=[1]>or<<not <[2] = (number1)>> and <[0] = ((number1) mod ((1) + (number2)))>>> then
say [NOT PRIME] for (2) secs
else
if <([sqrt v] of (number1)) > (number2)> then
Is (number1) prime? || counter ((1) + (number2)) :: custom
else
say [PRIME] for (2) secs
end
end
Last edited by ShinigamiBlacky (Sept. 25, 2018 18:00:55)
- ateesdalejr
- Scratcher
100+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
If you have iOS 12 and Shortcuts installed you can run my version of Fizz Buzz.
No idea about the security of nofile so… run at your own risk.
https://nofile.io/f/oTRRAyZCzsB/fizzbuzz.shortcut
…
- gp469-sensei
- Scratcher
15 posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Here's one with only 1 (or 2) block: The shortest way to find whether a number is a prime needs only 5 Blocks
I cheated around needing to reset the itterator by using custom Blocks :Ddefine Is (number1) prime? || counter (number2)
if <<(number1)=[1]>or<<not <[2] = (number1)>> and <[0] = ((number1) mod ((1) + (number2)))>>> then
say [NOT PRIME] for (2) secs
else
if <([sqrt v] of (number1)) > (number2)> then
Is (number1) prime? || counter ((1) + (number2)) :: custom
else
say [PRIME] for (2) secs
end
end
https://scratch.mit.edu/projects/241134479/
when green flag clicked
forever
add (letter (((<[data v] contains [p] ?> * (8)) + (<[data v] contains [c] ?> * (4))) + ((<(item (1 v) of [data v] :: list) = ((length of [data v] :: list) + (1))> * (2)) + <((item (1 v) of [data v] :: list) mod ((length of [data v] :: list) + (1))) = [0]>)) of [cppccccpppppppp]) to [data v]
end
- ShinigamiBlacky
- Scratcher
100+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
sure if you use a list as counter and output you can save blocks….
but you have to reset the list which means adding more 2 Blocks (which is still better than mine…)
and even this can made a lot better and even a bit smaller (only 3 Blocks and its resetting itself)
but you have to reset the list which means adding more 2 Blocks (which is still better than mine…)
and even this can made a lot better and even a bit smaller (only 3 Blocks and its resetting itself)
define Is (number1) prime?
delete (all v) of [data2 v]
repeat until <<([sqrt v] of (number1)) < ((2) + (length of [data2 v] :: list))> or <[0] = (item (last v) of [data2 v] :: list)>>
add ((number1) mod ((2) + (length of [data2 v] :: list))) to [data2 v]
end
<not <[0] = (item (last v) of [data2 v] :: list)>> // this is the output
Last edited by ShinigamiBlacky (Sept. 28, 2018 18:24:17)
- badatprogrammingibe
- Scratcher
500+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Smallest fibonacci:
define f(x) (y)
f (y)((x)+(y))
- onlyNones
- Scratcher
61 posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Counter:
Given input n, output all numbers from 1 to n.
This challenge introduces for loops.
Example:
Given input n, output all numbers from 1 to n.
This challenge introduces for loops.
Example:
Input: 5
Output:
1
2
3
4
5
########################## E ^ ^ ^
| | v v |v| |||v | N | | ^ | ^
v | | | | v ||v v C | ^ | ^ | |
| | | | || v O ^ | | | | ^ |
| | v | || D | | | ^| | | |
| v v || E | | ^ || | | |
v vv D ##########################
- bybb
- Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Counter:
Given input n, output all numbers from 1 to n.
This challenge introduces for loops.
Example:Input: 5
Output:
1
2
3
4
5
(lambda x:print("".join(str(i)+("\n"if not i==x else"")for i in range(1, x+1))))(5)
def c(n,m): return str(n)+"\n"+c(n+1,m)if n!=m else str(n) print(c(0,5))
Last edited by bybb (Oct. 1, 2018 11:56:58)
- onlyNones
- Scratcher
61 posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Recursion:def c(n,m): return str(n)+"\n"+c(n+1,m)if n!=m else str(n) print(c(0,5))
…your recursion uses 2 input?
########################## E ^ ^ ^
| | v v |v| |||v | N | | ^ | ^
v | | | | v ||v v C | ^ | ^ | |
| | | | || v O ^ | | | | ^ |
| | v | || D | | | ^| | | |
| v v || E | | ^ || | | |
v vv D ##########################
- bybb
- Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
YesRecursion:def c(n,m): return str(n)+"\n"+c(n+1,m)if n!=m else str(n) print(c(0,5))
…your recursion uses 2 input?
- 1a3c5e7g9i
- Scratcher
100+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
That's twenty three blocks. Reporters and booleans are blocks, too. The shortest way to find whether a number is a prime needs only 5 Blocks
I cheated around needing to reset the itterator by using custom Blocks :Ddefine Is (number1) prime? || counter (number2)
if <<(number1)=[1]>or<<not <[2] = (number1)>> and <[0] = ((number1) mod ((1) + (number2)))>>> then
say [NOT PRIME] for (2) secs
else
if <([sqrt v] of (number1)) > (number2)> then
Is (number1) prime? || counter ((1) + (number2)) :: custom
else
say [PRIME] for (2) secs
end
end
Wao how is that possible
Uh Squidward? Yes Spongebob? You might want to look at this… HOLY SHRIMP! If we don't act now, well, think about how many clams I'll have to cough up JUST to see an extra portion of nick.com… or worse, to see ALL of it in general! *** Reader! This is not a fictional sketch! It actually is, but that's not the point. Learn more at the link that Spongebob left at the first instance of “this”.
- novice27b
- Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Write a program to find the nth fibonacci number in O(log n) time.
i use arch btw
- TheLogFather
- Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Are you aware that the nth Fib.no. is just A^n – B^n, where A is (1+sqrt(5))/2 and B is (1-sqrt(5))/2…? Write a program to find the nth fibonacci number in O(log n) time.
Since |B|<1, that means B^n goes to zero as n increases, which means simply using round(A^n) works pretty well.
See https://scratch.mit.edu/discuss/post/1568178/ for a simple Scratch version
EDIT: oops! –Forgot the division by sqrt(5) in above equations. See the post linked above for correct ones…
Or perhaps you're asking for a solution that's not limited to only ~15 digits (or whatever precision you're using) – i.e. uses some bignum arithmetic to avoid integer precision limits during the recursion…?
Last edited by TheLogFather (Oct. 9, 2018 11:20:37)
Siggy the Kumquat slayer:
Main account: DadOfMrLog –– Frameworks for basic pen-rendered 3D in scratch (see studio). Examples:
- - - - 3D Text - - - - - - Simple shapes - - - Controllable structures - - - On the ground - - - - - - In space - - - -
- - - - 3D Text - - - - - - Simple shapes - - - Controllable structures - - - On the ground - - - - - - In space - - - -
- goldfish678
- Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
yup. counting reporters/booleans, the shortest is either @badatprogrammingibe's method or my method (both 13 blocks) ((i think)). That's twenty three blocks. Reporters and booleans are blocks, too.
can we still get it shorter?? :thinking:
i'm planning on making a program that you can't use more than once in scratch, going to have to wait until tomorrow though bc it's getting super late here
- TheAspiringHacker
- Scratcher
100+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Would writing a rudimentary compiler or interpreter be a task appropriate for this thread?
Long live Kyoto Animation!
- XayCraft360
- Scratcher
100+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Make a program that you can't use more than once.
Example:#This program uses the 'os' module to remove itself. import os os.remove(__file__)
shorter:
import os; os.remove(__file__)
<Insert copy of NilsTheBest's OLD uncreative copy of adsuri's uncreative copy of Wahsp's uncreative copy of -ShadowOfTheFuture's- uncreative signature here>
I AM NO LONGER ON SCRATCH
- jokebookservice1
- Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
Well, depends how you count.Make a program that you can't use more than once.
Example:#This program uses the 'os' module to remove itself. import os os.remove(__file__)
shorter:import os; os.remove(__file__)
If you're talking about the number of lines, sure, it's shorter. But by number of characters, on a Unix-like machine, the first one is actually shorter, and on a Windows-like machine, they are the same length. You're replacing a newline character with a semicolon and a space.
And sure, the semicolon is optional, but then it's still a tie.
- goldfish678
- Scratcher
1000+ posts
What are some programming problems like "Hello, world" and "FizzBuzz"?
I have found a technically shorter Scratch primality test method. Since it is, in essence, a primality test and not a compositry test, we can argue that if the program says nothing when you give it a number, you can simply assume it is composite (or know that it is because I just told you this). This allows us to eliminate one of the “say” blocks and bring our test down to a grand total of 12 blocks.
The sad news is that unfortunately, I believe that's the end of the road, at least for this method. Eliminating the other say block would give us two identical outputs for two separate outcomes.
I have also found a way to create a “one-use” program in Scratch 1.4. When fed an imaginary or undefined math operator like (x / 0) or (sqrt of -1), Scratch 1.x doesn't just return an error, it stops the entire project, essentially breaking it and rendering it dysfunctional. This allows us to create a very straightforward one-time-use program in 5 blocks, with the variable “divisor” starting at any number that isn't 0. Note: This could also be done in a similar fashion with the square root block, but I didn't feel like taking another screenshot and it isn't any shorter anyway. It yields the same results.
But wait, there's more! There's an even shorter way to do this in just 4 blocks. Without further ado…
Check these in 1.4 if you don't believe me
That's all for now! Feel free to debate my questionable definition of output to your heart's content.
-Goldfish
The sad news is that unfortunately, I believe that's the end of the road, at least for this method. Eliminating the other say block would give us two identical outputs for two separate outcomes.
I have also found a way to create a “one-use” program in Scratch 1.4. When fed an imaginary or undefined math operator like (x / 0) or (sqrt of -1), Scratch 1.x doesn't just return an error, it stops the entire project, essentially breaking it and rendering it dysfunctional. This allows us to create a very straightforward one-time-use program in 5 blocks, with the variable “divisor” starting at any number that isn't 0. Note: This could also be done in a similar fashion with the square root block, but I didn't feel like taking another screenshot and it isn't any shorter anyway. It yields the same results.
But wait, there's more! There's an even shorter way to do this in just 4 blocks. Without further ado…
Check these in 1.4 if you don't believe me
That's all for now! Feel free to debate my questionable definition of output to your heart's content.
-Goldfish
Last edited by goldfish678 (Dec. 9, 2018 21:55:25)
- Discussion Forums
- » Advanced Topics
- » What are some programming problems like "Hello, world" and "FizzBuzz"?