Javascript multiplying strings - javascript

I was doing following leetcode question on multiplying
Given two non-negative integers num1 and num2 represented as strings, return the
product of num1 and num2, also represented as a string.
Note: You must not use any built-in BigInteger library or convert the inputs to
integer directly.
Example 1:
Input: num1 = "2", num2 = "3"
Output: "6"
Example 2:
Input: num1 = "123", num2 = "456"
Output: "56088"
Constraints:
- 1 <= num1.length, num2.length <= 200
- num1 and num2 consist of digits only.
- Both num1 and num2 do not contain any leading zero, except the number 0 itself.
For this used the following approach
Convert the string to int
Multiply the int
Algo for the same is
const numberMap = {
"0": 0,
"1": 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9
}
var multiply = function(num1, num2) {
let i = num1.length
let j = num2.length
let sum = currentPower = 0
let firstNumber = secondNumber = 0
while(i > 0 || j > 0) {
// if I or J is equal to zero, means we have itterated hence we will set the value to one
const firstNum = i > 0 ? (numberMap[num1[i-1]]) * (10**currentPower) : 0
const secondNum = j > 0 ? (numberMap[num2[j-1]]) * (10**currentPower) : 0
firstNumber += firstNum
secondNumber += secondNum
currentPower++
i--
j--
}
sum = firstNumber * secondNumber
return sum.toString()
};
but when the following input is given
"123456789"
"987654321"
it yields the following output "121932631112635260" instead of "121932631112635269"
Any idea how I can fix this?

You could multiply each digit with each other digit and take the indices as position.
Like you would do by hand, like
1 2 3 4 * 4 3 2 1
-------------------------
1 2 3 4
1 4 6 8
3 6 9 12
4 8 12 16
-------------------------
5 3 2 2 1 1 4
This approach uses the reversed arrays of the strings and reverse the result set as well.
Before retuning the result, the array is filterd by the leading zeros.
function multiply(a, b) {
var aa = [...a].reverse(),
bb = [...b].reverse(),
p = [],
i, j;
for (i = 0; i < aa.length; i++) {
for (j = 0; j < bb.length; j++) {
if (!p[i + j]) p[i + j] = 0;
p[i + j] += aa[i] * bb[j];
if (p[i + j] > 9) {
if (!p[i + j + 1]) p[i + j + 1] = 0;
p[i + j + 1] += Math.floor(p[i + j] / 10);
p[i + j] %= 10;
}
}
}
return p
.reverse()
.filter((valid => (v, i, { length }) => valid = +v || valid || i + 1 === length)(false))
.join('');
}
console.log(multiply('2', '3')); // 6
console.log(multiply('123', '456')); // 56088
console.log(multiply('9133', '0')); // 0
console.log(multiply('9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999', '9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999'));

function multiply() {
var str = document.getElementById('a').value;
var str1 = document.getElementById('b').value;
var l = str.length;
var l1 = str1.length;
var list = [];
var prod = 0;
var cur = 0;
list.length = l + l1;
// store 0 to to all list elements
list.fill(0);
// access string in reverse
for (var i = l - 1; i >= 0; i--) {
for (
var j = l1 - 1;
j >= 0;
j-- // access string in reverse
) {
// ASCII value of character and - ("0") ASCII Value 48
prod = (str.charCodeAt(i) - 48) * (str1.charCodeAt(j) - 48);
cur = list[i + j + 1] + prod;
list[i + j + 1] = cur % 10;
list[i + j] += parseInt(cur / 10);
}
}
var res = '';
res = list.join('');
if (res[0] == 0) {
// if First Char ==0 then remove
res = res.slice(1);
}
console.log(res);
}

var multiply = function(num1, num2) {
let v1=BigInt(num1)
let v2=BigInt(num2)
let v3=v1*v2
return v3.toString()
};

Just convert to Numbers, multiply, and then convert back to a string.
function mult(a, b){
return (Number(a) * Number(b)).toString();
}
In JavaScript, ints are not a specific type. They're the number type. We could use the parseInt() function instead of the Number() function to convert, but we know we're receiving an int as an input anyway, so there's no need for that kind of parsing.

Related

Having bugs in digPow function even I have tested it alot from codewars

