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 👍
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
+ 30
https://code.sololearn.com/c7uLpU940D78/?ref=app
+ 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
+ 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
*/
+ 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))
+ 14
My attempt in java
https://code.sololearn.com/ca2fb2BAF56M/?ref=app
+ 11
Here is my answer in Java version . See this
https://code.sololearn.com/c90LzmPS8zDa/?ref=app
+ 10
+ 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
+ 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.
+ 10
@giuseppe ,antony... don't spam plzzz ... try challenge ... if u can't then enjoy others code ☺
+ 9
some basic code..
https://code.sololearn.com/caoR1NI44qZt/?ref=app
+ 8
@saumya ... give the link of challenge in code or in comments ... so that viewer can know what this code is about ☺
+ 8
* Late to the party..
* My n00b approach (Python):
https://code.sololearn.com/cmO925GesTdH
+ 8
https://code.sololearn.com/c4wD7Jwf63MR/?ref=app
+ 8
//wow ... so many answers to my challenge
thats gr8 ☺
+ 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