### astronomical problem | recursion

Hi, so the problem is this:

In the 18th century, the astronomer Johann Daniel Titius proposed a rule, later
recorded by Johann Elert Bode, for calculating the distance from the sun to
each of the planets known at that time. To apply that rule, which is now known
as the Titius-Bode Law, you begin by writing down the sequence
b_1 = 1 b_2 = 3 b_3 = 6 b_4 = 12 b_5 = 24 b_6 = 48 . . .
where each subsequent element in the sequence is twice the preceding one. It
turns out that an approximate distance to the ith planet can be computed from
this series by applying the formula

d_i = (4 + b_i) / 10

The distance d_i is expressed in astronomical units (AU), which correspond to
the average distance from the earth to the sun (approximately 93,000,000
miles). Except for a disconcerting gap between Mars and Jupiter, the
Titius-Bode law gives reasonable approximations for the distances to the seven
planets known at the time:
Mercury 0.5 AU
Venus 0.7 AU
Earth 1.0 AU
Mars 1.6 AU
? 2.8 AU
Jupiter 5.2 AU
Saturn 10.0 AU
Uranus 19.6 AU

Write a recursive function getTitiusBodeDistance(k) that calculates
the expected distance between the sun and the k^th planet, numbering outward
from Mercury starting with 1. Test your function by writing a program that
displays the distances to each of these planets in tabular form.

I cannot think how to make it to simple problems and solve recursively. Currently, I have a very short program solved with iterations. I cannot think of any simple problems for this problem. I also think my if statement is wrong ` if (k < 1 ) return 0.5 `:) I am trying to write this program using recursion!

 ``1234567891011121314151617181920212223`` ``````#include using namespace std; double arr [] = { 1, 3, 6, 12, 24, 48, 96, 192 }, d; double getTitiusBodeDistance(int k) { if (k < 1) { return 0.5; } else double arr [] = { 1, 3, 6, 12, 24, 48, 96, 192 }, d; double d = (4 + arr[k]) / 10; return d; } int main() { for (int i=0; i < 8; i++) cout << "Planet " << i << " | " << getTitiusBodeDistance(i) << endl; return 0; }``````

Output:
 Planet 0 | 0.5 Planet 1 | 0.7 Planet 2 | 1 Planet 3 | 1.6 Planet 4 | 2.8 Planet 5 | 5.2 Planet 6 | 10 Planet 7 | 19.6
Last edited on
The recursion comes in the calculation of the value for a term in the Titius-Bode sequence. There is no reason for an array in either a recursive or iterative version. The function should calculate the term value so that main looks like:

 ``12345`` ``````int main() { for (int i=1; i < 8; i++) cout << "Planet " << i << " | " << (4+getTitiusBodeTerm(i))/10.0 << endl; }``````
Thanks cire. How I could make this function recursive? :)
Last edited on
You call the function from inside the function. ;)

I would suggest retooling the iterative version to not use an array, and using that as a jumping off point, then consider the recursion in terms of the iterative loop. (You actually don't have an iterative version. Iterative solutions use a loop.)

The first order of business then is: What is the terminating condition for the recursion?
Last edited on
I know that it's a function from inside the function, but can't figure out what would be the commands to call the function inside the function.
 ``12345678910111213141516`` ``````#include void getTitiusBodeDistance(int k) { if (k > 1) getTitiusBodeDistance(k -1); std::cout << "Planet " << k << std::endl; } int main() { getTitiusBodeDistance(7); return 0; }`````` ```Planet 1 Planet 2 Planet 3 Planet 4 Planet 5 Planet 6 Planet 7 Press any key to continue . . .```
Last edited on
 I know that it's a function from inside the function, but can't figure out what would be the commands to call the function inside the function.

You could look up relative easier ones before... A fine example will be a program to calculate factorial.... google it and try to understand
Done the factorial already. I understood it and now am trying to understand a bit more complex problems.
Last edited on
 b_1 = 1 b_2 = 3 b_3 = 6 b_4 = 12 b_5 = 24 b_6 = 48 . . . where each subsequent element in the sequence is twice the preceding one.

Therefore, to get b_6, you need to find b_5, and to get b_5, you need to get b_4, and so on.
How would that help me write the recursive function?
 How would that help me write the recursive function?

Because that's what makes the function recursive. I'm sure there are other ways to write it, but this is what I would do.

Let f_i be the ith factorial.
To get f_3, you need to get f_2, and to get f_2, you need f_1, which is your base case.

 ``1234567891011121314151617181920212223`` ``````double getTitiusBodeDistance(int k) { // base case if ( k == 1 ) { } else if ( k == 2 ) { } /* Now to get b_4, you need b_3, and to get b_3, you need b_2, and so on See the pattern? To get b_k, you need b_(k-1), and to get b_(k-1), you need b_(k-2), and so on This is very similar to a recursive factorial function. */ }``````
Last edited on
 ``12345678`` ``````int b( int i ) // invariant: i > 0 { if( i < 2 ) return 1 ; // base case 1: b_1 == 1 else if( i == 2 ) return 3 ; // base case 2: b_2 == 3 else return 2 * b(i-1) ; // otherwise: each subsequent element is twice the preceding one. } double distance( int i ) { return ( b(i) + 4 ) / 10.0 ; } // note: avoid integer division ``````
Last edited on
Topic archived. No new replies allowed.