r/Julia • u/ayariv06 • Sep 29 '26
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")
View all comments
Show parent comment
1
u/markkitt Sep 29 '26
If it is 10 minutes every time you run it, that is a bit concerning. Some delay is expected the first time you run it, but then it should br faster the next time you run it in the REPL. If you would like help with diagnostics, let me know.