Perhaps it’s a cheat to use the information of how many numbers there are.
It does make the solution unscalable, but we weren’t asked to make it scalable, so eh. And if I did want to make it work for any parameter, all I’d have to do is put the numbers into a list and cite the list’s length, which isn’t much work.
To quote shuna, with my “solution on paper” “the coding was straight forward enough”.
Python
def problem_2():
a, b, c = 1, 2, 3;
sumN = 0;
for i in range(11):
sumN += b;
a = b + c;
b = a + c;
c = a + b;
return sumN;
Ruby
def problem_2
a, b, c = 1, 2, 3;
sumN = 0;
11.times {
sumN += b;
a = b + c;
b = a + c;
c = a + b;
}
sumN;
end
(I removed return for Feijoa, but I like my semicolons.)
Perl
sub problem_2 {
my $a = 1;
my $b = 2;
my $c = 3;
my $sumN = 0;
for (1..11) {
$sumN += $b;
$a = $b + $c;
$b = $a + $c;
$c = $a + $b;
}
return $sumN;
}
Common Lisp
(defun problem-2 ()
(let ((a 1) (b 2) (c 3) (sum-n 0))
(loop for i from 1 to 11
do
(incf sum-n b)
(setq
a (+ b c)
b (+ a c)
c (+ a b)))
sum-n))
Your Ruby code is practical, but it’s kind of complicated and not very Ruby-like. Well, you can write Ruby however you want, but most code I see is written more simply, without semicolons or return.
Summing things yourself and writing for is also usually not necessary; it’s more pleasant and maybe efficient to make use of the methods in the Enumerable module when you can.
Also, since this is Ruby, why not dynamically modify a core class to allow it to be more expressive?
Summary
def problem1
Numeric.class_eval do
def divisible_by_3_or_5
self % 3 == 0 or self % 5 == 0
end
end
(0...1000)
.select(&:divisible_by_3_or_5)
.sum
end
One would think that I’d get myself some nice node environment with a file watcher and an auto compilation-and-run on save, but I’ve actually made all of these so far in the developer’s console in the browser.
here’s my code, any ideas on improving efficiency?
Summary
def sumEvenFib(a):
i = 1
j = 1
k = 0
evenSum = 0
while (i+j) < a:
k = i + j
evenSum = evenSum + k
j = i + k
i = j + k
return evenSum
print(sumEvenFib(4000000))
One would be to use a do… while loop if python has them to remove the add operation on every condition check by reusing a variable. But it depends on how far you want to go with the “efficiency”. The most efficient solution would have a predetermined hardcoded array of all the even fibb numbers and then just reducing it to a sum.
How come? It’s descriptive of its exact functionality in a java style.
Yeah that is a fair point, I was mainly wondering about general runtime class. But afaik there is no closed expression to get Fibbonacci numbers. It does converge towards Φ^x eventually, but I don’t think it’s possible to use this to get a precise result fast.
Yeah I honestly don’t know much about optimising scripting languages aside from the algorithmic complexity. I think if you want an optimal solution to begin with you should use a compiled language, preferably one that is compiled directly to ASM, and then go from there. For instance c++ has templates so I think you can pre-generate a fibb array up to N during compile time, then do a manual malloc and traverse it by direct pointers to the memory with the compiler optimisations disabled so as to bypass CPU caching. That or simply switching over to ASM in the first place.
Or actually a bit of a hacky way to save time is to compile C into ASM with optimisations disabled and then manually edit the generated ASM code to optimise it.
Depends on what you mean by closed. There is a formula in terms of powers of the golden ratio.
It’s on Wikipedia,
Just depends on whether you want to deal with powers of floating point numbers or instead maybe symbolically/algebraically with the properties of phi. You can always reduces powers of it into linear terms since it satisfies a quadratic polynomial. Seems to be similar with psi=1-phi.
You can calculate the Fibonacci numbers with matrices and look for some tricks with fast exponential of matrices. I don’t know if any of it is better or worse though. Lots of ideas either way.