]> git.cryptolib.org Git - arm-crypto-lib.git/blob - host/get_primes.rb
Adding Khazad
[arm-crypto-lib.git] / host / get_primes.rb
1 #!/usr/bin/ruby 
2 # get_primes.rb
3 =begin
4     This file is part of the ARM-Crypto-Lib.
5     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
6
7     This program is free software: you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 =end
20
21 $primes = [2,3,5]
22
23 def check_prime(a)
24   q = Math.sqrt(a)
25   $primes.each{ |p|
26     return true if p>q 
27         return false if a%p==0
28   }
29   return true
30 end
31
32 def find_primes(n)
33   a = $primes.last+2
34   while $primes.size < n
35     if check_prime(a)
36           $primes << a
37         end  
38         a += 2
39   end
40 end
41
42 if ARGV.size!=1
43   STDERR.print <<EOF
44   Usage: ruby #{$0} n
45    
46 EOF
47   exit(1)
48 end
49
50 find_primes(ARGV[0].to_i);
51 print($primes.join(', '))
52 puts("")