How do I make equations with the values in a for loop

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Anzol8

Hi I’m a beguinner in coding and using godot, I’m learning how to use for loops, but I can’t figure how to do certain things, I want to do the sum of all numbers in the loop, exponential but only for the odd numbers for example, factorial, the average of all numbers, and every number that’s divisible by a certain number. I’ve searched this but I can’t find anything so thanks if someone can help.

:bust_in_silhouette: Reply From: Wakatta

Answer

Sorry but but this is one of those questions where it is harmful to get a direct answer

“You can’t find, what you don’t know you’re looking for” - Wakatta

A brief review on how for loops work
And what you’re looking for is called an Algorithm . There are entire library sections dedicated to this but a simple YouTube search on How to create algorithms can suffice and will benefit you more long term.

:bust_in_silhouette: Reply From: Gluon

You are asking for a whole series of different questions to be answered here and to be honest you need to do a bit of learning I think. I suggest going through the godot documentation Search — Godot Engine (stable) documentation in English where you will find a lot of tutorials to help.

I will get you started with the easiest algorithm you are asking for though, this is how you would find the sum of all numbers in a range.

var id = 0
for x in 10:
	id = id + x
print(id)

What the above does is declare a variable, then iterates over a range of 10 each time adding the number to the total of all the other numbers and at the end prints that result to the terminal.

And Just like Gluon started is how you should continue for this sort of problem
Do each section separately then put it all together

Wakatta | 2022-11-26 01:40

thanks, as I said I’m a beguinner in this, and I was having some trouble understanding for loops, I searched for the answer but coudn’t find it clear, but thanks for answering and giving me direction.

Anzol8 | 2022-11-26 03:44