I have a task that I've been stuck with for too long. I should consider all possible expressions from 1 to N as follows:
n = 5; 1 % 2 % 3 % 4 % 5 = ?
where % can be addition, subtraction or multiplication (+, -, *) I have to consider all possible combinations of these operations and calculate how many resulting expressions will be equal to n itself.
So, for example, for n = 4, the answer is 1, because there is only one expression equal to n.
1 + 2 - 3 + 4 = 4
There are a few cautions: multiplication is more strongly associated than the other two operations. For example,
1 + 2 + 3 * 4 * 5 + 6
need to analyze how
1 + 2 + (3 * 4 * 5) + 6
In addition, multiplication can be used only 5 times per line (not in general), therefore everything that is under n = 20 can be placed in integers. To solve this problem, I wrote this recursive tree, but with higher values ββlike n = 15, my result becomes wrong.
[N ] - [Expected result] [My program result] [5 ] - [ 3] [ 3] [6 ] - [ 1] [ 1] [9 ] - [ 27] [ 27] [15] - [ 3932] [ 3911] [16] - [ 9803] [ 9327] [17] - [ 23209] [ 22942]
Iβve been trying to diagnose this for almost a week and canβt make it work properly ... I tried to make the code as readable as possible and comment on where it is needed. Just to explain what the code does - it creates a tree where (+, - and *) are branches at each iteration. Each node is the sum of the expression to this point, so when we reach depth = n, all the end nodes are all kinds of sums of the expression - all we need to do is check to see if they are equal to n. Illustrated below:

#include <stdio.h> int n; int result = 0; void tree(int depth, int sum, int mul, int last) { //DEPTH = recursion from 1 to n //SUM = the sum of the expression //MUL = counter to track how many consecutive multiplications have been done //LAST = previous number added to sum //if n nodes reached if (depth == n) { if (sum == n) { //count result result++; } return; } //build tree depth++; if (mul % 5 != 0) { //if multiplication hasn't been used 5x in a row tree(depth, (sum - last) + (last * depth), mul + 1, last * depth); } else { //else dont build a multiplication branch, but reset the counter mul = 1; } //build addition and subtraction trees tree(depth, sum + depth, mul, depth); tree(depth, sum - depth, mul, depth * -1); } int main(int argc, char **argv) { scanf("%i", &n); tree(1, 1, 1, 1); printf("%i\n", result); return 0; }
UPDATE 1: MUL COUNTER CORRECTED
#include <stdio.h> int n; int result = 0; void tree(int depth, int sum, int mul, int last) { //DEPTH = recursion from 1 to n //SUM = the sum of the expression //MUL = counter to track how many consecutive multiplications have been done //LAST = previous number added to sum //if n nodes reached if (depth == n) { if (sum == n) { //count result result++; } return; } //build tree depth++; if (mul < 5) { //if multiplication hasn't been used 5x in a row tree(depth, (sum - last) + (last * depth), mul + 1, last * depth); } else { //else dont build a multiplication branch, but reset the counter mul = 0; } //build addition and subtraction trees tree(depth, sum + depth, mul, depth); tree(depth, sum - depth, mul, depth * -1); } int main(int argc, char **argv) { scanf("%i", &n); tree(1, 1, 0, 1); printf("%i\n", result); return 0; }
Changes: Fixed counters and initial values ββin accordance with the answers (thanks!), But the program still produces incorrect results at high values, updated data:
[N ] - [Expected result] [My program result] [5 ] - [ 3] [ 3] [6 ] - [ 1] [ 1] [9 ] - [ 27] [ 27] [15] - [ 3932] [ 3924] [16] - [ 9803] [ 9781] [17] - [ 23209] [ 23121]
The results are closer!