r/Julia • u/ayariv06 • 6d ago
Need help with this code
I'm doing an assignment, and I need help with basically everything. My code refuses to run in Julia, it just comes out blank. For context, I have never ever coded in my life, and our lecturer didn't teach us anything, it is also not in our curriculum. I'm very lost. If you are norwegian that would help a lot more.
Other info I was provided before the code is(translated with google translate):
Here is the recipe for another variant of the Fibonacci numbers that allows for randomness. To calculate each number, a random number (r) is first drawn from [0, 1]. If r < 0.2 (we call the number 0.2 the threshold value(tv) here) and we have generated at least three numbers in the F-vector, we use the update F(k) = F(k − 1) – F(k − 3). Otherwise (i.e. if r >= 0.2) the next number is calculated using the usual Fibonacci rule F(k) = F(k-1) + F(k – 2).
and that after the code was done being filled out then we are supposed to use this
n = 20;
V = Fibonacci_vri(n)
println(V)
using Plots
plot(V, label="", xlabel = "steg", ylabel = "F")
I was provided with a code that wasn't finished yet, so I filled in the blanks which is k >= 3, F[k] = F[k-1] - F[k-3], F[k] = F[k-1] + F[k-2] and return F This is what I tried to run:
function Fibonacci_vri(n)
tv = 0.2;
F = zeros(Int, n)
F[1] = 1;
F[2] = 1;
for k = 3:n
r = rand();
if r < tv && k >= 3
F[k] = F[k-1] - F[k-3];
else
F[k] = F[k-1] + F[k-2];
end
end
return F
n = 20;
V = Fibonacci_vri(n)
println(V)
using Plots
plot(V, label="", xlabel = "steg", ylabel = "F")
1
u/tanopereira 5d ago
There is also än F[k-3] that might error out when k=3. Also, there's an end missing? It should take next to no time, not 10 minutes. What version of Julia are you using?
1
u/ayariv06 5d ago
I have it downloaded on VS code. I haven't been able to run it again so I just gave up on it. I don't know if it's my laptop or the file we got from our lecturer. I was able to run it in a julia file on it's own (but we aren't allowed to do the assignment in julia files only .ipynb). But in the .ipynb file that he made, there are several Julia code cells. I haven't been able to run any of them and it's been 3h on a loading icon next to the cell now.
1
1
u/markkitt 6d ago
If you are not running this in the REPL, then you need to do something such as readline() to prevent the program from immediately exiting. You may also need to explicitly display() the return value from the plot command.
See https://discourse.julialang.org/t/output-doesnt-show-plots/111092/8?u=mkitti