Python Forum
How do I stop this fast factorization program from printing beyond the 3rd output?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I stop this fast factorization program from printing beyond the 3rd output?
#1
I have a problem with this python37 code during printing and I would like it not to print out beyond the 3rd print statement of the loop. The program is super fast for a factorization program if I can only have a number (print_factors(f)) to print just below the 4th output when needed.

For example 3 prints like this:

The factors of 3 are: 1, 3

If I input 2047 it prints like this:

The factors of 2047 are: 1, 23, 89, 2047

However I would just like it to show up to 89. Here is the code:

You can get some points if you answer this at stackexchange:

https://stackoverflow.com/questions/5922...-3rd-outpu


import math
while True:
    def print_factors(x):
       print("The factors of",x,"are:")
       for i in range(1,x+1):
           if x % i == 0:
               print(i)
               


    p = int(input('Enter a prime number and if the output is 1 and itself its prime: '))
    
   
    k = ((p**2*2))
    l = (((pow(2, k + 1, 2 * k) - 1) % (2 * k)))
    f = (k//2//p)
    print_factors(f)
Reply
#2
You can generate factors and slice the iterator
import itertools as itt

def gen_factors(x):
    for i in range(i, x + 1):
        if n % i == 0:
            yield i

def print_factors(x, maxcnt=None):
    g = gen_factors(x)
    if maxcnt is not None:
        g = itt.islice(g, 0, maxcnt)
    for f in g:
        print(f)
...
if __name__ == '__main__':
    print_factors(f, 3)
Reply
#3
@Gribouillis

I get this error when I use your code straight out of the box.

print_factors(f, 3)
NameError: name 'f' is not defined


Also maybe there is a way to suppress the primes and just print the composites up the 3rd output. I don't know how though. :(
Reply
#4
Pleiades Wrote:NameError: name 'f' is not defined
You need to define f first.
Reply
#5
Gribouillis

I'm having trouble with def f, can you show me please Grib thanks. It's holding up what I would like to see regarding not printing beyond 3.
Reply
#6
In the first code, I suppose it's yours, you wrote f = .... You only need to do the same.
Reply
#7
(Dec-07-2019, 06:47 PM)Gribouillis Wrote: In the first code, I suppose it's yours, you wrote f = .... You only need to do the same.

Thanks a million I figured it out with your help of course :) Dance

import itertools as itt
while True: 
    def gen_factors(x):
        for i in range(1, x + 1):
            if x % i == 0:
                yield i
     
    def print_factors(x, maxcnt=None):
        g = gen_factors(x)
        if maxcnt is not None:
            g = itt.islice(g, 0, maxcnt)
        for p in g:
            print(p)
    p = int(input('Enter a prime number and if the output is 1 and itself its prime: '))
    p = (2**p-1)
    k = ((p**2*2))
    f = (k//2//p)
    if __name__ == '__main__':
        print_factors(p, 3)

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how come i can't stop program Shi_Shi_Shi_Shi 4 606 Nov-16-2023, 06:20 PM
Last Post: deanhystad
  How to programmatically stop a program in Jupyter Notebook? Mark17 11 36,984 Feb-12-2023, 01:41 PM
Last Post: jp21in
  waiting for barcode scanner output, while main program continues to run lightframe109 3 4,641 Sep-03-2020, 02:19 PM
Last Post: DeaD_EyE
  How to get program output from subprocess.Popen? glestwid 1 2,367 Aug-19-2020, 05:44 AM
Last Post: buran
  how can we record a video file from our program output (moving object) Zhaleh 0 1,806 Aug-03-2020, 02:47 PM
Last Post: Zhaleh
  Printing output without print usage susmith552 1 1,658 Feb-07-2020, 07:52 PM
Last Post: Clunk_Head
  How to Stop Sentinel Value from Entering Final Output ZQ12 3 3,226 Nov-11-2019, 07:25 AM
Last Post: perfringo
  Juicy highly composite number needs factorization kevolegend 15 5,579 Jun-03-2019, 01:11 PM
Last Post: heiner55
  class program no output under python 3.5.1 jolinchewjb 1 2,365 Jan-22-2019, 10:53 AM
Last Post: Larz60+
  ciscolib cdp output list printing support anna 3 3,328 Jul-25-2018, 12:18 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020