Results 1 to 10 of 10

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Street Racer Rain9333's Avatar
    Join Date: Feb:2007
    Location: Bg, Gb
    Posts: 824

    Java int to array[]

    , , . :

    Code:
     ,     n!   n 
     [1..100].        
     ,   100!       long/int.  
          ,      
    .   512      {2, 1, 5}.
          ,    
     (       
      ).
    :

    Code:
    import java.util.Scanner;
    
    
    public class Factorial {
    
    	public static void method(int n){
    		int sumLength;
    		int[] sum = new int[1];
    		sum[0]=1;
    		for (int i=1;i<=n;i++){
    			
    			multiply(sum,i);
    			sumLength = (multiply(sum,i)).length;
    			sum= new int[sumLength];
    			for (int k=0;k<sumLength;k++ ){
    				sum[k] = multiply(sum,i)[k];
    			}
    			
    		System.out.print("\nFactorial of "+i+" is: ");
    		for (int numbers: sum){	
    			System.out.print(numbers);
    			}	
    		}
    		
    	}
    	
    	
    	public static int[] revString (int[] array){
    		
    		int length = array.length;
    		int[] reversed = new int[length];
    		for (int index = 0; index < length; index++) {
    			
    			reversed[length - index - 1] = array[index];
    		}
    		return reversed;
    	}
    	
    	public static int[] multiply(int[] array, int num){
    	
    		int temp, arrayLength;
    		arrayLength = array.length;
    		int[] multiplyTemp = new int [arrayLength];
    		int[] reverse = new int [arrayLength];
    		revString(array);
    		for (int k=0;k<arrayLength;k++ ){
    			reverse[k] = revString(array)[k];
    		}
    		
    		for (int i=0;i<arrayLength;i++){
    			
    			temp = reverse[i];
    			multiplyTemp[i] = temp*num;
    		}
    		
    		return multiplyTemp;
    	}
    	
     	public static void main(String[] args) {
    		
    		Scanner input = new Scanner (System.in);
    		System.out.print("Enter factorial of: ");
    		int facNum = input.nextInt();
    		method(facNum);
    		
    	}
    
    }
    MSI X570-A PRO, AMD 5800X3D, Noctua NH-D15, Corsair Vengeance LPX 2x8GB-3600, ASUS RTX ROG 3080 Strix White OC, Samsung 980 Pro 1TB NVMe, Corsair RM750, FD Define R5, Dell 32" 4k G3223Q 144Hz + 3xSamsung G5 32" 144hz, SimLab P1-X, Simucube 2 Pro, HE Ultimate, SRB GT3 Wheel

  2. #2
    Registered User
    Join Date: Dec:2007
    Location: Sofia
    Posts: 366
    , - - 2 , .
    , / . . .

  3. #3
    Street Racer Rain9333's Avatar
    Join Date: Feb:2007
    Location: Bg, Gb
    Posts: 824
    Quote Originally Posted by Pheoman View Post
    , - - 2 , .
    , / . . .
    :

    Code:
    public static int[] add(int[] num1, int[]num2){
     		
     		int len;
     		int num1Len = num1.length;
     		System.arraycopy(revArray(num1),0,num1,0,num1Len);
     		int num2Len = num2.length;
     		System.arraycopy(revArray(num2),0,num2,0,num2Len);
     		if (num1Len>num2Len){
     			len=num1Len;
     		}
     		else{
     			len=num2Len;
     		}
     		int[] result= new int[len];
     		int temp;
     		int carry=0;
     		for(int i=0;i<len;i++){
     			if((i+1)>num1Len){
     				temp = (0+num2[i]);
     			}
     			else if ((i+1)>num2Len){
     				temp = (num1[i]+0);
     			}
     			else {
     				temp = (num1[i]+num2[i]);
     			}
     			System.out.println("temp "+i+" e : "+temp);
     			if((temp>8)&&(carry==1)){
     				result[i]=0;
     				carry=1;
     			}
     			else if((temp>9)&&(carry==0)){
     				temp=temp+carry;
     				result[i]=temp%10;
     				carry = 1;
     			}
     			else{
     				temp=temp+carry;
     				result[i]=(temp%10);
     				carry = 0;
     			}
     		}
     			
     		return(revArray(result));
     	}
    revArray / revString Array, String/. , -, - . / /. String 123 -> array[3]={1,2,3} .
    MSI X570-A PRO, AMD 5800X3D, Noctua NH-D15, Corsair Vengeance LPX 2x8GB-3600, ASUS RTX ROG 3080 Strix White OC, Samsung 980 Pro 1TB NVMe, Corsair RM750, FD Define R5, Dell 32" 4k G3223Q 144Hz + 3xSamsung G5 32" 144hz, SimLab P1-X, Simucube 2 Pro, HE Ultimate, SRB GT3 Wheel

  4. #4
    Registered User
    Join Date: Dec:2007
    Location: Sofia
    Posts: 366
    int[] , , ArrayList<int>.

    - , , ( ASCII '0' '9' '0', 0 9).

    - , , '0', '0' '9', .

  5. #5
    Street Racer Rain9333's Avatar
    Join Date: Feb:2007
    Location: Bg, Gb
    Posts: 824
    Quote Originally Posted by Pheoman View Post
    int[] , , ArrayList<int>.

    - , , ( ASCII '0' '9' '0', 0 9).

    - , , '0', '0' '9', .
    , . ArrayList<int>,
    MSI X570-A PRO, AMD 5800X3D, Noctua NH-D15, Corsair Vengeance LPX 2x8GB-3600, ASUS RTX ROG 3080 Strix White OC, Samsung 980 Pro 1TB NVMe, Corsair RM750, FD Define R5, Dell 32" 4k G3223Q 144Hz + 3xSamsung G5 32" 144hz, SimLab P1-X, Simucube 2 Pro, HE Ultimate, SRB GT3 Wheel

  6. #6

    Join Date: Sep:2005
    Location: Sofia
    Posts: 18,517
    Quote Originally Posted by Rain9333 View Post
    , , . :

    Code:
            
     ,   100!       long/int.
    java.math.BigInteger. . .
    Last edited by vvvlado; 27th May 2010 at 22:40.

  7. #7
    Street Racer Rain9333's Avatar
    Join Date: Feb:2007
    Location: Bg, Gb
    Posts: 824
    , / , /lists /. / /.

    @Pheoman / / - /100% /. , , .

    Code:
    public static void add(int[] num1, int[]num2){
     		
     		int len;
     		int num1Len = num1.length;
     		System.arraycopy(revArray(num1),0,num1,0,num1Len);
     		int num2Len = num2.length;
     		System.arraycopy(revArray(num2),0,num2,0,num2Len);
     		
     		if (num1Len>num2Len){
     			len=num1Len;
     		}
     		else{
     			len=num2Len;
     		}
     		
     		ArrayList<Integer> result = new ArrayList<Integer>();
     		int temp;
     		int carry=0;
     		for(int i=0;i<len;i++){
     			if((i+1)>num1Len){
     				temp = (0+num2[i]);
     			}
     			else if ((i+1)>num2Len){
     				temp = (num1[i]+0);
     			}
     			else {
     				temp = (num1[i]+num2[i]);
     			}
     			
     			if((temp>8)&&(carry==1)&&(i==len-1)){
     				temp=temp+carry;
     				while (temp!=0){
     					result.add(temp%10);
     					temp = temp/10;
     				}
     				
     			}
     			
     			else if((temp>8)&&(carry==1)){
     				result.add(0);
     				carry=1;
     			}
     			else if((temp>9)&&(carry==0)){
     				temp=temp+carry;
     				result.add(temp%10);
     				carry = 1;
     			}
     			else{
     				temp=temp+carry;
     				result.add(temp%10);
     				carry = 0;
     			}
     		}
     		
     		System.out.print("Sum of these numbers is: ");
     		int resultLen = result.size();
     		for (int i=0; i<resultLen; i++) {
     			System.out.print(result.get(resultLen-i-1));
     		}
     	}
    MSI X570-A PRO, AMD 5800X3D, Noctua NH-D15, Corsair Vengeance LPX 2x8GB-3600, ASUS RTX ROG 3080 Strix White OC, Samsung 980 Pro 1TB NVMe, Corsair RM750, FD Define R5, Dell 32" 4k G3223Q 144Hz + 3xSamsung G5 32" 144hz, SimLab P1-X, Simucube 2 Pro, HE Ultimate, SRB GT3 Wheel

  8. #8

    Join Date: Sep:2005
    Location: Sofia
    Posts: 18,517


    Code:
        BigInteger big = new BigInteger("1");
        int factoriel = 100;
        for (Integer i = 1; i<=factoriel; i++) {
          BigInteger bi = new BigInteger(i.toString());
          big = big.multiply(bi);
        }
        System.out.println(big);

  9. #9
    Street Racer Rain9333's Avatar
    Join Date: Feb:2007
    Location: Bg, Gb
    Posts: 824
    vvvlado, . , / . 100 double . , 30000 / big integer , /.
    EDIT : Unbounded range.
    MSI X570-A PRO, AMD 5800X3D, Noctua NH-D15, Corsair Vengeance LPX 2x8GB-3600, ASUS RTX ROG 3080 Strix White OC, Samsung 980 Pro 1TB NVMe, Corsair RM750, FD Define R5, Dell 32" 4k G3223Q 144Hz + 3xSamsung G5 32" 144hz, SimLab P1-X, Simucube 2 Pro, HE Ultimate, SRB GT3 Wheel

  10. #10
    Registered User
    Join Date: Mar:2011
    Location:
    Posts: 3
    .
    , .

    Code:
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class CalculateFactorial {
    	public static int[] multiplicationArrayToNumber(int number,int...array){
    		int arrayLength=array.length;
    		int[] resultArray=new int[arrayLength];//      
    		for(int i=0;i<arrayLength;i++){ 
    			resultArray[arrayLength-1-i]=array[arrayLength-1-i]*number; // -     
    		}
    		return resultArray; 
    	}
    	
    	public static int[] becomeElementInArrayToSingleInteger(int...array){ //       
    		int arrayLength=array.length;										//       - 
    		int ofMind=0;														//5+7=12,  2  1  :))
    		for(int i=0;i<arrayLength;i++){										//       
    			array[arrayLength-1-i]=array[arrayLength-1-i]+ofMind;
    			ofMind=array[arrayLength-1-i]/10;
    			array[arrayLength-1-i]=array[arrayLength-1-i]%10;
    		}
    		return array;
    	}
    	public static int countZeroBeforNumber(int...array){ //     
    		int counter=0;									//         
    		for(int i=0;i<array.length-1;i++){
    			if(array[i]==0){
    				counter++;
    				continue;
    			}else{
    				break;
    			}
    		}
    		return counter;
    	}
    	public static String convertArrayToString(int number,int...array){ //       
    		int inArrayLength=array.length;									//-  ,    
    		int outArrayLength=array.length-number;							//      (  )
    		int[] arrayWithoutZero=new int[outArrayLength];					//     
    		for(int i=0;i<outArrayLength;i++){
    			arrayWithoutZero[outArrayLength-1-i]=array[inArrayLength-1-i];
    		}
    		String realValue="";
    		for(int i=0;i<outArrayLength;i++){
    			realValue+=arrayWithoutZero[i];
    		}
    		
    		return realValue;
    	}
    	
    	public static void main(String[] args) {
    		Scanner input=new Scanner(System.in);
    		System.out.print("Enter number's length, example - 4 maximum value is 9999 ! ");
    		int length=input.nextInt();
    		int[] targetArray=new int[length]; //           
    		targetArray[targetArray.length-1]=1;
    		System.out.print("Enter to which number calculate factorial: ");
    		int n=input.nextInt();
    		for(int i=1;i<=n;i++){
    			targetArray=multiplicationArrayToNumber(i,targetArray);
    			int countZero=countZeroBeforNumber(becomeElementInArrayToSingleInteger(targetArray));
    			String result=convertArrayToString(countZero,targetArray);
    			System.out.printf("Factorial from %d! is %s\n",i,result);
    		}	
    	}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |