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

Simple Omegle Bot Using Selenium With Python

Omegle is an online text-based and video-based chatting platform, which allows users from around the world to talk to complete strangers anonymously, for free! The text-based Omegle has a simple concept: Complete a Captcha  verification, connect automatically to a stranger, and after chatting get on to the next stranger. Apart from having a chit-chat with a stranger from the far side of the globe, Omegle poses as the perfect platform for other uses as well. At Omegle, you  can advertise your content, website, products and more for free. With access to about 40,000 strangers using Omegle at any given time, you can benefit if your ideas/advertisement is seen by potential customers. But of course, we can't advertise to each guy we meet again and again. I mean, come on, that's a lot of hard work, even if your message consists of a few words. But worry not, since that's where our Omegle Bot  comes in play. This bot works on a pre-determined set of messages that are to be conveye

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.

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 matc