-2
\$\begingroup\$

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 :]

\$\endgroup\$
13
  • 1
    \$\begingroup\$ 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? \$\endgroup\$
    – devnull
    Mar 13, 2014 at 3:14
  • 2
    \$\begingroup\$ No use of / * % - which are useless anyway, you probably mean + and -. \$\endgroup\$
    – sashkello
    Mar 13, 2014 at 3:26
  • 1
    \$\begingroup\$ If you're using a zero-indexed Fibonacci, the 0th term should be 0. \$\endgroup\$
    – Geobits
    Mar 13, 2014 at 3:48
  • 5
    \$\begingroup\$ 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? :) \$\endgroup\$ Mar 13, 2014 at 3:58
  • 1
    \$\begingroup\$ "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. \$\endgroup\$ Mar 13, 2014 at 7:51

4 Answers 4

1
\$\begingroup\$

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.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ The third term is 3 and not 2, The fifth term is 8 and not 5 please fix your code! \$\endgroup\$ Mar 13, 2014 at 5:45
  • 3
    \$\begingroup\$ @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. \$\endgroup\$ Mar 13, 2014 at 8:24
  • 1
    \$\begingroup\$ Well, i think its really a matter of conventions, but i changed the answer anyway. \$\endgroup\$
    – CCP
    Mar 13, 2014 at 12:43
1
\$\begingroup\$

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│
└───────┴──┘
\$\endgroup\$
1
\$\begingroup\$

Delphi

uses
  System.SysUtils,idglobal;
var
  i,n,m,ires:integer;
  a:TArray<integer>;
  res:string;
begin
  ires:=0;
  readln(n,m);
  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

\$\endgroup\$
1
\$\begingroup\$

C

int fsum (int start, int end) {
    int a[2]={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.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.