+ 31

🔴CHALLENGE 🔴Binomial Expansion

First of all, I would like to wish a fantastic new year for everyone in Milky Way Galaxy and wish you health and happiness in your entire life! Now, let's code the following little programming challenge: Your task is to compute and print the expansion of the binomial (X+Y)^N. Here X and Y are variables of the binomial and N is the power of the binomial and you have to receive N from the user. Example: For given N=2, the output must be X^2+2XY+Y^2 (here "^" is short for "to the power of"). For N>2, you can see the required output in my following code. Please google "binomial expansion", for the general formula.

2nd Jan 2018, 11:18 PM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
25 Réponses
+ 28
First of all same 2 u my friend 😃 /*U r wishing aliens also by saying milky way galaxy , i don't know when they celebrate new year ... or they celebrate or not*/ //btw here is my try ... works till 👉61👈 //used long,math as suggested by LukArToDo earlier ☺ https://code.sololearn.com/c1Y2xQZ4Iwyx/?ref=app
3rd Jan 2018, 6:15 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 16
Here is my C++ code for this problem: https://code.sololearn.com/cfIe4PwkL2Q3 In sololearn my code works for N as big as 18!
2nd Jan 2018, 11:19 PM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
+ 15
Here's modified code by Gaurav Agrawal, with BigInteger (over 100 shows time limit exceed 😐) https://code.sololearn.com/c2WcL5gbEn3i/?ref=app
4th Jan 2018, 7:47 AM
LukArToDo
LukArToDo - avatar
+ 13
@Blue Thanks for your interesting answer 👍
3rd Jan 2018, 2:54 PM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
+ 12
Here is a solution using PHP: https://code.sololearn.com/w43a0I4xq1o0/?ref=app Here, I set N=10. For other values of N you can change the variable $var at the beginning of the code. My original code which asks for user input does not work in sololearn. You can see it here: https://code.sololearn.com/w5cjfn4qDIW9/?ref=app The good news is this code works for bigger powers too!
3rd Jan 2018, 9:03 PM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
+ 11
My try, can compute correctly up to (x + y) ^ 29 https://code.sololearn.com/clHztcvGe7D7/?ref=app
3rd Jan 2018, 8:49 AM
David Akhihiero
David Akhihiero - avatar
3rd Jan 2018, 12:53 PM
777
777 - avatar
+ 10
https://code.sololearn.com/c9ITM79uFHo4/?ref=app
4th Jan 2018, 2:47 PM
Käzî Mrîdùl Høssäîn
Käzî Mrîdùl Høssäîn - avatar
3rd Jan 2018, 4:24 PM
Hamid
Hamid - avatar
+ 6
my try works till 20 I'll try to update it edit: updated now!!!! https://code.sololearn.com/czv5yESXra1x/?ref=app
4th Jan 2018, 9:48 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
+ 6
Hi This is my first code in java so forgive me if I didnt quite get the right format for the answer. I just wanted to compare Java and Python. This algorithem seems to work correctly and list coefficients upto binomial of power 33; whereas same algoritem works upto 130(when it gives time_out on sololearn IDE). I look forward to hearing your ideas on the matter. Happy Coding https://code.sololearn.com/cU3ioCkAUYHZ/?ref=app
6th Jan 2018, 5:43 PM
Hamid
Hamid - avatar
+ 5
one liner. works for large n (value 152 tested here) https://code.sololearn.com/c0KI5gvv94yT/?ref=app
3rd Jan 2018, 8:35 AM
VcC
VcC - avatar
+ 5
check out my code(Actually it's lukartodo's now😉)
6th Jan 2018, 3:48 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
+ 4
Well, my Code is not quite what you asked for but I thought It would be more interesting to focus on a more practical aproach: https://code.sololearn.com/crSgzV7zAoD7/?ref=app
3rd Jan 2018, 8:26 PM
Lucas Pardo
Lucas Pardo - avatar
+ 4
Happy New Year to you too, sir, I'm sure the rest of the inhabitants of the Milky Way Galaxy wish you health and happiness too :) Anyway, here you go, it works till a couple hundreds if you use small numbers... (Python 3) With actual numbers... https://code.sololearn.com/c6MD8dAI2CKh With variables... https://code.sololearn.com/c5NLh1cLH2Ni
3rd Jan 2018, 11:52 PM
Josue Fleitas
Josue Fleitas - avatar
+ 4
"""Enter a number 2 >= n <= 100 as timeout occurs > 100""" from math import factorial as fct def nCm(n, m): return fct(n)//(fct(m)*fct(n-m)) def bnmlExpnsn(n): print("(X + Y)^"+str(n)) if n==0: print("= 2") elif n==1: print("= X + Y") else: print("= X^"+str(n)) for i in range(1,n): print("+ "+str(nCm(n,i))+" X^"+str(n-i)+" Y^"+str(i)) print("+ Y^"+str(n)) try: bnmlExpnsn(int(input().split()[0])) except: print("A number must be entered first") exit() I may not have followed the exact rules, but I don't like the idea of having to scroll through the output from left to right. I may also have cheated by implementing math.factorial https://code.sololearn.com/cDvU5SE2errS/#py
6th Jan 2018, 5:18 AM
Adam Ross
Adam Ross - avatar
+ 4
My try. This was supposed to output up to 500 but already gets infinity at 171. So I cut it at 180. 😱 https://code.sololearn.com/Wwbr9KpHICXs/?ref=app
7th Jan 2018, 10:59 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 3
Here is my try! First, I made a Pascal's triangle recursive function (pasc()) and, from it, made the binomial expansion function (bin()). On my cellphone, all worked fine up to 150 on SoloLearn. In other interpreters, I got it to work up to 996. https://code.sololearn.com/cXfXra2XUwgO/?ref=app
3rd Jan 2018, 11:16 PM
Murillo Pyaia Alves Paixão
Murillo Pyaia Alves Paixão - avatar
+ 3
https://code.sololearn.com/cApZAx4QirgZ/?ref=app
6th Jan 2018, 10:16 AM
Arun
+ 3
Here is my implementation in Python. You can make the comparison for yourself! https://code.sololearn.com/ciEBBD9Z1wJJ/?ref=app
8th Jan 2018, 12:51 PM
Hamid
Hamid - avatar