aboutsummaryrefslogtreecommitdiff
path: root/challenge-104/paulo-custodio/forth/ch-1.fs
blob: b27623f2e4b78eb0c7da55923a563b826e58fe20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#! /usr/bin/env gforth

\ Challenge 104
\
\ TASK #1 � FUSC Sequence
\ Submitted by: Mohammad S Anwar
\ Write a script to generate first 50 members of FUSC Sequence. Please refer to
\ OEIS for more information.
\
\ The sequence defined as below:
\
\ fusc(0) = 0
\ fusc(1) = 1
\ for n > 1:
\ when n is even: fusc(n) = fusc(n / 2),
\ when n is odd: fusc(n) = fusc((n-1)/2) + fusc((n+1)/2)

: fusc  { n -- n }
    n 0=  IF 0 EXIT THEN
    n 1 = IF 1 EXIT THEN
    n 1 AND 0= IF       \ even
        n 2/ RECURSE
    ELSE
        n 1- 2/ RECURSE
        n 1+ 2/ RECURSE +
    THEN
;

: n-fusc    ( n -- )
    0 ?DO
        I FUSC .
    LOOP
    CR
;

NEXT-ARG S>NUMBER? 0= THROW DROP
n-fusc
BYE