-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Karthikeyan A K
committed
Aug 25, 2012
1 parent
2963f60
commit 8e673bf
Showing
11 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/ruby | ||
# hash_creation_1_a.rb | ||
marks = { :English => 50, :Math => 70, :Science => 75 } | ||
puts "Key => Value" | ||
marks.each { |a,b| | ||
puts "#{a} => #{b}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
n = 1 | ||
|
||
while (2**n) <= (100*n*n) | ||
# n += 0.01 | ||
n = n + 0.1 | ||
puts n | ||
end | ||
|
||
puts n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class Array | ||
def sum | ||
total = 0 | ||
for element in self | ||
total += element | ||
end | ||
total | ||
end | ||
|
||
def increment base = 1 | ||
array = self.collect {|x| x } | ||
array_length = array.length | ||
i = 0 | ||
loop do | ||
unless array[i] | ||
array << 0 | ||
return array | ||
end | ||
|
||
if array[i] % base == 0 and array[i] !=0 | ||
array[i] = 0 | ||
i = i+1 | ||
else | ||
array[i] = array[i]+1 | ||
return array | ||
end | ||
end | ||
end | ||
end | ||
|
||
number = 5 # just change this to get various combinations | ||
a = [0] | ||
loop do | ||
puts a.join ' + ' if a.sum == number | ||
break if a.length > number | ||
a = a.increment number | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# printa a diamond of desired height | ||
|
||
height = 7 # height should be minimu 3 and must be a odd number | ||
h = height - 2 | ||
width = 2*h-3 | ||
j=1 | ||
|
||
(h-1).downto(0) { |i| | ||
puts " "*(i) + "#"*j | ||
j=j+2 | ||
} | ||
|
||
(1).upto(h-1) { |i| | ||
puts " "*i + "#" * width | ||
width=width-2 | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
re |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Fixnum | ||
def prime? | ||
b = 0 | ||
a = self | ||
return true if a <= 2 | ||
2.upto(a){ |n| | ||
b = n | ||
break if a%n == 0 | ||
} | ||
b == a | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require './fixnum_prime.rb' | ||
|
||
# display every odd number from 4 to 100 as sum of two different prime | ||
# numbers | ||
|
||
prime_array = [] #empty array | ||
1.upto(100){ |x| prime_array << x if x.prime? } # gets all pime from 1 to 100 into prime_array | ||
|
||
4.step(100,2){ |num| | ||
for prime in prime_array | ||
diff = num - prime | ||
if prime_array.index diff | ||
puts "#{num} = #{prime} + #{diff}" | ||
break # comment break to have all possible combinations | ||
end | ||
end | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
require './fixnum_prime.rb' | ||
|
||
1.upto(100){ |x| puts x if x.prime? } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# program prints a pyramid | ||
height = 15 | ||
height.times{ |j| puts " "*(height-(1+j)) + "#"*(2*j+1) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# program prints a pyramid | ||
height = 15 | ||
space = height -1 | ||
i = 0 | ||
height.times{ | ||
puts " "*space + "#"*(2*i+1) | ||
space = space -1 | ||
i = i+1 | ||
} |