Home > Enterprise >  Question regarding BigInteger and Lambda Usage in this specific application
Question regarding BigInteger and Lambda Usage in this specific application

Time:11-24

BigInteger_Question

Sample_Output

I have no idea how to proceed with the code after what I have as attached. I wanted to try looping through the BigInteger N with a foreach but it doesn't seem to work. Next, I wanted to do a for(int i = 0), etc. loop but I do not know how to get location reference to a BigInteger variable. I don't know how to use lambda for this.

Thanks in advance for answering my queries.

import java.math.BigInteger;
import java.util.Random;

interface BigOperation{
    public BigInteger operation(BigInteger x,BigInteger y);
}

class LearningBigInteger {
    static BigInteger product(BigInteger n,BigOperation op1, BigOperation op2, BigOperation op3){
    }

    public static void main(String[] args) {
        BigOperation multiply = BigInteger::multiply;
        BigOperation divide = BigInteger::divide;
        BigOperation remainder = BigInteger::remainder;
        Random r = new Random();
        BigInteger n = BigInteger.valueOf(Math.abs(r.nextInt()));
        BigInteger m = product(n,multiply,divide,remainder);
    }
}

CodePudding user response:

First the code. Explanations appear after the code.

import java.math.BigInteger;

public class LearningBigInteger {
    private static BigOperation multiply;
    private static BigOperation divide;
    private static BigOperation remainder;

    private static BigInteger digitProduct(BigInteger x) {
        if (x.compareTo(BigInteger.TEN) <= 0) {
            return x;
        }
        else {
            BigInteger r = remainder.operation(x, BigInteger.TEN);
            BigInteger newX = divide.operation(x, BigInteger.TEN);
            return multiply.operation(r, digitProduct(newX));
        }
    }

    public static void main(String[] args) {
        multiply = (x, y) -> x.multiply(y);
        divide = (x, y) -> x.divide(y);
        remainder = (x, y) -> x.remainder(y);
        BigInteger x = new BigInteger("8584803");
        BigInteger product = BigInteger.ONE;
        while (x.compareTo(BigInteger.TEN) >= 0) {
            BigInteger r = remainder.operation(x, BigInteger.TEN);
            product = multiply.operation(product, r);
            x = divide.operation(x, BigInteger.TEN);
        }
        System.out.println(product);
        System.out.println(digitProduct(new BigInteger("12345")));
    }
}

interface BigOperation {
    public BigInteger operation(BigInteger x,BigInteger y);
}

Interface BigOperation is referred to as a functional interface because it contains exactly one [abstract] method.

A lambda expression is a shorthand way of writing an implementation of the sole method in a functional interface.

The way to write a lambda expression is to first write the list of parameters for the method, followed by the arrow symbol and finally followed by the method body. The syntax varies slightly depending on the number of method parameters and the number of lines in the method body. In the above code, the syntax is for a method with two parameters and the method body is just one line. The syntax would be different if either there was just one method parameter or if there were more than one line in the method body.

The code in your question uses method references which, I suppose, are a special form of lambda expression.

In the above code, method main contains the iterative approach while method digitProduct implements the recursive approach.

CodePudding user response:

class LearningBigInteger {
    // Iterable version
    static BigInteger product(BigInteger n, BigOperation... ops) {
        BigInteger temp = n;

        for (BigOperation op : ops) {
            // I only have temp to work it
            // so it is being processed
            // to itself, pointless i know!
            temp = op.operation(temp, temp);
        }

        return temp;
    }

    // Discrete version
    static BigInteger product(BigInteger n,BigOperation op1, BigOperation op2, BigOperation op3){
        BigInteger temp = n;

        // This layout is good
        // if you keep the same temp
        // variable as input!
        temp = op1.operation(temp, temp);
        temp = op2.operation(temp, temp);
        temp = op3.operation(temp, temp);

        return temp;
    }
}

Also I can't open the links you provided. Notify me when fixed!

CodePudding user response:

Try this.

interface BigOperation {
    BigInteger operation(BigInteger x, BigInteger y);
}

static class Iterative {
    static BigInteger product(BigInteger x, BigOperation op1, BigOperation op2, BigOperation op3) {
        BigInteger product = BigInteger.ONE;
        for ( ; x.compareTo(BigInteger.ZERO) > 0; x = op2.operation(x, BigInteger.TEN))
            product = op1.operation(product, op3.operation(x, BigInteger.TEN));
        return product;
    }
}

static class Recursive {
    static BigInteger product(BigInteger x, BigOperation op1, BigOperation op2, BigOperation op3) {
        if (x.compareTo(BigInteger.TEN) < 0)
            return x;
        else
            return op1.operation(op3.operation(x, BigInteger.TEN),
                product(op2.operation(x, BigInteger.TEN), op1, op2, op3));
    }
}

and

static void test(BigInteger x, BigOperation op1, BigOperation op2, BigOperation op3) {
    System.out.println("Given bit integer "   x);
    System.out.println("(Iterative) "   Iterative.product(x, op1, op2, op3));
    System.out.println("(Recursive) "   Recursive.product(x, op1, op2, op3));
}

public static void main(String[] args) {
    BigOperation multiply = BigInteger::multiply;
    BigOperation divide = BigInteger::divide;
    BigOperation remainder = BigInteger::remainder;
    test(new BigInteger("8584803"), multiply, divide, remainder);
    test(new BigInteger("12345"), multiply, divide, remainder);
}

output:

Given bit integer 8584803
(Iterative) 0
(Recursive) 0
Given bit integer 12345
(Iterative) 120
(Recursive) 120
  • Related