Post Reply 
OEIS A167390: Cumbersome formula
08-28-2022, 07:52 PM (This post was last modified: 08-28-2022 08:38 PM by Allen.)
Post: #3
RE: OEIS A167390: Cumbersome formula
It's probably more comprehensible to understand the function NOT through the OEIS description, but by looking a the derivative of the integer sequence..

[0,1,2,4,3,9,5,16,6,25,7,36,8,49,10,64 ]

so the general pseudo code is:
Code:

loop x over input n 
  if x is odd:
       tally += ((x+1)/2)^2
  else:
       if x is the next highest perfect square:
           small counter += 1
           square counter += 1
       small counter += 1 
       tally+=small counter
recall tally


This lends itself well to loops in RPN rather than a closed form solution (not optimized for size- probably missing some obvious byte savings):


(NOTE: Unlike the original OEIS sequence, I assume f(0)==0 so there is no offset to contend with.)

Code:

00 { 63-Byte Prgm }
01 STO 00   // 00 is the input value
02 SIGN     // put 1 on stack
03 STO 02   // 02 is the small counter
04 STO 03   // 03 is the skip ( n^2) counter
05 STO 04   // 04 increment counter from 1 to n
06 0
07 STO 01   // running tally ( starting with f(0)==0 ) 
08▸LBL 00   // <-------------------- main loop
09 RCL 04
10 2
11 MOD
12 X≠0?     // if counter is odd go to LBL 01
13 GTO 01   // \\ x is even 
14 RCL 03
15 1
16 +
17 X↑2
18 RCL 02
19 1
20 +
21 X≠Y?      //  check to see if the counter+1 is the next perfect square
22 GTO 03
23 ISG 02    // advance the small counter by 1 (extra)
24 ENTER
25 ISG 03    // advance the skip counter by 1 (to skip the next square)
26▸LBL 03    // x is NOT the next square.. only 
27 ISG 02    // advance the small counter by 1 (defult)
28 ENTER     // NOP
29 RCL 02
30 STO+ 01   // add the small counter to the tally
31 GTO 04
32▸LBL 01   // x is odd
33 1
34 RCL+ 04
35 2
36 ÷        // add ((x+1)/2)^2 to the tally
37 X↑2
38 STO+ 01
39▸LBL 04   //  end of loop
40 ISG 04   // increment the counter
41 ENTER
42 DSE 00   // check the outer loop counter
43 GTO 00
44 RCL 01   //recall result to X
45 .END.

17bii | 32s | 32sii | 41c | 41cv | 41cx | 42s | 48g | 48g+ | 48gx | 50g | 30b

Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: OEIS A167390: Cumbersome formula - Allen - 08-28-2022 07:52 PM



User(s) browsing this thread: 2 Guest(s)