#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import math

def print_help():
    print("USAGE")
    print("\t   ./106bombyx n [k | i0 i1]\n")
    print("DESCRIPTION")
    print("\t   n\tnumber of first generation individuals")
    print("\t   k\tgrowth rate from 1 to 4")
    print("\t   i0\tinitial generation (included)")
    print("\t   i1\tfinal generation (included)")

def help_me():
    if len(sys.argv) == 2:
        if sys.argv[1] == "-h":
            print_help()
        else:
            exit(84)

def error():
    if len(sys.argv) != 3 and len(sys.argv) != 4:
        print("ARG ERROR:\n\twrong number of arg")
        exit(84)
    try:
        n = int(sys.argv[1])
        if len(sys.argv) == 3:
            k = float(sys.argv[2])
        elif len(sys.argv) == 4:
            i0 = int(sys.argv[2])
            i1 = int(sys.argv[3])
    except:
        print("ARG ERROR:\n\tOne or multiple arg aren't number")
        exit (84)
    if n < 0:
        print("VALUE ERROR:\n\tn should be higher than 0")
        exit (84)
    if len(sys.argv) == 3:
        if k < 1 or k > 4:
            print("VALUE ERROR:\n\tk value should be betwenn 1 to 4")
            exit (84)
    elif len(sys.argv) == 4:
        if i0 > i1 :
            print("VALUE ERROR:\n\tinitial generation must be higher than final generation")
            exit (84)
        if i0 < 1 or i1 < 1:
            print("VALUE ERROR:\n\tinitial and final generation numbermust be higher than 1")
            exit(84)

def growth(n, k):
    for i in range(1, 101):
        print(i, '%.2f' % n)
        n = k * n * (1000 - n) / 1000
        if n < 0:
            n = 0
    return (0)

def scheme(n, i0, i1):
    k = 1.00
    stock = n
    while k <= 4.00:
        for i in range(i0 - 1):
            n = k * n * (1000 - n) / 1000
            if n < 0:
                n = 0
        for i in range(i1 - i0 + 1):
            print('%.2f' % k, '%.2f' % n)
            n = k * n * (1000 - n) / 1000
            if n < 0:
                n = 0
        k += 0.01
        n = stock
    return (0)

def main():
    if len(sys.argv) == 2 and sys.argv[1] == "-h":
        print_help()
        exit(84)
    error()
    n = int(sys.argv[1])
    if len(sys.argv) == 3:
        k = float(sys.argv[2])
        growth(n, k)
    elif len(sys.argv) == 4:
        i0 = int(sys.argv[2])
        i1 = int(sys.argv[3])
        scheme(n, i0, i1)

main()
