{"action":"created","issue":{"url":"https://api.github.com/repos/manastech/crystal/issues/751","labels_url":"https://api.github.com/repos/manastech/crystal/issues/751/labels{/name}","comments_url":"https://api.github.com/repos/manastech/crystal/issues/751/comments","events_url":"https://api.github.com/repos/manastech/crystal/issues/751/events","html_url":"https://github.com/manastech/crystal/pull/751","id":85924249,"number":751,"title":"adding the sieve of eratosthenes example.","user":{"login":"mrjbq7","id":24195,"avatar_url":"https://avatars.githubusercontent.com/u/24195?v=3","gravatar_id":"","url":"https://api.github.com/users/mrjbq7","html_url":"https://github.com/mrjbq7","followers_url":"https://api.github.com/users/mrjbq7/followers","following_url":"https://api.github.com/users/mrjbq7/following{/other_user}","gists_url":"https://api.github.com/users/mrjbq7/gists{/gist_id}","starred_url":"https://api.github.com/users/mrjbq7/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mrjbq7/subscriptions","organizations_url":"https://api.github.com/users/mrjbq7/orgs","repos_url":"https://api.github.com/users/mrjbq7/repos","events_url":"https://api.github.com/users/mrjbq7/events{/privacy}","received_events_url":"https://api.github.com/users/mrjbq7/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-06-07T17:55:13Z","updated_at":"2015-06-17T04:04:14Z","closed_at":"2015-06-16T16:50:20Z","pull_request":{"url":"https://api.github.com/repos/manastech/crystal/pulls/751","html_url":"https://github.com/manastech/crystal/pull/751","diff_url":"https://github.com/manastech/crystal/pull/751.diff","patch_url":"https://github.com/manastech/crystal/pull/751.patch"},"body":"I thought it might be nice to preserve the example in the ``samples`` directory."},"comment":{"url":"https://api.github.com/repos/manastech/crystal/issues/comments/112644574","html_url":"https://github.com/manastech/crystal/pull/751#issuecomment-112644574","issue_url":"https://api.github.com/repos/manastech/crystal/issues/751","id":112644574,"user":{"login":"mrjbq7","id":24195,"avatar_url":"https://avatars.githubusercontent.com/u/24195?v=3","gravatar_id":"","url":"https://api.github.com/users/mrjbq7","html_url":"https://github.com/mrjbq7","followers_url":"https://api.github.com/users/mrjbq7/followers","following_url":"https://api.github.com/users/mrjbq7/following{/other_user}","gists_url":"https://api.github.com/users/mrjbq7/gists{/gist_id}","starred_url":"https://api.github.com/users/mrjbq7/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mrjbq7/subscriptions","organizations_url":"https://api.github.com/users/mrjbq7/orgs","repos_url":"https://api.github.com/users/mrjbq7/repos","events_url":"https://api.github.com/users/mrjbq7/events{/privacy}","received_events_url":"https://api.github.com/users/mrjbq7/received_events","type":"User","site_admin":false},"created_at":"2015-06-17T04:04:14Z","updated_at":"2015-06-17T04:04:14Z","body":"(Actually, I tried using a simple bit array below and it slows down the sieve a little bit, perhaps I need to learn a bit more about how to express low-level math concepts used).\r\n\r\n```crystal\r\n# Compute prime numbers up to 100_000_000 with the Sieve of Eratosthenes\r\nmax = 1_000_000_000\r\n\r\nwheel = [2,4,2,4,6,2,6,4,2,4,6,6,2,6,4,2,6,4,6,8,4,2,4,2,4,\r\n         8,6,4,6,2,4,6,2,6,6,4,2,4,6,2,6,4,2,4,2,10,2,10]\r\n\r\nbits = ((((max / 210) + 1) * 210) / 2) + 6\r\n\r\nints = (bits + 31) >> 5\r\nsieve = Array.new(ints, 0xffffffff)\r\nsieve[0 >> 5] &= ~(1 << (0 & 31))\r\nsieve[4 >> 5] &= ~(1 << (4 & 31))\r\n\r\nupto = Math.sqrt(max).to_i\r\n\r\ni = 11\r\nwhile i < upto\r\n  wheel.each do |j|\r\n    if sieve[(i/2) >> 5] & (1 << ((i/2) & 31)) > 0\r\n      (i * i).step(max, i * 2) do |j|\r\n        sieve[(j/2) >> 5] &= ~(1 << ((j/2) & 31))\r\n      end\r\n    end\r\n    i += j\r\n  end\r\nend\r\n\r\nprimes = [2,3,5,7]\r\ni = 11\r\nwhile i <= max\r\n  wheel.each do |j|\r\n    if sieve[(i/2) >> 5] & (1 << ((i/2) & 31)) > 0\r\n      primes.push(i)\r\n    end\r\n    i += j\r\n    if i > max\r\n       break\r\n    end\r\n  end\r\nend\r\n\r\nputs primes.length\r\n```"}}