Take two integer as input from the user and write A program to find a number with maximum factors in that range. (biggest composite number between a range) in Java, Python , C , C++
To find the number with the maximum factors (i.e., the number with the most divisors) within a given range, we can follow a similar approach as the previous one but with an added requirement: we need to ensure the number is a composite number (not a prime number).
Here’s how the logic works:
- Composite number: A number with more than two divisors.
- We iterate through the given range, find the divisors for each number, and track the number with the maximum divisors, ensuring that it's a composite number.
Here is the code in Java, Python, C, and C++ to find the biggest composite number with the most divisors in the range.
Java Solution ( program to find a number with maximum factors)
import java.util.Scanner;
public class a_Try_Run {
public static void main(String args []){int n,m,count=0,max=0,num=0;Scanner read=new Scanner(System.in);System.out.println("Enter two numbers for the range: ");n=read.nextInt();m=read.nextInt();read.close();for(int i=n;i<=m;i++){for(int j=1;j<=i;j++){if(i%j==0){count++;}}if(count>max){max=count;num=i;}count=0;}System.out.print("The number with maximum factors is: "+num);}}
Python Solution ( program to find a number with maximum factors)
def main():n = int(input("Enter the first number (n) of the range: "))m = int(input("Enter the second number (m) of the range: "))max_factors = 0num = 0for i in range(n, m + 1):count = 0for j in range(1, i + 1):if i % j == 0:count += 1if count > max_factors:max_factors = countnum = iprint(f"The number with the maximum factors is: {num}")if __name__ == "__main__":main()
C Solution ( program to find a number with maximum factors)
#include <stdio.h>int main() {int n, m;int count, max_factors = 0, num = 0;printf("Enter two numbers for the range: ");scanf("%d %d", &n, &m);for (int i = n; i <= m; i++) {count = 0;for (int j = 1; j <= i; j++) {if (i % j == 0) {count++;}}if (count > max_factors) {max_factors = count;num = i;}}printf("The number with the maximum factors is: %d\n", num);return 0;}
C++ Solution (program to find a number with maximum factors)
#include <iostream>using namespace std;int main() {int n, m;int count, max_factors = 0, num = 0;cout << "Enter two numbers for the range: ";cin >> n >> m;for (int i = n; i <= m; i++) {count = 0;for (int j = 1; j <= i; j++) {if (i % j == 0) {count++;}}if (count > max_factors) {max_factors = count;num = i;}}cout << "The number with the maximum factors is: " << num << endl;return 0;}
The time complexity and space complexity of the provided code.
Code Overview:
The program takes two integers (n and m) as input and finds the number within the range [n, m] that has the maximum number of divisors (factors). It does this by iterating over each number in the range and counting the divisors of each number.
Time Complexity:
Outer Loop (for(int i=n; i<=m; i++)):
This loop runs from n to m, so it iterates m - n + 1 times, which is O(m - n).
Inner Loop (for(int j=1; j<=i; j++)):
For each number i, the inner loop runs from 1 to i, meaning it runs i times.
Therefore, the time complexity of the inner loop is proportional to i.
Total Time Complexity:
The total time complexity is the sum of the work done for each i in the outer loop: ∑i =nmO(i)=O(n)+O(n+1)+O(n+2)+...+O(m)\sum_{i=n}^{m} O(i) = O(n) + O(n+1) + O(n+2) + ... + O(m)
This is equivalent to the sum of the first m integers, which is O(m^2).
Thus, the overall time complexity of the program is O((m - n) * m) or simplified as O(m^2) in the worst case.
Space Complexity:
Variables:
The program uses a fixed amount of space for a few integer variables (n, m, count, max, num), which are all O(1).
Scanner Object:
The Scanner object (read) takes space for reading input, but this is a constant amount of space, so it also contributes O(1).
No extra data structures are used (like arrays, lists, etc.), so the space complexity is constant.
Thus, the space complexity of the program is O(1).
Summary:
Time Complexity: O((m - n) * m) or O(m^2)
Space Complexity: O(1)
Thank You for reading this post. You can check out more articles like this on www.physicswallah.in .
If you have any doubts then you can post a comment below.
No comments:
Post a Comment