Fibonacci in swift playground [duplicate] - swift

This question already has answers here:
Simple Swift Fibonacci program crashing (Project Euler 2)
(2 answers)
using for loop for fibonacci series to print values it will print up to 47 values above that it shows error [closed]
(1 answer)
Fibonacci Calculator stack overflows at 93nd number in Swift [duplicate]
(2 answers)
Closed 4 years ago.
can someone please guide me why this code that i wrote only works until userInput is less than 94 ?
func calculateFibonacciFucntionUntil(userInput: Int) {
var array = [0 ,1]
for i in 2...userInput {
array.append(i)
array[i] = array[i - 1] + array[i - 2]
print(array[i])
}
}
calculateFibonacciFucntionUntil(userInput: 10)

The issue is that Int can only store 64bit numbers (on 64bit platforms) and Fibonacci 95 is bigger than the maximum number that can be stored on 64bits.
Fibonacci 95 is 31 940 434 634 990 099 905, while the biggest number Int64 can hold is 2^63-1 = 9 223 372 036 854 775 807.
You can use Decimal for storing larger numbers than what Int can hold Decimal.greatestFiniteMagnitude is 3.4028236692093865e+165.
However, Swift doesn't have a built-in type for storing arbitrarily large numbers, so if you want to do that you'll either need to use a 3rd party library or implement an appropriate data type yourself.

Related

How is this MATLAB code (involving colon operator) resolved?

Recently, I wanted to calculate the next multiple of 5 of several values.
I was very confused by the output of this code, which should have done the trick:
7:11 - mod(7:11, 5) + 5
ans =
7 8 9 10 11 12 13 14
While the actual working solution was this:
(7:11) - mod(7:11, 5) + 5
ans =
10 10 10 15 15
So this seems to be related to operator precedence! But what exactly does the first command do, and why does it output a (1,8) vector?
Addendum: I have found that the first command can also be written as:
7:(11 - mod(7:11, 5) + 5)
Which already hints towards the explanation of the observed result, but I am still curious about the whole explanation.
Here's the list of MATLAB operator precedence
As you can see, parentheses, (), are solved first, meaning that mod(7:11,5) will be done first. Then point 6), the addition and subtraction are taken care of from left to right, i.e. 11-mod(7:11,5) and then 11-mod(7:11,5)+5. Then point 7), the colon, :, gets evaluated, thus 7:11-mod(7:11,5)+5.
As you noted correctly 7:11 - mod(7:11, 5) + 5 is the same as 7:(11 - mod(7:11, 5) + 5), as seen above using operator precedence.
Now to the second part: why do you obtain 8 values, rather than 5? The problem here is "making an array with an array". Basically:
1:3
ans =
1 2 3
1:(3:5)
ans =
1 2 3
This shows what's going on. If you initialise an array with the colon, but have the end point as an array, MATLAB uses only the first value. As odd as it may sound, it's documented behaviour.
mod(7:11,5) generates an array, [2 3 4 0 1]. This array is then subtracted from 11 and 5 is added [14 13 12 16 15]. Now, as we see in the documentation, only the first element is then considered. 7:[14 13 12 16 15] gets parsed as 7:14 and will result in 8 values, as you've shown.
Doing (7:11) - mod(7:11, 5) + 5 first creates two arrays: 7:11 and mod(7:11,5). It then subtracts the two arrays elementwise and adds 5 to each of the elements. Interesting to note here would be that 7:12 - mod(7:11, 5) + 5 would work, whereas (7:12) - mod(7:11, 5) + 5 would result in an error due to incompatible array sizes.

