#include <stdio.h>
int main()
{
int num,i;
printf("Enter a number");
scanf("%d",&num);
i=2;
while(i<=num-1)
{
if(num%i==0)
{
printf("Not a prime number\n");
break;
}
[B] i++;
[/B]}
if(i==num)
printf("Prime number\n");
}
8 replies to this topic
#1
Posted 27 November 2010 - 07:28 AM
Hi, this is a program to detect prime numbers or not. I understand what the program does except for the i++ part. Can someone tell me why does the program have i++ included?
|
|
|
#2
Posted 27 November 2010 - 08:35 AM
What is about this? :)
#include <stdio.h>
int main()
{
int num,i;
printf("Enter a number");
scanf("%d",&num);
i=2;
while(i<=num-1)
{
if(num%i==0)
{
printf("Not a prime number\n");
break;
}
i++;
}
if(i==num)
printf("Prime number\n");
}
#3
Posted 27 November 2010 - 09:50 AM
i++ is just fancy way of saying i = i + 1
Also, when checking for prime's, the fastest way I know of is checking up to square of that number.w
Also, when checking for prime's, the fastest way I know of is checking up to square of that number.w
#include <iostream>
#include <cmath>
bool is_prime(int n) {
if (n < 2) // 0 and 1 and not primes
return false;
int sn = sqrt(abs(n)); // abs if negative values is passed, and sqrt for speed
for (int i = 2; i < sn + 1; ++i)
if ((n % i) * n == 0)
return false;
return true;
}
int main() {
for (int i = 0; i < 20; ++i)
if (is_prime(i))
std::cout << i << " is prime\n";
return 0;
}
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#4
Posted 27 November 2010 - 10:05 AM
Flying Dutchman said:
Also, when checking for prime's, the fastest way I know of is checking up to square of that number.
Also, when checking for prime's, the fastest way I know of is checking up to square root of that number.
I am a student of C language.
#5
Posted 27 November 2010 - 10:36 AM
i++ here is increment of that number. i++ also same with i=i+1. For example, it starts from 2. And increments by one. It checks until num-1. Firstly it checkes 2, then 3,4,5, etc. If you dont put increment, then your program wont work. It ll only work with 2.
#6
Posted 27 November 2010 - 11:03 AM
Hi,
The program is running in a loop where it determines whether given number is a prime or not.
The logic is that it loop goes on until one of the following happens. i++ provides a next number each turn the loop runs
1. number is dividable by i
2. i reaches equal to number.
if you remove i++: the loop would go on forever:
Munir
The program is running in a loop where it determines whether given number is a prime or not.
The logic is that it loop goes on until one of the following happens. i++ provides a next number each turn the loop runs
1. number is dividable by i
2. i reaches equal to number.
if you remove i++: the loop would go on forever:
Munir
#7
Posted 27 November 2010 - 11:13 AM
nerio said:
Also, when checking for prime's, the fastest way I know of is checking up to square root of that number.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#8
Posted 27 November 2010 - 11:22 AM
Just to add a reference to Sieve of Eratosthenes algorithm for finding all prime numbers in range 2-N for any given N. It operates very fast, and can be memory optimized by spending 1 bit per number, meaning that N/8 bytes are required to find all prime numbers up to N.
#9
Posted 27 November 2010 - 12:25 PM
Thanks all! I now understand the entire program. It took me a while to understand the logic, it wasn't easy!
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









