bitwise – Factoring the powers of 2 in python

The powers of 2 are trivial to factor because all powers of 2 have one 1-bit and the rest 0-bits. Just iterate minus one 0-bit as follows. 10000,1000,100,10,1. And then convert the binary strings into base-10 and you got the factors for powers of 2.

Example

import operator
from operator import mul
from functools import reduce
import time

print('Find the factors of a power of 2')
print('This input number must be a power of 2')
X = 32

binary = "{0:b}".format(int(X))
l = len(binary)

# Checking if input is power of 2
# All binary strings that are a power of 2 must
# have only ONE 1 bit

if binary.count(str(1)) == 1:
  print('yes, this is a power of two')
else:
  print('no, this is not a power of 2')
  quit()  

start = (round(time.time() * 1000000))
count = 0
result = ();
for j in range(0, l):
        count = count + 1
        # create list of factors called result
        result.append(int(binary(0:l-count), 2))
        if len(result) == l-1:
          # I don't want to forget X itself is a factor
          result.append(X)
          end = (round(time.time() * 1000000))
          break

print(result)          
print('Factoring took', end-start, 'microseconds')

Output

Find the factors of a power of 2
This input number must be a power of 2
yes, this is a power of two
(16, 8, 4, 2, 1, 32)
Factoring took 0 microseconds

Question

What better names could I use to define my variables, and what could make the program run faster?

Is there a more efficient implementation to decide if a number is a power of 2?

factoring – How to find a number with factors X and prime factors K?

Thanks for contributing with a response to Mathematics Stack Exchange!

  • Please make sure answer the question. Please provide details and share your research!

But avoid

  • Ask for help, clarification or respond to other answers.
  • Make statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.

For more information, check out our tips on how to write great answers.

Factoring long trigonometric expressions that separate specific variables

I have a very long expression with trig functions:

n (2 a - h - 2 z) (-Sin(p (β - δ + t ω)) + Sin(p (β + δ + t ω)))

