[ASSIGNMENT] :::>>> FIND SUM OF PRODUCT OF 1st n NATURAL NUMBERS taken 2 at a time | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 143

[ASSIGNMENT] :::>>> FIND SUM OF PRODUCT OF 1st n NATURAL NUMBERS taken 2 at a time

example :::> if input is 4 then code have to calculate 1*2 + 1*3 + 1*4 + 2*3 + 2*4 + 3*4 output will be 35 //hope I am able to explain clearly what is the challenge //happy coding & best of luck 👍

20th Sep 2017, 8:07 AM
Changed
Changed - avatar
237 Answers
+ 70
if anyone want to know how to got that formula ... see here 👇 ... /* (a+b)^2 = (a^2 + b^2) + 2(ab) // do u notice something ... no // (a+b+c)^2 =(a^2 + b^2 + c^2) + 2(ab + ac + bc) ... now u hv noticed something ... yes //if not then see (a+b+c+d)^2 // (summation of a)^2 = summation of (a^2) + 2 (summation of ab) , // therefore , (n (n+1)/2)^2 = n (n+1)(2n+1)/6 + 2 (sum of product of 1st n natural numbers) ie ... ((sigma n)^2 - (sigma n^2)) /2 👈👈👈 //simplify it & u will get the req. expression & game over very fast ☺ //all hv given best answers ... thats why can't decide so just to help someone know more ... i marked this as best answer ... so everyone can see it first
21st Sep 2017, 7:07 AM
Changed
Changed - avatar
20th Sep 2017, 8:21 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 30
https://code.sololearn.com/c7uLpU940D78/?ref=app
20th Sep 2017, 9:23 AM
Changed
Changed - avatar
+ 29
Here's a Python solution: from itertools import combinations number = int(input()) singles = [i for i in range(1, number + 1)] combos = [x for x in combinations(singles, 2)] products = [a * b for (a, b) in combos] print(sum(products)) https://code.sololearn.com/caor19oJ7om7/#py
21st Sep 2017, 4:17 AM
David Ashton
David Ashton - avatar
+ 16
/* //hello all👋 //thanks for that gr8 response ... all have given good solutions //👍 // some having doubt that for input as 1 ... the output will be 0 bcz no combination can be made ... bcz for any 1 combination u need 2 elements at least ☺ //i can't try all codes ... but thumbs up for all //once again thanks all for such a gr8 responce to my 1st challenge ☺☺☺ //don't forget to add link to this challenge in ur code ... so that a viewer /learner can know what the code is about //challenge is easy ... but to know new logics & ways is good //happy coding */
26th Sep 2017, 10:07 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 15
print("enter the number") n=int(input()) sum=0 for i in range(1, n): for j in range(i+1, n+1): sum+=i*j print("The sum of product of %d natural numbers 2 at a time is ==> %d" %(n, sum))
20th Sep 2017, 5:03 PM
Saifudeen K Jamal
Saifudeen K Jamal - avatar
25th Sep 2017, 8:05 PM
David Akhihiero
David Akhihiero - avatar
+ 11
Here is my answer in Java version . See this https://code.sololearn.com/c90LzmPS8zDa/?ref=app
25th Sep 2017, 4:21 PM
Abhishek Pun
Abhishek Pun - avatar
20th Sep 2017, 9:22 AM
Ferhat Sevim
Ferhat Sevim - avatar
+ 10
Well, I know there's a mathematical formula which gives you an answer right away, but... here is just another (not the best) option... https://code.sololearn.com/cZoCmJWNBhHH/#py
20th Sep 2017, 6:58 PM
Alexander Podogov
Alexander Podogov - avatar
23rd Sep 2017, 5:53 AM
ni5han7
ni5han7 - avatar
+ 10
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int sum=0; int n= scan.nextInt(); for(int i=1;i<n;i++ ) for(int j=i;j<n;j++) sum+=j*n; System.out.println(sum); } } This is written in Java.
25th Sep 2017, 3:22 PM
Vahid Shirbisheh
Vahid Shirbisheh - avatar
+ 10
@giuseppe ,antony... don't spam plzzz ... try challenge ... if u can't then enjoy others code ☺
26th Sep 2017, 10:25 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
20th Sep 2017, 11:04 AM
Siddu Kori
Siddu Kori - avatar
+ 8
@saumya ... give the link of challenge in code or in comments ... so that viewer can know what this code is about ☺
25th Sep 2017, 5:04 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 8
* Late to the party.. * My n00b approach (Python): https://code.sololearn.com/cmO925GesTdH
26th Sep 2017, 12:41 AM
noobcøder
noobcøder - avatar
+ 8
https://code.sololearn.com/c4wD7Jwf63MR/?ref=app
26th Sep 2017, 4:10 AM
Bijay Shah
Bijay Shah - avatar
+ 8
//wow ... so many answers to my challenge thats gr8 ☺
26th Sep 2017, 11:08 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
20th Sep 2017, 11:52 AM
Kartik
Kartik - avatar
+ 7
I used @Importal's math formula and simplified. Then tried two different lambda at Python. SoloLearn doesn't have reduce(). It is very nice with Python, you can find so many functions doing same answer. https://code.sololearn.com/coQ1A9MNKGs0/?ref=app
21st Sep 2017, 6:15 AM
Ferhat Sevim
Ferhat Sevim - avatar