# Prime numbers between given two numbers in Julia and Python

**URL:** <https://discourse.julialang.org/t/prime-numbers-between-given-two-numbers-in-julia-and-python/73626>\
**Category:** New to Julia\
**Tags:** python\
**Created:** [December 25, 2021, 9:56pm UTC](https://discourse.julialang.org/t/prime-numbers-between-given-two-numbers-in-julia-and-python/73626 "2021-12-25T21:56:23Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![apoorvvyas52](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apoorvvyas52/32/31749_2.png) [@apoorvvyas52](https://discourse.julialang.org/u/apoorvvyas52)\
**Post date:** [December 25, 2021, 9:56pm UTC](https://discourse.julialang.org/t/prime-numbers-between-given-two-numbers-in-julia-and-python/73626/1 "2021-12-25T21:56:23Z")

</div>

See the Python code for finding prime numbers between given two numbers lower and upper  
‘’'lower = 3  
upper = 15

print(“Prime numbers between”, lower, “and”, upper, “are:”)

for num in range(lower, upper + 1):

```
   for i in range(2, num):
       if (num % i) == 0:
           break
   else:
       print(num)

```

‘’’

**Python Output:**  
Prime numbers between 3 and 15 are:  
3  
5  
7  
11  
13

Almost similar code in Julia gives incorrect output. See the Julia code below  
‘’'lower = 3  
upper = 15  
println("Prime numbers between ", lower, " and “, upper, " are:”)  
for num in lower:upper

```
   for i in 2: num-1
       if (num % i) == 0
           break
       
       else
       println(num)
            break
    end
    end

```

end  
‘’’  
**Julia Output:**  
Prime numbers between 3 and 15 are:  
3  
5  
7  
9  
11  
13  
15

Clearly, 9 and 15 are not primes. Why does Python code gives correct output and Julia code does not. Any way to fix it using the same algorithmic logic?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 25, 2021, 10:04pm UTC](https://discourse.julialang.org/t/prime-numbers-between-given-two-numbers-in-julia-and-python/73626/2 "2021-12-25T22:04:30Z")

</div>

Julia doesn’t have an `else` clause for `for` loops like Python. You can use a boolean variable to record if the mod check `break` never ran.

---

<div class="post-metadata">

**Author:** ![apoorvvyas52](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apoorvvyas52/32/31749_2.png) [@apoorvvyas52](https://discourse.julialang.org/u/apoorvvyas52)\
**Post date:** [December 25, 2021, 10:06pm UTC](https://discourse.julialang.org/t/prime-numbers-between-given-two-numbers-in-julia-and-python/73626/3 "2021-12-25T22:06:03Z")

</div>

> [@apoorvvyas52](#):
>
> See the Python code for finding prime numbers between given two numbers lower and upper  
> ‘’'lower = 3  
> upper = 15
> 
> print(“Prime numbers between”, lower, “and”, upper, “are:”)
> 
> for num in range(lower, upper + 1):
> 
> ```julia
> for i in range(2, num):
> if (num % i) == 0:
> break
> else:
> print(num)
> 
> ```
> 
> ‘’’
> 
> **Python Output:**  
> Prime numbers between 3 and 15 are:  
> 3  
> 5  
> 7  
> 11  
> 13
> 
> Almost similar code in Julia gives incorrect output. See the Julia code below  
> ‘’'lower = 3  
> upper = 15  
> println("Prime numbers between ", lower, " and “, upper, " are:”)  
> for num in lower:upper
> 
> ```julia
> for i in 2: num-1
> if (num % i) == 0
> break
>        
> else
> println(num)
> break
> end
> end
> 
> ```
> 
> end  
> ‘’’  
> **Julia Output:**  
> Prime numbers between 3 and 15 are:  
> 3  
> 5  
> 7  
> 9  
> 11  
> 13  
> 15
> 
> Clearly, 9 and 15 are not primes. Why does Python code gives correct output and Julia code does not. Any way to fix it using the same algorithmic logic?

You can use a boolean variable to record if the mod check `break` never ran. – can you explain what do you mean by that?

---

<div class="post-metadata">

**Author:** ![apoorvvyas52](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apoorvvyas52/32/31749_2.png) [@apoorvvyas52](https://discourse.julialang.org/u/apoorvvyas52)\
**Post date:** [December 25, 2021, 10:10pm UTC](https://discourse.julialang.org/t/prime-numbers-between-given-two-numbers-in-julia-and-python/73626/4 "2021-12-25T22:10:34Z")

</div>

Thanks Kristoffer. Appreciate the help but I don’t understand what you mean . Can you explain in slight detail. I don’t understand when you say " You can use a boolean variable to record if the mod check `break` never ran." And moreover, I am running else clause for the if condition in my loop in Julia and for “for” loop.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [December 25, 2021, 10:14pm UTC](https://discourse.julialang.org/t/prime-numbers-between-given-two-numbers-in-julia-and-python/73626/5 "2021-12-25T22:14:03Z")

</div>

Your code needs to check that `(num % i) == 0` is not true for all the numbers in the range and only then print. Right now it oeuvre if it is false for only one number in the range. Remove the last `break` to make it more obvious.

---

<div class="post-metadata">

**Author:** ![apoorvvyas52](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apoorvvyas52/32/31749_2.png) [@apoorvvyas52](https://discourse.julialang.org/u/apoorvvyas52)\
**Post date:** [December 25, 2021, 10:29pm UTC](https://discourse.julialang.org/t/prime-numbers-between-given-two-numbers-in-julia-and-python/73626/6 "2021-12-25T22:29:56Z")

</div>

‘’’  
lower = 3  
upper= 15  
for num =lower:upper  
for j = 2:num-1  
if (num%j)==0  
break  
else  
print(num)  
print(" ")

```
end
  
end

```

end’‘’

output after removing the last “break” is as follows  
3 5 5 5 7 7 7 7 7 9 11 11 11 11 11 11 11 11 11 13 13 13 13 13 13 13 13 13 13 13 15

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [December 25, 2021, 10:31pm UTC](https://discourse.julialang.org/t/prime-numbers-between-given-two-numbers-in-julia-and-python/73626/7 "2021-12-25T22:31:47Z")

</div>

This question was also posted on [Prime numbers between given two numbers in Julia and Python comparison - Stack Overflow](https://stackoverflow.com/questions/70483017/prime-numbers-between-given-two-numbers-in-julia-and-python-comparison). Is the answer given there clear for you?

---

<div class="post-metadata">

**Author:** ![apoorvvyas52](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apoorvvyas52/32/31749_2.png) [@apoorvvyas52](https://discourse.julialang.org/u/apoorvvyas52)\
**Post date:** [December 25, 2021, 11:23pm UTC](https://discourse.julialang.org/t/prime-numbers-between-given-two-numbers-in-julia-and-python/73626/8 "2021-12-25T23:23:02Z")

</div>

The code given by you on stack overflow is working correctly but my poor brain couldn’t comprehend it’s logic. If possible kindly explain. Appreciate your help though. Thank you.

---

<div class="post-metadata">

**Author:** ![NiclasMattsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niclasmattsson/32/21988_2.png) [@NiclasMattsson](https://discourse.julialang.org/u/NiclasMattsson)\
**Post date:** [December 25, 2021, 11:29pm UTC](https://discourse.julialang.org/t/prime-numbers-between-given-two-numbers-in-julia-and-python/73626/9 "2021-12-25T23:29:54Z")

</div>

The Stack Overflow answer by @bkamins is probably quite clear to experienced Julians, but I suspect it’s difficult to understand for beginners due to the mixed usage of loops and vectorized notation, the anonymous function and the short-circuited `||`. Since the question was marked as “New to Julia” I think it might be appropriate to highlight how simple Julia code can be.

In this case I think an extra `isprime()` function improves readability and hides the loop breaking awkwardness, so here’s my suggestion:

```julia
function isprime(num)
    for j = 2:num-1
        if (num % j) == 0
            return false
        end
    end
    return true
end

function primesbetween(lower, upper)
    println("Prime numbers between $lower and $upper are:")
    for n in lower:upper
        if isprime(n)
            println(n)
        end
    end
end

julia> primesbetween(3,15)
Prime numbers between 3 and 15 are:
3
5
7
11
13

```

I also added `primesbetween()` to emphasize how Julia code should always be wrapped in functions.

---

<div class="post-metadata">

**Author:** ![apoorvvyas52](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apoorvvyas52/32/31749_2.png) [@apoorvvyas52](https://discourse.julialang.org/u/apoorvvyas52)\
**Post date:** [December 26, 2021, 12:03am UTC](https://discourse.julialang.org/t/prime-numbers-between-given-two-numbers-in-julia-and-python/73626/10 "2021-12-26T00:03:17Z")

</div>

this is also a nice way to solve the same problem. Thanks. Really appreciate it.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [December 26, 2021, 8:53am UTC](https://discourse.julialang.org/t/prime-numbers-between-given-two-numbers-in-julia-and-python/73626/11 "2021-12-26T08:53:45Z")

</div>

> [@apoorvvyas52](#):
>
> If possible kindly explain.

I have edited the code on Stack Overflow with an explanation. Maybe here I would just add that in `any(i -> num % i == 0, 2:num-1)` the `i -> num % i == 0` part defines a function that takes a number `i` and returns `true` if `i` divides `num`.

---

<div class="post-metadata">

**Author:** ![nstgc](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nstgc](https://discourse.julialang.org/u/nstgc)\
**Post date:** [December 30, 2021, 9:26pm UTC](https://discourse.julialang.org/t/prime-numbers-between-given-two-numbers-in-julia-and-python/73626/12 "2021-12-30T21:26:55Z")

</div>

I just wanted to stop by to throw in my two sense regarding the provided solution. I’ve personally found that “when in doubt, make a new function”. I’m not sure if the real programmers among the user base agree with that or not, but as a novice, changing my mindset to use functions as a go-to solution for my problems helped me make Julia work for me.
