S n = 1 + 2 − 3 × 4 ÷ 5 + 6 − 7 × 8 ÷ 9 + 1 0 − ⋯ n
Consider the above sum where the first n positive integers are listed in ascending order, and the mathematical operations ( + , − , × , ÷ ) are repeated in that order.
Find the value of S 3 1 4 1 7 . Give your answer to 3 decimal places.
Details and Assumptions :
BODMAS ( Order of Operations ) is obeyed.
A couple of explicit examples: S 4 S 5 = 1 + 2 − 3 × 4 = ( 1 + 2 ) − ( 3 × 4 ) = 3 − 1 2 = − 9 = 1 + 2 − 3 × 4 ÷ 5 = ( 1 + 2 ) − ( 3 × 4 ÷ 5 ) = 3 − 1 2 ÷ 5 = 0 . 6 .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The mathematics simplification is outstanding, I was asking if there are some math hacks for this prob!
With Excel of cells:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
We can check sum of terms with step accumulations to agree each other.
-3.59816362860588 is the numerical value obtained.
Answer: − 3 . 5 9 8
There are thousands of Codes to solve this.
You can identify the Reappeared Sequence of the operators. s = 1 + ( 2 − 3 ∗ 4 / 5 ) + ( 6 − 7 ∗ 8 / 9 ) + ( 1 0 − 1 1 ∗ 1 2 / 1 3 )
Now, get ready for the Loop. As you see, that "1" is kept out of the braces, you have to set the initial value of Sum,
s=1;
.
Then you take the 4 variables - a,b,c,d. You see, always, b = a + 1 c = b + 1 d = c + 1
And it occurs again and again. In the 1st loop,
a=2
. You can declare it inside the
for
loop. To get the numbers of the 2nd loop,
a+=4
. Because then you get
a
=
6
in the 2nd, and
a
=
1
0
in the 3rd loop and blah blah blah!
So, the for loop is
for(a=2; ; a+=4)
. Look carefully, we didn't specified the condition for the loop, because using the
break;
statement, we can do that job.
Then, inside the loop, first produce the other numbers, b , c , d .
Now comes the most important. If a = n , s + = a ; then Break the loop.
But if b = n , then s + = a − b ; , and Break ! Similar for c = n and d = n . Notice their statements in the program below.
If nothing of them isn't equals to
n
, that means that they haven't reached the value of
n
yet, which will happen from the beginning. For that, the statement is,
s+=a-((b*c)/d);
. Remember no
Break
this time, as it needs to continue until any of the variables reach the
n
.
So, that's it. Here's the code after all this detailed explanation...
Compile, run it, Enter n=31417 and get the answer!
Any queries, comment box is open for you of course.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
Alternatively, you can use recursive functions which shortens the code a lot. There's a nice recurrence relation for S n which is,
S n = ⎩ ⎪ ⎪ ⎪ ⎪ ⎨ ⎪ ⎪ ⎪ ⎪ ⎧ n ∗ ( n − 1 ) ∗ ( − 1 ) + S n − 2 ( ( n − 1 ) ÷ n ) ∗ ( n − 2 ) ∗ ( − 1 ) + S n − 3 n + S n − 1 n ∗ ( − 1 ) + S n − 1 ∀ n ≡ 0 ( m o d 4 ) ∀ n ≡ 1 ( m o d 4 ) ∀ n ≡ 2 ( m o d 4 ) ∀ n ≡ 3 ( m o d 4 ) ; S 1 = 1
This allows us to implement a recursive function for computing S n very easily. Here 's the C++ implementation of the above recurrence relation to compute S n .
Log in to reply
Can you post a new solution with this logic? That really simplifies the Code.
p=31417
x=2
y=3
s1=0.00
s2=0.00
while x<=p:
s1=s1+x
x=x+4.00
while y<=p:
if y+2<=p:
s2=0.00+s2+y*(y+1)/(y+2.00)
elif p-y==1:
s2=0.00+s2+y*(y+1)
elif p-y==0:
s2=0.00+s2+y
y=y+4.00
print s1+1-s2
With which programming language you wrote the Code? Or its a Pseudocode ?
Problem Loading...
Note Loading...
Set Loading...
S − 1 = 2 − 3 × 4 ÷ 5 + 6 − 7 × 8 ÷ 9 + . . .
S − 1 = n − n + 3 ( n + 1 ) × ( n + 2 ) + . . . = n − ( n + n + 3 2 ) + . . . = − n + 3 2 − . . . − N + 4 2
n = N + 1
n = { 2 , 6 , 1 0 , . . . , N + 1 }
S = 1 − 2 × ( n + 3 1 + . . . + N + 4 1 ) = 1 − 2 × ∑ n = 1 4 N + 1 4 n + 1 1
MATLAB scripts