Images, snippets, snapshots, math

View Gabriele Lami's profile on LinkedIn

venerdì 3 ottobre 2008

My first Ruby program!

This is my first Ruby program!

N = 13
def un( el, en )
[1, en-1].include?(el%en)?re=1:re=" "; return re
end

(N-1).times do a
(1...N-1).to_a.each{b print un((b+1)*(a+1),N), " "}
print "\n"
end
A little obscure, isn't it?
The output is ( as desired ):

1                     1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1

The same program in Scala can be written in this way:
object something {
val un = ( el:Double, en:Double ) =>
if( Array(1,en-1).contains( el%en) ) "1 " else " "

def main(args : Array[String]) : Unit = {
val N = 23
for(i <- 1 to N-1){ new Array(N-1)

.indices.foreach(el => print( un((el+1)*(i),N) ))
println()
}
}
}
and the output with N=23 is:
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1

Nessun commento: