# Calculate Fibonacci series from nth term to mth term [duplicate]

Your goal is to calculate the sum of Fibonacci series from n th term to m th term (including both terms).

• No use of / * % or mathematically what they are in your language of choice.
• No use of special functions or code language specialized in doing so !
• You have to produce the series yourself and not by any special function or code language.

## OUTPUT

#1

0 5
series : 1 1 2 3 5 8
sum    : 20

#2

2 5
series : 2 3 5 8
sum    : 18


Best of luck :]

• Question says nth to mth (including both). So 0 5 would imply 6 terms; similarly 2 5 would imply 4 terms. Could you edit your question to make it consistent? – devnull Mar 13 '14 at 3:14
• No use of / * % - which are useless anyway, you probably mean + and -. – sashkello Mar 13 '14 at 3:26
• If you're using a zero-indexed Fibonacci, the 0th term should be 0. – Geobits Mar 13 '14 at 3:48
• The site has 23 Fibonacci questions already. (Type fibonacci is:question into the search.) Did you consider giving a different number sequence a look in? :) – Jonathan Van Matre Mar 13 '14 at 3:58
• "Do the same calculation as this question but tweak the output slightly" doesn't make it a different question. See Jonathan's comment. There are lots of interesting sequences in OEIS which no questions have been asked about. – Peter Taylor Mar 13 '14 at 7:51

# Julia

fib(n)=n<2?1:fib(n-1)+fib(n-2)
sumfib(n,m)=sum([fib(i) for i=n:m])


Output examples:

julia> sumfib(0,5)  # 1  1  2  3  5  8
20

julia> sumfib(2,4)  # 2  3  5
10

julia> sumfib(2,5)  # 2  3  5  8
18


EDIT: Changed to fit question's fibonacci convention.

• The third term is 3 and not 2, The fifth term is 8 and not 5 please fix your code! – Mukul Kumar Mar 13 '14 at 5:45
• @Mukul Kumar. Both convention actually exist (either f(0) = 0 and f(3)=2 or f(1)=1 and f(3)=3). But since you don't follow yourself the most current and modern convention, you can't ask the author of the current solution to change his/her code. I think the author shouldn't change it, and the question should allow both conventions. – Thomas Baruchel Mar 13 '14 at 8:24
• Well, i think its really a matter of conventions, but i changed the answer anyway. – CCP Mar 13 '14 at 12:43

# J (35 characters)

A solution in J with Fibonacci sequence starting at index 0 and value 0 according to most current conventions. The slash / has absolutely nothing to do with mathematical operation in J.

(;+/)@:(+/@(!|.)@i."0)@([+i.@>:@-~)


For instance:

3 (;+/)@:(+/@(!|.)@i."0)@([+i.@>:@-~) 6
┌───────┬──┐
│2 3 5 8│18│
└───────┴──┘


# Delphi

uses
System.SysUtils,idglobal;
var
i,n,m,ires:integer;
a:TArray<integer>;
res:string;
begin
ires:=0;
SetLength(a,m+1);
for i:=0to m+1 do
a[i]:=iif(i<2,1,a[i-1]+a[i-2]);
for i:=n to m do
begin
write(Format('%d ',[a[i]]));
inc(ires,a[i]);
end;
WriteLn('|'+IntToStr(ires));
end.


Input: 0 5
Output: 1 1 2 3 5 8 | 20

Input: 1 20
Output: 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 | 28655

# C

int fsum (int start, int end) {
int a={1,0};
int p=1,b=1,d=0;
for (;p<=end+2;b=!b) {
a[b]+=a[!b];
d=p++==start+1?a[b]:d;
}
return a[!b]-d;
}


To make it more interesting, I've decided to see if I could a) use a static amount of memory, and b) produce the requested sum without actually adding up the [n..m] elements. So my code doesn't print out all the elements (since it wasn't required), but produces the correct result nevertheless.