Concepts of Mathematics programmed in Ruby.
Should return the factorial for any integer input which is greater than 0.
19
121645100408832000
- Check user input,
n
, exit program for negative input. - Initiate a variable,
total
, which will hold the factorial of n with value1
as 0! is equal to 1. - Initiate a loop which will initiate from integer
1
ton
where for each iteration the varaibletotal
will be updated with the product of the number its iterating at,i
, andtotal
, wheretotal
holds the value of previous iteration.
Ruby provides a funtion Math.gamma
which returns the approximation factorial of n-1 for any n greater than 0. I have also used this function in the program to compare the results with the logic code I created. The answer of this method changes slightly compared to my logic as we give high input numbers as gamma
is an approximation.
The solution of this problem can be found in factorial/program.rb
file.
Clone this repo, navigate to the factorial/
directory and run the following commands in Terminal:
ruby program.rb
This will run the given code file. Enter appropraite input to get desired output.
Should return list of factors in ascending order for any integer input which is greater than equal to 1.
100
[1, 2, 4, 5, 10, 20, 25, 50, 100]
- Check user input,
n
, exit program for negative input. - If user input is
1
then end the program with the output1
. - If user input is greater than 1 then initiate an array
factors
which will hold all the numbers that are factors ofn
. We know for any numbern
,1
andn
itself are common factors, hence we initiate array with these 2 factors. - Since we know that prime numbers have no factors other than
1
andn
itself, we check for it with the help ofprime
library and end the program. - Now, for composite numbers we initiate a loop which will initiate from integer
2
(since we know1
is a factor for anyn
wheren > 0
) ton-1
(with same logic) where for each iteration it will check the following:- Stop the loop if the loop variable
factor
is already in ourfactors
array. This means its calculation is already been done and also for the numbers tilln
. - If number isn't in
factors
then we check ifn
is fully divisible byfactor
. If it is fully divisible it will return remainder0
. And then we append thefactor
infactors
. After that we would also like to append another factorsecond_factor
tofactors
list for whichfactor
was fully divisible. To findsecond_factor
, we don / factor
. Before addingsecond_factor
to list we check if its equal tofactor
, because for many integersn
such as 36 where its square root is 6, it has bothfactor
andsecond_factor
as equal to 6. If both factors are equal we dont addsecond_factor
tofactors
to avoid duplicate factor and this also means that no new factors ofn
will be found after, so we stop the loop. If both factors aren't equal then we append tofactors
. - If
factor
isn't fully divisible then we move on to next iteratiom.
- Stop the loop if the loop variable
- Finally, we have our
factors
array ofn
. We show the list to the user by arranging numbers in ascending order with.sort
method.
The solution of this problem can be found in factors/program.rb
file.
Clone this repo, navigate to the factors/
directory and run the following commands in Terminal:
ruby program.rb
This will run the given code file. Enter appropraite input to get desired output.
Should return whether given positive integer by user, n
, is fibonacci number or not. If its a Fibonacci number then show its index number in the fiobancci series. And if its not a fibonacci number then show the nearest fibonacci numbers both before and after the number n
.
The program will calculate fibonacci numbers for positive integers only and not negative.
23
3
Output for 23
Number entered is not a fibonacci number.
Its previous nearest fibonacci number is 21.
And next nearest fibonacci number is 34.
Output for 3
Success! Number you entered is a fibonacci number.
Its index in fibonacci sequence is 4.
- Perform calculation only for integer input where
n
> 0. - Initialize an array,
fibonacci_seq
, which will hold the fibonacci numbers and initiate it with values of0
&1
so we can calculate next fibonacci numbers. - Initiate a loop where in each iteration it performs addition of last 2 fibonacci numbers in
fibonacci_seq
array to find next fiobanacci number. Break the loop if the fibonacci number calculated in this iteration is greater than the input numbern
. However if its less thann
then append the fibonacci number tofibonacci_seq
. Break the loop if the fibonacci number is equal ton
. - Will continue iteration (Step 3) until output of each iteration is equal to or greater than
n
. - If user entered a fibonacci number then show output of its index in the
fibonacci_seq
. Otherwise show the nearest fibonacci numbers both before and aftern
.
The solution of this problem can be found in fibonacci/program.rb
file.
Clone this repo, navigate to the fibonacci/
directory and run the following commands in Terminal:
ruby program.rb
This will run the given code file. Enter appropraite input to get desired output.
Should return whether given positive integer by user, n
, is kaprekar number or not.
9
113
Output for 9
9 is a Kaprekar number.
Output for 113
113 is not a Kaprekar number.
- Perform calculation only for integer input where
n
> 0. - Take Square of
n
and save in a variablen_sqr
. - Convert
n_sqr
to an array,arr
. - We want our length of elements in
arr
to be an even number so it can be easily grouped into 2 where each group will be of equal lengths. So for odd length we input0
before the number so it doesn't change its value and we have even length. So forn_sqr
whose value is121
whose length is 3 and after adding0
we have0121
which is equal to121
. - Count the number of elements in
arr
or in other words count the number of digits ofn_sqr
and save inarr_count
. - Initialize a variable,
total
which will calculate the sum of groups with0
. - Now we calculate the number of elements of
arr
we need to group for addition. We know that we want 2 groups and sincearr_count
is even it will easily divide into 2 and give us the number of members we will have for each group. Store this number inmembers
var. - Now we slice the list where each group has
members
number of elements ofarr
with the help of.each_slice
method. Each iteration of this loop will take agroup
(iteration var) of these members, join the elements and add them to outtotal
var. - If
total == n
then its kaprekar else it isn't.
The solution of this problem can be found in kaprekar/program.rb
file.
Clone this repo, navigate to the kaprekar/
directory and run the following commands in Terminal:
ruby program.rb
This will run the given code file. Enter appropraite input to get desired output.
Should return whether given positive integer by user, n
, is prime or composite number.
12
7
Output for 12
12 is a composite number.
Output for 7
7 is a prime number.
- Perform calculation only for integer input where
n
>= 0. - End program for negative integer input.
- 0 & 1 are 2 numbers are neither prime nor composite so for input
n
with value of0
or1
, end program. - We also know that any number which is fully divisble by 2 or in other words if its even, then it wont be a prime number but composite. But 2 is the only even number that is prime so for
n == 2
its prime number. - Now to check if a odd number is prime or not we need to run loop from
3
ton-1
because we known
is a factor of itself and we dont start from2
because no odd number is fully divisible by2
. - We can loop through each number from
3
ton
but what ifn
was a very large odd number? Looping through each number and checking if its fully divisible would be take time and put load on memory. To avoid this we only loop from3
ton * 0.5
because we know that factors can be found if there are halfway through only, so looping through all the way ton
isn't practical. - For any
n
greater than5
, loop from3
ton * 0.5
. And for anyn
less than5
loop from3
ton - 1
. - Now in each loop we see if
n
is divisible byfactor
(loop var). If it is then its a composite number and we exit the program. - It will continue to loop until it finds a factor that fully divides
n
. - If it can't find factors then
n
is a Prime number.
The solution of this problem can be found in prime_composite/program.rb
file.
Clone this repo, navigate to the prime_composite/
directory and run the following commands in Terminal:
ruby program.rb
This will run the given code file. Enter appropraite input to get desired output.
Should return simplest form of rational number for any integers input by user, in the form of n1/n2
where n1
can be any integer whereas n2
can also be any integer except 0
.
-91/-73
-21/98
Output for -91/-73
The greatest common divisor is 1 of -91/-73.
And it is already in its simplest form.
Output for -21/98
The greatest common divisor is 7 of -21/98.
And its simplest form is -3/14.
- Take input from user in a rational form.
- Check if
n2
is not0
. If it is then exit the program since dividing any integer by0
will lead to infinity. - To convert the fraction input to simplest form we need to first find
n1
&n2
Greatest Common Divisor (GCD). Ruby provides.gcd
method to find out. - GCD calculation will raise error for unexpected string input. We will wrap
begin-rescue
to GCD calculation and gently infrom user and exit the program. - After finding GCD of
n1/n2
, we will check ifgcd == 1
. If it is, it means that input fraction is already in its simplest form. We give output to the user and stop the program. - For
gcd
other than1
, divide bothn1
&n2
bygcd
to find other factor that which multiplied bygcd
returnsn1
orn2
. Store the value in new variable calledsimple_n1
&simple_n2
respectively. - Convert
simple_n1
&simple_n2
variables to rational numbers and store them in new varsimplest_form
. - Show results to user.
The solution of this problem can be found in ratio/program.rb
file.
Clone this repo, navigate to the ratio/
directory and run the following commands in Terminal:
ruby program.rb
This will run the given code file. Enter appropraite input to get desired output.