So I am a beginner in C and while I have experience in python I can't wrap my brain around a simple problem.
Finding prime numbers in Python was a easier task if I remember but I am facing difficulties while doing the same in C.
So from the tutorials I've been following this is code to find the prime numbers between 3 and 100 using arrays:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main()
{
int p;
int i;
int primes[50] = { 0 };
int primeIndex = 2;
bool isPrime;
// hardcode prime numbers
primes[0] = 2;
primes[1] = 3;
for (p = 5; p <= 100; p = p + 2)
{
isPrime = true;
for (i = 1; isPrime && p / primes[i] >= primes[i]; ++i)
if (p % primes[i] == 0)
isPrime = false;
if (isPrime == true)
{
primes[primeIndex] = p;
++primeIndex;
}
}
for (i = 0; i < primeIndex; ++i)
printf("%i ", primes[i]);
printf("\n");
return 0;
}
The first for loop in the above code was understandable but what happened after the second one is confusing me.
What is the logic behind the p / primes[i] >= primes[i] condition in the second for loop?
For example if I follow the loop taking p = 5 and apply the condition it'll be 5 / primes[1] >= primes[1]:
Since we know primes[1] = 3 this will become 5 / 3 >= 3 which immediately becomes false.
Please explain what is happening after the first for loop
Edit: I have added the python code which I can do by myself. Why isn't it possible to do the same way in C:
lst = [2]
for i in range(3, 100):
for j in range(2, i):
if (i % j) == 0:
break
else:
lst.append(i)
print(lst)