Ask Your Question
0

How to factor an integer apparently including irrational numbers

asked 2023-10-07 06:07:10 +0200

Ys1123 gravatar image

updated 2023-10-07 08:47:39 +0200

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 flag offensive close merge delete

Comments

Homework ?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-10-07 10:34:37 +0200 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2023-10-07 15:59:18 +0200

Max Alekseyev gravatar image

updated 2023-10-07 16:43:10 +0200

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.

edit flag offensive delete link more

Comments

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.

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

You need to expand a before converting:

ZZ(a.expand()).factor()
Max Alekseyev gravatar imageMax Alekseyev ( 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]
Ys1123 gravatar imageYs1123 ( 2023-10-08 15:46:06 +0200 )edit
0

answered 2023-10-07 14:52:46 +0200

Emmanuel Charpentier gravatar image

updated 2023-10-07 14:56:31 +0200

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,

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-10-07 06:07:10 +0200

Seen: 206 times

Last updated: Oct 07 '23