I have an integer X and a list of all the factors of X. I want to output the ratio between X and a factor, and for each time set X to that ratio. I want this algorithm to continue till the ratio is bigger than or equal to 1.
For example: X = 36.
All factors (but 1 and 36): 2, 3, 4, 6, 9, 12, 18,
Algorithm: 36 / 2 = 18 --> 18 / 3 = 6 --> 6 / 4 = 1.5 --> 1.5 / 6 < 1 --> Stop
Output: [18, 6, 1.5]. Question:
How do I get this output?
What i have written:
var arr = [];
for (var i = 2; i < X; i++) {
if (X % i == 0) {
arr.push(i);
}
}
var temp = [];
var index = 0;
while (X / arr[index] >= 1) {
index += 1;
X = X / arr[index];
temp.push(X / arr[index]);
}
var arr = [];
var X = 36
for (var i = 2; i < X; i++) {
if (X % i == 0) {
arr.push(i);
}
}
var temp = arr.reduce((acc, item, index) => {
if(!(X/item < 1)){
acc.push(X/item)
X = X/item;
}
return acc;
}, [])
console.log(temp)
You could calculate the first value before you go into the while loop an check the asssigned x. If the value passes the condition, you could push that value to the result array and increment the index and calculate a new value.
function fn(x) {
var array = [], // declare all variable on top
i,
result = [];
for (var i = 2; i < x; i++) {
if (x % i == 0) {
array.push(i);
}
}
i = 0; // prepare index for looping
x /= array[i]; // take a first possible value
while (x >= 1) { // check value
result.push(x); // push value
i++; // increment index
x /= array[i]; // take the next value for checking
}
return result;
}
console.log(fn(36))
A bit shorter with Array#every
function fn(x) {
var array = [], // declare all variable on top
i,
result = [];
for (var i = 2; i < x; i++) {
if (x % i == 0) {
array.push(i);
}
}
array.every(v => (x /= v) >= 1 && result.push(x));
return result;
}
console.log(fn(36))
Your question is a little bit confuse. Is this what you want?
Basically, I update the factor after each iteration (if >= 1) and store into an array the output.
// Generate a range of integers (starting at 2, and we should not including the input)
// get only values divisible by the input (filter)
const generateRange = (input) => {
return Array.from({length: (input - 2)}, (v, k) => k + 2).filter(i => {
return input % i === 0;
})
}
const divideFactors = (input) => {
const list = generateRange(input) // filtered range
let output = []
let factor = input
for (const item of list) {
let ratio = factor / item
if (ratio < 1) { //if ratio < 1, break the loop
break;
}
output.push(ratio) // update output array with new ratio
factor = ratio; // update factor variable with current ratio
}
return output
}
console.log(divideFactors(36))
More info:
Array.from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from
For of: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
Related
Question: Create a function that takes a positive integer and returns the next bigger number that can be formed by rearranging its digits. For example:
12 ==> 21
513 ==> 531
2017 ==> 2071
//nextBigger(num: 12) // returns 21
//nextBigger(num: 513) // returns 531
//nextBigger(num: 2017) // returns 2071
I am trying to compare two Array and get correct array as answer. In do...while loop I am comparing the two array by increment second array by one.
function nextBigger(n){
let nStrg = n.toString();
let nArr = nStrg.split('');
function compareArr(Ar1,Ar2){
if(Ar2.length>Ar1.length){
return false;
}
for(let i=0; i<Ar1.length; i++){
let num = Ar1[i];
for(let j=0; j<Ar2.length; j++){
if(Ar2.lastIndexOf(num) !== -1){
Ar2.splice(Ar2.lastIndexOf(num), 1);
break;
}
else{
return false;
break;
}
}
}
return true;
}
let nextNumArr;
let m = n;
do{
let nextNum = m+1
m=nextNum
let nextNumStrg = nextNum.toString();
nextNumArr = nextNumStrg.split('')
console.log(compareArr(nArr, nextNumArr))
}
while(compareArr(nArr, nextNumArr) == false)
console.log(nextNumArr)
return parseInt(nextNumArr.join())
}
nextBigger(12);
This gives me empty array at the end;
[2,0,1,7].join() will give you '2,0,1,7', can use [2,0,1,7].join('') and get '2017'
All looks a bit complicated. How about:
const nextLarger = num => {
const numX = `${num}`.split(``).map(Number).reverse();
for (let i = 0; i < numX.length; i += 1) {
if ( numX[i] > numX[i + 1] ) {
numX.splice(i, 2, ...[numX[i+1], numX[i]]);
return +(numX.reverse().join(``));
}
}
return num;
};
const test = [...Array(100)].map(v => {
const someNr = Math.floor(10 + Math.random() * 100000);
const next = nextLarger(someNr);
return `${someNr} => ${
next === someNr ? `not possible` : next}`;
}).join('\n');
document.querySelector(`pre`).textContent = test;
<pre></pre>
See also
function nextbig(number) {
let nums = []
number.toString().split('').forEach((num) => {
nums.push(parseInt(num))
})
number = nums
n = number.length
for (var i = n - 1; i >= 0; i--) {
if (number[i] > number[i - 1])
break;
}
if (i == 1 && number[i] <= number[i - 1]) {
return 'No greater possible'
}
let x = number[i - 1];
let smallest = i;
for (let j = i + 1; j < n; j++) {
if (number[j] > x &&
number[j] < number[smallest])
smallest = j;
}
let temp = number[smallest];
number[smallest] = number[i - 1];
number[i - 1] = temp;
x = 0
for (let j = 0; j < i; j++)
x = x * 10 + number[j];
number = number.slice(i, number.length + 1);
number.sort()
for (let j = 0; j < n - i; j++)
x = x * 10 + number[j];
return x
}
console.log(nextbig(12))
console.log(nextbig(513))
console.log(nextbig(2017))
In compareArr you are deleting elements as you find them, which is correct to do, to make sure duplicates actually occur twice etc. However, that also deletes the elements from nextNumArr in the calling context, because the array is passed by reference and not by value. You need to do a manual copy of it, for example like this: compareArr(nArr, [...nextNumArr]).
I have used a different approach, first I search for all possible combinations of the given numbers with the permutator function. This function returns an array of possible numbers.
Then I sort this array of combinations and look for the index of the given number in the main function.
Once I have this index I return the position before the given number.
function nextbig(num){
function permutator(inputArr){
let result = [];
const permute = (arr, m = []) => {
if (arr.length === 0) {
result.push(m)
} else {
for (let i = 0; i < arr.length; i++) {
let curr = arr.slice();
let next = curr.splice(i, 1);
permute(curr.slice(), m.concat(next))
}
}
}
permute(inputArr)
return result;
}
let arrNums = num.toString().split('')
let combinations = permutator(arrNums).map(elem => parseInt(elem.join("")))
combinations.sort((a, b) => {
return b - a
})
let indexOfNum = combinations.findIndex(elem => elem === num)
let nextBigIndex = indexOfNum <= 0 ? 0 : indexOfNum - 1
return (combinations[nextBigIndex])
}
console.log(nextbig(12))
console.log(nextbig(517))
console.log(nextbig(2017))
I need to write a prime factorisation function that returns an object whose keys are a prime factor and values are the exponents of the corresponding prime factors. E.g.
console.log(primeFactorisation(4)) // { 2: 2 }
console.log(primeFactorisation(6)) // { 2: 1, 3: 1 }
My code so far is below but I'm stuck. Any help much appreciated.
const primeFactorisation = (num) => {
let result = {};
for (let i = 2; i < num; i++) {
if (num % i === 0) {
result[i] = i;
num /= i;
}
}
return result
}
When a number is found, increment it on the result object instead of assigning i to the result object.
You also need a nested loop to keep testing for the number until it no longer divides evenly, and you also need i <= num, not i < num, so that the last factor is caught:
const primeFactorisation = (num) => {
let result = {};
for (let i = 2; i <= num; i++) {
while (num % i === 0) {
result[i] = (result[i] || 0) + 1;
num /= i;
}
}
return result
}
console.log(
primeFactorisation(6),
primeFactorisation(4),
);
I implemented the Largest Triple Products algorithm, but I use sort which makes my time complexity O(nlogn). Is there a way to implement it without a temporary sorted array?
The problem:
You're given a list of n integers arr[0..(n-1)]. You must compute a list output[0..(n-1)] such that, for each index i (between 0 and n-1, inclusive), output[i] is equal to the product of the three largest elements out of arr[0..i] (or equal to -1 if i < 2, as arr[0..i] then includes fewer than three elements).
Note that the three largest elements used to form any product may have the same values as one another, but they must be at different indices in arr.
Example:
var arr_2 = [2, 4, 7, 1, 5, 3];
var expected_2 = [-1, -1, 56, 56, 140, 140];
My solution:
function findMaxProduct(arr) {
// Write your code here
if(!arr || arr.length === 0) return [];
let helper = arr.slice();
helper.sort((a,b)=>a-b); // THIS IS THE SORT
let ans = [];
let prod = 1;
for(let i=0; i<arr.length; i++) {
if(i < 2) {
prod *= arr[i];
ans.push(-1);
}
else {
if(i === 3) {
prod *= arr[i];
ans.push(prod);
} else if(arr[i] < helper[0]) {
ans.push(prod);
} else {
const min = helper.shift();
prod /= min;
prod *= arr[i];
ans.push(prod);
}
}
}
return ans;
}
Thanks
You don't need to sort it. You just maintain an array of the largest three elements at each index.
For the first three elements it is simple you just assign the product of them to the third element in the result.
For the next elements, you add the current element to the three-largest-element-array and sort it and take the elements from 1 to 3 ( the largest three ) and assign the product of those at that index in result array. Then update the three-element-array with largest three.
Complexity :
This sort and slice of three-element-array should be O(1) because each time atmost 4 elements are there in the array.
Overall complexity is O(n).
You can do it as follows :
function findMaxProduct(arr) {
if(!arr) return [];
if (arr.length < 3) return arr.slice().fill(-1)
let t = arr.slice(0,3)
let ans = arr.slice().fill(-1,0,2) //fill first two with -1
ans[2] = t[0]*t[1]*t[2];
for(let i=3; i<arr.length; i++) {
t.push(arr[i]);
t = t.sort().slice(1,4);
ans[i] = t[0]*t[1]*t[2];
}
return ans;
}
I am keeping the array ordered (manually). Then just get the first 3 elements.
function findMaxProduct(arr) {
let results = [];
let heap = [];
for (let i = 0; i < arr.length; i++) {
// Insert the new element in the correct position
for (let j = 0; j < heap.length; j++) {
if (arr[i] >= heap[j]) {
heap.splice(j, 0, arr[i]);
break;
}
}
// No position found, insert at the end
if (heap.length != i + 1) {
heap.push(arr[i]);
}
if (i < 2) {
results.push(-1);
} else {
results.push(heap[0] * heap[1] * heap[2]);
}
}
return results;
}
You can make an array that holds three currently largest integers, and update that array as you passing through original array. That's how you will always have three currently largest numbers and you will be able to solve this with O(n) time complexity.
I think there's a faster and more efficient way to go about this. This is a similar thought process as #Q2Learn, using Python; just faster:
def findMaxProduct(arr):
#create a copy of arr
solution = arr.copy()
# make first 2 elements -1
for i in range(0,2):
solution[i] = -1
#for each item in copy starting from index 2, multiply item from 2 indices b'4 (notice how each index of arr being multiplied is reduced by 2, 1 and then 0, to accommodate each move)
for i in range(2, len(arr)):
solution[i] = arr[i-2] * arr[i-1] * arr[i]
return solution
check = findMaxProduct(arr)
print(check)
Single Scan Algorithm O(n)
We don't need to necessarily sort the given array to find the maximum product. Instead, we can only find the three largest values (x, y, z) in the given stage of iteration:
JavaScript:
function findMaxProduct(arr) {
let reults = []
let x = 0
let y = 0
let z = 0
for(let i=0; i<arr.length; i++) {
n = arr[i]
if (n > x) {
z = y
y = x
x = n
}
if (n < x && n > y) {
z = y
y = n
}
if (n < y && n > z) {
z = n
}
ans = x*y*z
if (ans === 0) {
results.push(-1)
} else {
results.push(ans)
}
return ans;
}
Python:
def findMaxProduct(arr):
results = []
if not arr:
return []
x = 0
y = 0
z = 0
for i, n in enumerate(arr):
if n > x:
z = y
y = x
x = n
if n < x and n > y:
z = y
y = n
if n < y and n > z:
z = n
ans = x*y*z
if ans == 0:
results.append(-1)
else:
results.append(ans)
print(results)
public int[] LargestTripleProducts(int[] input)
{
var ansArr = new int[input.Length];
var firstLargetst = input[0];
var secondLargetst = input[1];
ansArr[0] = ansArr[1] = -1;
for (int i = 2; i < input.Length; i++)
{
ansArr[i] = firstLargetst * secondLargetst * input[i];
if (firstLargetst < input[i] && firstLargetst < secondLargetst)
{
firstLargetst= input[i];
continue;
}
if (secondLargetst < input[i] && secondLargetst < firstLargetst)
{
secondLargetst= input[i];
}
}
return ansArr;
}
Python solution based on #SomeDude answer above. See explanation there.
def findMaxProduct(arr):
if not arr:
return None
if len(arr) < 3:
for i in range(len(arr)):
arr[i] = -1
return arr
three_largest_elem = arr[0:3]
answer = arr.copy()
for i in range(0, 2):
answer[i] = -1
answer[2] = three_largest_elem[0] * three_largest_elem[1] * three_largest_elem[2]
for i in range(3, len(arr)):
three_largest_elem.append(arr[i])
three_largest_elem = sorted(three_largest_elem)
three_largest_elem = three_largest_elem[1:4]
answer[i] = three_largest_elem[0] * three_largest_elem[1] * three_largest_elem[2]
return answer #Time: O(1) n <= 4, to Overall O(n) | Space: O(1)
Python has it's in-built package heapq, look at it for it.
Credit: Martin
> Helper function for any type of calculations
import math
> Heap algorithm
import heapq
> Create empty list to append output values
output = []
def findMaxProduct(arr):
out = []
h = []
for e in arr:
heapq.heappush(h, e)
if len(h) < 3:
out.append(-1)
else:
if len(h) > 3:
heapq.heappop(h)
out.append(h[0] * h[1] * h[2])
return out
Hope this helps!
I am trying to implement sumToOne(num) which sums a given integer’s digits repeatedly until the sum is only one digit - and return that one-digit result.
Example: sumToOne(928) returns 1, because 9+2+8 = 19, then 1+9 = 10, then 1+0 = 1.
Everything is working fine, but I wasn't able to call back recursion(sum);.
Could anyone point out what I am missing cause I am new in recursive function logic, please ?
function sumToOne(num) {
const nxNum = String(num).split('');
const length = nxNum.length;
let sum = 0;
let recursion = (sum) => {
for (let i = 0; i < length; i++) {
sum += + +nxNum[i];
}
if (sum < 10) {
return sum;
}
recursion(sum);
}
}
sumToOne(928);
Sometimes having an inner function helps with recursion, but not in this case.
If the number passed as an argument has at least two digits, we can use our function again and only return the actual number (breaking recursion) once it's just one digit (< 10):
function sumToOne(num) {
if (num < 10) return num;
const nxNum = String(num).split('');
const length = nxNum.length;
let sum = 0;
for (let i = 0; i < length; i++) {
sum += +nxNum[i];
}
return sumToOne(sum);
}
Notice that you can convert this function to a one-liner by using Array.prototype.reduce (if readability is not crucial):
const sumToOne = n => n < 10 ? n : sumToOne(String(n).split('').map(Number).reduce((s, d) => s + d));
Variables declared with the let keyword can have Block Scope.
Variables declared inside a block {} cannot be accessed from outside the block:
Example
{
let x = 2;
}
// x can NOT be used here
Try your code using the var keyword.
Recursive solutions exist everywhere!
const sumToOne = (n = 0) =>
n < 10
? n
: sumToOne(sum(digits(n)))
const sum = ([ n = 0, ...more ]) =>
more.length === 0
? n
: n + sum(more)
const digits = (n = 0) =>
n < 10
? [ n ]
: [ ...digits(Math.floor(n / 10)), n % 10 ]
console.log(sumToOne(928)) // 1
console.log(sumToOne(111)) // 3
console.log(sumToOne(999)) // 9
I'm trying to come up with a solution where I need to roll a number of dice (all of the same size) and come to a specified number. Provided I have all the validation in place to make sure the numbers are valid and could theoretically arrive at the desired result, does anyone have a good algorithm for solving this? Note it should appear random, not just a straight divide.
Some examples
roll 3 d6 and get 14 -> so it could output 5,3,6 or 6,6,2
roll 4 d20 and get 66 -> so it could output 16,14,19,17
I need a generic function that can accept a dice of any size, any amount to be rolled and the desired result.
My initial attempt is below, though this doesn't produce the desired output (you can ignore the mod for now, this was to also allow modifiers). This example is also missing the validation that the desired output is achievable,but that's not part of the question.
let desired = 19
let mod = 0
let dm = desired - mod
let n = 5;// number of dice
let d = 6 // dice sides
let nums = []
for(i =0; i< n; i++) {
nums.push(Math.round(Math.random() * Math.round(d)) + 1)
}
let sum = nums.reduce((acc,val) => acc + val)
nums = nums.map(a => Math.round((a/sum) * dm))
let diff = dm - (nums.reduce((acc,val) => acc + val))
function recursive(diff) {
let ran = nums[Math.random() * Math.round(nums.length -1)]
if(nums[ran] + diff > d || nums[ran] + diff < 1) {
recursive(diff)
} else {
nums[ran] += diff
}
}
while(diff != 0) {
recursive(diff)
diff += diff < 0 ? 1 : -1;
}
alert(nums)
recursive:
function foo(desired, rolls, sides, current) {
if (rolls === 0) {
return current.reduce((s, c) => s + c) === desired ? current : null;
}
const random = [];
for (let i = 1; i <= sides; i++) {
const randomIndex = Math.floor(Math.random() * (random.length + 1))
random.splice(randomIndex, 0, i);
}
for (const n of random) {
const result = foo(desired, rolls - 1, sides, [...current, n]);
if (result) {
return result;
}
}
}
console.log(foo(14, 3, 6, []))
non-recursive:
function foo(desired, rolls, sides) {
const stack = [[]];
while (stack.length) {
const current = stack.pop();
const random = [];
for (let i = 1; i <= sides; i++) {
const randomIndex = Math.floor(Math.random() * (random.length + 1));
random.splice(randomIndex, 0, i);
}
for (const n of random) {
if (current.length === rolls - 1) {
if (current.reduce((s, c) => s + c + n) === desired) {
return [...current, n];
}
} else {
stack.push([...current, n]);
}
}
}
}
console.log(foo(14, 3, 6));
non-recursive with minimum memory consumption:
function foo(desired, rolls, sides) {
const currentIndexes = Array(rolls).fill(0);
const randoms = Array.from({ length: rolls }, () => {
const random = [];
for (let i = 1; i <= sides; i++) {
const randomIndex = Math.floor(Math.random() * (random.length + 1));
random.splice(randomIndex, 0, i);
}
return random;
})
while (true) {
if (currentIndexes.reduce((s, idx, i) => s + randoms[i][idx], 0) === desired) {
return currentIndexes.map((idx, i) => randoms[i][idx]);
}
for (let i = currentIndexes.length - 1; i >= 0; i--) {
if (currentIndexes[i] < sides - 1) {
currentIndexes[i] += 1;
break;
}
currentIndexes[i] = 0;
}
}
}
console.log(foo(14, 3, 6));
non-recursive solution with minimum memory consumption and increased performance by calculating the last roll based on previous rolls.
function foo(desired, rolls, sides) {
const currentIndexes = Array(rolls - 1).fill(0);
const randoms = Array.from({ length: rolls - 1 }, () => {
const random = [];
for (let i = 1; i <= sides; i++) {
const randomIndex = Math.floor(Math.random() * (random.length + 1));
random.splice(randomIndex, 0, i);
}
return random;
})
while (true) {
const diff = desired - currentIndexes.reduce((s, idx, i) => s + randoms[i][idx], 0);
if (diff > 0 && diff <= sides) {
return [...currentIndexes.map((idx, i) => randoms[i][idx]), diff];
}
for (let i = currentIndexes.length - 1; i >= 0; i--) {
if (currentIndexes[i] < sides - 1) {
currentIndexes[i] += 1;
break;
}
currentIndexes[i] = 0;
}
}
}
console.log(foo(66, 4, 20));
Soluton in ruby:
def foo(count, dim, desired, results = [])
return results if count == 0
raise ArgumentError if count > desired
raise ArgumentError if count * dim < desired
max_roll = (dim <= desired - count) ? dim : desired - count + 1
min_roll = [(desired - (count-1) * dim), 1].max
roll = (rand(min_roll..max_roll))
results << roll
foo(count - 1, dim, desired - roll, results)
results
end
puts foo(3, 6, 11).inspect
puts foo(2, 6, 11).inspect
puts foo(4, 4, 11).inspect
Results:
[3, 4, 4]
[5, 6]
[2, 3, 4, 2]
So basically it is recursive function. For each step:
roll a dice (within allowed range min_roll..max_roll)
call same function but reduce count by already consumed number by dice and extend results array by value of roll
Note one thing: with this behaviour you may have larger numbers in the beginning of result. To avoid this just shuffle result of function in the end of it