Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

How to calculate the GCD(Greatest Common divisor)

HI all

Could any one tell me how to calculate the GCD of the two field values in qlikview..

for ex:

I have field A and B like

A B

10 20

6 9

20 11

the output should be come as like

A B GCD

10 20 10

6 9 3

20 11 1

hope it will give clear understanding

Could anyone help me...

thanks in advance

5 Replies
Not applicable
Author

The method is the Euclidean Algorithm and the code is way beyond me. This is a representation of the code in C. Macro guy needed

http://en.literateprograms.org/Special:Downloadcode/Euclidean_algorithm_%28C%29

This is the code breakdown explanation

http://en.literateprograms.org/Euclidean_algorithm_(C)

Not applicable
Author

Hi,

Below is a simplification of the calculation of GCD.

"I'll explain the algorithm with an example. Let's find the gcd of
210 and 990.

Divide 990 by 210: quotient 4, remainder 150; ignore quotient
Divide divisor from previous step by remainder from previous step
divide 210 by 150: quotient 1, remainder 60; ignore quotient
Divide divisor from previous step by remainder from previous step
divide 150 by 60: quotient 2, remainder 30; ignore quotient
Divide divisor from previous step by remainder from previous step
divide 60 by 30: quotient 2, remainder 0.

When the remainder becomes 0, as it always will, then go to the
previous step and choose the remainder, in this case 30. This is the
gcd! Neat!"

Using MOD(x1,x2) is probably the way to go with this.

hector
Specialist
Specialist

Hi, taking the idea from here http://en.wikipedia.org/wiki/Euclidean_algorithm i've made this

1st: Create a Function in the macro editor


function gcd(a, b)
while b <> 0
t = b
b = a mod b
a = t
wend
gcd = a
end function


then, use it in the script


DATA:
LOAD
F1,
F2,
gcd(F1,F2) as GCD
INLINE [
F1, F2
10,20
6, 9
20,11
4,3
];


And the result is

F1 F2 GCD
10 20 10
6 9 3
4 3 1
20 11 1
4 2 2

Rgds

Not applicable
Author

This is helpful.

I have a different datamodel where columns are shown as rows (attached).

Is it possible to calculate the GCD based on this model ?

I need to show the ratio between Designers vs Layout and vice-versa. In order to get the ratio, we need to get the GCD first.

ProjectSkillsMonthHeadcount
ProjectADesigners1-Oct-116.25
ProjectALayout1-Oct-110.50
ProjectBDesigners1-Nov-115.50
ProjectBLayout1-Nov-111.50

vijay_iitkgp
Partner - Specialist
Partner - Specialist

Hi Anthony,

You can do this. But for this you need to get designers and Layout as columns. Please load table with where clause Skills =Designers and name headcount as Designer Headcount and then Join it with same table on where clause Skills= Layout and Head count as Layout Headcount on Primary key (May be Project and Month) and use the above defined function.

Hope this will help.

Regards

VIjay