Program to evaluate the expression (√X+1)^6 + (√X-1)^6

Given a number X. The task is to find the value of the below expression for the given value of X.

(\sqrt[]{X} +1)^6 + (\sqrt[]{X}-1)^6

Examples:

Input: X = √2
Output: 198
Explanation: 2[(\sqrt[]{2})^6 + 15 (\sqrt[]{2})^4 + 15\sqrt[]{2})^2 + 1]
= 198



Input: X = 3
Output: 4160

Approach: The idea is to use Binomial expression. We can take these two terms as 2 binomial expressions. By expanding these terms we can find the desired sum. Below is the expansion of the terms.

 \newline (X+1)^6 = {6_C}_0 X^6+6_c_1 X^5+{6_C}_2 X^4+{6_C}_3 X^3+{6_C}_4 X^2+{6_C}_5 X+{6_C}_6 \newline (X-1)^6 = {6_C}_0 X^6-6_c_1 X^5+{6_C}_2 X^4-{6_C}_3 X^3+{6_C}_4 X^2-{6_C}_5 X+{6_C}_6  \newline (X+1)^6+(X-1)^6 =2[{6_C}_0 X^6+{6_C}_2 X^4+{6_C}_4 X^2+{6_C}_6] \newline \newline  (X+1)^6+(X-1)^6=2[X^6 + 15 X^4 + 15 X^2 +1] ---EQ(1)

Now put X= \sqrt[]{2} in EQ(1)

 \newline (\sqrt[]{2}+1)^6+(\sqrt[]{2}-1)^6 = 2[(\sqrt[]{2})^6 + 15 (\sqrt[]{2})^4 + 15\sqrt[]{2})^2 + 1] \newline = 2(8 + 15 x 4 + 15 x 2 + 1 ) \newline = 2(8 + 60 + 30 + 1) \newline = 198

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to evaluate the given expression
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the sum
float calculateSum(float n)
{
    int a = int(n);
  
    return 2 * (pow(n, 6) + 15 * pow(n, 4) 
            + 15 * pow(n, 2) + 1);
}
  
// Driver Code
int main()
{
    float n = 1.4142;
  
    cout << ceil(calculateSum(n)) << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to evaluate the given expression
import java.util.*;
  
class gfg
{
// Function to find the sum
public static double calculateSum(double n)
{
    return 2 * (Math.pow(n, 6) + 15 * Math.pow(n, 4
            + 15 * Math.pow(n, 2) + 1);
}
  
// Driver Code
public static void main(String[] args)
{
    double n = 1.4142;
    System.out.println((int)Math.ceil(calculateSum(n)));
}
}
//This code is contributed by mits

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to evaluate 
# the given expression
  
import math
  
#Function to find the sum
def calculateSum(n):
      
    a = int(n)
      
    return (2 * (pow(n, 6) + 15 * pow(n, 4
            + 15 * pow(n, 2) + 1))
      
#Driver Code
if __name__=='__main__':
    n = 1.4142
    print(math.ceil(calculateSum(n)))
  
# this code is contributed by 
# Shashank_Sharma

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to evaluate the given expression
using System;
class gfg
{
// Function to find the sum
public static double calculateSum(double n)
{
    return 2 * (Math.Pow(n, 6) + 15 * Math.Pow(n, 4) 
            + 15 * Math.Pow(n, 2) + 1);
}
  
// Driver Code
public static int Main()
{
    double n = 1.4142;
    Console.WriteLine(Math.Ceiling(calculateSum(n)));
    return 0;
}
}
//This code is contributed by Soumik

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to evaluate 
// the given expression
  
//Function to find the sum
function calculateSum($n)
    $a = (int)$n;
      
    return (2 * (pow($n, 6) + 
            15 * pow($n, 4) +
            15 * pow($n, 2) + 1));
}
  
// Driver Code
$n = 1.4142;
echo ceil(calculateSum($n));
  
// This code is contributed by mits
?>

chevron_right


Output:

198


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.