# Indices of square numbers that are also pentagonal [closed]

First 15 numbers of the A046173:

1, 99, 9701, 950599, 93149001, 9127651499, 894416697901, 87643708742799, 8588189040096401, 841554882220704499, 82463790268588944501, 8080609891439495856599, 791817305570802005002201, 77590015336047156994359099, 7603029685627050583442189501


Your task is simple, output the first 20 numbers of this sequence on separate lines.

A simple explanation to this sequence, (you can also refer to the link above), is
As n increases, this sequence is approximately geometric with common ratio $$\ r = \lim_{n\to\infty} \frac{a(n)}{a(n-1)} = (\sqrt2 + \sqrt3)^4 = 49 + 20 \times \sqrt6 \$$

This is code-golf, so shortest code wins!

• Why should the output be displayed on separate lines? Why not just rely on our standard I/O formats? Or is this supposed to be a kolmogorov-complexity challenge? (If so, I guess the largest results should not be approximated, but that should be clearly specified.) Jan 11 at 8:28
• Since, according to the OEIS, this sequence is given by $a(n) = 98 \cdot a(n-1) - a(n-2)$, this is a chameleon challenge for a Fibonacci variant. of which we've had lots of similar challenges.
– xnor
Jan 11 at 8:51
• The formula you given is property of given sequence but not the definition of it. For example: You can say, "except first item, all items are odd" for "prime numbers" sequence. But you cannot list prime numbers only based on it.
– tsh
Jan 11 at 9:58
• If you are only requiring output first 20 items. I would suggest you simply include all 20 items in your post so everyone may verify their correctness by easily comparing with given results.
– tsh
Jan 11 at 10:05
• I suggest you use Sandbox before posting, so all issues are resolved there, not on the main site. Jan 11 at 11:42

# Vyxalj, 22 bytes

λ²(3ṙ-x)/2∆qt:⌊=;20ȯ


Try it Online!

Finally a good use for symbolic algebra in a golfing language. Good luck porting this to Jelly lol.

## Explained

λ²...∆qt:⌊=;20ȯ
λ                 # Create a lambda, that takes a single argument n and:
²                #   squares n
...            #   pushes the string "(3x^2-x)/2" (this is the formula for pentagonal numbers - I found it on Wikipedia)
∆q          #   and solves that for x (as in, it uses Sympy to solve it as if it were an equation) - this will give a list of up to 2 solutions - a negative and positive solution in that order
t         #   Push that positive solution because that's what we're interested in checking
:⌊=      #   Does the floor of that solution equal that solution? This checks to see if it's an integer solution, as only integer solutions are plugged into the original formula anyway
;     # Close the lambda
20ȯ  # and push the first 20 numbers where that lambda is truthy - this times out online, but it works given infinite time and resources
# The j flag joins that list on newlines - I think it's okay here because the main focus of the challenge is generating the numbers

• lyxal would you be kind enough to explain the code? Jan 11 at 8:25
• @dialfrost that's what I'm working on now :) Jan 11 at 8:25
• ah ic sry lol i like how ur name is similar to the language :) Jan 11 at 8:25
• @dialfrost it's what I call an inability to name things creatively :p Jan 11 at 8:26

# 05AB1E, 15 bytes

With strict I/O as defined in the challenge description: the first 20 items newline delimited:

21®1‚λ£98*s-}¦»


Try it online.

With default I/O rules: outputs the 1-based $$\n^{th}\$$ term (10 bytes):

®1‚λè98*s-


Try it online.

Explanation:

Uses the formula: $$\a(n)=98\times a(n-1)-a(n-2)\$$ with offset $$\-1,1\$$, the first formula defined on the oeis-page.

     λ       # Start a recursive environment,
21    £      # to get the first 21 terms
®1‚        # Starting with a(0)=-1 and a(1)=1
# With every following a(n) defined as:
98*   #  Multiply the implicit a(n-1) by 98
s- #  Subtract the implicit a(n-2) from it
}¦      # After the recursive environment: remove the first -1 term
»     # Join the 20 terms by newlines
# (after which it is output implicitly as result)

λ         # Start a recursive environment,
è        # to output the (implicit) input'th term
®1‚          # Starting with a(0)=-1 and a(1)=1
# With every following a(n) defined as:
98*s-   #  Same as above: 98*a(n-1)-a(n-2)
# (after which the result is output implicitly)


# Pari/GP, 44 bytes

for(i=0,19,print(([-1,1]*[0,-1;1,98]^i)[2]))


Try it online!

Using the formula $$\a(n)=98\ a(n-1)-a(n-2)\$$.

# JavaScript (Node.js), 50 bytes

A full program.

for(v=q=1n;~v%39n;v=q-98n*(q=-v))console.log(v+'')


Try it online!