math_func 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/math_func.rb +120 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5dfcb47db64caa12565c6a62c633190a88504111
|
4
|
+
data.tar.gz: e67d37e345b15786226119289e4a1fce90d5bc8d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e27018baae3c4fa32626d4ba77770c7d4e7172bf4d9e54a760fb4f48b7ca785db9cae95309c55e390cf4a6aec99300d4c08fde8ef154f1d83fe9e7b2200463fa
|
7
|
+
data.tar.gz: ce20577fa5a0911c38dc9ca8683ba4c44ac34fb8a57f28e540c533591848c3555af01350745550b8b1c3cd077a468ffa80058b14ba522b3586b7e9da823b91d3
|
data/lib/math_func.rb
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
module Modules
|
2
|
+
class Inherit
|
3
|
+
def factorial(n)
|
4
|
+
if n == 0 || n == 1
|
5
|
+
1
|
6
|
+
else
|
7
|
+
n = n * factorial(n-1)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
class Pascal
|
13
|
+
include Modules
|
14
|
+
def func
|
15
|
+
fact = Inherit.new
|
16
|
+
prompt = "Enter a number between the given range"
|
17
|
+
loop do
|
18
|
+
print "Menu:\n1 : Armstrong Number\n2 : Factorial\n3 : Prime Number\n4 : Pascal Triangle\n0 : Exit\nPlease enter your choice : "
|
19
|
+
selection = gets.to_i
|
20
|
+
if selection == 1
|
21
|
+
def Armstrong(n)
|
22
|
+
num = n
|
23
|
+
sum = 0
|
24
|
+
temp = num
|
25
|
+
while num>0
|
26
|
+
sum = sum + (num%10) * (num%10) * (num%10)
|
27
|
+
num = num/10
|
28
|
+
end
|
29
|
+
if temp == sum
|
30
|
+
"#{temp} is an Armstrong Number"
|
31
|
+
|
32
|
+
else
|
33
|
+
"#{temp} is not an Armstrong Number"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
print "Enter a number: "
|
37
|
+
n = gets.to_i
|
38
|
+
# catch (n > 0) do
|
39
|
+
# while n <= 0
|
40
|
+
# puts "Enter number only"
|
41
|
+
# throw id <= 0 unless id = gets.to_i
|
42
|
+
# end
|
43
|
+
# end
|
44
|
+
puts Armstrong(n)
|
45
|
+
elsif selection == 2
|
46
|
+
print "Enter your choice:\n1 : Factorial\n2 : ncr\n3 : npr\n"
|
47
|
+
print "Enter a number between 1 to 3 : "
|
48
|
+
|
49
|
+
select = gets.to_i
|
50
|
+
case select
|
51
|
+
when 1
|
52
|
+
puts"Factorial:"
|
53
|
+
print "Enter a number: "
|
54
|
+
n = gets.to_i
|
55
|
+
print "Factorial of #{n} of is ", fact.factorial(n), "\n\n"
|
56
|
+
|
57
|
+
when 2
|
58
|
+
puts "ncr:"
|
59
|
+
print "Enter the value of n "
|
60
|
+
n = gets.to_i
|
61
|
+
print "Enter the value of r "
|
62
|
+
r = gets.to_i
|
63
|
+
res = fact.factorial(n) / (fact.factorial(r))
|
64
|
+
|
65
|
+
puts "ncr of given variables is #{res}"
|
66
|
+
when 3
|
67
|
+
puts "npr:"
|
68
|
+
print "Enter the value of n: "
|
69
|
+
n = gets.to_i
|
70
|
+
print "Enter the value of r: "
|
71
|
+
r = gets.to_i
|
72
|
+
res = fact.factorial(n) / (fact.factorial(r) * fact.factorial(n-r))
|
73
|
+
puts "ncr of given variables is #{res}"
|
74
|
+
end
|
75
|
+
elsif selection == 3
|
76
|
+
print "Enter a number \t"
|
77
|
+
n = gets.to_i
|
78
|
+
def prime(n)
|
79
|
+
puts "That's not an integer." unless n.is_a? Integer
|
80
|
+
is_prime = true
|
81
|
+
for i in 2..n-1
|
82
|
+
if n % i == 0
|
83
|
+
is_prime = false
|
84
|
+
end
|
85
|
+
end
|
86
|
+
if is_prime
|
87
|
+
puts "#{n} is prime!"
|
88
|
+
else
|
89
|
+
puts "#{n} is not prime."
|
90
|
+
end
|
91
|
+
end
|
92
|
+
prime(n)
|
93
|
+
elsif selection == 4
|
94
|
+
print "Enter how many rows you want: "
|
95
|
+
n = gets.to_i
|
96
|
+
def triangle(n)
|
97
|
+
fact = Inherit.new
|
98
|
+
for i in (0..n)
|
99
|
+
for j in (0..n-i-1)
|
100
|
+
print " "
|
101
|
+
end
|
102
|
+
for j in (0..i)
|
103
|
+
res = fact.factorial(i)/(fact.factorial(j) * fact.factorial(i-j))
|
104
|
+
print res
|
105
|
+
print " "
|
106
|
+
end
|
107
|
+
puts "\n"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
triangle(n)
|
111
|
+
elsif selection == 0
|
112
|
+
puts "Thank you"
|
113
|
+
break
|
114
|
+
else
|
115
|
+
print "Enter a number from the given range only"
|
116
|
+
redo
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: math_func
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Manohar V
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-30 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: It helps you to select an option from the several functions like Armstrong
|
14
|
+
number, factorial, prime number,NCR,NPR,Pascal Triangle.
|
15
|
+
email: manoharmano225@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/math_func.rb
|
21
|
+
homepage: http://rubygems.org/gems/math_func
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.5.1
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Math functions
|
45
|
+
test_files: []
|