Hello I want to ask what's wrong with my solution when I am gonna test the solution it appears that expected -1 to equal 12933
The whole instructions are from codewors
The Question:
Some numbers have funny properties. For example:
89 --> 8¹ + 9² = 89 * 1
695 --> 6² + 9³ + 5⁴= 1390 = 695 * 2
46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
Given a positive integer n written as abcd... (a, b, c, d... being digits) and a positive integer p
we want to find a positive integer k, if it exists, such that the sum of the digits of n taken to the successive powers of p is equal to k * n.
In other words:
Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k
If it is the case we will return k, if not return -1.
Note: n and p will always be given as strictly positive integers.
digPow(89, 1) should return 1 since 8¹ + 9² = 89 = 89 * 1
digPow(92, 1) should return -1 since there is no k such as 9¹ + 2² equals 92 * k
digPow(695, 2) should return 2 since 6² + 9³ + 5⁴= 1390 = 695 * 2
digPow(46288, 3) should return 51 since 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
My Solution:
function digPow(p, n) {
let arrayP = [...String(p)].map((e) => Number(e));
let arrayPLength = arrayP.length;
let nums = [];
let firstResult = [];
for (let i = n; i < 5000; i++) {
nums.push(i);
}
for (let i = n; i < nums.length; i++) {
let currentPow = [];
let j = i;
let k = i + arrayPLength;
while (j < k) {
currentPow.push(j);
j++;
if (j == k) break;
}
let result = 0;
for (let h = 0; h < arrayP.length; h++) {
result = arrayP[h] ** currentPow[h] + result;
}
firstResult.push(result);
}
for (let i = 0; i < firstResult.length; i++) {
for (let j = 0; j < 100; j++) {
if (firstResult[i] == p * j) return j;
}
}
return -1;
}

find sum of multiples 3 and 5, JS

I'm given a number and I need to find the sum of the multiples of 3 and 5 below the number.
For example:
20 => 78 = 3 + 5 + 6 + 9 + 10 + 12 + 15 + 18
My code works, but not for numbers greater than 1,000,000 (I tested it for 100,000 - it gives the result with 2sec delay). So, it should be optimized. Could someone help me? Why is my code slow? Thanks.
My logic is as follows:
add multiples to an array
filter duplicate values
sum all values
my code:
function sumOfMultiples(number) {
let numberBelow = number - 1;
let numberOfThrees = Math.floor(numberBelow / 3);
let numberOfFives = Math.floor(numberBelow / 5);
let multiples = [];
let multipleOfThree = 0;
let multipleOfFive = 0;
for (var i = 0; i < numberOfThrees; i++) {
multiples.push(multipleOfThree += 3);
}
for (var j = 0; j < numberOfFives; j++) {
multiples.push(multipleOfFive += 5);
}
return multiples
.filter((item, index) => multiples.indexOf(item) === index)
.reduce((a, b) => a + b);
}
You can also do this without using any loops.
For example if N is 1000, the sum of all multiples of 3 under 1000 is 3 + 6 + 9 ..... 999 => 3( 1 + 2 + 3 .... 333)
Similarly for 5, sum is 5(1 + 2 + 3 .... 200). But we have to subtract common multiples like 15, 30, 45 (multiples of 15)
And sum of first N natural numbers is N*(N+1)/2;
Putting all of this together
// Returns sum of first N natural numbers
const sumN = N => N*(N+1)/2;
// Returns number of multiples of a below N
const noOfMulitples = (N, a) => Math.floor((N-1)/a);
function sumOfMulitples(N) {
const n3 = noOfMulitples(N, 3); // Number of multiples of 3 under N
const n5 = noOfMulitples(N, 5); // Number of multiples of 5 under N
const n15 = noOfMulitples(N, 15); // Number of multiples of 3 & 5 under N
return 3*sumN(n3) + 5*sumN(n5) - 15*sumN(n15);
}
You can just run a loop from 1 to number, and use the modulo operator % to check if i divides 3 or 5:
function sumOfMultiples(number) {
var result = 0;
for (var i = 0; i < number; i++) {
if (i % 5 == 0 || i % 3 == 0) {
result += i;
}
}
return result;
}
console.log(sumOfMultiples(1000));
console.log(sumOfMultiples(100000));
console.log(sumOfMultiples(10000000));
You can do that just using a single loop.
function sumOfMultiples(number) {
let sum = 0;
for(let i = 1; i < number; i++){
if(i % 3 === 0 || i % 5 === 0){
sum += i;
}
}
return sum;
}
console.time('t');
console.log(sumOfMultiples(100000))
console.timeEnd('t')
You can do something like this
Set the difference equal to 5 - 3
Start loop with current as 0, keep looping until current is less than number,
Add 3 to current in every iteration,
Add difference to current and check if it is divisible by 5 only and less than number, than add it final result,
Add current to final result
function sumOfMultiples(number) {
let num = 0;
let difference = 5 - 3
let current = 0
while(current < number){
current += 3
let temp = current + difference
if((temp % 5 === 0) && (temp %3 !== 0) && temp < number ){
num += temp
}
difference += 2
if(current < number){
num += current
}
}
return num
}
console.log(sumOfMultiples(20))
console.log(sumOfMultiples(1000));
console.log(sumOfMultiples(100000));
console.log(sumOfMultiples(10000000));
you can do something like this
function multiplesOfFiveAndThree(){
let sum = 0;
for(let i = 1; i < 1000; i++) {
if (i % 3 === 0 || i % 5 === 0) sum += i;
}
return sum;
}
console.log(multiplesOfFiveAndThree());

Minimum number of digit changes to get two arrays to have the same value

I am pretty new to coding and I'm trying my best, but after hours and hours of research i still cant figure this out. I'm trying to make these two separate arrays the same with the minimum number of moves. I can only ++ or -- one number at a time.
This is the challenge:
No reordering of the digits is allowed For example, consider two arrays: Andrea's [123, 543] and Maria's [321, 279]. For the first digit, Andrea can increment the 1 twice to achieve 3. The 2's are equal already. Finally, she decrements her 3 twice to equal 1. It took 4 moves to reach her goal. For the second integer, she decrements 5 three times, increments 4 three times and 3 six times. It took 12 moves to convert the second array element. In total, it took 16 moves to convert both values comprising the complete array.
let a = [1234, 4321]
let m = [2345, 3214]
function minimumMoves(a, m) {
// Write your code here
let numMoves = 0;
let num1 = '' ;
let num2 = '' ;
let digit1 = '';
let digit2= '';
for (let i = 0; i < a.length; i++)
{
num1 = a[i];
while (num1 != 0) {
digit1 = num1 % 10;
digit2 = num2 % 10;
num1 = Math.trunc(num1 / 10);
num2 = Math.trunc(num2 / 10);
numMoves = numMoves + Math.abs(digit1 - digit2);
}
}
return numMoves
}
For getting only the count for changina a string of digits to another, you could add the absolute delta of the digits at a place.
function count(a, b) {
return Array.from(a).reduce((s, v, i) => s + Math.abs(v - b[i]), 0);
}
console.log(count('123', '321'));
In my opinion you should make a function that effectively takes a single digit and while it is greater than the other number aka needs to be decremented it does that:
const incrementWhileNeeded = (target, currentValue) =>
Math.abs(target - currentValue)
Then, you need split the numbers into their digits (you can do this the mathematical way using % like it looks like you've done, but just for simplicity something like: String(num1).split('').map(Number) will take 451 and change it to [4, 5, 1].
Then, your next step is to map that function (incrementWhileNeeded) to each individual digit: just focus on the first number (and then apply forEach or .map to apply that function to all of them.
So that will look something like:
firstNumberArray.map(incrementWhileNeeded)
Which will respond with as you explained [1, 0, 2].
Then .reduce() this so that you can get the sum of the counts.
So this will reduce using [1,0,2].reduce((accumulator, current) => accumulator + current) to 3.
So for the full functionality:
const incrementWhileNeeded = (target, currentValue) =>
Math.abs(target - currentValue)
const calculateMinimumMoves = (fullNumber, targetNumber) => {
const numArray = String(fullNumber).split('').map(Number)
const targetArray = String(targetNumber).split('').map(Number)
const diffArray = numArray.map((currentElement, targetArray[index]) => incrementWhileNeeded(currentElement, targetArray[index])
return diffArray.reduce((accumulator, current) => accumulator + current, 0)
}
const minimumMoves = (array1, array2) =>
array1.reduce((accumulator, current, index) =>
accumulator + calculateMinimumMoves(current, array2[index]),
0)
Check this code out:
a = [1234, 4321]
b = [2345, 3214]
function minimumMoves(a, m) {
let numMoves1 = 0, numMoves2 = 0;
let num1 = '', num2 = '';
let digit1 = '', digit2 = '';
//Forward
for (let i = 0 ; i < a.length ; i++)
{
num1 = a[i];
num2 = m[i];
for (let j = 0 ; j < a.length ; j++)
{
digit1 = num1 % 10;
digit2 = num2 % 10;
numMoves1 += Math.abs(digit1-digit2);
num1 = (num1 - digit1) / 10;
num2 = (num2 - digit2) / 10;
}
}
//Backward
for (let i = 0 ; i < a.length ; i++)
{
num1 = m[i];
num2 = a[i];
for (let j = 0 ; j < a.length ; j++)
{
digit1 = num1 % 10;
digit2 = num2 % 10;
numMoves2 += Math.abs(digit1-digit2);
num1 = (num1 - digit1) / 10;
num2 = (num2 - digit2) / 10;
}
}
if (numMoves1>numMoves2)
{
//Answer is numMoves1
} else if (numMoves1<numMoves2)
{
//Answer is numMoves2
} else {
//Answer is any one, i.e, either numMoves1 or numMoves2
}
}
If you need quick verification for this code, navigate Here.
And then paste this code:
/******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
public class Main
{
public static void main(String[] args) {
Integer[] a = {1234, 4321};
Integer[] m = {2345, 3214};
Integer numMoves1 = 0, numMoves2 = 0;
Integer num1 = 0, num2 = 0;
Integer digit1 = 0, digit2 = 0;
//Forward
for (Integer i = 0 ; i < a.length ; i++)
{
num1 = a[i];
num2 = m[i];
for (Integer j = 0 ; j < a.length ; j++)
{
digit1 = num1 % 10;
digit2 = num2 % 10;
numMoves1 += Math.abs(digit1-digit2);
num1 = (num1 - digit1) / 10;
num2 = (num2 - digit2) / 10;
}
}
//Backward
for (Integer i = 0 ; i < a.length ; i++)
{
num1 = m[i];
num2 = a[i];
for (Integer j = 0 ; j < a.length ; j++)
{
digit1 = num1 % 10;
digit2 = num2 % 10;
numMoves2 += Math.abs(digit1-digit2);
num1 = (num1 - digit1) / 10;
num2 = (num2 - digit2) / 10;
}
}
if (numMoves1>numMoves2)
{
//Answer is numMoves1
} else if (numMoves1<numMoves2)
{
//Answer is numMoves2
} else
{
//Answer is any one, i.e, either numMoves1 or numMoves2
}
System.out.println(numMoves1 + " & " + numMoves2);
}
}
I hope this algorithm helps ;)
//This code works....
// Check this out ....
public class Main
{
public static void main(String[] args) {
Integer[] a = {1234, 4321};
Integer[] m = {2345, 3214};
Integer numMoves1 = 0;
Integer num1 = 0, num2 = 0;
Integer digit1 = 0, digit2 = 0;
//Forward
for (Integer i = 0 ; i < a.length ; i++)
{
num1 = a[i];
num2 = m[i];
while(num1>0)
{
digit1 = num1 % 10;
digit2 = num2 % 10;
numMoves1 += Math.abs(digit1-digit2);
num1 = (num1 - digit1) / 10;
num2 = (num2 - digit2) / 10;
}
}
System.out.println(numMoves1);
}
}
Here is the solution for finding the minimum moves to match each of the elements of the two different array.
let a = [1234, 4321]
let m = [2345, 3214]
function minimumMoves(a, m) {
// Write your code here
let numMoves = 0;
let num1 = '' ;
let num2 = '' ;
let digit1 = '';
let digit2= '';
for (let i = 0; i < a.length; i++)
{
num1 = a[i];
num2 = m[i];
while (num1 != 0) {
digit1 = num1 % 10;
digit2 = num2 % 10;
num1 = Math.trunc(num1 / 10);
num2 = Math.trunc(num2 / 10);
numMoves = numMoves + Math.abs(digit1 - digit2);
}
}
return numMoves;
}
console.log(minimumMoves(a, m));

Calculate the sum of positive values smaller or equal to a number

I am trying to calculate the sum of positive values smaller or equal to the entered number, for ex: 5 -> 1+2+3+4+5 = 15
I came up with this:
var num = Number(prompt("Enter a number "));
sum = 0;
i = num;
do {
sum = sum += i;
i--
document.write(sum);
} while (i > 0);
I don't understand what I am doing wrong.
i think this is correct code:
var num = Number(prompt("Enter a number "));
sum = 0;
i = num;
do
{
sum += i;
i--;
}
while (i > 0);
document.write(sum);
and i suggest you to use this formula : document.write((num * (num + 1)) / 2);
If you look closer to your task, you'll find out, that:
If Num = 1, the sequence to be summed is [1]
if Num = 2, the sequence is [1, 2]
if Num = 3, the sequence is [1, 2, 3]
You can imagine, that you have a square with sides equal to num, for example, when num = 4:
****
****
****
****
And you need to summ 1, 2, 3, 4:
***#
**##
*###
####
See? It's a square of a triangle.
It could be calculated by formula: num * (num + 1) / 2
So, you code could be:
var num = Number(prompt("Enter a number "));
document.write(num * (num + 1) / 2)
You are writing the sum on each loop instead you have to print it finally. If you want to print the numbers then keep it an array and join them with + symbol before writing. To make it in ascending order change the loop condition.
var num = Number(prompt("Enter a number "));
sum = 0;
i = 1;
nums = [];
do {
sum = sum += i;
nums.push(i++);
}
while (i <= num);
document.write(nums.join(' + ') + ' = ' + sum);
Do with increment instead of decrements.And also show result of sum outside of loop .Not with in loop.And create array to append increment value.Finally print with document.write
var num=Number(prompt("Enter a number "));
sum = 0;
i = 1;
var a=[];
do {
sum +=i;
a.push(i)
i++;
}
while (num >= i);
document.write(a.join('+')+'='+sum)
You should write the answer at the end of loop and make this simple sum += i;.
var num = Number(prompt("Enter a number"));
sum = 0;
i = num;
do {
sum += i;
i--;
}
while (i > 0);
document.write(sum);
var number = 5, // Your number
result = 0;
while ( number !== 0 ) {
result += number;
number--;
}
document.write(result);
Fast and precious solution.
Here is the case with complete check and display as you need: JAVA
public static void main ( String arg[]) {
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
System.out.println("Number entered : " + number);
int sum =1 ;
if(number > 1) {
int nextNumber = 1;
System.out.print(nextNumber);
do {
// sum of all the positive numbers
nextNumber++ ;
sum = nextNumber + sum;
System.out.print( " + " + nextNumber);
}while(nextNumber < number);
System.out.print(" = " + sum);
}
}
var num = Number(prompt("Enter a number"));
sum = 0;
for (i = num; i > 0; i--) {
sum += i;
}
document.write(sum);

Find absolute sum of minimum slice - codility [duplicate]

This question already has answers here:
Finding minimal absolute sum of a subarray
(11 answers)
Closed 4 years ago.
Hi I've taken Codility test twice and scored 0. Please help me in solving the issue using JavaScript.
A non-empty array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P ≤ Q < N, is called a slice of array A. The sum of a slice (P, Q) is the total of A[P] + A[P+1] + ... + A[Q].
A min abs slice is whose absolute sum is minimal.
For example, array A such that:
A[0] = 2
A[1] = -4
A[2] = 6
A[3] = -3
A[4] = 9
contains the following slice among others:
(0,1), whose absolute sum is = |2 + (-4)| = 2
(0,2), whose absolute sum is = |2 + (-4) + 6| = 4
(0,3), whose absolute sum is = |2 + (-4) + 6 + (-3)| = 1
(1,3), whose absolute sum is = |(-4) + 6 + (-3)| = 1
(1,4), whose absolute sum is = |(-4) + 6 + (-3) + 9| = 8
(4,4), whose absolute sum is = |9| = 9
Both slices (0,3) and (1,3) are min abs slice and their absolute sum equals 1.
Write a function:
function solution(A);
that, given a non-empty array A consisting of N integers, return the absolute sum of min abs slice.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..1,000,000];
each element of array A is an integer within the range [−10,000..10,000];
Here is my solution:
function solution(A, i = 0, sum = 0) {
const N = A.length;
if (N === 0) {
return 0;
}
if (N == 1) {
return Math.abs(A[0]);
}
A.sort();
// All positives
if (A[0] >= 0 && A[N - 1] >= 0) {
return Math.abs(A[0]);
}
// All Negatives
if (A[0] <= 0 && A[N - 1] <= 0) {
return Math.abs(A[N - 1]);
}
let currAbsSum = 0;
let minAbsSum = Number.MAX_SAFE_INTEGER;
for (var i = 0; i < N; i++) {
let j = N - 1;
while (j >= i) {
currAbsSum = Math.abs(A[i] + A[j]);
if (currAbsSum === 0) {
return 0;
}
minAbsSum = Math.min(currAbsSum, minAbsSum);
if (Math.abs(A[i]) > Math.abs(A[j])) {
i++;
} else {
j--;
}
}
if (A[i] > 0) break;
}
return minAbsSum;
}
Here is a javascript version of O(n log n) answer taken from here:
function solution(A) {
if (1 == A.length) return Math.abs(A[0]);
let sums = new Array(A.length + 1);
let minAbsSum = Number.MAX_SAFE_INTEGER;
sums[0] = 0;
for (var i = 0; i < A.length; i++) {
sums[i + 1] = A[i] + sums[i];
}
sums.sort();
for (var i = 1; i < sums.length; i++) {
minAbsSum = Math.min(minAbsSum, Math.abs(sums[i] - sums[i - 1]));
}
return minAbsSum;
}
console.log(solution([2, -4, 6, -3, 9]))
console.log(solution([10, 10, 10, 10, 10, -50]))

Categories

Resources