Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Alessio Saltarin http://axsaxs.altervista.org

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Eratostene Sieve in Ruby

A simple Eratostene Sieve implementation
for the computing of prime numbers

#!/usr/bin/env ruby

#
# Compute prime numbers
# with the good old Eratostene Sieve method
# Author: Alessio Saltarin
#


# Eratostene Sieve computer class
class Sieve
    # Constructor
    # maxprime: max upper limit
    def initialize(maxprime)
            puts "Initializing sieve."
            @max = maxprime
            @sieve = Array.new
            @sieve[0] = 0
            @sieve[1] = 0
            for i in 2 ... @max
                    @sieve[i] = 1
            end
    end
    
    # Return a list of prime numbers
    def primes
            printf("Computing primes to %d ...\n", @max)
            crivello()
            puts "Done."
            retprimes = Array.new
            for k in 0..@max
                    if @sieve[k] == 1
                            retprimes.push(k)
                    end
            end
            return retprimes
    end
    
    # Sieve (Crivello) algorithm
    def crivello
            currprime = nextprime(0)
            while (currprime * currprime < @max)
                    i = currprime * currprime
                    while (i < @max)
                            @sieve[i] = 0
                            i += currprime
                    end
                    currprime = nextprime(currprime)
            end
    end

    # Return next prime
    # lastprime: the last computer prime number
    def nextprime(lastprime)
        
            for i in lastprime+1...@max
                    return i if @sieve[i] == 1
            end
    end
	
	
end

# Print a list of prime numbers
def printPrimes(primes)
    primes.each { |nr| printf("Prime: %d\n", nr) }
end

# Print the sum of prime numbers
def printSumPrimes(primes)
    sum = 0
    puts("Computing...")
    primes.each { |nr|  sum += nr }
    puts("Done.")
    printf("Prime Sum: %d\n", sum)
end

# Main
crivello = Sieve.new(1000000) # Compute primes for the first 1000000 natural numbers
printSumPrimes(crivello.primes)

MD5 and SHA1 in Ruby

// A Simple MD5/SHA1 calculator in Ruby

#!/usr/bin/ruby1.8 -w

#
# A Simple Hash Code Calculator
# Returns SHA1 and MD5 hash for any given file
# Author: Alessio Saltarin
#

require 'digest/md5'
require 'digest/sha1'

$BUFLEN = 1024

class Hasher
	# Constructor
	# method = "SHA1" or "MD5"
	# filepath = Full filepath
	def initialize(method, filepath)
		if (method.upcase == "-SHA1")
			@hashfunc = Digest::SHA1.new
			@hashname = "SHA1"
		else
			@hashfunc = Digest::MD5.new
			@hashname = "MD5"
		end
		@fullfilename = filepath
	end

	def hashname
		@hashname
	end

	# Compute hash code
	def hashsum
		open(@fullfilename, "r") do |io|
			puts "Reading "+@fullfilename
			counter = 0
			while (!io.eof)
				readBuf = io.readpartial($BUFLEN)
				putc '.' if ((counter+=1) % 3 == 0)
				@hashfunc.update(readBuf)
			end
		end
		return @hashfunc.hexdigest
	end
end

def usage
	puts "Usage: Hasher.rb [-SHA1|-MD5] filename"
end

def printresult(filename, method, sum)
	puts "\n" + filename + " ==> "+ method + ": " + sum	
end

#Program starts
if (ARGV.length == 2)
	hashcomp = Hasher.new(ARGV[0], ARGV[1])
	printresult(ARGV[1], hashcomp.hashname, hashcomp.hashsum)
	
elsif (ARGV.length == 1)
	hashcomp = Hasher.new("-MD5", ARGV[0])
	printresult(ARGV[0], hashcomp.hashname, hashcomp.hashsum)
else
	usage
end

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS