The Student Room Group

Python factorisation help please

Hi, could someone tell me how to write a program that finds the primes p and q that multiply to make a number, n. e.g. 80209 × 92683 = 7434010747?
Reply 1
Original post by flumefan1
Hi, could someone tell me how to write a program that finds the primes p and q that multiply to make a number, n. e.g. 80209 × 92683 = 7434010747?


Please don't repeat threads. @ghostwalker gave a good starting point in your other thread. Post what you've done so far. It should be one loop and a couple of lines in the body.
And a quick Google would produce more than one example.
Reply 3
Original post by mqb2766
Please don't repeat threads. @ghostwalker gave a good starting point in your other thread. Post what you've done so far. It should be one loop and a couple of lines in the body.

I have
import math
def prime_multiplier(n):
n = p*q
if p / 3 <= sqrt(n)

Now what?
Reply 4
Original post by flumefan1
I have
import math
def prime_multiplier(n):
n = p*q
if p / 3 <= sqrt(n)

Now what?

No real idea of what you're doing
* Why define a function? You're obviously just beginning to learn?
* You're doing the inverse of what you want. Given n, find p & q not the other way round
* Where is the loop, where is a zero remainder test, ...
As with all programming problems, it's better to start small and build it up. So why not get it working for the two prime factors of 35? Hard code numbers etc where necessary, but try a simple loop and decision. Also, Im not sure if you're clear about what the algorithm is you're trying to implement.
(edited 3 years ago)
Reply 5
Original post by mqb2766
No real idea of what you're doing
* Why define a function? You're obviously just beginning to learn?
* You're doing the inverse of what you want. Given n, find p & q not the other way round
* Where is the loop, where is a zero remainder test, ...
As with all programming problems, it's better to start small and build it up. So why not get it working for the two prime factors of 35? Hard code numbers etc where necessary, but try a simple loop and decision. Also, Im not sure if you're clear about what the algorithm is you're trying to implement.

I don’t get your first two questions there?
I don’t know what a zero remainder test is?
No I’m not sure, we have to investigate the time taken to factorise a number that’s the product of 2 primes but I don’t know how to go about this.
(edited 3 years ago)
Original post by flumefan1
I don’t get your first two questions there?
I don’t know what a zero remainder test is?
No I’m not sure, we have to investigate the time taken to factorise a number that’s the product of 2 primes but I don’t know how to go about this.


A zero remainder test tests to see if the remainder is zero.

15%3=0

15%2=1

As you're just starting out with programming why not try a few simple exercises?

The following would do for a start.

Can you write a program that allows you to input a number and print the square root of the number?
Reply 7
Original post by Plücker
A zero remainder test tests to see if the remainder is zero.

15%3=0

15%2=1

As you're just starting out with programming why not try a few simple exercises?

The following would do for a start.

Can you write a program that allows you to input a number and print the square root of the number?

yes I can I’m not that bad at python
Original post by flumefan1
yes I can I’m not that bad at python

What are you having trouble with then?
Reply 9
Turns out what I did for the first question is completely wrong we're meant to use Bezout's identity?
ed+by=gcd
But ed=1+A(p-1)(q-1) where A is a constant and I've never used this formula before, nor do I know how to find A.
Could someone help please?
Original post by flumefan1
Turns out what I did for the first question is completely wrong we're meant to use Bezout's identity?
ed+by=gcd
But ed=1+A(p-1)(q-1) where A is a constant and I've never used this formula before, nor do I know how to find A.
Could someone help please?

Must admit, as this is (assessed) Uni coursework I feel like I've tried to give appropriate help over the past few days and seen little evidence of your work. There are examples in the previous links about getting d/e etc from p/q/n. You could have learnt enough python from scratch in that time and programmed the simple examples in those links. I was confused about the algorithm you were trying to incorporate in that python snippet and I still am. Plugging the rsa formulae into well known maths software gives all the answers for your coursework that I've tried with no programming.

You shouldn't be quoting the actual numbers from your coursework. Similarly, the sticky at the top of the forum says to post your current work and expect hints.
(edited 3 years ago)
If you want to create a function where two numbers share the same GCD then you could define a function def prime_numbers( m,n):
# For both numbers to share the same factor use integer division (n) div (m) implies that any natural number divided by another natural number greater than 0, means m > 0.
Therefore define your function, def whatever_function(m,n): , use the if statement (m == 0): # first condition
return n
else: return whatever_function(m, # Second condition modular arithmetic)
Then assign values to variables let's say x,y , x = int(input("Enter num1: ") and y = int(input("Enter num2: ") and call the function with print(whatever_function(x,y)). So when you run it, the program will ask you to input two numbers and return the Greatest Common Divisor.
Alternatively you can use lambda functions and compare the run time.
Reply 12
Original post by ThiagoBrigido
If you want to create a function where two numbers share the same GCD then you could define a function def prime_numbers( m,n):
# For both numbers to share the same factor use integer division (n) div (m) implies that any natural number divided by another natural number greater than 0, means m > 0.
Therefore define your function, def whatever_function(m,n): , use the if statement (m == 0): # first condition
return n
else: return whatever_function(m, # Second condition modular arithmetic)
Then assign values to variables let's say x,y , x = int(input("Enter num1: ") and y = int(input("Enter num2: ") and call the function with print(whatever_function(x,y)). So when you run it, the program will ask you to input two numbers and return the Greatest Common Divisor.
Alternatively you can use lambda functions and compare the run time.

Thank you, I'll have a look :smile:
what are lambda functions?

Quick Reply

Latest