Divinacci Sequence

23

3

Divinacci (OEIS)

Perform the Fibonacci sequence but instead of using:

f(n) = f(n-1)+f(n-2)

Use:

f(n) = sum(divisors(f(n-1))) + sum(divisors(f(n-2)))

For an input of n, output the nth term, your program should only have 1 input.


First 14 terms (0-indexed, you may 1-index; state which you used):

0  | 0     # Initial               | []
1  | 1     # Initial               | [1] => 1
2  | 1     # [] + [1]              | [1] => 1
3  | 2     # [1] + [1]             | [1,2] => 3
4  | 4     # [1] + [1,2]           | [1,2,4] => 7
5  | 10    # [1,2] + [1,2,4]       | [1,2,5,10] => 18
6  | 25    # [1,2,4] + [1,2,5,10]  | [1,5,25] => 31
7  | 49    # [1,2,5,10] + [1,5,25] | [1,7,49] => 57
8  | 88    # [1,5,25] + [1,7,49]   | [1, 2, 4, 8, 11, 22, 44, 88] => 180
9  | 237   # [1,7,49] + [180]      | [1, 3, 79, 237] => 320
10 | 500   # [180] + [320]         | [1, 2, 4, 5, 10, 20, 25, 50, 100, 125, 250, 500] => 1092
11 | 1412  # [320] + [1092]        | [1, 2, 4, 353, 706, 1412] => 2478
12 | 3570  # [1092] + [2478]       | [1, 2, 3, 5, 6, 7, 10, 14, 15, 17, 21, 30, 34, 35, 42, 51, 70, 85, 102, 105, 119, 170, 210, 238, 255, 357, 510, 595, 714, 1190, 1785, 3570] => 10368
13 | 12846 # [2478] + [10368]      | [1, 2, 3, 6, 2141, 4282, 6423, 12846] => 25704
Etc...

You may choose whether or not to include the leading 0. For those who do: the divisors of 0 are [] for the purpose of this challenge.

It's lowest byte-count wins...

Magic Octopus Urn

Posted 2017-06-23T13:51:34.740

Reputation: 19 422

15All natural numbers divide 0, thus its divisor sum is +∞. – Dennis – 2017-06-23T14:42:37.563

9@Dennis finally someone who doesn't think that 1 + 2 + 3 + ... = -1/12. – Leaky Nun – 2017-06-23T14:54:44.560

1@Dennis We can get rid of the 0 and make this valid though :P. Or you can just submit a Mathematica answer of Infinity if you want. – Magic Octopus Urn – 2017-06-23T15:05:12.797

The Jelly answer would be shorter. :P You can either change the sequence (the answer probably would need tweaking as well) or change its description (start with base values 0, 1, 1). – Dennis – 2017-06-23T15:19:33.833

@Dennis The Jelly answer could've been just ÷...with input getting fed from STDIN. – Erik the Outgolfer – 2017-06-23T15:57:12.890

Why should we assume that the divisors of 0 are []? Just make 2 -> 1 an Initial as Dennis suggested...problem solved. ;) – Erik the Outgolfer – 2017-06-23T16:00:23.707

@EriktheOutgolfer 7 answers already, you sure it affects none of them? – Magic Octopus Urn – 2017-06-23T16:01:20.003

1@carusocomputing If it doesn't change the sequence, how can it affect answers? – Martin Ender – 2017-06-23T17:36:32.323

Alternatively, relaxing the rules is always possible (i.e. making it optional whether the zero is included in the sequence or not). – Martin Ender – 2017-06-23T17:36:59.997

@MartinEnder is this worded correctly? I'm just genuinely confused on what you guys want me to edit specifically, if you could suggest an edit that'd be awesome. – Magic Octopus Urn – 2017-06-23T19:22:33.870

That edit works. – Martin Ender – 2017-06-23T19:31:27.957

@LeakyNun excuse me, you saying it isn't? – tox123 – 2018-04-01T02:17:04.977

@tox123 happy april fools – Leaky Nun – 2018-04-02T20:23:50.413

Answers

10

05AB1E, 9 bytes

XÎFDŠ‚ÑOO

Try it online!

Explanation

XÎ          # initialize stack with 1,0,input
  F         # input times do
   D        # duplicate
    Š       # move down 2 places on the stack
     ‚      # pair the top 2 elements on the stack
      Ñ     # compute divisors of each
       OO   # sum twice

Emigna

Posted 2017-06-23T13:51:34.740

Reputation: 50 798

Tons of swapping going on heh! Interesting. – Magic Octopus Urn – 2017-06-23T14:07:18.137

2I like how the last couple bytes are forcefully yelling at the reader. – Rohan Jhunjhunwala – 2017-06-24T18:07:15.563

1You won this by 2 minutes lol. – Magic Octopus Urn – 2017-08-17T21:47:52.747

8

Mathematica, 45 40 bytes

