Custom common factor block

These math-related predicates check for IF one value is equal, less than, or greater than another value and then report True or False (a Boolean value). Thus they are Predicate shaped. Blocks like Sum, Product, Minus, Mod, and more are also math related, but they don’t check whether something is true, they take some inputs and give an output based on those inputs. Given that these are math blocks, that output will be a number and not a Boolean, and thus these math blocks should and are Reporters. If we follow the pattern of the primitive blocks (which you are by no mean obliged to) your block should be a reporter too.

In the case of IF, the result of this math block won’t be useful. Because it will report a number, the IF block won’t know what to do and actually has a special behaviour for numbers: it treats 0 as false and everything else as true. This means that for your IF statement to not trigger, the value would have to be 0 which is only the case for the GCF of 0 and 0. Regardless, doing something only if the GCF isn’t 0 does not have a use case (that I can think of). As for the other blocks you mentioned (=,>,<), they don’t have Boolean inputs and can totally take things like the Sum block directly with no problem.

I would like to end this lengthy post by saying that you can totally use whatever organization method you want. I’m not trying to control that. I’m just trying to clarify what you may or may not already know about how Predicates versus Reporters are used by primitives and most people on the forum.

PS: I liked your elegant definition of the block using the Euclidean algorithm!

It’s SUPER cool you actually noticed the theorem I used. Also thank you for all the feedback! And yes I’m aware of the difference. It’s just I was more or less trying to get an invalid point across.

Edit. Thanks for teaching me a bit. I’m still learning after all!

Yes, it’s just that that method makes it confusing for others using your block. This includes myself.
We’re not saying to ditch the method for yourself, it’s just that changing the type of the block when you share it to make it correct and easier to use for others is, ideally, not too hard :slight_smile: .

Understandable. Just forgot to do that!

Just a reminder of what I’ve covered about my block shape. @sathvikrias

Here’s the block. Sorry for the delay, It uses the other block I made earlier. Hope this helps!

If your wondering why this block below

-Is empty. It’s because Snap! Automatically takes numbers from the list and puts it into the block

Custom Blocks script pic 8

This likely could have been written cleaner and better but I tried my best and got a working block so I’m fine with it. It works with more than 3 numbers by hitting the arrows on the right side of the block. Thanks to @ethan7946 for the idea to use lists

It works for me! thank you so much, I am confused about how the code works, but I wouldn’t wanna waste your time asking questions! thanks again!

It’s fine to ask questions about how something works! I’ll never get bored of sharing how something of mine works. Especially if it’s a learning opportunity. Ask away! Also, if you want other math related blocks I’m making a small library of them. As well as other custom blocks.

I do recommend asking in segments instead of asking how the entire thing works. Because it’s a lot to me as well!

How does that [combine (Numb) using [GCF of _ and _ ]] work?

A simple explanation: it basically crams the list into the GCF block, but in an 1 by 1order. So if gets the GCF of everything in the list. Although I could be wrong because I don’t understand lists that well either :wink:

You don’t need that list variable. You also don’t need all those if statements, the combine block already gracefully handles single-item lists by reporting that single item, and two-item lists by calling the provided function only once. This is what your block can look like:

I don’t know how much you and @im_hoodie understand about lists, but I sense @bh coming with a long but beautifully crafted explanation, so I’ll keep it to COMBINE. To begin, I assume you understand how MAP works (if you don’t, this picture should help):


In the case of MAP, the first input is a function which takes one input - the item you are mapping over. So the “TIMES 10” function takes 7 and gives 70, 4 and gives 40, and 32 and gives 320. Simple enough, right?

Well, in the case of COMBINE, the function you are providing takes two inputs and gives back one. This is best explained with an example:
untitled script pic (23)
To help keep things clear, I am going to just describe what happens in this particular case. To begin with, COMBINE takes the first two items of the list, 4 and 7 and multiplies them together, getting 28. Now it takes 28 and the third item of the list, and multiplies these two together, getting 84. Finally, it takes 84 and multiplies that with the last item of the list, that is 2, to get 168, which it reports. Here’s a nice little diagram of how that plays out:


Combine should only be used on associative functions - that is, functions that don’t care what order their inputs are calculated in. Using other functions like minus, in practice, doesn’t make sense because things like (4 - 7) - 3 - 2 aren’t equal to 4 - (7 - 3) - 2. Also, the “fold order” of COMBINE isn’t guaranteed to remain the same by the Snap devs. That means that, at some point in the future, COMBINE might switch to doing this:

So I did all that block work for you to show me how easy it is? (You made it look easy) Sorry that I suck with lists lol

Edit: so what do I do to make it not bug out?

associative functions - that is, functions that don’t care what order their inputs are in.

In this otherwise excellent explanation, you are slightly mixing up “associative” and “commutative.” (Easy to get them mixed up since − is neither.) Associative functions, as your specific example

things like (4 - 7) - 3 - 2 aren’t equal to 4 - (7 - 3) - 2

correctly shows, are the ones that don’t care about the grouping of the inputs. It’s commutative functions that don’t care about the order of the inputs, e.g. 2+3=3+2 but 2−3≠3−2.


Could you be more specific? Are you uncertain about the idea of lists? (As in, a list is a bunch of things wrapped up into one thing.) Or is it the notation, things like :reverse_button::play_button:︎ in the LIST block?

I didn’t actually mix up commutative and associative, I just mistyped what I was thinking in my head. I meant to say “the order their inputs are calculated in” rather than “the order their inputs are in”. Whoops.

I’m sorry, but I don’t know what bugging out you are describing. If you show me what you mean, then I can try to help.

I understand the very basic concept of lists and how they work. But only enough to make basic script lists because of their complexity. Although due to your

And @mark4sisb’s nice explanation I understand lists a bit more now. Thank you!

What you explained here

Ah, you don’t have to worry about that being an issue because GCF is associative. That means, just like with multiplication, the order you combine the inputs won’t affect your results.

Don’t understand why GCF is associative? Let’s say you want the GCF of a, b, and c. The greatest common factor of a and b contains, by definition all the common factors of a and b. Thus, when we take the GCF of that number and c, we will get the GCF of a, b, and c. Similarly, the same applies if we first take the GCF of b and c, and then take that number and find its GCF with a.

Okay, thanks for explaining!

Hello again! @im_hoodie I’ve made a common factors block that reports all the common factors up the the greatest common factor. I thought it would be useful on your part and wanted to share it. Here’s the block:

And here is the result

Custom Blocks script pic 53

in snap, why say report([text V] of (factors) ? remember, report (factors) works.