200_success
- 145.5k
- 22
- 190
- 478
public static void main(String[] args) {
int m = askInt("Enter the value of M: ", 100, 10000),
n = askInt("Enter the value of N: ", 1, 100);
intlong solution = findSolution(m, n);
System.out.println("Minimum number is: " + solution);
System.out.println("Total number of digits: " + numberOfDigits(solution));
}
- Your program isn't object-oriented anyway, so there's no point in instantiating
a
and callinginput()
as an instance method. (Furthermore, it's weird thatinput()
, which is an instance method, stores its results instatic
variables instead of instance variables.) - There is a lot of repetition within
input()
. A general-purpose integer-prompting routine, used for both M and N, would be better. m
,n
, andndigit
should not be static variables, as that makes them essentially global variables. Any function in the class can alter their values as a side-effect, making your code harder to analyze — you have to read all of the code to understand any of it.- The solution needs to be a
long
, in case N is large. As @OleTange pointed out, if N is 99, then the solution must be at least 99999999999, which won't fit in anint
. Note that brute-force enumeration is a very poor strategy for such large numbers.
static int sumOfDigits(intlong num) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
static int numberOfDigits(intlong num) {
return (int)Math.log10(num) + 1;
}
public static intlong findSolution(int m, int n) {
for (intlong i = m + 1; ; i++) {
if (sumOfDigits(i) == n) {
return i;
}
}
}
public static void main(String[] args) {
int m = askInt("Enter the value of M: ", 100, 10000),
n = askInt("Enter the value of N: ", 1, 100);
int solution = findSolution(m, n);
System.out.println("Minimum number is: " + solution);
System.out.println("Total number of digits: " + numberOfDigits(solution));
}
- Your program isn't object-oriented anyway, so there's no point in instantiating
a
and callinginput()
as an instance method. (Furthermore, it's weird thatinput()
, which is an instance method, stores its results instatic
variables instead of instance variables.) - There is a lot of repetition within
input()
. A general-purpose integer-prompting routine, used for both M and N, would be better. m
,n
, andndigit
should not be static variables, as that makes them essentially global variables. Any function in the class can alter their values as a side-effect, making your code harder to analyze — you have to read all of the code to understand any of it.
static int sumOfDigits(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
static int numberOfDigits(int num) {
return (int)Math.log10(num) + 1;
}
public static int findSolution(int m, int n) {
for (int i = m + 1; ; i++) {
if (sumOfDigits(i) == n) {
return i;
}
}
}
public static void main(String[] args) {
int m = askInt("Enter the value of M: ", 100, 10000),
n = askInt("Enter the value of N: ", 1, 100);
long solution = findSolution(m, n);
System.out.println("Minimum number is: " + solution);
System.out.println("Total number of digits: " + numberOfDigits(solution));
}
- Your program isn't object-oriented anyway, so there's no point in instantiating
a
and callinginput()
as an instance method. (Furthermore, it's weird thatinput()
, which is an instance method, stores its results instatic
variables instead of instance variables.) - There is a lot of repetition within
input()
. A general-purpose integer-prompting routine, used for both M and N, would be better. m
,n
, andndigit
should not be static variables, as that makes them essentially global variables. Any function in the class can alter their values as a side-effect, making your code harder to analyze — you have to read all of the code to understand any of it.- The solution needs to be a
long
, in case N is large. As @OleTange pointed out, if N is 99, then the solution must be at least 99999999999, which won't fit in anint
. Note that brute-force enumeration is a very poor strategy for such large numbers.
static int sumOfDigits(long num) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
static int numberOfDigits(long num) {
return (int)Math.log10(num) + 1;
}
public static long findSolution(int m, int n) {
for (long i = m + 1; ; i++) {
if (sumOfDigits(i) == n) {
return i;
}
}
}
- Your program isn't object-oriented anyway, so there's no point in instantiating
a
and callinginput()
as an instance method. (Furthermore, it's weird thatinput()
, which is an instance method, stores its results instatic
variables instead of instance variables.) - There is a lot of repetition within
input()
. A general-purpose integer-prompting routine, used for both M and N, would be better. m
,n
, andndigit
should not be static variables, as that makes them essentially global variables. Any function in the class can alter their values as a side-effect, making your code harder to analyze — you have to read all of the codcode to understand any of it.
- Your program isn't object-oriented anyway, so there's no point in instantiating
a
and callinginput()
as an instance method. - There is a lot of repetition within
input()
. A general-purpose integer-prompting routine, used for both M and N, would be better. m
,n
, andndigit
should not be static variables, as that makes them essentially global variables. Any function in the class can alter their values as a side-effect, making your code harder to analyze — you have to read all of the cod to understand any of it.
- Your program isn't object-oriented anyway, so there's no point in instantiating
a
and callinginput()
as an instance method. (Furthermore, it's weird thatinput()
, which is an instance method, stores its results instatic
variables instead of instance variables.) - There is a lot of repetition within
input()
. A general-purpose integer-prompting routine, used for both M and N, would be better. m
,n
, andndigit
should not be static variables, as that makes them essentially global variables. Any function in the class can alter their values as a side-effect, making your code harder to analyze — you have to read all of the code to understand any of it.
Loading
lang-java