Julia Tutorial Check if a number is prime

Julia Tutorial For Begineers to  check if a number is prime number

What is prime number?

 A prime number definition is  a number which is divided by itself  and one is called as prime number . By any other number the prime number cannot be divided and if we try to divide the prime number by any other number between one and the prime number it will not give remainder as zero

Here we write a small program in Julia to check if the number is prime number





In the above program we have define a method called as prime number . In the method "primeNumber(number) "   number is a variable and we can reuse the method for any number to check.

function primeNumber(number)
         count =0
       for j = 1:number


Later we define a counter to increase itself by one when a number is divide by other number and the remainder is zero

In the for loop we are defining the range between one and the prime number specified, the number will be taken from the argument

if number%j == 0
           count=count+1
            println(number, " % ",j , "  =  ", number%j)
       end

Here we write a if condition to check if the number when divided  yields remainder as zero. If the remainder is zero then the counter is increased to one


Now we will see the result by passing a number





function primeNumber(number)
count =0
   for j = 1:number

       if number%j == 0
           count=count+1
            println(number, " % ",j , "  =  ", number%j)
       end
        end
       if(count == 2)
            println(number ,": is a prime number")
else
     println(number ,": is not a prime number")
          end
       end

Comments