cancel
Showing results for
Did you mean:

# IsPrime

Super User
481 Views
Highlighted
Super User

## IsPrime

This nifty little Quick Measure will tell you if a number is prime or not.

```mIsPrime =
VAR __num = MAX([Number])
VAR __max = INT(SQRT(__num))
VAR __table = GENERATESERIES(2,__max,1)
RETURN
SWITCH(TRUE(),
__num = 1, 0,
ISBLANK(COUNTROWS(FILTER(__table1,[__mod]=0))),1,
0
)```

Basically, compute the nearest integer to the square root of the number. Generate a table of all values between 2 and that number. Compute the modulus of the number in question and each of the table values. If one of the rows has a modulus of 0, it's not a prime number.

Included in the file is a representation of the Ulam Spiral, a nifty little construct that occurs when you write positive integers down in a square spiral pattern like:

Apparently, Ulam discovered the spiral in 1963 while doodling during the presentation of "a long and very boring paper" at a scientific meeting. This narrows down the potential presentations by exactly 0.