top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Prime numbers in Range?

0 votes
306 views

I am writing a program to print the prime numbers in the given range, I know we can pick number one by one and try to see if it is divisible by 1 to n-1 or not. Do we have a better way to achieve this?

posted Jun 26, 2014 by Salil Agrawal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Try something like
Approach 1:
(a^n) mod n = a mod n where a and n are co prime then n is a prime number. (you know how to simplify the mod and ^ operations else use google)

Approach 2:
If number is not divisible by 2-sqrt(n) then also n is prime, this will save some iteration.

answer Jun 26, 2014 by anonymous
Let me try 1st seems to be promising...
0 votes

You dont have to check every number's divisibility by 1 to n-1. Just check the divisibility by 2 to n/2

and moreover to reduce complexity(time and space both) n can be incremented by 2, once you are on odd number.. as even numbers cant be prime for sure.

Like prime numbers between 1-20 are
2,
3, [start incrementing n by 2 to check further and keep checking division of n from 2 to n/2],
5,
7,
9 (while checking division here from 2 to n/2, and you would find its not a prime),
11,
13,
15 (while checking division here from 2 to n/2, and you would find its not a prime)
17,
19
Note: 1 is neither prime nor composite

or
if your range starts from 1 - n always then
keep a list of checked prime numbers and later check the divisibility of any number by primes only (which are less than or equal to half of n).

just check out these approaches, if they might be useful.

answer Jun 26, 2014 by Shobhit Upadhyay
Similar Questions
0 votes

208/5000
Write a program that will fill prime numbers starting with the first prime number within a given range in an array?
* Number is a prime if it is is divisible only by 1 and itself.
* Size of array is user-defined.
* User defines number of elemenets in array.(needs to be dinamicly allocated)
* Print created array necessary at the end.

+2 votes

it is illegal to share some certain prime numbers in US and if you do so,you will get arrested.Follow this link to get more details :
https://www.youtube.com/watch?v=LnEyjwdoj7g
My question is : it is very easy to find multiplication of two prime numbers but tough to get what are the prime numbers which multiplication results this number . Example : 7*11= 77 but you are given 77 = ? * ? where ?= two prime numbers only (ans = 7 and 11).Another example : 11*13= 143 but you need to solve it for 143 and you output : 11 13
So I need a program ,efficient one, to get solutions to such type of mathematical problem.
Adding more examples:
input:1. 21 -> 3,7
2. 189 -> 3,7,9
3.1363 -> 29,47 .... and if is not possible then print -1 example 1. 4 and its factors 2*2 where 2 is repeated so print -1
2. 9-> -1
3. 24->-1 ( prime factors are 2^3 * 3= 8*3 where 8 is not prime so print -1).
Thanks in advance.

+2 votes

How to find prime numbers between given intervals? Sample code along with the complexity would be helpful?

+4 votes

Requirements:

1.No input should be processed, and the output should be in the form of 2 3 5 7 11 13 ... etc.
2. No reserved words in the language are used at all
3.The language should at least allow structured programming, and have reserved words (otherwise point 2 would be moot).

...