<p>Example using build-in functions (sure, not the best ;-) ).</p>

<p>Supposing that <code>number</code> is our number:</p>

<pre><code>{% set result = 0 %}
{% if number / number|round == 1 %}
    {% set result = 1 %}
    {% if number &gt; 3 %}
        {% for i in 2..number-1 if result == 1 %}
            {% if number is divisibleby(i) %}
                {% set result = 2 %}
            {% endif %}
        {% endfor %}
    {% endif %}
{% endif %}
</code></pre>

<p>Now, <code>result</code> equals to:</p>

<ul>
<li><p><code>0</code>, if <code>number</code> is not integer</p></li>
<li><p><code>1</code>, if <code>number</code> is prime </p></li>
<li><p><code>2</code>, if <code>number</code> is composite</p></li>
</ul>

CC BY-SA 3.0
2014-07-10T15:19:26.427Z
http://en.wikipedia.org/wiki/Prime_number
carlcs
CC BY-SA 3.0
2014-07-10T15:21:02.103Z
Also: `if result == 1` in line 4 makes no sense
carlcs
CC BY-SA 3.0
2014-07-10T15:30:25.177Z
@Christian I have already read about prime number (what am I missing?). And line 4 is valid. Not sure what is wrong.
nicael
CC BY-SA 3.0
2014-07-10T15:47:03.890Z
Sorry nicael, I overlooked that you are overwriting the result var if your number is a composite. Only read this: `{% if number / number|round == 1 %}{% set result = 1 %}` and immediately the word integer came to mind.
carlcs
CC BY-SA 3.0
2014-07-10T15:57:00.873Z
@Christian Yes, it does )) So what? Some simpler check which I don't know about?
nicael
CC BY-SA 3.0
2014-07-10T16:08:15.063Z
All good, nicael! I only thought "huh these are integers and not primes" and posted my dumb comment. Sorry again!
carlcs
CC BY-SA 3.0
2014-07-10T15:06:29.383Z
2014-07-10T15:25:58.780Z
2014-07-10T15:25:58.780Z
nicael
<p>I think the question of the use-case is vital here. If you want to check whether a large number is prime or not, you will need a plugin and a lot of math.</p>

<p>If your number is guaranteed not too big, I would use twig's <a href="http://twig.sensiolabs.org/doc/templates.html#containment-operator" rel="nofollow">in</a> operator:</p>

<pre><code>{% set primes = [2, 3, 5, 6, 11, 13, 17, 19] %}
{% if n &gt; primes|last %}
  too big to tell
{% elseif n in primes %}
  {{n}} is prime
{% else %}
  {{n}} is composite
{% endif %}
</code></pre>

<p>Or check for prime factors in a loop:</p>

<pre><code>{% set primes = [2, 3, 5, 6, 11, 13, 17, 19] %}
{% if n &gt; (primes|last) * (primes|last) %}
  too big to tell
{% else %}
  {% set isComposite = false %}
  {% for p in primes %}
    {% if (not isComposite) and  (n is divisibleby (p)) %}
      {{n}} is composite (divisible by {{p}})
      {% set isComposite = true %}
      {# optional break #}
    {% endif %}
  {% endfor %}
  {% if not isComposite %}
    {{n}} is prime
  {% endif %}
{% endif %}
</code></pre>

<p>(<a href="https://craftcms.stackexchange.com/questions/862/is-it-possible-to-break-out-of-a-loop-in-twig">Twig doesn't have a native break tag, but you can use a plugin</a>)</p>

CC BY-SA 3.0
2014-07-11T16:33:56.567Z
2014-07-11T16:33:56.567Z
Marion Newlevant
<p>How do I check if a given number is prime in twig? </p>

CC BY-SA 3.0
2014-07-10T16:12:28.387Z
I'm curious about the use case you want to implement this, nicael.
carlcs
CC BY-SA 3.0
2014-07-10T16:40:27.350Z
@Christian Well... I'll think :D
nicael
<templating><variable><number>
How to check if number is prime?
# How to check if number is prime?

(Asked by: username_0 on 2014-07-10)

How do I check if a given number is prime in twig?

**Tags:** templating, variable, number

> **Comments:**
> **username_1**: I'm curious about the use case you want to implement this, username_0.  
> **username_0**: @Christian Well... I'll think :D  

---

## 2 answers

### Answer by username_0 on 2014-07-10. Score: 2

Example using build-in functions (sure, not the best ;-) ).
Supposing that number is our number:
{% set result = 0 %}
{% if number / number|round == 1 %}
    {% set result = 1 %}
    {% if number > 3 %}
        {% for i in 2..number-1 if result == 1 %}
            {% if number is divisibleby(i) %}
                {% set result = 2 %}
            {% endif %}
        {% endfor %}
    {% endif %}
{% endif %}

Now, result equals to:

0, if number is not integer
1, if number is prime 
2, if number is composite

> **Comments:**
> **username_1**: http://en.wikipedia.org/wiki/Prime_number  
> **username_1**: Also: `if result == 1` in line 4 makes no sense  
> **username_0**: @Christian I have already read about prime number (what am I missing?). And line 4 is valid. Not sure what is wrong.  
> **username_1**: Sorry username_0, I overlooked that you are overwriting the result var if your number is a composite. Only read this: `{% if number / number|round == 1 %}{% set result = 1 %}` and immediately the word integer came to mind.  
> **username_0**: @Christian Yes, it does )) So what? Some simpler check which I don't know about?  
> **username_1**: All good, username_0! I only thought "huh these are integers and not primes" and posted my dumb comment. Sorry again!  

---

### Answer by username_2 on 2014-07-11. Score: 1

I think the question of the use-case is vital here. If you want to check whether a large number is prime or not, you will need a plugin and a lot of math.
If your number is guaranteed not too big, I would use twig's in operator:
{% set primes = [2, 3, 5, 6, 11, 13, 17, 19] %}
{% if n > primes|last %}
  too big to tell
{% elseif n in primes %}
  {{n}} is prime
{% else %}
  {{n}} is composite
{% endif %}

Or check for prime factors in a loop:
{% set primes = [2, 3, 5, 6, 11, 13, 17, 19] %}
{% if n > (primes|last) * (primes|last) %}
  too big to tell
{% else %}
  {% set isComposite = false %}
  {% for p in primes %}
    {% if (not isComposite) and  (n is divisibleby (p)) %}
      {{n}} is composite (divisible by {{p}})
      {% set isComposite = true %}
      {# optional break #}
    {% endif %}
  {% endfor %}
  {% if not isComposite %}
    {{n}} is prime
  {% endif %}
{% endif %}

(Twig doesn't have a native break tag, but you can use a plugin)


---

