JavaScript. Find prime numbers, very fast.

158 views
Skip to first unread message

Juan Antonio

unread,
Jul 30, 2018, 12:19:58 PM7/30/18
to MIT App Inventor Forum
Find prime numbers by JavaScript, gets primes very fast.

<!DOCTYPE html>
<html>
<head><meta charset="utf-8"></head><body>
<script>

   datos =  window.AppInventor.getWebViewString(); // Entrada de datos.

   window.AppInventor.setWebViewString("" + getPrimes(datos));  // Respuesta a CadenaDeWebView
   
   function getPrimes(max) {
    var sieve = [], i, j, primes = [];
    for (i = 2; i <= max; ++i) {
        if (!sieve[i]) {
            // i has not been marked -- it is prime
            primes.push(i);
            for (j = i << 1; j <= max; j += i) {
                sieve[j] = true;
            }
        }
    }
    return primes;
}

</script>
</body></html>
JS code: https://stackoverflow.com/questions/11966520/how-to-find-prime-numbers-between-0-100

Sieve_numeros_primos.aia
Reply all
Reply to author
Forward
0 new messages