473,395 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

program for prime numbers in c language

What is the minimum range of numbers which we need to go necessarily to check either a number is prime or not?(for all integers)
Jan 12 '13 #1

✓ answered by weaknessforcats

Just stop the loop if you get any factor at all. A prime number has only itself and one as factors. If you get to n/2 and no factors, you have a prime.

6 3205
weaknessforcats
9,208 Expert Mod 8TB
Think about it. The largest number that can divide into another number cannot be more than half as large since 2 is the lowest factor.

BTW: This is not a C++ question.
Jan 12 '13 #2
I know that I must go till n/2 but I was asking for the minimum range,because I don`t want my loop to run so many times!
And its a programme on which I am working not c++
Jan 13 '13 #3
weaknessforcats
9,208 Expert Mod 8TB
Just stop the loop if you get any factor at all. A prime number has only itself and one as factors. If you get to n/2 and no factors, you have a prime.
Jan 13 '13 #4
donbock
2,426 Expert 2GB
To determine if N is a prime number you only need to do trial divisions for potential factors up to the square root of N. However, don't call the sqrt() function -- instead, [after the trial division] stop if the square of the potential factor is greater than or equal to N.

If the purpose of your program is to determine if some arbitrary integer is prime then go ahead and use the trial-division algorithm. However, if your goal is to find all prime numbers less than some particular limit then you don't need to divide at all! Look up "sieve of Eratosthenes".
Jan 15 '13 #5
To check whether given number is prime or not use following code...
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. void main()
  4. {
  5.    int n, c = 2;
  6.  
  7.    printf("Enter a number to check if it is prime\n");
  8.    scanf("%d",&n);
  9.  
  10.    for ( c = 2 ; c <= n - 1 ; c++ )
  11.    {
  12.       if ( n%c == 0 )
  13.       {
  14.          printf("%d is not prime.\n", n);
  15.      break;
  16.       }
  17.    }
  18.    if ( c == n )
  19.       printf("%d is prime.\n", n);
  20.  
  21.   getch();
  22. }
Jan 24 '13 #6
Banfa
9,065 Expert Mod 8TB
You could use that code but it is horribly inefficient.

It checks to see if the number is divisible by all the even numbers when having checked to see if it is divisible by 2 and it isn't it is by definition not divisible by any other even number. This is easy to avoid.

In a similar vain it checks to see if the number is divisible by all multiples of 3 when having checked to see if it is divisible by 3 and it isn't it is by definition not divisible by any other multiple of 3. This is also easy to avoid.

It checks all numbers up to n-1 to see if they are a divisor of n there by checking about squareRoot(n) times more numbers that is required. Also easy to fix.

These inefficiencies mean that if you were trying to check if 10007 is prime you would test 294 times more numbers than required to determine that it is prime (10005 instead of 34 (ish)).
Jan 24 '13 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: AshifToday | last post by:
this was my and my frineds little project in earlier classes, the program seperates the composite and prime numbers in two sections of the screen ===================== /* This program has...
13
by: Don | last post by:
> How do you write a program to print the prime numbers > up to 1 million? #include <stdio.h> /* This program implements a blindingly fast O(n^n) algorithm to find prime numbers, using an...
3
by: triplejump24 | last post by:
Hey. Im trying to make program that basically displays all the prime numbers. I need to use bool and for but im not quite sure if i have this right. So far i have this bunch of a mess but can...
25
by: johnmsimon | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
60
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
4
by: Caffiend | last post by:
Thanks to everyone who helped me out with my early version of the prime number calculator... Here is the complete and updated version including unsigned 64 bit integers...mmmmmm....... Lots of fun...
5
by: trillianx | last post by:
I know there has been lot of discussion on Prime numbers program but I have very specific question. Here is my program: # Find the prime numbers # This is a user input program that lets you...
2
Blackout
by: Blackout | last post by:
Hi, I'm having problems with this C program. Whenever I run it, it doesn't print anything. The program is supposed to compute and display all the prime numbers from 1 - 300 using the sieve of...
4
by: Villanmac | last post by:
Hi i have made the following program to make a fucntion that determines if a number is prime. Than i am supposed to use this function to print all prime numbers between 1 and 10000. What am I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.