+ 1

# Maybe someone can help me... I don't get why 16 is printed/added to the array .....

9 Answers

+ 2

if (k % prime[j] == 0) {
k++;
j=0;
}
You need to set j = -1.
j gets incremented by the loop. If you set it to 0 you overjump 0 because of j++. The loop restarts at 1.

+ 2

Jan Your welcome :)

+ 1

Oh yes, you're right.
Thank u so much!
du kommst auch aus Deutschland?

+ 1

Yes, I know it already. But it is not as fast as my algorithm, isn't it?

0

Previously I thought about incrementing k by 2, cause every even number cannot be a prime at all. That would have solved the Problem as well, because checking modulo2 wouldn't be necessary than.

0

It would be one operation less of course xD

0

Jan
If you are interested you can also have a look on the sieve of eratosthenes: https://www.sololearn.com/learn/969/?ref=app

0

Jan I am not so good in calculating time complexity ;)

0

So my programme will return the the first n primes. The Sieve will return an unknown count of primes up to a specific number.
Yeah you can add such a function to the sieve through an estimation by the primefunction, but it wouldn't be that trivial than and of course more complex.