Skip to content

Instantly share code, notes, and snippets.

@dmitriy-kiriyenko
Created May 30, 2012 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitriy-kiriyenko/2834414 to your computer and use it in GitHub Desktop.
Save dmitriy-kiriyenko/2834414 to your computer and use it in GitHub Desktop.
Find two skipped numbers in a natural sequence
# Run it only with ruby 1.9.
range = (1..(ARGV[0] || 10).to_i)
@g = Random.new
def gen_skips(n, range, ready = [])
return ready if ready.size == n
gen_skips(n, range, ready.push(@g.rand(range)).uniq)
end
skips = gen_skips(2, range).sort
fn = 'data.txt'
puts "Generating...\n\n"
step = [range.first, (range.last - range.first + 1)/100].max.to_i
File.open(fn, 'w') do |f|
range.each do |n|
print '*' if n % step == 0
f.puts(n) unless skips.include?(n)
end
end
puts "\n\nGenerated #{fn} with numbers from #{range.first} to #{range.last} with #{skips.join(', ')} skipped."
@dmitriy-kiriyenko
Copy link
Author

Обновлённая производительность:

                                            10000    1000000        100000000
=====================================================================================
  python andy128kpython.py                   0.046      1.188       couldn't wait
  sbcl --script andy128k.lisp                0.073      0.675       heap exchausted
  ruby bronislav.rb                          0.148      0.761       couldn't wait
  ruby zhck.rb                               0.149      0.739       couldn't wait
  ruby dmitriy-kiriyenko.rb                  0.152      1.139       couldn't wait
* ruby raven29_cool.rb                       0.145      0.158       0.161
  ruby raven29.rb                            1.885   3:34.870       not measured
  mysql test_num < dmitriy-kiriyenko.sql  1:13.460   not measured   not measured

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment