Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that doesn\'t use math.h that inputs two positive integers a and

ID: 3621303 • Letter: W

Question

Write a program that doesn't use math.h that inputs two positive integers a and b and approximates the value x such that x^(1/a) + x^(1/b) = 1.
More precisely, output to a 5-digit precision, a positive value x such that |x^(1/a)+x^(1/b)-1|<10^(-5).
For example, if a = 2 and b = 3, the program can output 0.18504 since 0.185041^(1/2)+0.185041^(1/3)-1=5.8*10^(-6)<10^(-5).
There are several ways to solve this problem, one uses Newton-Raphson on an appropriately defined variable y = f(x) which when substituted in the original equation makes it easier to solve in y.
Sample runs of the program are shown below.
(~)$ a.out
a and b: 2 3
0.18504
(~)$ a.out
a and b: 1 1
0.50000
(~)$ a.out
a and b: 4 4
0.06250
(~)$ a.out
a and b: 100 2
0.00314

Explanation / Answer

This took a fair amount of work. :) Hope it helps. Please rate! For an explanation of the Newton-Raphson Method, you can take a look at http://www.shodor.org/unchem/math/newton/index.html #include // This will estimate the value of x^(1/n) to precision 1*10^-6 // To do this we will use x^(1/n) = y and estimate x = y^n // Once we have y^n to precision (based on value of x), // divide by y^n by y in a loop to get x^(1/n) double estimateValue2(double x, int n) { double dy = 1; double y = 1; // x0 = 1 - some random guess double fy; // f(y) = x = y^n (or y^n - x = 0) double fyPrime; // f'(y) = n*y^(n-1) while (dy > 0.000001 || dy < -0.000001) { double yToNMinus1 = 1; double yToN; int i; for (i = 0; i 0.00001 || dx < -0.00001) { val1 = estimateValue2(x, a); val2 = estimateValue2(x, b); fx = val1 + val2 - 1; fxPrime = (val1 /(x * a)) + (val2 /(x * b)); dx = fx / fxPrime; if (x - dx < 0) x = dx - x; else x = x - dx; } return x; } int main() { int a, b; double x; printf("a and b: "); scanf("%d %d", &a, &b); if (a
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote