Advertisement
Guest User

Dart lang prime numbers benchmark

a guest
Apr 8th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'dart:core';
  2. import 'dart:math';
  3. import 'dart:io';
  4.  
  5. get_primes7(num n) {
  6.     if (n < 2) { return []; }
  7.     if (n == 2) { return [2]; }
  8.  
  9.     var s = [];
  10.     for (var i = 3; i < n + 1; i += 2) {
  11.         s.add(i);
  12.     }
  13.  
  14.     var mroot = sqrt(n).round();
  15.     var half = s.length;
  16.     var i = 0;
  17.     var m = 3;
  18.  
  19.     while (m <= mroot) {
  20.         if (s[i] != 0) {
  21.             var j = ((m*m-3)/2).round();
  22.             s[j] = 0;
  23.             while (j < half) {
  24.                 s[j] = 0;
  25.                 j += m;
  26.             }
  27.         }
  28.         i = i + 1;
  29.         m = 2*i + 3;
  30.     }
  31.  
  32.     var res = [2];
  33.     for (var x = 0; x < s.length; x++) {
  34.         if (s[x] != 0) {
  35.             res.add(s[x]);
  36.         }
  37.     }
  38.     return res;
  39. }
  40.  
  41. main() {
  42.     for (var i = 0; i < 10; i++) {
  43.         var res = get_primes7(10000000);
  44.         stdout.writeln("Found " + res.length.toString() + " prime numbers.");
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement