Skip to main content

Simple Program for calculating Permutations and Combinations

Permutations and Combinations


If you are a 12th grader like me, you would surely agree that the chapter 'Permutations and Combinations' of Mathematics is easily one of the most heinous chapters. It has straightforward formulas for permutations and combinations, but it is the calculation that gets to us.

Generally questions from this chapter are pretty easy if you get the logic, but sometimes the expression involves pretty large numbers and it can get messy. Now, that is something your normal calculator can do, can it? So lets leave that for our pet Python.


The Code

This is a very simple program with just the logic, but I shall explain the code part by part. Skip to the end if you would like to download the python file. No strings attached.


Ok, so I'd like the code to run as long as I want in the command prompt. Inputting the expression as exp, we take an empty list to store each digit of the expression so as to seperate the numbers from the letter:

    

the variable exp_pace stores the index of the letter in the inputted expression. left_num and right_num are respectively the n and r in the expression nCr or nPr. Note that even though left_num and right_num are integers, they are of type string. nature is storing the type of expression for use of formula.


So we run two for loops to get the n and r and convert them to type int. Going ahead is the formula. Instead of just telling python to keep multiplying the numerator as well as denominator by a number 1 less than the previous number (factorial), we tell it to multiply till it reaches the number equal to (n-r), which will get cancelled. Then the division of r! depends on whether we are calculating for permutation or combination.


So that was that. We now have a small program that will instantly give value of the permutation or combination no matter how big it is.

The Working



Full Code:

Download the Below Python File for free and start using the Permutation and Combination Calculator. No tweaks required. Start using right away!:


So enjoy putting billions and watch your pc suffer calculating them!

Comments

Popular posts from this blog

English Dictionary With Python And Tkinter!

English Dictionary on Python Have you ever tried to read from the small Oxford Dictionaries? Those small yet bulky books have thousands of words cramped on a page the length of your finger! And on top of that, not to forget the hassle of flipping across the pages, searching for your word, God knows where is hiding on which page! Nowadays though, hardly does anyone ever use the classic dictionaries. With the access of technology on our fingertips, one tap on Google, and you would be on with your way.  But how about making a program of your own which can do the same for you? Sounds interesting? Such a program can enable you to run it and keep it opened, while you are reading a book, so that you can search the definition of a new word you encountered. What if you are writing a book, perhaps a report? You know what you are supposed to write, but you choose to get a word for it, so that you sound professional. So you just search for a short definition and the program find the word with ...

Predicting Stock Prices using Machine Learning (XGBoost)

  Today, I'll show you how to create a machine learning program to predict stock prices. Machine learning is used in a variety of fields, and we can utilize its ability to learn and predict from data to predict useful variables, with minimal error. Therefore in theory, we can apply the same on stocks to predict the next closing price, so that we can make a killing gain. However, stock markets are highly unstable. Their price movement often depends on decisions taken by the company, favor and reaction of investors, social impact, human emotions, and price movements of some other related stocks. These types of data cannot be made available to a program. The prediction can be close only if the market remains relatively stable. You don't actually need to learn machine learning using python to understand how this program works, if not minimal. I'll describe each machine learning process. And don't forget to download the source code of this program, link provided at the end. ...

Hang Man Game using Python with Tkinter

  Hang Man At some point of your childhood, you must have played this silly game. For those who are unfamiliar, this game involves guessing a random word letter-by-letter. For every wrong guess the player makes, the picture proceeds to make the set to hang the man. For example, on the first wrong guess, the poles appear, on the second, the rope appears, or somewhat to this effect. Upon making enough mistakes, the full picture is complete and it is assumed the Man is HANGED. I present to you the Hangman Game on Python. I have used Tkinter for the User Interface (I mean, come on, who likes the command prompt? At least not me...). You get to add your desired words in the python list   words in the code and you can play this game to your heart's wish, knowing you have done something new. Some of you may be having projects,  So just look at the steps below and skip the code to the end, where you will get the download link to the entire code! The First Step Before you...