← Back to challenges

Perfect Square Binomial

JavaScriptHardalgebramath

Instructions

A perfect square binomial is a trinomial that when factored gives you the square of a binomial. For example, the trinomial x²+2x+1 is a perfect square binomial because it factors to (x+1)².

Three integers (a, b, and c) are randomly (and independently) chosen between 1 and n (inclusive) where n is an integer greater than one. Create a function that takes a number n as an argument and returns the number of triplets (a, b, c) such that ax²+bx+c is a perfect square binomial.

Examples

perfectSquare(6) ➞ 3

perfectSquare(30) ➞ 21

perfectSquare(100) ➞ 81

Notes

Trinomials like 2x²+4x+2=2(x+1)² are not considered to be perfect square binomials but trinomials like 4x²+8x+4=(2x+2)² are (in this challenge).

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.