Printing prime numbers from 1 through 100
Three ways:
1.
int main ()
{
for (int i=2; i<100; i++)
for (int j=2; j*j<=i; j++)
{
if (i % j == 0)
break;
else if (j+1 > sqrt(i)) {
cout << i << " ";
}
}
return 0;
}
2.
int main ()
{
for (int i=2; i<100; i++)
{
bool prime=true;
for (int j=2; j*j<=i; j++)
{
if (i % j == 0)
{
prime=false;
break;
}
}
if(prime) cout << i << " ";
}
return 0;
}
3.
#include <vector>
int main()
{
std::vector<int> primes;
primes.push_back(2);
for(int i=3; i < 100; i++)
{
bool prime=true;
for(int j=0;j<primes.size() && primes[j]*primes[j] <= i;j++)
{
if(i % primes[j] == 0)
{
prime=false;
break;
}
}
if(prime)
{
primes.push_back(i);
cout << i << " ";
}
}
return 0;
}
Edit: In the third example, we keep track of all of our previously calculated primes. If a number is divisible by a non-prime number, there is also some prime <= that divisor which it is also divisble by. This reduces computation by a factor of primes_in_range/total_range.
Prime number from 1 to 100 in C
You have a couple of problems
First, the for(j=2;j<=i;j++)
loop should be for(j=2;j<i;j++)
, as if j is equal to i, j % i == 0
will always be true
Second, your flag variable is not doing what you might think it is doing. As HolyBlackCat suggested this is a good opportunity to learn how to use a debugger to see what is happening. You need to reset the flag variable, because once you find a single composite number, your flag will not reset so all following numbers will be flagged as composite. Add:
for(i=3;i<=100;i++)
{
flag = 0;
for(j=2;j<=i;j++)
{
C - Print all prime numbers from 1 to 100 using arrays
When you do this:
if(i % Primes[j] != 0 && Primes[j] !=0)
{
Primes[j]=i;
}
You're saying "if the current number is not divisible by the given prime number, replace the given prime number with the current number". This is not what you want.
You need to check if the current number is not divisible by any prime number. So you need to loop though the list of primes to make sure your number isn't divisible by any of them, and if so add the number to the end of the list. You can do that as follows:
int num_primes = 0;
for (i=2;i<101;i++)
{
int is_prime = 1;
for(j=0; j<num_primes && is_prime; j++)
{
if(i % Primes[j] == 0)
{
is_prime = 0;
}
}
if (is_prime) {
Primes[num_primes++] = i;
}
}
In the above code, we use num_primes
to count the number of primes we have so far, and is_prime
to see if we found a prime that divides the current number. As you divide each number by a prime, if the remainder is 0 you know the number is not prime and set is_prime
to 0. This also causes the inner loop to exit right away. Then if is_prime
is still set at the end of the inner loop, you have a prime and you add it to the end of the list.
You also have an off-by-one error in the printing loop:
for(k=0;k<51;k++)
Since Primes
has size 50, the largest valid index is 49. So change it to:
for(k=0;k<50;k++)
Print All Prime Numbers Between 1 and 100 using Java-script
This is how you can get the prime numbers without individual checks:
const isPrime = n => [...Array(n).keys()].slice(2).every(divisor => n % divisor !== 0)
const primeNumbers = [...Array(101).keys()].filter(isPrime)
console.log(primeNumbers)
Program that should print the first 100 prime numbers, only prints 0 and 1 instead
You forgot to add the curly brackets to the if:
if((i%j)==0) {
flag=1;
break;
}
So only the first line was under the condition, the break was executed each time.
Then you have a logical error, start the second loop with 2:
for(int i=1;i<100;++i){
for(int j=2;j<i;++j){
Now your code works. To understand why, add an additionally println like
public class Programma{
public static void main(String args[]){
int flag=0;
for(int i=1;i<100;++i){
for(int j=2;j<i;++j){
if((i%j)==0){
System.out.println("# " + i + "/" + j);
flag=1;
break;
}
}
if (flag==0)
System.out.println(i + " ");
flag = 0;
}
}
}
and try it with / without changed start values for the for loop.
Python displays all of the prime numbers from 1 through 100
try this
def is_prime(n):
status = True
if n < 2:
status = False
else:
for i in range(2,n):
if n % i == 0:
status = False
return status
for n in range(1,101):
if is_prime(n):
if n==97:
print n
else:
print n,",",
output
is2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97
Prime numbers between 1 to 100 in C Programming Language
The condition i==j+1
will not be true for i==2
. This can be fixed by a couple of changes to the inner loop:
#include <stdio.h>
int main(void)
{
for (int i=2; i<100; i++)
{
for (int j=2; j<=i; j++) // Changed upper bound
{
if (i == j) // Changed condition and reversed order of if:s
printf("%d\n",i);
else if (i%j == 0)
break;
}
}
}
Related Topics
Is Reading an Indeterminate Value Undefined Behavior
Scope VS Life of Variable in C
How to Read Files in Sequence from a Directory in Opencv
Random Output Different Between Implementations
Statically Declared 2-D Array C++ as Data Member of a Class
C++ Template Copy Constructor on Template Class
Super High Performance C/C++ Hash Map (Table, Dictionary)
How to Navigate Through a Vector Using Iterators? (C++)
Most Optimized Way of Concatenation in Strings
Why Do We Actually Need Private or Protected Inheritance in C++
What Are Declarations and Declarators and How Are Their Types Interpreted by the Standard
C++:Creating an Array with a Size Entered by the User
Statically Declared 2-D Array C++ as Data Member of a Class
Can Lambda Functions Be Templated
How Is the C++ Exception Handling Runtime Implemented
Boost Thread Throwing Exception "Thread_Resource_Error: Resource Temporarily Unavailable"