+ 1

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

https://code.sololearn.com/cKd558kmCMPL/?ref=app

30th Jan 2020, 12:41 AM
Jan
9 odpowiedzi
+ 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.
30th Jan 2020, 1:52 AM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Jan Your welcome :)
30th Jan 2020, 1:54 AM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Oh yes, you're right. Thank u so much! du kommst auch aus Deutschland?
30th Jan 2020, 1:53 AM
Jan
+ 1
Yes, I know it already. But it is not as fast as my algorithm, isn't it?
30th Jan 2020, 2:16 AM
Jan
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.
30th Jan 2020, 1:58 AM
Jan
0
It would be one operation less of course xD
30th Jan 2020, 2:06 AM
Jan
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
30th Jan 2020, 2:14 AM
Denise Roßberg
Denise Roßberg - avatar
0
Jan I am not so good in calculating time complexity ;)
30th Jan 2020, 2:18 AM
Denise Roßberg
Denise Roßberg - avatar
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.
30th Jan 2020, 2:24 AM
Jan