0

Even Fibonacci numbers sum whose values do not exceed four million. I am using multiline function in APL, but not getting the output:

result←Euler2 a;b;c;sum;i;limit
 b←0
 c←1
 sum←0
 i←0
 limit←4000000
 :For i :In limit
     a←b
     b←c
     c←a+b
     :If (0=2|c)
         sum←(sum+c)
     :EndIf
 :EndFor
 result←sum
Adám
  • 6,573
  • 20
  • 37
Pk1
  • 3
  • 1

2 Answers2

2

Warning: opinions below.

I would avoid using manual loops and tradfns. The resulting solution will (often) be clunky and slow. APL has powerful abilities for array processing, use them as much as you can.

Here's a different possible solution, using 'dfns'.

+/{⍵/⍨0=2|⍵}1↓{⍵,⍨+/2↑⍵}⍣{4000000<⊃⍺} 1 1
⍝ explanation:
{⍵,⍨+/2↑⍵} +/2↑⍵ the sum of the first two items in ⍵
 ⍵,⍨              prepend to ⍵
{⍵,⍨+/2↑⍵} 1 1   is 2 1 1
{⍵,⍨+/2↑⍵} 2 1 1 is 3 2 1 1
⍣  do this (see https://help.dyalog.com/18.0/Content/Language/Symbols/DieresisStar.htm)
{4000000<⊃⍺} 1 1      until the previous element (⊃⍺) is greater than 4 million, starting with the vector 1 1

      'result:', {⍵,⍨+/2↑⍵}⍣{⎕←⍺⋄100<⊃⍺} 1 1
2 1 1
3 2 1 1
5 3 2 1 1
8 5 3 2 1 1
13 8 5 3 2 1 1
21 13 8 5 3 2 1 1
34 21 13 8 5 3 2 1 1
55 34 21 13 8 5 3 2 1 1
89 55 34 21 13 8 5 3 2 1 1
144 89 55 34 21 13 8 5 3 2 1 1
result: 144 89 55 34 21 13 8 5 3 2 1 1

1↓...  remove the first element, as it is greater than 4 million
+/{⍵/⍨0=2|⍵} sum of the even numbers
rak1507
  • 392
  • 3
  • 6
  • Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. – Pk1 Nov 08 '21 at 08:47
  • yes, that is what my code does. – rak1507 Nov 08 '21 at 08:47
  • is it like using manual loop time complexity would be more? – Pk1 Nov 08 '21 at 08:49
  • No, the complexity should be the same, but manual loops will generally be slower and less idiomatic. – rak1507 Nov 08 '21 at 08:52
  • What's the use ⍺ left argument here?can we do it without using left argument? – Pk1 Nov 08 '21 at 09:07
  • I'm using https://help.dyalog.com/18.0/Content/Language/Symbols/DieresisStar.htm to iterate until the last number is greater than 4 million. – rak1507 Nov 08 '21 at 09:21
  • +/{⍵/⍨0=2|⍵} {⍵,⍨+/2↑⍵}⍣{4000000<⊃⍺}1 1 could you please explain it step by step? i am getting confuse using ⍺ (alpha)... – Pk1 Nov 08 '21 at 09:26
  • @Pk1 I've added an explanation to the answer – rak1507 Nov 08 '21 at 09:35
  • as here we are giving 1 and 1 the value of ⍺ and ⍵ , is there is any way that obtain the solution by considering the value of Omega(⍵) only?? like generalize solution? – Pk1 Nov 08 '21 at 11:30
  • you can turn the solution into a function like Euler2←{+/{⍵/⍨0=2|⍵}1↓{⍵,⍨+/2↑⍵}⍣(⍵{⍺⍺<⊃⍺})1 1}, Euler2 4000000 – rak1507 Nov 08 '21 at 12:06
  • Could you please explain ⍣(star dairies symbol) what exactly its doing here? i am not getting the exact use of it. Thanks for your time. – Pk1 Nov 08 '21 at 12:56
1

The :For construct takes a list of values after the :In keyword. You're only giving it the limit. Maybe you meant ⍳limit to give it all values from 1 until four million?

Adám
  • 6,573
  • 20
  • 37
  • yes, i am trying it to execute from 0 to 4 million times and generate the Fibonacci series. Then i am checking if its an even no then add it. – Pk1 Nov 08 '21 at 08:38