A Formula to Find Prime Numbers in a Loop

A formula to find prime numbers in a loop

Here's a little function that I found: (http://icdif.com/computing/2011/09/15/check-number-prime-number/) Seemed to work for me!

function isPrime($num) {
//1 is not prime. See: http://en.wikipedia.org/wiki/Prime_number#Primality_of_one
if($num == 1)
return false;

//2 is prime (the only even number that is prime)
if($num == 2)
return true;

/**
* if the number is divisible by two, then it's not prime and it's no longer
* needed to check other even numbers
*/
if($num % 2 == 0) {
return false;
}

/**
* Checks the odd numbers. If any of them is a factor, then it returns false.
* The sqrt can be an aproximation, hence just for the sake of
* security, one rounds it to the next highest integer value.
*/
$ceil = ceil(sqrt($num));
for($i = 3; $i <= $ceil; $i = $i + 2) {
if($num % $i == 0)
return false;
}

return true;
}

how to use for loop to calculate a prime number

The for loop you've used is correct for finding prime numbers. I would just another condition to it: if i > 1:. Also, you would want to print the prime number

for i in range(2, 101):
if i > 1: # Prime numbers are greater than 1
for j in range(2, i):
if (i % j) == 0:
print(i,"is a composite number")
break
else:
print(i,"is a prime number")

find prime numbers up to n digits using only 1 loop statement


//this program will print prime number from 2 to N . and break loop 
without using break statment

#include <iostream>

using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int range;
cin>>range;
int num=2;
for(int i=2;i<=num;i++){
if(num>range){
i=num+1;
}
else{
if(i==num){
cout<<num<<" ";
i=1;
num=num+1;
}
else if(num%i==0){
i=1;
num=num+1;
}
}
}
cout<<endl;
return 0;
}

Check Number prime in JavaScript

Time complexity: O(sqrt(n))

Space complexity: O(1)

const isPrime = num => {
for(let i = 2, s = Math.sqrt(num); i <= s; i++)
if(num % i === 0) return false;
return num > 1;
}

my python program is to find prime numbers between two intervals but i am getting 9 in the output can you please tell what mistake i am doing

In case of 9, you're checking if the number is divisible by 2, it's not, and then you print the number and don't check anything else.

You're also looping till i+1, meaning you check if the number is divisible by itself, which it is...

Try changing those:

start = int(input('enter starting point of interval:'))
end = int(input('enter ending point of interval:'))
for i in range(start,end+1):
if i>1:
for j in range(2,i):
if (i % j == 0):
break
else:
print(i, end = " ")

Also, instead of checking if i>1 in every loop, change the loop conditions, and you can loop until the square root.

End result:

import math
start = int(input('enter starting point of interval:'))
end = int(input('enter ending point of interval:'))
for i in range(max(start, 2),end+1):
if i % 2 == 0 and i != 2: continue
if all((i%j!=0) for j in range(3,int(math.sqrt(i))+1, 2)):
print(i, end = " ")

Python While Loop To Find Prime Does Not Terminate

Your loop is not changing n or i, which are the conditions on which it stops.

I think the correct code should be:

flag = 0
n = int(input('\nEnter whole number to check : '))
i = 2
while i <= (n/2):
if (n%i) == 0:
flag = 1
break
i += 1
if n == 1:
print('1 is neither prime nor composite')
elif flag == 0:
print(n,' is a prime number.')
elif flag == 1:
print(n,' is not a prime number.')

Checking whether a number is prime in Scala

What you did is a called defining a function so obviously it won't return anything and as a matter of fact, this function returns AnyVal which obviously won't help you much. I suspect that you actually need a Boolean type returned.

Since you are working with the REPL, you would want to define your function to check if a number is prime. I called it isPrime2 and then test it.

def isPrime2(i :Int) : Boolean = {
| if (i <= 1)
| false
| else if (i == 2)
| true
| else
| !(2 to (i-1)).exists(x => i % x == 0)
| }
// isPrime2: (i: Int)Boolean

(1 to 10).foreach(i => if (isPrime2(i)) println("%d is prime.".format(i)))
// 2 is prime.
// 3 is prime.
// 5 is prime.
// 7 is prime.

I would even suggest a simpler version if you care not using if else conditions :

def isPrime1(n: Int): Boolean = ! ((2 until n-1) exists (n % _ == 0))

Which also returns a Boolean.

EDIT:

As @TheArchetypalPaul stated, which was also implied, the problem is that your for loop isn't resulting in any value - you calculate a true/false value, but then don't do anything with it. So your else clause doesn't result in any value (in fact, it produces Unit). You need to return false as soon as you find a divisor - and the way to do that is probably exists as in isPrime1.



Related Topics



Leave a reply



Submit