{"id":16058699,"url":"https://github.com/juzershakir/mathematica","last_synced_at":"2025-09-15T18:38:32.744Z","repository":{"id":157834034,"uuid":"417171887","full_name":"JuzerShakir/mathematica","owner":"JuzerShakir","description":"Concepts of mathematics programmed in Ruby.","archived":false,"fork":false,"pushed_at":"2022-09-07T06:40:41.000Z","size":113,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-27T15:57:30.879Z","etag":null,"topics":["algebra","composite-numbers","factorial","fibonacci-numbers","kaprekar","mathematics","prime-numbers","rational-numbers","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JuzerShakir.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-14T14:54:08.000Z","updated_at":"2022-11-10T17:53:27.000Z","dependencies_parsed_at":"2024-06-23T01:16:05.000Z","dependency_job_id":null,"html_url":"https://github.com/JuzerShakir/mathematica","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/JuzerShakir/mathematica","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuzerShakir%2Fmathematica","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuzerShakir%2Fmathematica/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuzerShakir%2Fmathematica/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuzerShakir%2Fmathematica/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JuzerShakir","download_url":"https://codeload.github.com/JuzerShakir/mathematica/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuzerShakir%2Fmathematica/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275304360,"owners_count":25441158,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-15T02:00:09.272Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algebra","composite-numbers","factorial","fibonacci-numbers","kaprekar","mathematics","prime-numbers","rational-numbers","ruby"],"created_at":"2024-10-09T03:40:24.216Z","updated_at":"2025-09-15T18:38:32.699Z","avatar_url":"https://github.com/JuzerShakir.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mathematica\n\n\u003e Concepts of Mathematics programmed in Ruby.\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"assets/project_logo.png\" /\u003e\n\u003c/div\u003e\n\n\u003ch3\u003eTable Of Contents:\u003c/h3\u003e\n\n- [Factorial](#factorial)\u003cbr\u003e\n- [Factors](#factors)\u003cbr\u003e\n- [Fibonacci](#fibonacci)\u003cbr\u003e\n- [Kaprekar](#kaprekar)\u003cbr\u003e\n- [Prime \u0026 Composite](#prime-and-composite)\u003cbr\u003e\n- [Ratio](#ratio)\u003cbr\u003e\n\n---\n\n## Factorial\n\n### ❗ Problem Details\n\n\u003e [What is factorial?](https://www.cuemath.com/numbers/factorial/)\n\n\u003e [Why 0! = 1](https://www.youtube.com/watch?v=X32dce7_D48\u0026ab_channel=EddieWoo)\n\nShould return the factorial for any integer input which is greater than 0.\n\n#### ⌨️ Example input:\n\n```bash\n19\n```\n\n#### 💻 Expected output:\n\n```bash\n121645100408832000\n```\n\n### ✔️ Solution Details\n\n\u003ch4\u003e\u003cimg src=\"https://img.icons8.com/color/12/000000/idea.png\"/\u003e Logic\u003c/h4\u003e\n\n1. Check user input, `n`, exit program for negative input.\n2. Initiate a variable, `total`, which will hold the factorial of n with value `1` as 0! is equal to 1.\n3. Initiate a loop which will initiate from integer `1` to `n` where for each iteration the varaible `total` will be updated with the product of the number its iterating at, `i`, and `total`, where `total` holds the value of previous iteration.\n\nRuby 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.\n\nThe solution of this problem can be found in `factorial/program.rb` file.\n\n#### 📋 Local Execution\n\nClone this repo, navigate to the `factorial/` directory and run the following commands in Terminal:\n\n```bash\nruby program.rb\n```\n\nThis will run the given code file. Enter appropraite input to get desired output.\n\n---\n\n## Factors\n\n### ❗ Problem Details\n\n\u003e [What are factors?](https://www.britannica.com/science/factor-mathematics)\n\nShould return list of factors in ascending order for any integer input which is greater than equal to 1.\n\n#### ⌨️ Example input:\n\n```bash\n100\n```\n\n#### 💻 Expected output:\n\n```ruby\n[1, 2, 4, 5, 10, 20, 25, 50, 100]\n```\n\n### ✔️ Solution Details\n\n\u003ch4\u003e\u003cimg src=\"https://img.icons8.com/color/12/000000/idea.png\"/\u003e Logic\u003c/h4\u003e\n\n1. Check user input, `n`, exit program for negative input.\n2. If user input is `1` then end the program with the output `1`.\n3. If user input is greater than 1 then initiate an array `factors` which will hold all the numbers that are factors of `n`. We know for any number `n`, `1` and `n` itself are common factors, hence we initiate array with these 2 factors.\n4. Since we know that prime numbers have no factors other than `1` and `n` itself, we check for it with the help of `prime` library and end the program.\n5. Now, for composite numbers we initiate a loop which will initiate from integer `2` (since we know `1` is a factor for any `n` where `n \u003e 0`) to `n-1` (with same logic) where for each iteration it will check the following:\n   1. Stop the loop if the loop variable `factor` is already in our `factors` array. This means its calculation is already been done and also for the numbers till `n`.\u003cbr\u003e\n   2. If number isn't in `factors` then we check if `n` is fully divisible by `factor`. If it is fully divisible it will return remainder `0`. And then we append the `factor` in `factors`. After that we would also like to append another factor `second_factor` to `factors` list for which `factor` was fully divisible. To find `second_factor`, we do `n / factor`. Before adding `second_factor` to list we check if its equal to `factor`, because for many integers `n` such as 36 where its square root is 6, it has both `factor` and `second_factor` as equal to 6. If both factors are equal we dont add `second_factor` to `factors` to avoid duplicate factor and this also means that no new factors of `n` will be found after, so we stop the loop. If both factors aren't equal then we append to `factors`.\u003cbr\u003e\n   3. If `factor` isn't fully divisible then we move on to next iteratiom.\u003cbr\u003e\n6. Finally, we have our `factors` array of `n`. We show the list to the user by arranging numbers in ascending order with `.sort` method.\n\nThe solution of this problem can be found in `factors/program.rb` file.\n\n#### 📋 Local Execution\n\nClone this repo, navigate to the `factors/` directory and run the following commands in Terminal:\n\n```bash\nruby program.rb\n```\n\nThis will run the given code file. Enter appropraite input to get desired output.\n\n---\n\n## Fibonacci\n\n### ❗ Problem Details\n\n\u003e [What is fibonacci?](https://www.mathsisfun.com/numbers/fibonacci-sequence.html)\n\nShould 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`.\n\nThe program will calculate fibonacci numbers for positive integers only and not negative.\n\n#### ⌨️ Example input:\n\n```bash\n23\n```\n\n```bash\n3\n```\n\n#### 💻 Expected output:\n\n**Output for `23`**\n\n```\nNumber entered is not a fibonacci number.\nIts previous nearest fibonacci number is 21.\nAnd next nearest fibonacci number is 34.\n```\n\n**Output for `3`**\n\n```\nSuccess! Number you entered is a fibonacci number.\nIts index in fibonacci sequence is 4.\n```\n\n### ✔️ Solution Details\n\n\u003ch4\u003e\u003cimg src=\"https://img.icons8.com/color/12/000000/idea.png\"/\u003e Logic\u003c/h4\u003e\n\n1. Perform calculation only for integer input where `n` \u003e 0.\n2. Initialize an array, `fibonacci_seq`, which will hold the fibonacci numbers and initiate it with values of `0` \u0026 `1` so we can calculate next fibonacci numbers.\n3. 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 number `n`. However if its less than `n` then append the fibonacci number to `fibonacci_seq`. Break the loop if the fibonacci number is equal to `n`.\n4. Will continue iteration (Step 3) until output of each iteration is equal to or greater than `n`.\n5. 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 after `n`.\n\nThe solution of this problem can be found in `fibonacci/program.rb` file.\n\n#### 📋 Local Execution\n\nClone this repo, navigate to the `fibonacci/` directory and run the following commands in Terminal:\n\n```bash\nruby program.rb\n```\n\nThis will run the given code file. Enter appropraite input to get desired output.\n\n---\n\n## Kaprekar\n\n### ❗ Problem Details\n\n\u003e [What are Kaprekar numbers?](https://www.geeksforgeeks.org/kaprekar-number/)\n\n\u003e [List of Kaprekar Numbers](http://oeis.org/A053816)\n\nShould return whether given positive integer by user, `n`, is kaprekar number or not.\n\n#### ⌨️ Example input:\n\n```bash\n9\n```\n\n```bash\n113\n```\n\n#### 💻 Expected output:\n\n**Output for `9`**\n\n```\n9 is a Kaprekar number.\n```\n\n**Output for `113`**\n\n```\n113 is not a Kaprekar number.\n```\n\n### ✔️ Solution Details\n\n\u003ch4\u003e\u003cimg src=\"https://img.icons8.com/color/12/000000/idea.png\"/\u003e Logic\u003c/h4\u003e\n\n1. Perform calculation only for integer input where `n` \u003e 0.\n2. Take Square of `n` and save in a variable `n_sqr`.\n3. Convert `n_sqr` to an array, `arr`.\n4. 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 input `0` before the number so it doesn't change its value and we have even length. So for `n_sqr` whose value is `121` whose length is 3 and after adding `0` we have `0121` which is equal to `121`.\n5. Count the number of elements in `arr` or in other words count the number of digits of `n_sqr` and save in `arr_count`.\n6. Initialize a variable, `total` which will calculate the sum of groups with `0`.\n7. Now we calculate the number of elements of `arr` we need to group for addition. We know that we want 2 groups and since `arr_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 in `members` var.\n8. Now we slice the list where each group has `members` number of elements of `arr` with the help of `.each_slice` method. Each iteration of this loop will take a `group` (iteration var) of these members, join the elements and add them to out `total` var.\n9. If `total == n` then its kaprekar else it isn't.\n\nThe solution of this problem can be found in `kaprekar/program.rb` file.\n\n#### 📋 Local Execution\n\nClone this repo, navigate to the `kaprekar/` directory and run the following commands in Terminal:\n\n```bash\nruby program.rb\n```\n\nThis will run the given code file. Enter appropraite input to get desired output.\n\n---\n\n## Prime and Composite\n\n### ❗ Problem Details\n\n\u003e [What are Prime numbers?](https://thirdspacelearning.com/blog/what-is-a-prime-number/)\n\n\u003e [What are Composite Numbers?](https://www.mathsisfun.com/definitions/composite-number.html)\n\nShould return whether given positive integer by user, `n`, is prime or composite number.\n\n#### ⌨️ Example input:\n\n```bash\n12\n```\n\n```bash\n7\n```\n\n#### 💻 Expected output:\n\n**Output for `12`**\n\n```\n12 is a composite number.\n```\n\n**Output for `7`**\n\n```\n7 is a prime number.\n```\n\n### ✔️ Solution Details\n\n\u003ch4\u003e\u003cimg src=\"https://img.icons8.com/color/12/000000/idea.png\"/\u003e Logic\u003c/h4\u003e\n\n1. Perform calculation only for integer input where `n` \u003e= 0.\n2. End program for negative integer input.\n3. 0 \u0026 1 are 2 numbers are neither prime nor composite so for input `n` with value of `0` or `1`, end program.\n4. 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.\n5. Now to check if a odd number is prime or not we need to run loop from `3` to `n-1` because we know `n` is a factor of itself and we dont start from `2` because no odd number is fully divisible by `2`.\n6. We can loop through each number from `3` to `n` but what if `n` 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 from `3` to `n * 0.5` because we know that factors can be found if there are halfway through only, so looping through all the way to `n` isn't practical.\n7. For any `n` greater than `5`, loop from `3` to `n * 0.5`. And for any `n` less than `5` loop from `3` to `n - 1`.\n8. Now in each loop we see if `n` is divisible by `factor` (loop var). If it is then its a composite number and we exit the program.\n9. It will continue to loop until it finds a factor that fully divides `n`.\n10. If it can't find factors then `n` is a Prime number.\n\nThe solution of this problem can be found in `prime_composite/program.rb` file.\n\n#### 📋 Local Execution\n\nClone this repo, navigate to the `prime_composite/` directory and run the following commands in Terminal:\n\n```bash\nruby program.rb\n```\n\nThis will run the given code file. Enter appropraite input to get desired output.\n\n---\n\n## Ratio\n\n### ❗ Problem Details\n\nShould 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`.\n\n#### ⌨️ Example input:\n\n```bash\n-91/-73\n```\n\n```bash\n-21/98\n```\n\n#### 💻 Expected output:\n\n**Output for `-91/-73`**\n\n```\nThe greatest common divisor is 1 of -91/-73.\nAnd it is already in its simplest form.\n```\n\n**Output for `-21/98`**\n\n```\nThe greatest common divisor is 7 of -21/98.\nAnd its simplest form is -3/14.\n```\n\n### ✔️ Solution Details\n\n\u003ch4\u003e\u003cimg src=\"https://img.icons8.com/color/12/000000/idea.png\"/\u003e Logic\u003c/h4\u003e\n\n1. Take input from user in a rational form.\n2. Check if `n2` is not `0`. If it is then exit the program since dividing any integer by `0` will lead to infinity.\n3. To convert the fraction input to simplest form we need to first find `n1` \u0026 `n2` Greatest Common Divisor (GCD). Ruby provides `.gcd` method to find out.\n4. 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.\n5. After finding GCD of `n1/n2`, we will check if `gcd == 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.\n6. For `gcd` other than `1`, divide both `n1` \u0026 `n2` by `gcd` to find other factor that which multiplied by `gcd` returns `n1` or `n2`.\n   Store the value in new variable called `simple_n1` \u0026 `simple_n2` respectively.\n7. Convert `simple_n1` \u0026 `simple_n2` variables to rational numbers and store them in new var `simplest_form`.\n8. Show results to user.\n\nThe solution of this problem can be found in `ratio/program.rb` file.\n\n#### 📋 Local Execution\n\nClone this repo, navigate to the `ratio/` directory and run the following commands in Terminal:\n\n```bash\nruby program.rb\n```\n\nThis will run the given code file. Enter appropraite input to get desired output.\n\n---\n\n![](https://visitor-badge-reloaded.herokuapp.com/badge?page_id=juzershakir.mathematica\u0026color=000000\u0026lcolor=000000\u0026style=for-the-badge\u0026logo=Github)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuzershakir%2Fmathematica","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjuzershakir%2Fmathematica","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuzershakir%2Fmathematica/lists"}