How to solve repeatedly squared math problem?

Given real numbers x,a, and natural number n. Calculate the expression as shown on the image(https://i.ibb.co/N2PFhYL/2021-09-17-003451.png):
((...((x+a)^2+a)^2+...+2)^2 +a

Example:
Input:
2 0 1
Output:
4

What I did is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 #include<iostream>
#include<cmath>
using namespace std;
int main()
{
    float a, x, res;
    int n;
    cin >> x >> a >> n;
    res = x;
    for(int i = 1; i <= n; i++){
        res = pow(res+a, 2);
    }
    cout << res+a;
}


On the website(similar to codeforce) it fails at the second test.
But unfortunately I can't tell you what's the problem here. It just seems to me that everything should work fine.
Last edited on
> res = pow(mul+a, 2);
What's mul?

> Calculate the expression as shown on the image(I don't know how to attach a picture here so can you also help with it too)
Just put it on https://imgur.com/ and paste the link instead.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;

int main()
{
   double a, x;
   int n;
   cin >> x >> a >> n;
   x += a;
   for ( int i = 1; i <= n; i++ ) x = x * x + a;
   cout << x;
}
Last edited on
@salem c you’re right I mistyped and it’s actually res variable but it’s not the real problem here though. Edited my code
Thank you
Last edited on
> @salem c you’re right I mistyped and it’s actually res variable
There shouldn't be any typing at all!!!!
It should be a simple copy/paste from the code showing the problem you describe.

Not some half-baked 'more or less as I remember it'.

> but it’s not the real problem here though.
No shit.
Don't expect anyone to diagnose the problem if your code isn't representative to begin with.
@salem c
Right, it's my fault.
That's because the original code was this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    float a, x, mul;
    int n;
    cin >> x >> a >> n;
    mul = x;
    for(int i = 1; i <= n; i++){
        mul = pow(mul+a, 2);
    }
    cout << mul +a;
}

mul was a variable name of the previous code I was working on, so to avoid confusion I rewrited it to res. Should have compile it before posting

Edit: Thank you for the advice it's really helpful for the beginner such as myself
Last edited on
@lastchance It didn't work I am starting to think the problem isn't well-defined
its going to overflow relatively soon but I see nothing wrong with the problem.
using little data types (float instead of double or if available, long double) and int instead of int64 is not helping you with the potential to overflow.

do you have a solved case you can test against, eg 3 and 5 gives 42 or whatever (yes, I know that is not right).
just say x*x pow is not helpful for small int powers (its slower, not that it matters here I guess .. you will notice if you do millions of them)

its possible your code is right and just overflowed depending on the inputs. try like mul start at 1, a being 2, N being like 5 or something really simple.

style: typically one says 0 to N in c++
for(int i = 0; i < N; i++) //same as <=N starting at 1, but its convention to do it this way unless you use i in the loop and its value matters.

style: x serves no purpose. read into mul directly.

pretty sure its just overflow. Note that 2,0,5 is 4.blah e9 ... so it gets huge rapidly. My no-frills take:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
   double x,a; 
   unsigned int n;
   cin >> x >> a >> n;
for(int i = 0; i < n; i++)
{
   x+=a;
   x*=x;	
}	
cout << x << endl;
	
}
Last edited on
@jonnin Hi, I am very happy you showed me how to properly style my code. However it's still the same after incorporating your suggestion.

https://i.ibb.co/dr2F7mj/2021-09-17-025605.png

The website can only show three results: "Wrong answer", "Compilation error" and "Accepted"

Last edited on
If n is 1 then it looks like it should be (x + a)2 +a = ?, so it looks like you are missing a final + a after the for loop.
@The Grey Wolf

Added +a after the loop

cout << x+a << endl;

But it also fails
Last edited on
I need an example then, one that fails along with its correct answer, if you have any more.
could it be that it wants you to do it in uint64s instead of doubles? Seems possible but ?? hard to say. You could try it. Int 64 will allow more precise answers. Double will allow larger answers. It says the input is real, though... so that does not seem likely.

is this a gotcha where the errors round up rapidly if you don't factor the equation into some easier to compute form?
Last edited on
I think I might understand what is required...but it's 1:30 am and writing code on an ipad is a PITA...I'll post something tomorrow if no one else solves it first.
@LeoV3,
(1) Can you confirm that the order of input is x, a, n .... and not n, x, a or some other permutation.
(2) Is there any required format for the output?
(3) Can you show an image of the whole question and not just the one, slightly ambiguous, equation.

I stand by my code.
lastchance, I have just looked at your code, didn't see it before...that is pretty much what I came up with on paper. I toyed with what would be a sensible answer if n = 0, I know 'natural number n', but do we assume happy path input?

Edit I think I would put in a guard for n=0 and output the value of a...if there is no further info on if n=0 is considered a valid input.
https://mathworld.wolfram.com/NaturalNumber.html
Last edited on
Thanks @The Grey Wolf.

I always understood "natural numbers" to be strictly positive integers, so 1,2,3,...

It's possible that "real numbers" might have lost something in translation, and might conceivably mean simply positive or negative whole numbers, with an implication that the answer be required to be exact. (As it stands it will come out with a maximum of six significant digits as a double, unless told otherwise.)

@LeoV3, is there a link where we can enter our own solutions for this problem? (But no, I don't want to sign up for another site.) Also, what happens if you make x and a have type long long rather than double?
Last edited on
Here's the code again, with a guard for n=0, more digits in the output and some optioneering on the input types.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
using namespace std;

int main()
{
   double a, x;          // As stated in the question
// long long a, x;       // You might want to try this instead
   unsigned n;
   cin >> x >> a >> n;
   x += a;
   for ( int i = 1; i <= n; i++ ) x = x * x + a;
   cout << fixed << ( n ? x : a );       // Guard for n=0 and more digits in the output
}
Last edited on
Thanks everyone

It turned out to be problem tester's malfunction so I will give my answer as soon as my instructor fixes this problem

And sorry for misleading all of you
Ugg... a lot of universities are trying to use screwball auto-graders that are like unto the code contest/rater sites. With the expected results... the things don't work well and often give minimal feedback. I taught my brother in law (a professional student, he is nearing 60 and still taking classes) how to back-hack his code to make it work. I told him to submit code that just echoed the input to the screen, lift the input and debug based off what was actually being sent to the program.. the one he used was nice enough to give the expected output and YOUR output but not the input, lol. So with the 3 pieces (the fake output being the real input, and the real expected output, etc), he could try to debug..
the graders vary... see if yours has any backdoors like this that can help you to get ahead of problems and debug more efficiently. They are also dumb as a post, with this you can of course just hard code the outputs, meaning students have yet another way to cheat their way into the workforce. Joy.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main(){
	unsigned x=2, a=1, n=3;
	while(n--){
		x+=a;
		x*=x;
	}
	x+=a;
	cout<<x; //10202
}
Topic archived. No new replies allowed.