Concepts of Mathematics programmed in Ruby.
Should return the factorial for any integer input which is greater than 0.
19121645100408832000- Check user input,
n, exit program for negative input. - Initiate a variable,
total, which will hold the factorial of n with value1as 0! is equal to 1. - Initiate a loop which will initiate from integer
1tonwhere for each iteration the varaibletotalwill be updated with the product of the number its iterating at,i, andtotal, wheretotalholds 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.rbThis 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
1then end the program with the output1. - If user input is greater than 1 then initiate an array
factorswhich will hold all the numbers that are factors ofn. We know for any numbern,1andnitself are common factors, hence we initiate array with these 2 factors. - Since we know that prime numbers have no factors other than
1andnitself, we check for it with the help ofprimelibrary and end the program. - Now, for composite numbers we initiate a loop which will initiate from integer
2(since we know1is a factor for anynwheren > 0) ton-1(with same logic) where for each iteration it will check the following:- Stop the loop if the loop variable
factoris already in ourfactorsarray. This means its calculation is already been done and also for the numbers tilln. - If number isn't in
factorsthen we check ifnis fully divisible byfactor. If it is fully divisible it will return remainder0. And then we append thefactorinfactors. After that we would also like to append another factorsecond_factortofactorslist for whichfactorwas fully divisible. To findsecond_factor, we don / factor. Before addingsecond_factorto list we check if its equal tofactor, because for many integersnsuch as 36 where its square root is 6, it has bothfactorandsecond_factoras equal to 6. If both factors are equal we dont addsecond_factortofactorsto avoid duplicate factor and this also means that no new factors ofnwill be found after, so we stop the loop. If both factors aren't equal then we append tofactors. - If
factorisn't fully divisible then we move on to next iteratiom.
- Stop the loop if the loop variable
- Finally, we have our
factorsarray ofn. We show the list to the user by arranging numbers in ascending order with.sortmethod.
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.rbThis 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.
233Output 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&1so we can calculate next fibonacci numbers. - Initiate a loop where in each iteration it performs addition of last 2 fibonacci numbers in
fibonacci_seqarray 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 thannthen 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.rbThis 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.
9113Output 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
nand save in a variablen_sqr. - Convert
n_sqrto an array,arr. - We want our length of elements in
arrto 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 input0before the number so it doesn't change its value and we have even length. So forn_sqrwhose value is121whose length is 3 and after adding0we have0121which is equal to121. - Count the number of elements in
arror in other words count the number of digits ofn_sqrand save inarr_count. - Initialize a variable,
totalwhich will calculate the sum of groups with0. - Now we calculate the number of elements of
arrwe need to group for addition. We know that we want 2 groups and sincearr_countis even it will easily divide into 2 and give us the number of members we will have for each group. Store this number inmembersvar. - Now we slice the list where each group has
membersnumber of elements ofarrwith the help of.each_slicemethod. Each iteration of this loop will take agroup(iteration var) of these members, join the elements and add them to outtotalvar. - If
total == nthen 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.rbThis 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.
127Output 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
nwith value of0or1, 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 == 2its prime number. - Now to check if a odd number is prime or not we need to run loop from
3ton-1because we knownis a factor of itself and we dont start from2because no odd number is fully divisible by2. - We can loop through each number from
3tonbut what ifnwas 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 from3ton * 0.5because we know that factors can be found if there are halfway through only, so looping through all the way tonisn't practical. - For any
ngreater than5, loop from3ton * 0.5. And for anynless than5loop from3ton - 1. - Now in each loop we see if
nis 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
nis 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.rbThis 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/98Output 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
n2is not0. If it is then exit the program since dividing any integer by0will lead to infinity. - To convert the fraction input to simplest form we need to first find
n1&n2Greatest Common Divisor (GCD). Ruby provides.gcdmethod to find out. - GCD calculation will raise error for unexpected string input. We will wrap
begin-rescueto 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
gcdother than1, divide bothn1&n2bygcdto find other factor that which multiplied bygcdreturnsn1orn2. Store the value in new variable calledsimple_n1&simple_n2respectively. - Convert
simple_n1&simple_n2variables 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.rbThis will run the given code file. Enter appropraite input to get desired output.
