This is a rather theoretical Question: If i have a for loop i=1:Inf, will the loop run forever or stop, when the loop index exceeds realmax? The loop index still grows after reaching 2^53, so matlab seems to have some internal measures to counter precision loss in this case…
MATLAB: Is an infinite for loop infinite
for loopinfinityMATLAB
Related Solutions
You can't write multiple if conditions this way in MATLAB:
if -128 <= b <= ((2^7)-1)
You have to break it up into individual conditions. E.g.,
if -128 <= b & b <= ((2^7)-1)
Why are you doing the following:
b=min(A);
Hint: The following will turn A into a column vector of all the elements:
b = A(:);
Instead of using for loops, you can then write logic to test all of the elements of b at once to get your result.
Caution: You are going to have a problem with this part of your test for int64:
elseif (-2^63)<=b & b<=((2^63)-1)
The reason is that an int64 actually has more precision than a double, so you can get into trouble comparing numbers near the limits. E.g.,
>> intmax('int64')ans = int64 9223372036854775807>> b = 2^63;>> num2strexact(b)ans = '9.223372036854775808e18'
Just looking at these numbers you can see that b is too large to fit in an int64 class variable. But look what happens with the test that you have:
>> b <= ((2^63)-1)ans = logical 1
It returned true! Obviously this is an incorrect result for what you are trying to test. What is the problem? It is the fact that a double precision variable does not have enough precision to do the (2^63)-1 calculation exactly. E.g.,
>> eps(2^63)ans = 2048
The spacing between numbers near 2^63 in IEEE double is 2048, much larger than the 1 you are trying to do arithmetic with. E.g., look at these results:
>> num2strexact((2^63))ans = '9.223372036854775808e18'>> num2strexact((2^63)-1)ans = '9.223372036854775808e18'>> num2strexact((2^63)-10)ans = '9.223372036854775808e18'>> num2strexact((2^63)-100)ans = '9.223372036854775808e18'
You get the same number in each case. Subtracting 1, 10, or 100 had no effect on the answer because they are too far below eps(2^63). So, you will need to rewrite this test. Hint: What would happen if you tried to do this test in int64 arithmetic instead of double arithmetic? Try it out ...
Note that IF you are really trying to solve PE #2 (it has been a moderately long time since I was actively solving PE problems on a daily basis) then you are doing something wildly wrong. PE #2 only assks for the sum of Fibonacci numbers that do not exceed 4e6.
Instead, it looks like you are trying to solve a Cody problem, this one, in fact:
Note that it does NOT ask you to solve for the sum of the first 597455000 fibonacci numbers. It asks you to solve for the sum of even Fibonacci numbers that do not exceed that value. There is an immense difference!
See that your while loop is running like this:
while(x>=i)
WRONG! Think carefully about what the correct test would be. I won't do your Cody problem for you, as that teaches you nothing. Sorry.
The Fibonacci number with index as large as 597455000, thus F_597455000, would be a number with a vast number of decimal digits. (Do I really need to compute it, or even to compute the log of that number?) While that sum would be technically possible to accomplish, that is NOT what was asked. Anyway, as part of a PE problem, they would always ask you to compute that result modulo some large integer anyway.
So what you need to do is first, NOT compute and save the numbers, as that is inefficient. Just save the sum of the numbers that are even. In fact, it might even be there are easier ways to answer that problem, however, they may not all be the most code efficient ways for a top Cody score. I'd need to look to see if I wrote code for that Cody problem. While I may have done so, Cody has moved by leaps and bounds since I was actively solving Cody problems too. Drat. Now you have me wondering what I did there, or if not, maybe I want to go play Cody for a bit... Grant would want me to do so.
First, just for kicks, how many decimal digits does F_597455000 have?
We can just use the Binet formula here, then taking the log, base 10. Even simpler, we need only the bigger term in that formula.
Thus we can use
F_n = (1+ sqrt(5))^n / 2^n/ sqrt(5)
APPROXIMATELY so. This term completely dominates the smaller one for even reasonably small values of n. Then
log10(F_n) = (log10(1 + sqrt(5)) - log10(2))*n - log10(sqrt(5))
In MATLAB, we see that
n = 597455000;(log10(1 + sqrt(5)) - log10(2))*n - log10(sqrt(5))ans = 124860710.256066
So that Fibonacci number would have almost 125 MILLION decimal digits. This is not what was requested. As I said, a HUGE difference.
Anyway, If I REALLY wanted to compute the sum of the even Fibonacci numbers, I would probably use a cute little trick, and completely avoid a loop at all. And that is something I will not say any more about, as I hope others might be willing to take that as a challenge. Thus, what is the sum of the Fibonacci numbers that are divisible by 3, up to index 1000, all done without a loop? Note that the 1000'th Fibonacci number is itself huge.
fibonacci(1000)ans = 43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875
I'll add the answer if anyone wants to know it, though I won't pose my solution. Here is the modulus of that sum by my computations, modulo 1e9.
mod(S,1e9)ans =563887875
Since each term must be divisible by 3, so must be the sum.
mod(S,3)ans = 0
Best Answer