If[#<3,1,Tr@Divisors@#0[#-i]~Sum~{i,2}]&

Mathematica's divisor related functions Divisors, DivisorSum and DivisorSigma are all undefined for n = 0 (rightly so), so we start from f(1) = f(2) = 1 and don't support input 0.

Defining it as an operator instead of using an unnamed function seems to be two bytes longer:

±1=±2=1
±n_:=Sum[Tr@Divisors@±(n-i),{i,2}]

Martin Ender

Posted 2017-06-23T13:51:34.740

Reputation: 184 808

*7 bytes longer unless ± is 1 byte in a Mathematica supported encoding. – CalculatorFeline – 2017-06-23T16:31:32.390

@CalculatorFeline It is. (The default setting for $CharacterEncoding on Windows machines is WindowsANSI, i.e. CP 1252.) – Martin Ender – 2017-06-23T17:29:31.763

1Good to know. . – CalculatorFeline – 2017-06-23T18:48:25.617

5

Perl 6, 58 bytes

{my&d={sum grep $_%%*,1..$_};(0,1,{d($^a)+d $^b}...*)[$_]}

Try it online!

Sean

Posted 2017-06-23T13:51:34.740

Reputation: 4 136

4

Haskell, 55 bytes

f n=sum[a|n>1,k<-f<$>[n-1,n-2],a<-[1..k],mod k a<1]+0^n

Try it online!

One-indexed.

xnor

Posted 2017-06-23T13:51:34.740

Reputation: 115 687

3

Jelly, 10 9 bytes

ð,ÆDẎSð¡1

Try it online!

Thanks to Dennis for -1.

Erik the Outgolfer

Posted 2017-06-23T13:51:34.740

Reputation: 38 134

9 bytes – Dennis – 2017-06-23T15:49:33.360

@Dennis The 0 was implicit? – Erik the Outgolfer – 2017-06-23T15:52:20.883

When you take the number of iterations from STDIN, you get a niladic chain, and 0 is the implicit argument of niladic chains. – Dennis – 2017-06-23T15:53:34.863

@Dennis So ¡ and others will just try to take an argument from everywhere, even with a Ɠ? That's quite unexpected... – Erik the Outgolfer – 2017-06-23T15:54:57.583

Unless specified explicitly, ¡ et al. take the last command-line argument and, if there aren't any, reads a line from STDIN. – Dennis – 2017-06-23T15:56:27.300

3

Python 3, 88 83 81 bytes

f=lambda n:+(n<3)or g(f(n-1))+g(f(n-2))
g=lambda n,i=1:n>=i and(n%i<1)*i+g(n,i+1)

Try it online!

Excludes the 0

ovs

Posted 2017-06-23T13:51:34.740

Reputation: 21 408

3

Python 2, 76 bytes

f=lambda n:sum(a for k in[1,2][:n]for a in range(1,3**n-8)if f(n-k)%a<1)or n

Try it online!

Ridiculously slow.

xnor

Posted 2017-06-23T13:51:34.740

Reputation: 115 687

3

MATL, 16 15 bytes

Oliq:",yZ\s]+]&

This solution uses 0-based indexing.

Try it at MATL Online

Explanation

O        % Push the number literal 0 to the stack
l        % Push the number literal 1 to the stack
i        % Explicitly grab the input (n)
q        % Subtract 1
:        % Create the array [1...(n - 1)]
"        % For each element in this array...
  ,      % Do the following twice
    y    % Copy the stack element that is 1-deep
    Z\   % Compute the divisors
    s    % Sum the divisors
  ]      % End of do-twice loop
  +      % Add these two numbers together
]        % End of for loop
&        % Display the top stack element

Suever

Posted 2017-06-23T13:51:34.740

Reputation: 10 257

2

Haskell, 64 60 bytes

d n=sum[a|a<-[1..n],mod n a<1]
f=0:scanl((.d).(+).d)1f
(!!)f

Try it online!

vroomfondel

Posted 2017-06-23T13:51:34.740

Reputation: 2 094

2

PHP, 97 bytes

for($f=[0,$y=1];++$i<$argn;$x=$y,$y=$r)for($f[]=$r=$v=$n=$x+$y;--$v;)$n%$v?:$r+=$v;echo$f[$argn];

Try it online!

PHP, 101 bytes

for($f=$d=[0,1];$i<$argn;$d[]=$r)for($f[]=$r=$v=$n=$d[$i]+$d[++$i];--$v;)$n%$v?:$r+=$v;echo$f[$argn];

Try it online!

Jörg Hülsermann

Posted 2017-06-23T13:51:34.740

Reputation: 13 026

2

Pari/GP, 39 bytes

f(n)=if(n<3,1,sum(i=1,2,sigma(f(n-i))))

Based on Martin Ender's Mathematica answer.

Try it online!

alephalpha

Posted 2017-06-23T13:51:34.740

Reputation: 23 988

1

R, 81 bytes

f=function(n,a=1,b=1,d=numbers::divisors)`if`(n-1,f(n-1,b,sum(d(a))+sum(d(b))),a)

1-indexed, and excludes the 0 at the start of the sequence. That zero gave me a lot of trouble to implement, because the builtin numbers::divisors doesnt handle it well.

The rest is a modified version of the standard recursive function that implements the fibonacci sequence.

> f(1)
[1] 1
> f(2)
[1] 1
> f(3)
[1] 2
> f(5)
[1] 10
> f(13)
[1] 12846

JAD

Posted 2017-06-23T13:51:34.740

Reputation: 2 898