Number Factorisation

Hi

I have generated a random number in a text object and stored it as a number in a variable.

Is it possible to factor that number?

Best Wishes

What do you mean by factor and what do you want to do with that number?

Hi

I mean to break it down to a question, so if I get a random number 49, break it down to 7 x 7 so I can produce multiple choice questions.

I asked chat GPT and it came back with some Java :grimacing::

// Replace with your variable name
var player1QuestionValue = gdjs.evtTools.common.getVariableNumber(runtimeScene.getVariables().get(“Player1QuestionValue”));

var factors = [];

for (var i = 1; i <= player1QuestionValue; i++) {
if (player1QuestionValue % i === 0) {
factors.push(i);
}
}

console.log("Factors of " + player1QuestionValue + ": " + factors.join(', '));

My math is a bit weak. It’s been a while since I used factors.
If the number is 12, do you want
2,6
3,4
4,3
6,2

My math is a bit weak as well.

I want to generate three possible answers from the random number, one being correct and the other two being incorrect.

I think it is possible via Java script, Im asking Claude.ai right now.

You can use a loop like repeat or while and increase a variable as a counter. mod() is Modula. It returns the remainder. It uses compare 2 numbers to check for a remainder of 0.

This uses a button and text object

The result:
image

You could add the value to an array. A repeat would capture them all. A while could be used to only capture a specific number. I would add them all to an array and then randomly pick 2 otherwise it would only use the smallest factor.

Thanks for the help.

This looks very useful, I will experiment with mod()

I will continue to experiment with Java script as well.

:index_pointing_at_the_viewer: :slightly_smiling_face: :+1:

1 Like

This javascript would actually work, just modify the variable name it is using to the one it is supposed to be.

Good to know.

Soon Ai will be able to replace coders completely.

Check out Chat GPT, Claude.ai and Bing.ai

They are all good :+1:

Otherwise, take the problem the other way pick 3 prime numbers and multiply them. You will have more room to handle difficulty level.

Good idea

Thanks

I will take a look.

:+1: