1 Reply Latest reply on Mar 22, 2016 8:22 AM by Joe Oppelt

# How can I calculate the Greatest Common Divisor for two numbers?

Hi, good morning,

I have to calculate the GCD for two numbers but I was not able to do it using tableau, does anybody have a way to do that?

I really appreciate any help on this.

Thanks in advance

Martin

• ###### 1. Re: How can I calculate the Greatest Common Divisor for two numbers?

You might not want to do this in Tableau.  It's going to need an iterative operation, and Tableau calcs aren't made for that.

I can tell you this: If you do something like the following, you can determine if a number is a valid divisor:

IF (INT([number field]/[attempted divisor])*[attempted divisor]) = [number field] THEN  <do what you want to do when you have a valid divisor>  END

By forcing an INT of the first division operation in that syntax, you'll truncate the decimal portion.  So, for example, INT( 8 / 3 ) = 2.  (2.667 gets truncated to 2.)  And 2*3=6, and 6<>8, so this one would come up as FALSE.

In some basic programming language you can loop from 1 through [number field] and grab the iterations that comes up as TRUE.  But we don't have a "loop" operation in Tableau.  If you wanted to do it in tableau you would hard-code a bunch of nested IF statements that would accommodate your largest possible value.   But what if your largest value is a million?  And we don't have an array concept where you can store all the TRUE conditions so that you can compare those against some other number's list of divisors.

You're better off doing this in something outside of Tableau and bringing those results into the sheet through some data source where you've stored it.