#!/bin/ruby
# author: Lukasz Korecki, student no: 0617836
# purpose: simple program generating Pythagorian triples
# Vars:
given_number = 60
m = 1
# Number definitions using the article from wikipedia:
# http://en.wikipedia.org/wiki/Pythagorean_triple#Other_formulas_for_generating_triples
#
a = m*2 + 1
b = (m*2) * (m + 1)
c = ((m*2) * (m + 1)) + 1
sum = a + b + c
# Output and formatting
sep = "\t|\t"
puts "Your max value is " + given_number.to_s
puts sep+"small"+sep+"medium"+sep+"large"+ sep + "sum" + sep
puts "-" * 80
# Main loop calculating the numbers and outputs
while c < given_number
m +=1
puts sep + a.to_s + sep+ b.to_s + sep +c.to_s + sep + sum.to_s + sep
a = m*2 + 1
b = (m*2) * (m + 1)
c = ((m*2) * (m + 1)) + 1
sum = a + b +c
end
require 'rubygems'
require 'inline'
# By Peter Cooper - http://www.rubyinside.com/
# Oodles of inspiration and examples from
# http://www-128.ibm.com/developerworks/linux/library/l-affinity.html
class LinuxScheduler
inline do |builder|
builder.include '<sched.h>'
builder.include '<stdio.h>'
builder.c '
int _set_affinity(int cpu_id) {
cpu_set_t mask;
__CPU_ZERO(&mask);
__CPU_SET(cpu_id, &mask);
if(sched_setaffinity(getpid(), sizeof(mask), &mask ) == -1) {
printf("WARNING: Could not set CPU Affinity, continuing as-is\n");
return 0;
}
return 1;
}
'
end
# cpu_id is 0-based, so for core/cpu 1 = 0, etc..
def self.set_affinity(cpu_id)
self.new._set_affinity(cpu_id.to_i)
end
end
# Set this process's CPU affinity
LinuxScheduler.set_affinity(ARGV.first)
# Hog up all the CPU time on that processor
1000000.times { b = rand(100) ** 100 }
"The lessons here are:
1. Temporary strings and temporary objects will stockpile. Just because you leave a method doesn't mean they are gone....
2. Arrays, hashes, objects are memory cheap. Worry about your strings. Do you have string corpses everywhere?
3. Build your object from all the pieces, avoiding temporary objects as much as possible. Then, when you're done with it, toss the big object out of scope at once!"