solution Algorithm

Algorithm is a step-by-step procedure or a set of well-defined instructions to solve a specific problem or perform a certain task. In the context of computer science and programming, algorithms are essential as they provide a systematic approach to problem-solving and enable computers to process and analyze data, make decisions, and execute tasks efficiently. They are the backbone of software applications and systems, powering various functionalities and enabling automation. Algorithms can be simple, like sorting a list of numbers, or complex, like determining the best route for a self-driving vehicle. They are designed to be efficient, scalable, and adaptable to a wide range of applications and environments. The effectiveness of an algorithm is often determined by its time and space complexity, which describe the amount of computational resources required to execute the algorithm. Time complexity refers to the number of basic operations or steps needed to complete the algorithm, while space complexity refers to the memory space required to store data during the algorithm's execution. Optimizing an algorithm's performance involves minimizing its time and space complexity, ensuring that it can handle large-scale problems and deliver results in a reasonable amount of time. Algorithm design and analysis play a crucial role in the development of modern computing systems, as they provide the foundation for creating efficient, high-performance software and hardware solutions.
"""
Consider all integer combinations of ab for 2 <= a <= 5 and 2 <= b <= 5:

2^2=4,  2^3=8,   2^4=16,  2^5=32
3^2=9,  3^3=27,  3^4=81,  3^5=243
4^2=16, 4^3=64,  4^4=256, 4^5=1024
5^2=25, 5^3=125, 5^4=625, 5^5=3125

If they are then placed in numerical order, with any repeats removed, we get
the following sequence of 15 distinct terms:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

How many distinct terms are in the sequence generated by ab
for 2 <= a <= 100 and 2 <= b <= 100?
"""


def solution(n):
    """Returns the number of distinct terms in the sequence generated by a^b
    for 2 <= a <= 100 and 2 <= b <= 100.

    >>> solution(100)
    9183
    >>> solution(50)
    2184
    >>> solution(20)
    324
    >>> solution(5)
    15
    >>> solution(2)
    1
    >>> solution(1)
    0
    """
    collectPowers = set()

    currentPow = 0

    N = n + 1  # maximum limit

    for a in range(2, N):
        for b in range(2, N):
            currentPow = a ** b  # calculates the current power
            collectPowers.add(currentPow)  # adds the result to the set
    return len(collectPowers)


if __name__ == "__main__":
    print("Number of terms ", solution(int(str(input()).strip())))

LANGUAGE:

DARK MODE: