简体   繁体   中英

Smallest number with digits product of n

I need to find the smallest number which digit numbers product is equal to a given num.

import java.util.Scanner;

class timus_1014_2 {

    public static void main(String[] args){
        int[] arr = new int[10]; // eskan ban@  chem imanum inchi a statik,
        int prod = 1;
        int j = 0;

        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for (int i = 2; i < 10; ++i){
            if (n % i == 0) {
                arr[j] = i;
                j++;
            }
        }
        prod =  prod * arr[j];

        System.out.print(prod);

    }
}

Something is wrong with the logic, whats is the problem when I input 10 it should give 25 but it gives 0 . Please give ideas of how to make a program find a number which digits product is a given num.

If I understood your problem correctly you need a number whose product of digits equals a number N. Since you asked for new algorithm , you can chck following code.

Logic:

Note : For number whose prime factors are less than 10

  1. Get all factors from 9 -> 2
  2. add to list
  3. print in reverse or use stack instead of list
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter number");
        int num = in.nextInt();

        List<Integer> lst = new ArrayList<>();

        for (int p = 9; p >= 2; p--) {
            while (num % p == 0) {
                num /= p;
                lst.add(p);
            }
        }

        String smallestNumber = "";
        for (int i = lst.size() - 1; i >= 0; i--) {
            smallestNumber = smallestNumber + lst.get(i);
        }
        System.out.println("Smallest number : " + smallestNumber);
    }
}

Output :

Enter number
10
Smallest number : 25

Enter number
144
Smallest number : 289

Enter number
12
Smallest number : 26

I suggest you look at each error is fix it one by one. I also suggest you use an IDE which will show you the errors and you type and will help ensure you don't have an overwhelming number of errors and you can see if those error disappear based on your corrections.

BTW Often when you use an array for a short piece of code, it can often be eliminate as I suspect it can be removed in your case.

Static methods can not access non-static members of class. In your case prod is member variable of class but not static. To fix the error , try to make prod as static.

private static int prod = 1;

I would prefer , to make it local variable if no other method is using it.

The problem here is you need to create an object of the particular class to call a particular method associated with it

import java.util.Scanner;

class DigPro {
static int[] arr = new int[10]; // eskan ban@  chem imanum inchi a statik,
int prod = 1;
public static void main(String[] args){
Scanner in = new Scanner(System.in);
DigPro obj = new DigPro();
obj.prime(in.nextInt());

}



public void prime(int n){
for (int i = 1; i < 10; ++i){
   for (int j = 0; j < 9; ++j) {
       if (n % i == 0) {

           arr[j] = i;

       }
   prod =  prod * arr[j];
   }

}
System.out.print(prod);
}

}

Here you need to create an object say obj of DigPro class and then call prime(int n) method with that object. Also your division is startint with zero which is changed to one

In above code you are increasing j after the assigning value to arr[j] .You should do the following:-

prod = prod * arr[j-1];

Here it will multiply prod with last array updated. That is why you are getting zero. And for your another question find the smallest number which digit numbers product is equal to a given num has similar answer at this link .

Since this is actually a pretty interesting problem, I took the time to come up with a correct solution for all possible integer inputs.

import java.util.*;

public class Main{

    public static void main(String[] args) {
        System.out.println("Enter number:");
        int number = new Scanner(System.in).nextInt();

        Stack<String> factors = new Stack<>();
        if(number==0){
            factors.push("0");
        }else if(number==1){
            factors.push("1");
        }else{
            for(int f=9;f>1;f--){
                while(number%f==0){
                    factors.push(Integer.toString(f));
                    number/=f;
                }
            }
        }

        if(number<0){
            factors.push("-");
        }

        if(number>9){
            System.out.println("This is impossible.");
        }else{
            System.out.println("Smallest Number:");
            while(!factors.empty()) System.out.print(factors.pop());
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM