11
0
A polynomial is divisible by a factor (x-n)
if f(n)=0
for a function f
. Your job: to determine if a polynomial function f(x)
is divisible by (x-n)
.
The input
The input is in the form of (x-n), (Polynomial)
. Remember, if n is negative, (x-n)
will be in the input form of (x+n)
. For the polynomial, all exponents will be put in as ^
. Coefficients will be written next to the variable x
. An example polynomial could be 2x^2 + x^1
. There will be no spaces between anything. The term x
will be inputed as x^1
. So what would "normally" look like (x - 1)
will be (x^1-1)
. The coefficients and powers will always be integers. The coefficient one will be implicit if it is just x
. I.e., x
can be interpreted as 1x
The output
A boolean value. Truthy, or Falsey.
Thanks to @AlexA. For helping me clarify this!
Examples
Input:(x^1-1),(x^1-1)
Output: True
Input: (x^1+2),(2x^2+4x^1+2)
Output: False
Input: (x^1+7),(x^2-49)
Output: True
Rules
- This is code-golf, so shortest code in bytes wins
Unfortunately, I don't know how to implement the snippet leaderboard. If anyone knows how, feel free to edit the post.
Will the input be a string with that exact form, i.e. parens around the divisor candidate, a comma with zero or one space, and parens around the polynomial? – Alex A. – 2015-12-01T00:39:24.713
2Possible duplicate – intrepidcoder – 2015-12-01T00:39:31.267
Definitely not a duplicate of that. – intboolstring – 2015-12-01T00:40:28.217
@intrepidcoder This isn't a duplicate because the question isn't to factor a polynomial. It is to see if a polynomial can be divided by a linear factor. – intboolstring – 2015-12-01T00:48:41.940
Will polynomial coefficients always be integers? – Digital Trauma – 2015-12-01T01:15:22.947
That is correct. – intboolstring – 2015-12-01T01:22:38.700
In the "Input" section you say that terms with a coefficient of 1 will be written as
1x^
..., but in the examples you leave out the1
. Which is it? – feersum – 2015-12-01T02:08:54.107The input will be
x^...
. The coefficient of 1 is implicit if it is justx
– intboolstring – 2015-12-01T02:12:26.040Then you should change where it says
An example polynomial could be 2x^2 + 1x^1.
– feersum – 2015-12-01T02:14:39.863Nice catch! Fixed it! – intboolstring – 2015-12-01T02:29:30.137
There are still some inconsistencies in the described notation and the actually used notation: the input is really in the form
(x^1-n),(Polynomial)
; it's not true that "all exponents" are given explicitly, because the constant term is e.g.-1
rather than-1x^0
(or-x^0
); the sentence about the implicit1
should usex^a
or a specific value ofa
rather than justx
. It would also be worth stating explicitly that terms with0
coefficient are (or only may be?) omitted, and saying whether the terms are always ordered by exponent, descending. – Peter Taylor – 2015-12-01T07:05:11.590I suggest using lists/array of integers as inputs insted of strings one has to parse. – flawr – 2015-12-01T11:10:14.353
@flawr keep in mind that if the input was an array of integers, this challenge would be 100x easier. – intboolstring – 2015-12-01T15:13:16.850
@intboolstring Yes it would be, and I suggested this because I thought you wanted to do a challenge that was about checking divisibility and not mainly about parsing, as many other code-golf challenges... – flawr – 2015-12-01T15:46:01.930
@flawr. Ok, makes sense. – intboolstring – 2015-12-01T15:53:20.293