Skip to content

Instantly share code, notes, and snippets.

@dtinth
Created February 2, 2011 13:14
Show Gist options
  • Save dtinth/807654 to your computer and use it in GitHub Desktop.
Save dtinth/807654 to your computer and use it in GitHub Desktop.
fs = require 'fs'
sys = require 'sys'
(-> # force global scope
input =
if process.argv[2]
try
fs.readFileSync process.argv[2], 'utf-8'
catch e
''
else
''
@print = (x...) ->
sys.print y + '\n' for y in x
@scanNumber = ->
if m = input.match(/[\-0-9\.]+/)
input = input.substr(m.index + m[0].length)
parseFloat(m[0])
else
false
@scanWord = ->
if m = input.match(/\S+/)
input = input.substr(m.index + m[0].length)
m[0]
else
false
@scanText = ->
if m = input.match(/\S(.*\S|)/)
input = input.substr(m.index + m[0].length)
m[0]
else
false
@scanArray = ->
result = input.split '\n'
input = ''
result
@generatePrimeDB = (upto) ->
max = Math.ceil(Math.sqrt(upto))
out = {}
for i in [2..max]
if not out[i]?
j = i * 2
while j <= upto
out[j] = 1
j += i
ret = []
for i in [2..upto]
if not out[i]
ret.push i
out.list = ret
out
@call = (func) ->
func()
).call null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment