fibodin.py
#!/usr/bin/env python
#
# Ask the user for: m and n,
# Print out, the first m Fibonacci nunbers that are divisible by n
# with no residue.
#
# Author: Yotam Medini [email protected] -- Created: 2005/June/06
#
import sys; # For stdin/stdout and exit
import string # To split the line
sys.stdout.write("""
Please enter two integer numbers M and N,
and I will show you the first M Fibonacci numbers that
are divisible by N.
""")
user_line = sys.stdin.readline()
ul_split = string.split(user_line)
if len(ul_split) != 2:
sys.stderr.write("Sorry I did not get two numbers\n")
sys.exit(1)
# Take the separated strings
m_string = ul_split[0]
n_string = ul_split[1]
# Convert the strings to integer numbers
m = int(m_string)
n = int(n_string)
#########################################################
# Check the numbers. We must have m >= 0 and n > 0. #
#########################################################
if m < 0:
sys.stderr.write("You, yes YOU, owe me %d Fibonacci numbers\n" % -m)
sys.exit(1)
if n == 0:
sys.stderr.write("I will not even try to divide by ZERO!\n")
sys.exit(1)
if n < 0:
sys.stderr.write("Warning: n = %d < 0, I will use absolute |n| value\n"
% n)
n = -n # Now n > 0 and we forgive the user...
#######################################################
# Finally - fight for Fibonacci fat numbers for fun #
#######################################################
f1 = 0 # First Fibonacci number
f2 = 1 # Second Fibonacci number
found_count = 0 # How many found so far
while found_count < m:
if f1 % n == 0:
sys.stdout.write("Fibonacci number: %d\n" % f1)
found_count = found_count + 1
f3 = f1 + f2 # Next Fibonacci number
f1 = f2 # We are done with old f1
f2 = f3 # Make room for future
sys.stdout.write("For more Fibonacci numbers, run me again with new M,N\n")
sys.exit(0)
Generated by GNU enscript 1.6.4.