using for loop for fibonacci series to print values it will print up to 47 values above that it shows error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
var num = "100"
var num = text2.text.toInt()
var temp = 0
var temp2 = 1
if (nu == 1) {
println(1)
}
else {
for var valued = 2; valued<num;++valued {
var temp3 = temp + temp2
temp = temp2
temp2 = temp3
println("\(temp3)")
}
I want to print the fibonacci series of number. The number should be any one that should be choose by user. My code is above i have to choose num as 100 but it will print up to 47 values. The final value print here is 1836311903. It will not print up to 100. It shows an error. How i find the fibonacci series for number 100.
Fib(47) is 2,971,215,073.
2,971,215,073 is larger than 231 - 1 ... which is the largest 32 bit signed integer.
Hence your calculation is overflowing. (In a lot of programming languages, you wouldn't have gotten an error. You would have just gotten the wrong answer.)
How i find the fibonacci series for number 100.
You can't use simple integer arithmetic. You will need to use the Swift equivalent of Java's BigInteger class.
BigInteger equivalent in Swift?

How do you generate a random number in swift? [duplicate]

This question already has answers here:
How to generate a random number in Swift?
(26 answers)
Closed 8 years ago.
tl:dr; How do I generate a random number, because the method in the book picks the same numbers every time.
This seems to be the way in Swift to generate a random number, based on the book released from Apple.
protocol RandomNumberGenerator {
func random() -> Double
}
class LinearCongruentialGenerator: RandomNumberGenerator {
var lastRandom = 42.0
let m = 139968.0
let a = 3877.0
let c = 29573.0
func random() -> Double {
lastRandom = ((lastRandom * a + c) % m)
return lastRandom / m
}
}
let generator = LinearCongruentialGenerator()
for _ in 1..10 {
// Generate "random" number from 1-10
println(Int(generator.random() * 10)+1)
}
The problem is that in that for loop I put at the bottom, the output looks like this:
4
8
7
8
6
2
6
4
1
The output is the same every time, no matter how many times I run it.
The random number generator you created is not truly random, it's psueodorandom.
With a psuedorandom random number generator, the sequence depends on the seed. Change the seed, you change the sequence.
One common usage is to set the seed as the current time, which usually makes it random enough.
You can also use the standard libraries: arc4random(). Don't forget to import Foundation.
Pseudorandom number generators need a "seed" value. In your case, if you change lastRandom with any number, you'll get a different sequence.

Matlab increment bug? [duplicate]

This question already has answers here:
Matlab gives wrong answer
(3 answers)
Closed 8 years ago.
I don't know what's going on but when I use i = 0.1:0.1:7 in my code I get the integers 1 and 2, it then skips 3 but gets 4, 5, 6 and 7 no problems.
x=zeros((t_f/h)+1,1);
x(1,1)=0;
table=zeros(t_f,1);
for i=0.1:0.1:7;
x(round(i/h)+1,1)=i;
if ~mod(i,1)
table(i,1)=i;
end
end
Then to test I returned these
table
a=[x(1) x(11) x(21) x(41) x(51) x(61) x(71)]
a=[x(1) x(11) x(21) x(31) x(41) x(51) x(61) x(71)]
It's not finding 3 as an integer because i never is 3, it's 3.000... but 1, 2, 4, 5, 6 and 7 are integers.
It a floating point number representation issue. When the numbers are constructed in this way, the number you expect to be exactly 3 is off by a small amount (4.440892e-16). This is expected behaviour, not a bug, see: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Matlab, how to convert a string of integers into a vector? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
convert string to number array in matlab
Is there a simple way in Matlab to convert a string like this
'123456789'
into a vector like this ?
[1 2 3 4 5 6 7 8 9]
If all you have is contiguous characters from 0 to 9:
v = double(s)-'0';
double(s) converts a string into an array where each element is the ASCII code of the corresponding character. To obtain the numberic values we subtract '0' (which is in fact 48 in ASCII) and since digits have a sequential representation in ASCII code ('1' = 49, '2' = 50, etc.) we end up with intended result.
one way would be using regexp for this. But of course it only works for single digit numbers.
>> str = '123456789';
>> num = regexp(str,'\d')
num =
1 2 3 4 5 6 7 8 9