Hossein
(Hossein Amerkashi)
October 19, 2019, 11:46am
#1
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?

1 Like

Hossein
(Hossein Amerkashi)
October 19, 2019, 12:50pm
#3
Don’t use extension (will add that into requirements). The whole challenge is to code by yourself to come up with solutions

1 Like

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

Hossein
(Hossein Amerkashi)
October 19, 2019, 1:09pm
#6
That’s modulo operation. Use Math modulo block. wiki

vknow360
(Sunny Gupta)
October 19, 2019, 3:13pm
#9
Well done…
But take a deep look at java code.

ravigarg
(Ravi Garg)
October 19, 2019, 3:14pm
#10
Already used the same logic!

1 Like

vknow360
(Sunny Gupta)
October 19, 2019, 3:16pm
#11
I know but variables declaration/initialization can be improved either in example code or your blocks.

Hossein
(Hossein Amerkashi)
October 19, 2019, 3:53pm
#12
@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

Taifun
October 19, 2019, 4:02pm
#13
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

ravigarg
(Ravi Garg)
October 19, 2019, 4:31pm
#14

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.

Hossein
(Hossein Amerkashi)
October 19, 2019, 5:22pm
#15
Updated requirements as above

ravigarg
(Ravi Garg)
October 19, 2019, 6:05pm
#16

So, is this the final solution

I ADDED the check if no is 1,0.

2 Likes

Hossein
(Hossein Amerkashi)
October 19, 2019, 6:33pm
#17
1 Like

Hossein
(Hossein Amerkashi)
closed
November 18, 2019, 6:33pm
#18
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.