Java Code Help

gembaknb

Active member
  • Oct 13, 2014
    899
    50
    28
    It is seen that the number, 125874 and its double 251748, contain exactly the same digits, but in a different order. Write a java program to find and print the smallest positive integer, x such that 2x, 3x, 4x, 5x and 6x contain the same digits.

    Machan array anan manan use karanna baaa.. Only loops mchn.. for, while, do while walin thmi gema gahanna oona
     

    Laknath

    Well-known member
  • Oct 18, 2006
    1,380
    65
    63
    street
    int start = 1;
    bool found = false;
    int result = 0;

    while (!found) {
    start *= 10;
    for (int i = start; i < start * 10 / 6; i++) {
    found = true;
    for (int j = 2; j <= 6; j++) {
    if (!isPerm(i, j * i)) {
    found = false;
    break;
    }
    }
    if (found) {
    result = i;
    break;
    }
    }
    }


    meka try karala balpan podak wenas karala
     
    Last edited:

    KingCM

    Well-known member
  • Jul 23, 2013
    6,920
    948
    113
    www.biogen.lk
    HTML:
    public class Problem52 {
    
    	public static void main(String[] args){
    
    		
    		boolean isNotFound=true;
    		int data=1;
    		String six_multiplier="";
    		String five_multiplier="";
    		String four_multiplier="";
    		String three_multiplier="";
    		String two_multiplier="";
    
    		while(isNotFound){
    
    		    six_multiplier=(6*data)+"";
    		    five_multiplier=(5*data)+"";
    		    four_multiplier=(4*data)+"";
    		    three_multiplier=(3*data)+"";
    		    two_multiplier=(2*data)+"";
    
    		    if(five_multiplier.length()==six_multiplier.length() &&
    		        four_multiplier.length()==six_multiplier.length()&&
    		    	three_multiplier.length()==six_multiplier.length()&&
    		    	two_multiplier.length()==six_multiplier.length() ){
    
    		    	for(int index=0;index=0 &&
    
    		    		 four_multiplier.indexOf(str)>=0 &&
    		    		 three_multiplier.indexOf(str)>=0 &&
    		    		 two_multiplier.indexOf(str)>=0){
    		    			isNotFound=false;
    
    		    		}else{  
    		    			isNotFound=true;
    		    			break;	
    		    		}
    		    	}
    		    }
    		    
    		    data++;
    		}
    	   
    System.out.println("6X "+six_multiplier+" 5X "+five_multiplier+" 4X "+four_multiplier+" 3X "+three_multiplier+" 2X "+two_multiplier);
    
    System.out.println("The smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits is "+(data-1));
    
    System.out.println("Total time taken:"+ ((System.currentTimeMillis()-start_time)/1000)+"sec");
    
    }
    
    }

    https://chennaitechblog.wordpress.com/2013/06/02/permuted-multiples/
     

    gembaknb

    Active member
  • Oct 13, 2014
    899
    50
    28

    int start = 1;
    bool found = false;
    int result = 0;

    while (!found) {
    start *= 10;
    for (int i = start; i < start * 10 / 6; i++) {
    found = true;
    for (int j = 2; j <= 6; j++) {
    if (!isPerm(i, j * i)) {
    found = false;
    break;
    }
    }
    if (found) {
    result = i;
    break;
    }
    }
    }


    meka try karala balpan podak wenas karala


    meke me isperm kiyna eka bladdi eeka array walin ne aran thiyenne.. Array walin ganna lesi bn.. mt oona loop ekak magin test karanna
     

    gembaknb

    Active member
  • Oct 13, 2014
    899
    50
    28
    Code:
    import java.util.*;
    
    public class Main
    {
    	public static void main(String[] args)
    	{
    		int num1=0, tmp1, num2, multi1, multi2, sum1, sum2, len1, len2;
    		do{
    			num1++;
    			tmp1 = num1;
    			num2 = num1 * 2; 
    			multi1 = 1;
    			multi2 = 1;
    			sum1 = 0;sum2 = 0;len1 = 0;len2 = 0;
    			while (tmp1 != 0)
    			{
    				multi1 *= ((tmp1 % 10 == 0) ?1: tmp1 % 10);
    				sum1 += (tmp1 % 10);
    				tmp1 /= 10;
    				len1++;
    			}
    			while (num2 != 0)
    			{
    				multi2 *= ((num2 % 10 == 0) ?1: num2 % 10);
    				sum2 += num2 % 10;
    				num2 /= 10;
    				len2++;
    			}
    			
    			if (multi1 == multi2 && sum1==sum2 && len1==len2)
    			{
    				System.out.println("permuted");
    			}
    		}while((multi1!=multi2) || (sum1!=sum2) || (len1!=len2));
    		System.out.println(num1);
    	}
    }


    Meeka try kra mchn.. eeth samahara numericals walata awl bn..