All possible boxes inside a cube recursively | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

All possible boxes inside a cube recursively

Could you come up with recursive function to calculate all the boxes inside an N by N by N cube? By looping I get this: N -> boxes 1 -> 0 2 -> 18 3 -> 180 4 -> 900 5 -> 3150 The recursive function may be somethig like: f(N) => ... + f(N-1) Here's the recursion to calculate rectangles inside N by N square as a 2D analogy: return N<2? 0 : (Math.pow(N,3) - Math.pow(N,2)) + rec(N-1);

12th Jan 2022, 6:08 AM
Fernando Moceces
Fernando Moceces - avatar
2 Answers
0
Definitely possible, would take me quite some time though and the recursion would become increasingly ram hungry as the values get bigger
12th Jan 2022, 1:46 PM
AnonyMouse
AnonyMouse - avatar
0
AnonyMouse If you manage you are also encouraged to come up with the recursive function to calculate all possible hyper boxes inside 4-dimensional hypercube with side N. By looping I got: N -> hyperboxes 1 -> 0 2 -> 64 3 -> 1198 4 -> 9646 5 -> 49646 And see if you can generalize the recursion to calculate all possible rectangles/boxes/hyperboxes inside a square/cube/hypercube with side N. For consideration, all possible square/cube/hypercube inside another square/cube/hypercube can be recursively calculated as follows: return x<2 ? 1 : Math.pow(x,y) + sqr(x-1,y); where x is the side of square/cube/hypercube and y is 2 for square, 3 for cube and 4 for hypercube (the dimensions). I was wondering if rectangles inside square and its higher dimensional analogs can be recursively calculated by a general function as well
14th Jan 2022, 2:05 PM
Fernando Moceces
Fernando Moceces - avatar