Try this (read comments in the code below):
from math import sqrt
def fibonacci(n):
return int(((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5))) # WA algorithm
# Handle N from user input
while True:
N = int(input("Enter N: "))
Y = int(input("Enter Y: "))
if N < 1 or Y < 1:
print("Plese enter a positive integers for N and Y")
else:
break
count = 0 # Counter of the instances of fibonacci numbers containing Y digits
for n in range(N):
fib = fibonacci(n) # Calls fibonacci
print(fib) # Print Fibonacci number
if len(str(fib)) == Y: # Number of digits in the Fibonacci number
count += 1 # Count up if number of digits = Y
print("\nFibonacci numbers with {} digits occured {} times" .format(Y, count))
The algorithm for calculating Fibonacci numbers comes from Wolfram Alpha
EDIT:
In order to save time to find results of multiple Y
s, you can keep statistics in a dictionary like so:
from math import sqrt
# Handle N from user input
while True:
N = int(input("Enter N: "))
if N < 1:
print("Plese enter a positive integers for N and Y")
else:
break
size = {} # Dictionary with number of occurences of lengths of Fibonacci numbers
count = 0 # Counter of the instances of fibonacci numbers containing Y digits
for n in range(N):
fib = int(((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5))) # WA algorithm
print(fib) # Print Fibonacci number
s = str(len(str(fib)))
if s not in size:
size[s] = 1
else:
size[s] += 1
print()
for key, val in size.items():
print("{}\toccured\t{} times" .format(key, val))
This will yield an output like:
Enter N: 13
0
1
1
2
3
5
8
13
21
34
55
89
144
1 occured 7 times
2 occured 5 times
3 occured 1 times