Division Math

Get help using Construct 2

Post » Thu Mar 21, 2013 9:13 pm

Hi guys,

Sorry if this isn't really the correct place for this, but I tried looking online for a solution, and can't find it anywhere, and the things I've seen people pull of with numbers on this forum are incredible.

I'm trying to set up a system that will work out the division of a number, and so I have Var1/Var2=Var3, but I only want to use numbers that will divide into whole numbers. I have therefore set it up so that it will only show me even numbers, and to do this, I used the expression choose(2,4,6,8...) as the first variable, and then 2 as the second, as that is the only way I can guarantee a whole number.

I thought about having a system that would divide the first variable by 2, and then generate randomly from 0 to that figure, but I knew I'd end up getting decimals (10/4=2.5).

Is there an equation that would choose numbers that would generate a whole number as a result?

Division Example
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
68
S
8
G
6
Posts: 930
Reputation: 9,358

Post » Thu Mar 21, 2013 9:52 pm

What exactly is your ultimate goal? Is it really just to divide 2 numbers resulting in a whole number. If it is, you've already done that. However, I'm guessing your ultimate goal is something a bit more complex. Could you elaborate a bit?

When trying to convert numbers with decimals into integers, the following expressions are useful:

round(x): Round x to the nearest whole number e.g. round(5.6) = 6
ceil(x): Round up x e.g. ceil(5.1) = 6
floor(x): Round down x e.g. floor(5.9) = 5

Taken straight from the system expressions page of the manual.
B
44
S
13
G
7
Posts: 338
Reputation: 10,379

Post » Thu Mar 21, 2013 10:10 pm

I would like a system that can find all of the whole number divisions of each number.

E.g's
10 = (1,2,5)
15 = (1,3,5)
28 = (1,2,4,7,14)

The only save number I can figure out is 2.

Rounding up or down won't work.
@bearboxmedia
www.bearboxmedia.com

Nintendo Wii U Developer using Construct 2
B
68
S
8
G
6
Posts: 930
Reputation: 9,358

Post » Fri Mar 22, 2013 1:57 am

Ah, I misinterpreted you original post. You're looking to determine all factors for a number. Well I'm sure there is a more elegant way to do it, but here's one method:

Demo
Capx

I use a method called trial factoring. Basically, you take the starting number, take its square route and round it up to the nearest integer(i.e. whole number). Let's call that number "i". Now, try dividing the original number by every integer between 0 and i. For every division resulting in an integer, both "i" and the result are factors. Here's an example:

Starting number = 22
The square route of 22 rounded up = 5

22/1 = 22, so 1 and 22 are factors
22/2 = 11, so 2 and 11 are factors
22/3 = not an integer, so no factors
22/4 = not an integer, so no factors
22/5 = not an integer, so no factors

I hope this helps.


EDIT - Updated capx to use a much less convoluted method for testing for integers, incorporated an array to make sorting easier, and annotated everything. @AnD4D

zatyka2013-03-22 12:41:02
B
44
S
13
G
7
Posts: 338
Reputation: 10,379

Post » Fri Mar 22, 2013 6:49 pm

The % operator would be useful in this situation. It gives the remainder after a division, so if a%b = 0 then a is divisible by b.

Here is one event way to do it:

global number toFactor=28
global string factors=""

start of layout
repeat toFactor times
system compare toFactor%(loopindex+1) = 0
--- add loopindex%1 & " " to factors
B
79
S
24
G
54
Posts: 4,738
Reputation: 40,739


Return to How do I....?

Who is online

Users browsing this forum: Artpunk, GenkiGenga, Kagubro, Mirlas, zeneizel and 24 guests