Home > other >  Why does my program keep printing "0" instead of what the functions tell it?
Why does my program keep printing "0" instead of what the functions tell it?

Time:01-06

My code keeps generating "0" and I'm not sure what to do. My instructions are to write a function that finds Emilia Numbers, and then use it to count how many Emilia numbers would you find for the numbers from 1 to 1000

def is_emilia_number(num):
        # Find all the factors of the number
        factors = []
        for i in range(1, num 1):
        if num % i == 0:
            factors.append(i)

    # Check if the number has at least two sets of factors where the sum of one set equals the difference of another set
    for i in range(len(factors)):
        for j in range(i 1, len(factors)):
            for k in range(j 1, len(factors)):
                for l in range(k 1, len(factors)):
                    if factors[i]   factors[j] == factors[k] - factors[l]:
                        return True

    return False

# Initialize a counter to 0
counter = 0

# Iterate through the numbers from 1 to 1000
for num in range(1, 1001):
    # Check if the number is an Emilia number
    if is_emilia_number(num):
        counter  = 1

# Print the counter
print(counter) # should print 8

I tried working on the functions separately too but I don't know why it's not working. here's examples: a positive Integer will be an “Emilia Number” if it has at least two sets of factors where the sum of one set equals the difference of another set. 6 is an Emilia Number. It has a sets of factors (6,1) and (2,3) where 2 3 = 6 – 1 30 is an Emilia Number. It has sets of factors (10,3) and (15,2) where 10 3 = 15 – 2 84 is an Emilia Number. It has sets of factors (21,4) and (28,3) where 21 4 = 28 – 3

CodePudding user response:

I think the issue lies within your nested for loop. You're continuously adding the start of the loop by 1 meaning it'll skip over some of the values that you're trying to use for comparison. You can try swapping out your nested for loop with this one:

for i in factors:
    for j in factors:
        if i != j:
            for k in factors:
                if i != k:
                    for l in factors:
                        if k != l and i != l:
                            if i   j == k - l:
                                print(i   j)
                                print(i, j, k, l)
                                return True

There seems to be a lot more than 8 numbers that satisfy the requirement though. I added in two prints to show which values satisfy the requirement.

This is the full code that I edited:

def is_emilia_number(num):
    # Find all the factors of the number
    factors = []
    for i in range(1, num 1):
        if num % i == 0:
            factors.append(i)

    for i in factors:
        for j in factors:
            if i != j:
                for k in factors:
                    if i != k:
                        for l in factors:
                            if k != l and i != l:
                                if i   j == k - l:
                                    print(i   j)
                                    print(i, j, k, l)
                                    return True

# Initialize a counter to 0
counter = 0

# Iterate through the numbers from 1 to 1000
for num in range(1,1000):
    # Check if the number is an Emilia number
    if is_emilia_number(num):
        counter  = 1

# Print the counter
print(counter) # should print 8

CodePudding user response:

I think your understanding of the definition of Emilia numbers is incorrect. I think you are meant to consider pairs of factors (f,g) where f*g==n.A number is an Emilia number iff it has two pairs of factors (f,g) and (h,k) such that f g=k-h. If you just allow arbitrary pairs factors of n (without requiring that they multiply to n) then there are many more than 8 solutions.

If you don't restrict your attention to pairs of factors (f,g) where f*g==n then consider any multiple of 12. It will have 1,2,3,4 among its factors. Note that 4-1 == 1 2 so this number would get classified as an Emilia number. This would mean that any multiple of 12 is an Emilia number. There are 83 multiples of 12 less than 1000. If there are only 8 Emilia numbers less than 1000 then all multiples of 12 can't be Emilia numbers. Therefore I am pretty sure that you should restrict your attention to pairs of factors (f,g) where f*g==n

The following function only considers pairs of factors that multiply to the input number. However with this implementation there are still 34 Emilia numbers between 1 and 1000. This function will print the reason for classifying a number as Emilia. As far I can tell its results are all ok, and there are 34 Emilia numbers between 1 and 1000.

def is_emilia_number(num):
    # Find all the factors of the number
    factors = []
    for i in range(1, num 1):
        if num % i == 0:
            factors.append(i)
    for f in factors:
        g = num // f
        sum = f   g
        for h in factors: 
            i = num // h
            if i > h:
                (i, h) = (h, i)
            diff = h-i
            if sum == diff:
                print(f"num: {num} is an Emilia number because {f} {g} == {h}-{i} {f}*{g}={f*g} {h}*{i}={h*i}")
                return True
    return False
  • Related