1+ # Python to find the range of numbers that are squares within a specified limit
12# method 1
23print ('Numbers\t \t Squares' )
34print ('1 \t \t ' + str (1 * 1 ))
67print ('4 \t \t ' + str (4 * 4 ))
78
89
9- # method 2
10- def number_squares (n ):
11- print (str (n )+ ' \t \t ' + str (n * n ))
12-
13- start_num ,end_num = input ('enter the starting and ending number:' ).split (' ' )
14- if start_num <= end_num :
15- for n in range (int (start_num ),int (end_num )+ 1 ):
16- number_squares (n )
17- else :
18- print ('enter the proper range' )
19-
20-
21- # method 3 : optimized
22-
10+ # method 2:
2311def number_squares (start_num ,end_num ):
2412 if start_num > end_num :
2513 print ('Invalid Range: Starting number must be less than or equal to the ending number.' )
@@ -32,4 +20,34 @@ def number_squares(start_num,end_num):
3220 print ('Numbers\t \t Squares' )
3321 number_squares (start_num ,end_num )
3422except ValueError :
35- print ("Invalid input! Please enter two integers separated by a space." )
23+ print ("Invalid input! Please enter two integers separated by a space." )
24+
25+
26+ # Assignment
27+ # In Python to find the range of numbers that are both squares and cubes within a specified limit
28+ def number_square_cubes (start_num ,end_num ):
29+ if start_num > end_num :
30+ print ('Invalid Range: Starting number must be less than or equal to the ending number.' )
31+ return
32+ for n in range (start_num ,end_num + 1 ):
33+ print (f"{ n } \t \t { n * n } \t \t { n * n * n } " )
34+
35+ try :
36+ start_num ,end_num = map (int ,input ('Enter the starring and ending range with single space:' ).split (' ' ))
37+ print ('Numbers\t \t Squares\t \t Cubes' )
38+ number_square_cubes (start_num ,end_num )
39+ except ValueError :
40+ print ("Invalid input! Please enter two integers separated by a space." )
41+
42+
43+ # Time Complexity
44+
45+ # The main time complexity comes from the loop, which iterates over the numbers in the range [start_num, end_num].
46+ # As mentioned above, the loop runs N=end_num−start_num+1
47+ # N=end_num−start_num+1 times, and each iteration involves constant-time operations.
48+
49+ # Thus, the time complexity of the code is:
50+ # O(N)whereN=end_num−start_num+1
51+
52+ # If the size of the input range increases, the number of iterations increases linearly,
53+ # so the time complexity is O(N).
0 commit comments