I would like to simplify the term (-Sin(p (β - δ + t ω)) + Sin(p (β + δ + t ω)
to 2 Sinδ Cos (ωt + β).

Any suggestion? Simply using Simplify does not work in this case.
Thanks for your help.

python – Factoring and solving a quadratic polynomial

So I code things for me Fx-cg50 calculator micropython As I am a level student and a python beginner, and I need help to point out mistakes and some programming tips to further improve my code, and I cannot use sympy or numpy since they do not fit in the measly 16 MB of my calculator so I have to do everything from scratch: Disclaimer, my code cannot factor expressions when completing the square, so if you have any idea on how I could start, tell me, I was planning to use my program to start to get a factorized quadratic expression anyway, and move on from there.

and someone can tell me at what level my code is, relatively for the sake of reference.

from math import sqrt, pi, isclose, gcd

print("ax+by+c=0")


def gcd_2(a, b):
    while b:
        a, b = b, a % b
    return a


def fraction(a):
    factor = 0
    while True:
        factor += 1
        a_rounded = int(round(a*factor))
        if isclose(a*factor, a_rounded, abs_tol=0.01):
            text = ("{}/{}".format(a_rounded, factor))
            return text


def simplify_fraction(numer, denom):
    if denom == 0:
        return "Division by 0 - result undefined"

    # Remove greatest common divisor:
    common_divisor = gcd_2(numer, denom)
    (reduced_num, reduced_den) = (numer / common_divisor, denom / common_divisor)
    # Note that reduced_den > 0 as documented in the gcd function.

    if common_divisor == 1:
        return (numer, denom)
    else:
        # Bunch of nonsense to make sure denominator is negative if possible
        if (reduced_den > denom):
            if (reduced_den * reduced_num < 0):
                return (-reduced_num, -reduced_den)
            else:
                return (reduced_num, reduced_den)
        else:
            return (reduced_num, reduced_den)


def quadratic_function(a, b, c):
    a_original, b_original, c_original = a, b, c
    if b ** 2 - 4 * a * c >= 0:
        negative_factor = False
        if a < 0:
            negative_factor = True
        #  checks if gcf can be applied to simplify the quadratic expression
        b_divisible_by_a = (b % a == 0)
        c_divisible_by_a = (c % a == 0)
        check_divisible = (b_divisible_by_a and c_divisible_by_a)
        a_is_one = (a == (a/a))
        if negative_factor:
            gcf = -int(gcd(int(-a), gcd(int(-b), int(-c))))
            b, c, a = -b, -c, -a
        if a_is_one or (check_divisible is False):
            gcf = ""
        elif check_divisible and (a_is_one is False):
            if negative_factor is False:
                gcf = int(gcd(int(-a), gcd(int(-b), int(-c))))
            b, c, a = b/a, c/a, a/a
        x1 = (-b + sqrt(b ** 2 - 4 * a * c)) / (2 * a)
        x2 = (-b - sqrt(b ** 2 - 4 * a * c)) / (2 * a)
        # Added a "-" to these next 2 values because they would be moved to the other side of the equation
        mult1 = -x1 * a
        mult2 = -x2 * a
        (num1, den1) = simplify_fraction(a, mult1)
        (num2, den2) = simplify_fraction(a, mult2)
        if (num1 > a) or (num2 > a):
            # simplify fraction will make too large of num and denom to try to make a sqrt work
            print("No factorization")
            c =(b/2)**2

        else:
            # Getting ready to make the print look nice
            if (den1 > 0):
                sign1 = "+"
            else:
                sign1 = ""
            if (den2 > 0):
                sign2 = "+"
            else:
                sign2 = ""
            print("The Factored Form is:n{}({}x{}{})({}x{}{})".format(gcf, int(num1), sign1, int(den1), int(num2), sign2,int(den2)))
    else:
        # if the part under the sqrt is negative, you have a solution with i
        print("Solutions are imaginary")
    return


while True:
    try:
        stop_flag = 0
        stop_or_continue = ""
        a = float(eval(input("insert a: ").replace("pi", str(pi))))
        b = float(eval(input("insert b: ").replace("pi", str(pi))))
        c = float(eval(input("insert c: ").replace("pi", str(pi))))

        quadratic_function(a, b, c)

        discriminant = b ** 2 - 4 * a * c


        if discriminant == 0:
            x_one = (-b + sqrt(b ** 2 - 4 * a * c)) / (2 * a)   # x_one
            if eval(fraction(x_one)) % 1 == 0:
                print("x1 is : ", x_one)
            else:
                print("x1 is : ", fraction(x_one))
        if discriminant > 0:
            x_one = (-b + sqrt(b ** 2 - 4 * a * c)) / (2 * a)  # x_one
            if eval(fraction(x_one)) % 1 == 0:
                print("x1 is : ", x_one)
            else:
                print("x1 is : ", fraction(x_one))
            x_two = (-b - sqrt(b ** 2 - 4 * a * c)) / (2 * a)  # x_two
            if eval(fraction(x_two)) % 1 == 0:
                print("x2 is : ", x_two)
            else:
                print("x2 is : ", fraction(x_two))
        if discriminant < 0:
            pass

        while stop_or_continue != "a" or "b":
            stop_or_continue = input("Stop?: ")
            if stop_or_continue == "a":
                stop_flag = 1
                break
            if stop_or_continue == "b":
                stop_flag = 0
                break
        if stop_flag == 1:
            break

    except ValueError:
        pass

Factoring: Why is the result of the MMA polynomial modulation factor different from this article?

Why is the MMA polynomial modification factor not the same as the result of this work?

In(1):= f(x_) := x^6 - 8 x^4 - 5 x^3 + 4 x^2 + 5 x - 12
Factor(PolynomialMod(f(x), 3))
Factor(f(x), Modulus -> 3)
Out(1)= x (x+1) (x^4-x^3+2 x^2-x+2)
        x (x+1) (x+2) (x^3+2 x+1)

The results in this article are:

Consider the polynomial
$$
f (x) = x ^ 6-8x ^ 4-5x ^ 3 + 4x ^ 2 + 5x-12.
$$

Module $ p = $ 3 It is factored as $ f (x) equiv (x ^ 3-x) (x ^ 3-x + 1) = x (x-1) (x + 1) (x ^ 3-x + 1) $ So the Galois group has a cycle 3.

Factoring after polynomial long division

I have obtained the code to make a long polynomial division, but I want my students to also consider the solution if possible.enter the description of the image here

Factoring $ x ^ 2 + 4 $ [Elementary] – Math stack exchange

Thank you for contributing a response to Mathematics Stack Exchange!

  • Please make sure answer the question. Provide details and share your research!

But avoid

  • Ask for help, clarifications or respond to other answers.
  • Make statements based on opinion; Support them with references or personal experience.

Use MathJax to format equations. MathJax reference.

For more information, see our tips on how to write excellent answers.

Factoring a polynomial – Mathematica Stack Exchange

How to factor a polynomial by treating symbols that are not x $ (a, b) $ as constants?

Factor(a/b (a/b - 2) x^2 - 2 x (1 - a/b) a^2/b + a^4/b^2)

It doesn't work without more conditions. However it works for whole $ (a, b). $

This seems to work, right after submitting the question.

ClearAll(a, b);
A = a/b (a/b - 2);
B = -2 (1 - a/b) a^2/b;
C1 = a^4/b^2;
Factor(A x^2 + B x + C1)

Factoring: relatively raw linear equations

There are two linear equations ax + b and cx + d in which a, b, c and d are constants and integers. No matter what integer value of x, the values ​​of ax + b and cx + d are relatively prime (reformatted, mcd (ax + b, cx + d) = 1). What does this say about the value (s) of | ad-bc |

Matrices – Factoring Correlation Matrix

I have the following set of random variables:

$ X_i = Y * beta_i + Z_i * (1- beta_i) $ for $ i = 1, …, n $

$ Y, Z_i $ they are independent normal variables with distribution from $ Normal (0.1) $.

Now notice that:

$ E (X_i, X_j) = beta_i * beta_j $ for $ i neq j $

Y

$ E (X_i, X_j) = 1 $ for $ i = j $

My question has a correlation matrix $ M $ Is it possible to calculate the $ beta_i $& # 39; s. And if possible how to do it?

Even if the set of $ { beta_i } $ It is not unique, is it possible to find a solution $ { beta_i } $ that produces the correlation matrix $ M $.