class Pascal

Public Instance Methods

Armstrong(n) click to toggle source
# File lib/math_func.rb, line 21
def Armstrong(n)
        num = n
        sum = 0
        temp = num
        while num>0
                sum = sum + (num%10) * (num%10) * (num%10)
                num = num/10       
        end
        if temp == sum
                 "#{temp} is an Armstrong Number"
                
        else
                 "#{temp} is not an Armstrong Number"
        end
end
func() click to toggle source
# File lib/math_func.rb, line 14
def func
        fact = Inherit.new
        prompt = "Enter a number between the given range"
        loop do
                print "Menu:\n1 : Armstrong Number\n2 : Factorial\n3 : Prime Number\n4 : Pascal Triangle\n0 : Exit\nPlease enter your choice : "
                selection = gets.to_i
                if selection == 1
                def Armstrong(n)
                        num = n
                        sum = 0
                        temp = num
                        while num>0
                                sum = sum + (num%10) * (num%10) * (num%10)
                                num = num/10       
                        end
                        if temp == sum
                                 "#{temp} is an Armstrong Number"
                                
                        else
                                 "#{temp} is not an Armstrong Number"
                        end
                end
                        print "Enter a number: "
                        n = gets.to_i
                        # catch (n > 0) do
                        #   while n <= 0
                        #           puts "Enter number only"
                        #           throw id <= 0 unless id = gets.to_i
                        #   end
                        # end
                puts Armstrong(n)
                elsif selection == 2
                        print "Enter your choice:\n1 : Factorial\n2 : ncr\n3 : npr\n"
                        print "Enter a number between 1 to  3 : "
                        
                        select = gets.to_i
                        case select
                                when 1
                                        puts"Factorial:"
                                        print "Enter a number: "
                                        n = gets.to_i
                                        print "Factorial of #{n} of is ",  fact.factorial(n), "\n\n"
                                        
                                when 2
                                        puts "ncr:"
                                        print "Enter the value of n "
                                        n = gets.to_i
                                        print "Enter the value of r "
                                        r = gets.to_i
                                        res = fact.factorial(n) / (fact.factorial(r))
                                        
                                        puts "ncr of given variables is #{res}"
                                when 3
                                        puts "npr:"
                                        print "Enter the value of n: "
                                        n = gets.to_i
                                        print "Enter the value of r: "
                                        r = gets.to_i
                                        res = fact.factorial(n) / (fact.factorial(r) * fact.factorial(n-r))
                                        puts "ncr of given variables is #{res}"
                        end
                elsif selection == 3
                        print "Enter a number \t"
                        n = gets.to_i
                        def prime(n)
                        puts "That's not an integer." unless n.is_a? Integer
                        is_prime = true
                        for i in 2..n-1
                     if n % i == 0
                             is_prime = false
                     end
                     end
                        if is_prime
                     puts "#{n} is prime!"
                     else
                     puts "#{n} is not prime."
                     end
                                end
                                prime(n)
                elsif selection == 4
                        print "Enter how many rows you want: "
                        n = gets.to_i
                        def triangle(n)
                                fact = Inherit.new
                                for i in (0..n)     
                                        for j in (0..n-i-1)
                                        print " " 
                                        end
                                        for j in (0..i)
                                                res = fact.factorial(i)/(fact.factorial(j) * fact.factorial(i-j))
                                                print res
                                                print " "
                                        end
                                        puts "\n"
                                end
                        end
                        triangle(n)
                elsif selection == 0
                        puts "Thank you"
                        break
                else
                        print "Enter a number from the given range only"
                        redo
                end  
end    
end
prime(n) click to toggle source
# File lib/math_func.rb, line 78
   def prime(n)
   puts "That's not an integer." unless n.is_a? Integer
   is_prime = true
   for i in 2..n-1
if n % i == 0
        is_prime = false
end
end
   if is_prime
puts "#{n} is prime!"
else
puts "#{n} is not prime."
end
           end
triangle(n) click to toggle source
# File lib/math_func.rb, line 96
def triangle(n)
        fact = Inherit.new
        for i in (0..n)     
                for j in (0..n-i-1)
                print " " 
                end
                for j in (0..i)
                        res = fact.factorial(i)/(fact.factorial(j) * fact.factorial(i-j))
                        print res
                        print " "
                end
                puts "\n"
        end
end