Python Output Questions

Asked 2 years ago, Updated 2 years ago, 13 views

We're going to make a Fibonacci sequence by taking the digits from 2 to 17 If there is a decimal in the Fibonacci sequence from the number of digits entered, print it out If the Fibonacci sequence is not a decimal in the number of digits entered, I'm writing a program that prints "None Minority".

while True:
    p_n = int("Please enter the number of digits of the Fibonacci sequence you are obtaining. (2~17) : ")))
    if 1>= p_n or p_n>=18:
        print("Please enter a number that fits the range.")
        continue
    elif 1 < p_n < 18: # Search Range
        a = 10**(p_n-1); b = 10**p_n
        print("Search Range:", [a,b-1])
        break

i_1 = 0; i_2 = 1; fibo = 0 #i_1:1 clause, i_2:2 clause

while fibo<1000000000000000: #18 digits or less
    fibo = i_1 + i_2 
    i_1 = i_2
    i_2 = fibo
    if fibo<100000000000000000:

        import math
        i = 0 ; x = 0
        If a < int(fibo) and int(fibo) < b-1:#int(fibo) : p_n digits
            k = int(math.sqrt(int(fibo))) #Rooting preparation
            for i in range(2,k+1):
                result = int(fibo)%i
                fibo_ = fibo               
                If result == 0: # Remaining 0: Non-decimal
                    break
            If result!=0: #Remaining !=0:Minority
                print("Pivot prime:",fivo_)
            else :
                print ("Not a Fibo Minority")

In this situation, if p_n is 2,

Pivot prime: 13
No Fibo Minority
No Fibo Minority
No Fibo Minority
Fibo prime number: 89

It pops up like this

If p_n is 7

No pivot decimal
No Fibo Minority
No Fibo Minority
No Fibo Minority
No Fibo Minority

When there is a Fibo prime number (for example, when you enter 2), don't make the "No Fibo prime number When there is no Fibo prime number (for example, when you enter 7), I want to make 'No Fibo prime' appear. What should I do?

python

2022-09-21 10:11

1 Answers

I'll tell you after thinking that the code above works as you intended.

If you look at the code you've implemented, you've got the number of Fibonacci in the desired number of digits, and if the number is a decimal, you've got output, if not a decimal, you've got no Fibonacci.

To print this out only if the Fibonacci number is a prime number, otherwise there is no Fibonacci, you can do the following:

pn = []

# Fibonacci numbering part
    if result == 0:
        pn.append(fibo_)

if len(pn) == 0:
    print ('Pivot prime')
else:
    for i in range(len(pn)):
        print ('Pivot prime:', pn[i])

It's not that (although there's not much difference) If you want to print out the number once you get a decimal number, but only if you don't finally print out any numbers, you can use the following method.

i = 0

# Fibonacci numbering part
    if result == 0:
        print('Pivot prime:', fibo_)
        i += 1

if i == 0:
    print ('Pivot prime')

It didn't require a difficult concept, so you can find the exact location of each code with a little thought


2022-09-21 10:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.