Python Forum
math problem using recursion?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
math problem using recursion?
#5
As for the previous questions, the only requirement for the start number is that
it is a semi-prime. And I doubt whether the chain lengths would ever get above
20 or 30.

Ok, here is my revised code. I fixed the previous errors. I've added the routine
to pull out the data from the dictionary and create the new numbers. But I don't
really know where to go from here.

import math

sp_dict = {}
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

def semi():
    for i in range(9, 101, 2):   #for the real thing this range will be much larger
        divisors = []
        for j in primes:
            if j > math.sqrt(i):
                continue
            else:
                div = i / j
                if div == int(div):
                    b = div / j
                    if b == int(b) and b > 1:
                        continue
                    else:
                        divisors.append(j)
                        divisors.append(div)
        if len(divisors) == 2:
            value = "[" + str(divisors[0]) + ", " + str(int(divisors[1])) + "]"
            sp_dict[i] = value
        
    print()
    print(sp_dict)
    print()

# checks dictionary for entry. if there, pulls out factors
# and assigns to variables
def check_if_sp(n):
    if n in sp_dict:
        x = sp_dict[n]
        x = x.strip('[')
        x = x.strip(']')
        x = x.split(', ')
        fact1 = x[0]
        fact2 = x[1]
        print(f'{n} = {fact1} * {fact2}')
        return fact1, fact2
    else:
        print(n,' is not a semi-prime.')


a = semi()
n = int(input('Input a number: '))

fact1, fact2 = check_if_sp(n)

# creates 2 new numbers from the factors to check
new1 = int(fact1 + fact2)
new2 = int(fact2 + fact1)
print(f'New numbers are {new1} and {new2}')
Ok, how do I proceed to create the tree as in the image in the first post?
Reply


Messages In This Thread
math problem using recursion? - by mnh001 - Sep-01-2020, 03:54 PM
RE: math problem using recursion? - by DPaul - Sep-01-2020, 05:31 PM
RE: math problem using recursion? - by Larz60+ - Sep-01-2020, 06:52 PM
RE: math problem using recursion? - by mnh001 - Sep-01-2020, 08:17 PM
RE: math problem using recursion? - by mnh001 - Sep-01-2020, 10:57 PM
RE: math problem using recursion? - by DPaul - Sep-02-2020, 09:49 AM
RE: math problem using recursion? - by mnh001 - Sep-02-2020, 02:35 PM
RE: math problem using recursion? - by DPaul - Sep-02-2020, 05:22 PM
RE: math problem using recursion? - by bowlofred - Sep-02-2020, 05:36 PM
RE: math problem using recursion? - by mnh001 - Sep-02-2020, 06:24 PM
RE: math problem using recursion? - by Gribouillis - Sep-02-2020, 07:14 PM
RE: math problem using recursion? - by SmartGrid - Sep-03-2020, 12:55 PM
RE: math problem using recursion? - by mnh001 - Sep-03-2020, 02:34 PM
RE: math problem using recursion? - by DPaul - Sep-03-2020, 02:52 PM
RE: math problem using recursion? - by mnh001 - Sep-03-2020, 07:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Math Problem cryxma 2 2,023 Dec-21-2020, 09:53 PM
Last Post: Gribouillis
  GCF function w recursion and helper function(how do i fix this Recursion Error) hhydration 3 2,662 Oct-05-2020, 07:47 PM
Last Post: deanhystad
  Math problem in Python - pyqt5 rwahdan 6 5,920 Jun-18-2019, 08:11 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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