Daily Challenge #38

Determine if a number is Prime Number
Create a procedure that will determine if a number is Prime number (true) or not (false)

Examples

isPrime(2) -> True
isPrime(14) -> false

The first 20 primes are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, and 71

Requirements:

  • Use Procedure with return
  • Procedure takes single number
  • Returns True (if prime) or False (if not)
  • Should not use any extensions
  • Use only numbers 1 to 200

Sample Java program - use this for your logic:

Java Program to Check Whether a Number is Prime or Not

2 Likes

that’s all, but this may not be the desired solution of Hossein


but why should I reinvent the wheel? :wink:

1 Like

:sweat_smile: Don’t use extension (will add that into requirements). The whole challenge is to code by yourself :slight_smile: to come up with solutions

1 Like

Ok, I guessed that

1 Like

What that % mean I know that is reminder but I didn’t understand can you explain sir please

That’s modulo operation. Use Math modulo block. wiki

image

I did not test it:

2 Likes


20 Blocks

4 Likes

Well done…
But take a deep look at java code.

Already used the same logic!

1 Like

I know but variables declaration/initialization can be improved either in example code or your blocks.

@ravigarg Very good job. You need to add the logic for number 1. Try your procedure and see what you get for procedure(1)

1 Like

additional challenge: if you got it working for smaller numbers you might want to check, if your solution still works for larger numbers, for example 104729 or even 15485863…

question: why does it not work for larger numbers anymore? And how could this be fixed?

Taifun

1 Like


It did work, but for the second number i.e. 15485863 I had to wait for 4-5 minutes.

It does, but dividing long numbers again and again with long numbers take a lot of time and makes a situation of an infinite loop.

Still Thinking on this one!

Thanks! WIll surely add that.

Updated requirements as above :point_up_2:


So, is this the final solution
I ADDED the check if no is 1,0.

2 Likes

Solved by both @bodymindpower and @ravigarg

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.