Is it a factor of a polynomial?

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 , 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.

intboolstring

Posted 2015-12-01T00:20:42.420

Reputation: 463

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 the 1. Which is it? – feersum – 2015-12-01T02:08:54.107

The input will be x^.... The coefficient of 1 is implicit if it is just x – intboolstring – 2015-12-01T02:12:26.040

Then you should change where it says An example polynomial could be 2x^2 + 1x^1. – feersum – 2015-12-01T02:14:39.863

Nice 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 implicit 1 should use x^a or a specific value of a rather than just x. It would also be worth stating explicitly that terms with 0 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.590

I 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

Answers

5

Pyth - 39 bytes

This is a monstrous combination of regexp and eval. I like the approach, but will try to improve the implementation.

It uses the Polynomial Remainder Theorem.

K_sPe:z"-|\+"3!v.ssXPtw,\^\x,"**""*K"\*

Doesn't work online because of eval use.

Maltysen

Posted 2015-12-01T00:20:42.420

Reputation: 25 023

3

Casio Basic, 19 bytes

judge(mod(b,a)=0

As it turns out, the fx-CP400 can do mod on algebraic expressions!

Polynomial and factor should be entered as expressions. 16 bytes for the code, 3 bytes to enter a,b into the parameter value box.

numbermaniac

Posted 2015-12-01T00:20:42.420

Reputation: 639

1

Axiom 77 180 Bytes

f(a:UP(x,INT),b:UP(x,INT)):Boolean==(ground?(a)or ground?(b)=>false;p:=b;r:=a;if degree(a::POLY INT,x)>degree(b::POLY INT,x)then(p:=a;r:=b);(p rem r)$UP(x,FRAC INT)~=0=>false;true)

the previous solution

v(a,b)==(ground?(a) or ground?(b) or (b rem a)$UP(x,FRAC INT)~=0=>false;true)

was wrong because it assume degree(b)>=degree(a) one bug i wrote... test and results

(3) -> f(x^1-1,x^1-1)
   (3)  true
                                                            Type: Boolean
(4) -> f(x^1+1,2*x^2+4*x^1+2)
   (4)  true
                                                            Type: Boolean
(5) -> f(x^1+2,2*x^2+4*x^1+2)
   (5)  false
                                                            Type: Boolean
(6) -> f(x^1+7,x^2-49)
   (6)  true
                                                            Type: Boolean
(7) -> f(1, 1)
   (7)  false
                                                            Type: Boolean
(8) -> f(1, x^2+1)
   (8)  false
                                                            Type: Boolean
(9) -> f(x^8-1, x^2-1)
   (9)  true

RosLuP

Posted 2015-12-01T00:20:42.420

Reputation: 3 036

1

MATLAB, 103 99 97 95 93 bytes

I'm trying some different things, and got this to work to save a couple of bytes:

eval([regexprep(input(''),{'.+?1(.+)\),','(\d)x'},{'x=str2num(''$1'');disp(~','$1\*x'}) 41]);

If I can reduce that down even further, I will post an explanation.


Old code an explanation

t=sscanf(input(''),'(x^1%d),%s')';x=-t(1);disp(~eval(regexprep([t(2:end) ''],'(\d)x','$1\*x')))

This also works with Octave. You can try it online. I've save the program as a script named isFactor.m, so you can just enter isFactor at the prompt. [Note: in Octave spits out a warning while running - MATLAB doesn't generate this].

The input must be in the format '(x^1+7),(x^2-49)' as per the question. The quotes marks are added so MATLAB/Octave knows it is a string.

The output is either a 0 or a 1 depending on whether or not it is true or false.


So, the code works as follows. First we request an input, and then parse it. The parsing string extracts the signed number after the first (x^1 in the string - this is our value of n. Then it continues to extract the string (%s) after the ), in the input - this is our expression.

t=sscanf(input(''),'(x^1%d),%s')';

Next, we extract the value of n and set x equal to it - we are going to evaluate whether the expression equals zero when n==x, so this is why we store the value to x. Also we negate the extracted number, because of the minus sign when parsing.

x=-t(1);

We will then display the output which is a boolean

disp(

The output is basically the logical negation of our evaluated equation. If f(x) is zero, this will return 1, otherwise it will result in zero.

     ~eval(

We are evaluating the input expression, but to do this, we need to reformat it slightly so that MATLAB can understand. When we read the string, it is actually an array of double type, so we need to convert that to a character array. Before the conversion we also get rid of the first element as that is what we used for n. We then need to replace any occurrence of x which is preceded by a number (e.g 4x) by the same thing but with a multiplication (*) sign in between so MATLAB can calculate it.

           regexprep(char([t(2:end) ''],'(\d)x','$1\*x')
     )
)

Tom Carpenter

Posted 2015-12-01T00:20:42.420

Reputation: 3 990

1

VBScript, 118 116 bytes

a=inputbox(""):for i=0 to 9:a=replace(a,i&"x",i&"*x"):next:b=split(a,","):x=-eval(b(0)):msgbox not cbool(eval(b(1)))

Since we know that the first part of the input is a linear polynomial, we only need to check if its root matches that of the second polynomial; and we need to prepare the term for eval by inserting * as needed.

Hilbert

Posted 2015-12-01T00:20:42.420

Reputation: 41