# How to factor an integer apparently including irrational numbers

I want to do prime factorization of following $a$

a=sqrt(2)+6-sqrt(2)
a.factor()


But sage says simply

6


What should I do?

(I originally wanted to factor $\frac{(3+\sqrt{8})^i +(3-\sqrt{8})^i}{2}$ for i in [1..40])

edit retag close merge delete

Homework ?

( 2023-10-07 10:34:37 +0200 )edit

Sort by ยป oldest newest most voted

Check type(a):

a=sqrt(2)+6-sqrt(2)
print( type(a) )


which gives:

<class 'sage.symbolic.expression.expression'="">

and so a is not an integer but a symbolic expression. If you want to factor a as an integer, convert it to an integer type first:

print( ZZ(a).factor() )


which gives 2 * 3 as expected.

more

Thank you. Then I want to move on original question:

a=((3+sqrt(8))^3+(3-sqrt(8))^3)/2
ZZ(a).factor()


But then sage put out error.

( 2023-10-08 05:43:06 +0200 )edit

You need to expand a before converting:

ZZ(a.expand()).factor()

( 2023-10-08 13:43:35 +0200 )edit

Thank you again! The problem solved by following code!

S=[((3+sqrt(8))^i+(3-sqrt(8))^i)/2 for i in [1..40]]
[ZZ(a.expand()).factor() for a in S]

( 2023-10-08 15:46:06 +0200 )edit

What should I do?

Nothing ;-).

Let's start from the binomial formula :

sage: a, b, c, i, j = var("a, b, c, i, j")
sage: BF=(a+b)^i==sum(a^j*b^(i-j)*binomial(i, j), j, 0, i, hold=True) ; BF
(a + b)^i == sum(a^j*b^(i - j)*binomial(i, j), j, 0, i)


($${\left(a + b\right)}^{i} = {\sum_{j=0}^{i} a^{j} b^{i - j} \binom{i}{j}}$$

Let's generalize it by using wildcards :

sage: w0, w1, w2 = (SR.wild(u) for u in  range(3))
sage: BFG = BF.subs({a:w0, b:w1, i:w2}) ; BFG
($1 +$0)^$2 == sum($1^($2 - j)*$0^j*binomial($2, j), j, 0,$2)


$${\left(\1 + \0\right)}^{\2} = {\sum_{j=0}^{\2} \1^{\2 - j} \0^{j} \binom{\2}{j}}$$

and use it on a general form of your original expression :

sage: Ex=(a + b)^i + (a - b)^i ; Ex
(a + b)^i + (a - b)^i


$${\left(a + b\right)}^{i} + {\left(a - b\right)}^{i}$$

sage: ExT=Ex.subs(BFG) ; ExT
sum(a^j*(-b)^(i - j)*binomial(i, j), j, 0, i) + sum(a^j*b^(i - j)*binomial(i, j), j, 0, i)


$${\sum_{j=0}^{i} a^{j} \left(-b\right)^{i - j} \binom{i}{j}} + {\sum_{j=0}^{i} a^{j} b^{i - j} \binom{i}{j}}$$

which can be rewritten ("contracted") as a single sum :

sage: ExTR=ExT.maxima_methods().sumcontract() ; ExTR
sum(a^j*(-b)^(i - j)*binomial(i, j) + a^j*b^(i - j)*binomial(i, j), j, 0, i)


$${\sum_{j=0}^{i} a^{j} \left(-b\right)^{i - j} \binom{i}{j} + a^{j} b^{i - j} \binom{i}{j}}$$

whose summand can be factored as :

sage: ExTR.operands()[0].collect_common_factors()
a^j*((-b)^(i - j) + b^(i - j))*binomial(i, j)


$$a^{j} {\left(\left(-b\right)^{i - j} + b^{i - j}\right)} \binom{i}{j}$$

Now, assuming that $a,b\in\mathbb{R},\,b>0$ :

• if $i-j$ is odd, $\left(\left(-b\right)^{i - j} + b^{i - j}\right)=-\left(b\right)^{i - j} + b^{i - j}=0$

• if $i-j$ is even, $\left(\left(-b\right)^{i - j} + b^{i - j}\right)=2\,b^{i - j}$.

All non-zero terms of the sum are therefore products whose $b$ factor is at an even power ; therefore, if $b$ is a square root, these terms can all be simplified as a real without (external) radical.

That's (more or less) what Sage does when putting its computation results in its "preffered format", which has already been the cause of very many questions...

HTH,

more