skip to Main Content

Does anybody know how to convert a string to a BigInt without using the BigInt() constructor or any other libs like math etc?

So, my task is to implement arithmetic operations on strings without relying on bigint or arithmetic libraries. Can someone give me a hint?
Input is a valid string with a positive number inside, output should be bigint.

ChatGPT give me an answer with BigInt().
I tried to use typeOf() operator (I don`t know am I allowed to use it).

2

Answers


  1. Chosen as BEST ANSWER

    I finally got my answer approved by my mentor.

    String.prototype.plus = function(str) {
        let i = this.length - 1;
        let k = str.length - 1;
        let result = '';
        let carry = 0;
        
        while (i >= 0 || k >= 0 || carry != 0) {
            const dg1 = i >= 0 ? parseInt(this[i]) : 0;
            const dg2 = k >= 0 ? parseInt(str[k]) : 0;
            const sum = dg1 + dg2 + carry;
            carry = parseInt(sum / 10);
            result = (sum % 10) + result;
            i--;
            k--;
        }
        
        return result;
    }
    
    String.prototype.minus = function (str) {
        let i = this.length - 1;
        let k = str.length - 1;
        let result = '';
        let remind = 0;
    
        while (i >= 0 || remind != 0) {
            const dg1 = parseInt(this[i]);
            const dg2 = k >= 0 ? parseInt(str[k]) : 0;
            let sum = dg1 - dg2 - remind;
            if (sum < 0) {
                sum = (10 + dg1) - dg2 - remind
                remind = 1
            }else {
                remind = 0
            }
            result = (sum % 10) + result;
            i--;
            k--;
        }
        return result.replace(/^0*(?=[0-9])/g, '');
    }
    
    String.prototype.divide = function (str) {
    
        let oparatedDigits = '';
        let result = '';
        
        for (let i = 0; i < this.length; i++) {
            oparatedDigits += this[i];
            let count = 0;
    
            while (parseInt(oparatedDigits) >= parseInt(str)) {
                oparatedDigits = oparatedDigits.minus(str); 
                count++;
            }
            result += count;
        }
        return result.replace(/^0*(?=[0-9])/g, '');
    }
    
    String.prototype.multiply = function (str) {
        let i = str.length - 1;
     
        let carry = 0; 
        let partialMiltiplication = 0;
        let subresult = '';
        let result = '';
    
        while ( i >= 0 ) {
            const dgSTR = i >= 0 ? +str[i] : 0;
            let k = this.length - 1;
            subresult = '';
    
            while (k >= 0){
    
                const dgTHIS = k >= 0 ? +this[k] : 0;
    
                partialMiltiplication = dgSTR*dgTHIS + carry;
                carry = parseInt(partialMiltiplication /10)
                subresult = (partialMiltiplication % 10) + subresult 
               
                k--;
            } 
      
            subresult = subresult + '0'.repeat(str.length - 1 -i)
            result = result.plus(subresult);
            
            i--;
            
        }
        return result
    }
        
    console.log(('23546345634563456346534565346'.plus('23546345634563456346534565346')))
    console.log(('23546345634563456346534565346'.minus('23546345634563456346534565346')))
    console.log('23546345634563456346534565346'.multiply('23546345634563456346534565346'))
    console.log('23546345634563456346534565346'.divide('23546345634563456346534565346'))


  2. You mean something like this?

    NOTE: The numbers in the examples are exceeding MAX_SAFE_INTEGER

    const addLargeNumbers = (strNum1, strNum2) => {
      // Start from the end of both strings
      let result = '';
      let carry = 0;
      let i = strNum1.length - 1;
      let j = strNum2.length - 1;
    
      while (i >= 0 || j >= 0 || carry > 0) {
        const digit1 = i >= 0 ? parseInt(strNum1.charAt(i), 10) : 0;
        const digit2 = j >= 0 ? parseInt(strNum2.charAt(j), 10) : 0;
    
        const sum = digit1 + digit2 + carry;
        result = (sum % 10) + result;
        carry = Math.floor(sum / 10);
    
        i--;
        j--;
      }
    
      return result;
    };
    console.log(addLargeNumbers("19007199254740991","29007199254740991"))
    
    const compareLargeNumbers = (strNum1, strNum2) => {
      strNum1 = strNum1.replace(/^0+/, '');
      strNum2 = strNum2.replace(/^0+/, '');
      
      if (strNum1.length > strNum2.length) return 1;
      if (strNum1.length < strNum2.length) return -1;
      
      for (let i = 0; i < strNum1.length; i++) {
        if (strNum1[i] > strNum2[i]) return 1;
        if (strNum1[i] < strNum2[i]) return -1;
      }
      
      return 0;
    };
    
    console.log(compareLargeNumbers("19007199254740991","19007199254740991")); // same === 0
    console.log(compareLargeNumbers("19007199254740991","29007199254740991")); // str1 < str2:  -1
    console.log(compareLargeNumbers("29007199254740991","19007199254740991")); // str2 < str1:  1
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search