Given the length of edges of an irregular tetrahedron. The task is to determine the volume of that tetrahedron.
Let Edge length of pyramids be u, U, v, V, w, W.
Examples:
Input: u = 1000, v = 1000, w = 1000, U = 3, V = 4, W = 5 Output: 1999.9947 Input: u = 2000, v = 2000, w = 2000, U = 3, V = 4, W = 5 Output: 3999.9858
Formula to calculate Volume of an irregular Tetrahedron in terms of its edge lengths is:
A =
Volume = sqrt(A/288) =
sqrt(4*u*u*v*v*w*w – u*u*(v*v + w*w – U*U)^2 – v*v(w*w + u*u – V*V)^2 – w*w(u*u + v*v – W*W)^2 + (u*u + v*v – W*W) * (w*w + u*u – V*V) * (v*v + w*w – U*U)) / 12
Below is the implementation of above approach:
C++
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std;#define db double// Function to find the volumevoid findVolume(db u, db v, db w, db U, db V, db W, db b){ // Steps to calculate volume of a // Tetrahedron using formula db uPow = pow(u, 2); db vPow = pow(v, 2); db wPow = pow(w, 2); db UPow = pow(U, 2); db VPow = pow(V, 2); db WPow = pow(W, 2); db a = 4 * (uPow * vPow * wPow) - uPow * pow((vPow + wPow - UPow), 2) - vPow * pow((wPow + uPow - VPow), 2) - wPow * pow((uPow + vPow - WPow), 2) + (vPow + wPow - UPow) * (wPow + uPow - VPow) * (uPow + vPow - WPow); db vol = sqrt(a); vol /= b; cout << fixed << setprecision(4) << vol;} // Driver codeint main(){ // edge lengths db u = 1000, v = 1000, w = 1000; db U = 3, V = 4, W = 5; db b = 12; findVolume(u, v, w, U, V, W, b); return 0;} |
Java
// Java implementation of above approach import java.util.*;import java.lang.*;import java.io.*; class GFG{ // Function to find the volumestatic void findVolume(double u, double v, double w, double U, double V, double W, double b){ // Steps to calculate volume of a // Tetrahedron using formula double uPow = Math.pow(u, 2); double vPow = Math.pow(v, 2); double wPow = Math.pow(w, 2); double UPow = Math.pow(U, 2); double VPow = Math.pow(V, 2); double WPow = Math.pow(W, 2); double a = 4 * (uPow * vPow * wPow) - uPow * Math.pow((vPow + wPow - UPow), 2) - vPow * Math.pow((wPow + uPow - VPow), 2) - wPow * Math.pow((uPow + vPow - WPow), 2) + (vPow + wPow - UPow) * (wPow + uPow - VPow) * (uPow + vPow - WPow); double vol = Math.sqrt(a); vol /= b; System.out.printf("%.4f",vol);} // Driver codepublic static void main(String args[]){ // edge lengths double u = 1000, v = 1000, w = 1000; double U = 3, V = 4, W = 5; double b = 12; findVolume(u, v, w, U, V, W, b);} } |
Python 3
# Python 3 implementation of above approach # from math lib import everythingfrom math import * # Function to find the volume def findVolume(u, v, w, U, V, W, b) : # Steps to calculate volume of a # Tetrahedron using formula uPow = pow(u, 2) vPow = pow(v, 2) wPow = pow(w, 2) UPow = pow(U, 2) VPow = pow(V, 2) WPow = pow(W, 2) a = (4 * (uPow * vPow * wPow) - uPow * pow((vPow + wPow - UPow), 2) - vPow * pow((wPow + uPow - VPow), 2) - wPow * pow((uPow + vPow - WPow), 2) + (vPow + wPow - UPow) * (wPow + uPow - VPow) * (uPow + vPow - WPow)) vol = sqrt(a) vol /= b print(round(vol,4)) # Driver code if __name__ == "__main__" : # edge lengths u, v, w = 1000, 1000, 1000 U, V, W = 3, 4, 5 b = 12 findVolume(u, v, w, U, V, W, b) # This code is contributed by ANKITRAI1 |
C#
// C# implementation of above approachusing System; class GFG{ // Function to find the volumestatic void findVolume(double u, double v, double w, double U, double V, double W, double b){ // Steps to calculate volume of a // Tetrahedron using formula double uPow = Math.Pow(u, 2); double vPow = Math.Pow(v, 2); double wPow = Math.Pow(w, 2); double UPow = Math.Pow(U, 2); double VPow = Math.Pow(V, 2); double WPow = Math.Pow(W, 2); double a = 4 * (uPow * vPow * wPow) - uPow * Math.Pow((vPow + wPow - UPow), 2) - vPow * Math.Pow((wPow + uPow - VPow), 2) - wPow * Math.Pow((uPow + vPow - WPow), 2) + (vPow + wPow - UPow) * (wPow + uPow - VPow) * (uPow + vPow - WPow); double vol = Math.Sqrt(a); vol /= b; Console.Write(System.Math.Round(vol, 4));} // Driver codepublic static void Main(){ // edge lengths double u = 1000, v = 1000, w = 1000; double U = 3, V = 4, W = 5; double b = 12; findVolume(u, v, w, U, V, W, b);}} // This code is contributed// by ChitraNayal |
PHP
<?php// PHP implementation of above approach // Function to find the volumefunction findVolume($u, $v, $w, $U, $V, $W, $b){ // Steps to calculate volume of // a Tetrahedron using formula $uPow = pow($u, 2); $vPow = pow($v, 2); $wPow = pow($w, 2); $UPow = pow($U, 2); $VPow = pow($V, 2); $WPow = pow($W, 2); $a = 4 * ($uPow * $vPow * $wPow) - $uPow * pow(($vPow + $wPow - $UPow), 2) - $vPow * pow(($wPow + $uPow - $VPow), 2) - $wPow * pow(($uPow + $vPow - $WPow), 2) + ($vPow + $wPow - $UPow) * ($wPow + $uPow - $VPow) * ($uPow + $vPow - $WPow); $vol = sqrt($a); $vol /= $b; echo $vol;} // Driver code // edge lengths$u = 1000;$v = 1000;$w = 1000;$U = 3;$V = 4;$W = 5;$b = 12; findVolume($u, $v, $w, $U, $V, $W, $b); // This code is contributed// by Shivi_Aggarwal ?> |
1999.9947
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


