Pergunta de entrevista da empresa Yahoo

Find the prime numbers less than 10,000 using dynamic programming.

Resposta da entrevista

Sigiloso

16 de jan. de 2012

int inc = 2; int j = 5; int k = 0; int prime[1000]; boolean jprime = false; while (k < 1000) { j = j + inc; inc = 6 - inc; int sqrt = sqrt(j); for(int l = 0; l < sqrt; l++) { if (j /prime[l])*prime[l] == 1) jprime = false; } if (!jprime) continue; else prime[k] = j; k++ } The above logic works over throdd numbers. All prime numbers are throdd numbers. The format of throdd numbers is 6k + 1 || 6k + 5; Calculate the next throdd and check if it is prime by dividing it by already calcualted primes. We dont need to check the divisiblity will all the calculated results so far and only till square root of next potential number.