what type of big o notation is this algorithm? I need help double checking - big-o

can someone tell me what type of o notation this algorithm is, i think its O(n^2) but i want to make sure im getting this right
function uniqueQuadratic(words) {
const unique = []; // * = O(?)
for (
let i = 0; // * = O(?)
i < words.length; // * = O(?)
i++ // * = O(?)
) {
const word = words[i]; // * = O(?)
let isUnique = true; // * = O(?)
for (
let c = 0; // * = O(?)
c < i; // * = O(?)
c++ // * = O(?)
) {
const comparing = words[c]; // * = O(?)
if (comparing === word) { // * = O(?)
isUnique = false; // * = O(?)
}
}
if (isUnique) { // * = O(?)
unique[unique.length] = word; // * = O(?)
}
}
return unique;

Yes it’s O(n^2) because it has an outer loop that iterates n times and an inner loop that also iterates n times, resulting in n * n iterations. The O(n^2) notation is an algorithm whose time complexity is directly proportional to the square of the size of the input.

function uniqueQuadratic(words) {
const unique = []; // * = O(1)
for (
let i = 0; // *
i < words.length; // *
i++ // * = O(words.lengh)
) {
const word = words[i]; // * = O(1)
let isUnique = true; // * = O(1)
for (
let c = 0; // *
c < i; // *
c++ // * = O(words.length)
) {
const comparing = words[c]; // * = O(1)
if (comparing === word) { // * = O(1)
isUnique = false; // * = O(1)
}
}
if (isUnique) { // * = O(1)
unique[unique.length] = word; // * = O(1)
}
}
return unique;
Result O(n2) where n = words.length

Related

Goertzel complex signal

I have a Goertzel algorithm realization in C. And it work correct for not complex signal.
float goertzel(int numSamples,int TARGET_FREQUENCY,int SAMPLING_RATE, cufftDoubleComplex* modData,bool Im)
{
double Z = M_PI * 2. * (double(TARGET_FREQUENCY) / double(SAMPLING_RATE));
double constantaA = 2 * cos(Z);
double TMPVAR_v0 = 0;
double TMPVAR_v1 = 0;
double TMPVAR_v2 = 0;
double resultat;
for(int n =0; n < numSamples; n++) {
if(!Im)
TMPVAR_v0 = (modData[n].x + constantaA * TMPVAR_v1 - TMPVAR_v2);
else
TMPVAR_v0 = (modData[n].y + constantaA * TMPVAR_v1 - TMPVAR_v2);
TMPVAR_v2=TMPVAR_v1;
TMPVAR_v1=TMPVAR_v0;
}
resultat = (TMPVAR_v1*TMPVAR_v1 + TMPVAR_v2*TMPVAR_v2 - constantaA * TMPVAR_v1 * TMPVAR_v2);
return resultat;
}
But i can not understand how i can use it for complex signal.
Please, help me.....

Hash a Set of Integers from a Domain into a Set of Buckets

Say I have a set of integers ranging between 1-100. I will only have 5 of these integers drawn out of a hat. I want to then take those 5 integers and place them into 5 buckets guaranteed unique (without having to deduplicate or anything using something like quadratic probing). Wondering how to do that.
For example, say I have these numbers (random from 1-100):
1 5 20 50 100
I then want to take those numbers and place them into these 5 buckets:
a b c d e
Using some hash function to accomplish it. For example, perhaps like this:
hash(1) -> b
hash(5) -> a
hash(20) -> e
hash(50) -> d
hash(100) -> c
Wondering how to write the hash function so that it takes a number x from a domain of numbers D and a set of numbers D(X) from that domain, and outputs 1 bucket b from the set of buckets B.
H : D(X) -> B
Next time around I might have 6 numbers between 1 and 1,000, going into 6 buckets. So then I would need a new hash function that works using those constraints (6 numbers, 6 buckets, range 1-1,000).
The goal is as few steps as possible.
Note: The hash function for this example won't take integers in a domain larger than 10,000 lets say, as well as the size of the set of integers limited to some small number too like 1,000.
Update
Basically I am trying to get this to happen:
// var domain = [1, 2, ..., 100]
// var set = [1, 5, 20, 50, 100]
// var buckets = [1, 2, 3, 4, 5]
hash(1) // 2
hash(5) // 1
hash(20) // 5
hash(50) // 4
hash(100) // 3
function hash(integer) {
if (integer == 1) return 2
if (integer == 5) return 1
if (integer == 20) return 5
if (integer == 50) return 4
if (integer == 100) return 3
}
But I don't know how to construct that hash function dynamically.
One solution (in JavaScript) would be to just create a map like this:
var map = {
1: 2,
5: 1,
20: 5,
50: 4,
100: 3
}
But that's sort of cheating because the object in JavaScript is implemented as a hashtable underneath (or something like that). So I am looking for how to do this at a low level, just using basically what assembly gives you.
Pretty much, I want to do this:
1
5 |
| | 20
| | 50 |
| | 100 | |
[ slot1, slot2, slot3, slot4, slot5 ]
Where 1 is somehow "hashed" to go into that slot2 in an array of size 5 (that slot is arbitrary for this example), etc.
Suppose the domain of your integer values is the range from 0 to n-1, and you want the set of values [x0, x1, ..., xk-1] to map to values from 0 to k-1.
Create an array of n values containing the numbers from 0 to k-1 in roughly equal amounts, for example [a0 = 0, a1 = 1, ..., ak = 0, ..., an = n%k].
Then for each of the k values in the initial set (xi, where i = 0 .. k-1), change the k-th element of this array to i, either by direct assignment or by swapping with a value from elsewhere (taking care not to clobber a value set for a previous element of the initial set).
Then to hash a value y, just fetch the y-th value from this array.
DEMO
Here's a Javascript demo that basically implements the above algorithm, except that instead of pre-filling the array with values from 0 to k-1, it first inserts the hash values for the selected items, then fills the remaining items with the repeating sequence of numbers from 0 to k-1. You will probably get better collision resistance by using a random sequence instead of incrementing values, but I hope you get the picture.
var hash_array;
function generate_hash() {
var i, j, k;
var v = document.getElementById;
var n = document.getElementById("n").value;
// Create a new hash lookup table
hash_array = Array(n);
// Initialize every value to -1
for (i=0; i<n; i++) hash_array[i] = -1;
// Map the given values to the first k hash buckets
var initial_values = document.getElementById("init").value.split(/ +/);
k = initial_values.length;
for (i=0; i<k; i++) {
hash_array[initial_values[i]] = i;
}
// Fill the remaining buckets with values from 0 to k-1
// This could be done by selecting values randomly, but
// here we're just cycling through the values from 0 to k-1
for (i=j=0; i<hash_array.length; i++) {
if (hash_array[i] == -1) {
hash_array[i] = j;
j = (j + 1) % k;
}
}
document.getElementById("gen").innerHTML = "Hash lookup table:<br>" + hash_array.join(", ");
}
<h2>Demo</h2>
<p>Creating a hash function that works on integer values less than <i>n</i>. What is the value of <i>n</i>?<br>
<input type="number" id="n" min="6" max="100" value="20"/></p>
<p>Enter a few different values separated by spaces. These will hash to the first buckets<br/>
<input type="text" size="40" id="init" value="2 3 5 6 9"/></p>
<p id="gen"><button onclick="generate_hash(); return false">Generate hash table</button></p>
Something like this should work:
Create a set of bucket IDs and populate it ahead of hashing (assumption here is that set guarantees uniqueness). This means that you have to know in advance how many buckets you want.
For each element from the input set calculate hash(element) modulo bucketIds.size to find index of the next ID to use.
Remove the resulting bucket ID from the set of bucked IDs
Repeat (until you are done or the set of IDs is exhausted)
Feel free to inspect the noddy implementation in JS using arrays (Node8).
If you'd like a function that's not a straight map, you could also experiment with Polynomial Regression.
Here's a JavaScript example using some free code under the GNU license.
/***************************************************************************
* Copyright (C) 2018 by Paul Lutus *
* lutusp#arachnoid.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
// classic Gauss-Jordan matrix manipulation functions
var gj = gj || {}
gj.divide = function(A, i, j, m) {
for (var q = j + 1; q < m; q++) {
A[i][q] /= A[i][j];
}
A[i][j] = 1;
}
gj.eliminate = function(A, i, j, n, m) {
for (var k = 0; k < n; k++) {
if (k != i && A[k][j] != 0) {
for (var q = j + 1; q < m; q++) {
A[k][q] -= A[k][j] * A[i][q];
}
A[k][j] = 0;
}
}
}
gj.echelonize = function(A) {
var n = A.length;
var m = A[0].length;
var i = 0;
var j = 0;
var k;
var swap;
while (i < n && j < m) {
//look for non-zero entries in col j at or below row i
k = i;
while (k < n && A[k][j] == 0) {
k++;
}
// if an entry is found at row k
if (k < n) {
// if k is not i, then swap row i with row k
if (k != i) {
swap = A[i];
A[i] = A[k];
A[k] = swap;
}
// if A[i][j] is != 1, divide row i by A[i][j]
if (A[i][j] != 1) {
gj.divide(A, i, j, m);
}
// eliminate all other non-zero entries
gj.eliminate(A, i, j, n, m);
i++;
}
j++;
}
}
// a simple data class
function Pair(x,y) {
this.x = x;
this.y = y;
};
Pair.prototype.toString = function() {return x + ',' + y};
// matrix functions
var matf = matf || {}
// a weak substitue for printf()
matf.number_format = function(n,p,w) {
s = n.toExponential(p);
while(s.length < w) {
s = ' ' + s;
}
return s;
}
// produce a single y result for a given x
matf.regress = function(x, terms) {
var y = 0;
var m = 1;
for (var i = 0; i < terms.length;i++) {
y += terms[i] * m;
m *= x;
}
return y;
}
// compute correlation coefficient
matf.corr_coeff = function(data, terms) {
var r = 0;
var n = data.length;
var sx = 0;
var sx2 = 0, sy = 0, sy2 = 0, sxy = 0;
var x, y;
for (var i = 0;i < data.length;i++) {
pr = data[i];
var x = matf.regress(pr.x, terms);
var y = pr.y;
sx += x;
sy += y;
sxy += x * y;
sx2 += x * x;
sy2 += y * y;
}
var div = Math.sqrt((sx2 - (sx * sx) / n) * (sy2 - (sy * sy) / n));
if (div != 0) {
r = Math.pow((sxy - (sx * sy) / n) / div, 2);
}
return r;
}
// compute standard error
matf.std_error = function(data, terms) {
var r = 0;
var n = data.length;
if (n > 2) {
var a = 0;
for (var i = 0;i < data.length;i++) {
pr = data[i];
a += Math.pow((matf.regress(pr.x, terms) - pr.y), 2);
}
r = Math.sqrt(a / (n - 2));
}
return r;
}
// create regression coefficients
// for provided data set
// data = pair array
// p = polynomial degree
matf.compute_coefficients = function(data, p) {
p += 1;
var n = data.length;
var r, c;
var rs = 2 * p - 1;
//
// by request: read each datum only once
// not the most efficient processing method
// but required if the data set is huge
//
// create square matrix with added RH column
m = Array();
for (var i = 0; i < p; i++) {
mm = Array();
for (var j = 0; j <= p; j++) {
mm[j] = 0;
}
m[i] = mm;
}
//double[][] m = new double[p][p + 1];
// create array of precalculated matrix data
mpc = Array();
for(var i = 0;i < rs;i++) {
mpc[i] = 0;
}
mpc[0] = n;
for (var i = 0;i < data.length;i++) {
pr = data[i];
// process precalculation array
for (r = 1; r < rs; r++) {
mpc[r] += Math.pow(pr.x, r);
}
// process RH column cells
m[0][p] += pr.y;
for (r = 1; r < p; r++) {
m[r][p] += Math.pow(pr.x, r) * pr.y;
}
}
// populate square matrix section
for (r = 0; r < p; r++) {
for (c = 0; c < p; c++) {
m[r][c] = mpc[r + c];
}
}
// reduce matrix
gj.echelonize(m);
// extract result column
terms = Array();
for (var i = 0;i < m.length;i++) {
mc = m[i];
terms[i] = mc[p];
}
return terms;
}
// test the system using known data
matf.test = function() {
var xd = [-1,0,1,2,3,5,7,9];
var yd = [-1,3,2.5,5,4,2,5,4];
data = Array();
for(var i = 0;i < xd.length;i++) {
data[i] = new Pair(xd[i],yd[i]);
}
terms = compute_coefficients(data,6);
var prec = 16;
var width = 24;
for(var i = 0;i < terms.length;i++) {
print(number_format(terms[i],prec,width) + ' * x^' + i);
}
cc = corr_coeff(data,terms);
print ('cc = ' + number_format(cc,prec,width));
se = std_error(data,terms);
print('se = ' + number_format(se,prec,width));
}
//test();
// "data" is an array of Pair(x,y) data
// p = polynomial degree
matf.process_data = function(data,p) {
var terms = matf.compute_coefficients(data,p);
var cc = matf.corr_coeff(data,terms);
var se = matf.std_error(data,terms);
return [terms,cc,se];
}
/**** END Paul Lutus' code ****/
function f(cs, x){
let n = cs.length - 1;
let result = 0;
for (let i=0; i<cs.length; i++)
result += cs[i] * Math.pow(x, i);
return result;
}
var data = [[1,1], [5,2], [20,3], [50,4], [100,5]];
var xy_data = []
for (let i of data)
xy_data.push(new Pair(i[0], i[1]));
var result = matf.process_data(xy_data, xy_data.length - 1);
for (let i=0; i<data.length; i++)
console.log(data[i][0], f(result[0], data[i][0]));

Is there a way to determine m based on the heuristic theorem given this implementation?

Bloom proposed the technique for applications where the amount of source data would require an impractically large amount of memory if "conventional" error-free hashing techniques were applied. He gave the example of a hyphenation algorithm for a dictionary of 500,000 words, out of which 90% follow simple hyphenation rules, but the remaining 10% require expensive disk accesses to retrieve specific hyphenation patterns.
public class BloomFilter {
int m;
int k;
HashSet<String> map = new HashSet<>();
public BloomFilter(){
int c = 100;
float e = 0.1f;
m = (int) Math.floor( -1 * c * Math.log(e) / (Math.log(2)*Math.log(2)) ) + 1;
k = (int) Math.floor( 0.7 * m / (float) c ) + 1;
}
private static int[] createHashes(String key, int hashes, int m) {
byte[] data = key.getBytes();
int[] result = new int[hashes];
MessageDigest digestFunction;
try {
digestFunction = MessageDigest.getInstance("MD5");
} catch (Exception e) {
throw new RuntimeException();
}
int k = 0;
byte salt = 0;
while (k < hashes) {
byte[] digest;
digestFunction.update(salt);
salt++;
digest = digestFunction.digest(data);
for (int i = 0; i < digest.length / 4 && k < hashes; i++) {
int h = 0;
for (int j = (i * 4); j < (i * 4) + 4; j++) {
h <<= 8;
h |= ((int) digest[j]) & 0xFF;
}
result[k] = Math.abs(h % m);
k++;
}
}
return result;
}
public void add(String s){
map.add(Arrays.toString(createHashes(s, k, m)));
}
public boolean contains(String s){
return map.contains(Arrays.toString(createHashes(s, k, m)));
}
}
From Wikipedia bloom filter
m = - (n ln p )/((ln 2)^2)
This means that for a given false positive probability p, the length of a Bloom filter m is proportionate to the number of elements being filtered n.[5] While the above formula is asymptotic (i.e. applicable as m,n → ∞), the agreement with finite values of m,n is also quite good;
So m - the number of bits committed, is based on the required false rate.

Algo to calculate sum of sub-rectangles which allows updates as well

I have a matrix MAT[M][N] and have q queries.
In each query I have one of these 2 types of operations
1. Update MAT[x][y] with k where x,y and k will be given
2. return sum of sub rectangle where left top corner indices are (x1,y1) and right bottom indices are (x2,y2).
Ex-
Mat -
1 1 1 1
1 1 1 1
1 1 1 1
queries
type 1,2,2,5
type 2,1,1,3,3 => 13(output)
A standard solution to this problem is a two-dimensional binary index tree. It can perform both required operations in O(log n * log m) time per query and it occupies O(n * m) additional space(where n and m are the dimensions of the given matrix). It is efficient and relatively easy to implement. Here is my code:
/**
* Instances of this class represent a two-dimensional binary index tree.
*
* They support the following operations:
* 1. Updating the value of one element.
* 2. Finding the sum of all elements in a specified rectangle.
*
* The time complexity of both operation is O(log n * log m), where n and m
* are the initial matrix dimensions.
*/
template<class T>
class Bit2D {
public:
/**
* Creates a new instance of this class using the matrix as the initial
* matrix.
* The matrix must be non-empty.
*/
Bit2D(const std::vector<std::vector<T>>& matrix): _matrix(matrix) {
_rowsCount = matrix.size();
_colsCount = matrix[0].size();
_sum.assign(_rowsCount, std::vector<T>(_colsCount, 0));
for (int row = 0; row < _rowsCount; row++) {
for (int col = 0; col < _colsCount; col++) {
_update(row, col, _matrix[row][col]);
}
}
}
/**
* Returns the sum of all elements in the
* ((lowerRow, lowerCol), (upperRow, upperCol)) rectangle.
* All bounds are inclusive.
* lowerRow must be not greater than upperRow.
* lowerCol must be not greater than upperCol.
*
* If any of these four values is outside the bounds of the initial matrix,
* undefined behavior is invoked.
*/
T getSum(int lowerRow, int lowerCol, int upperRow, int upperCol) const {
return _getSum(upperRow, upperCol) - _getSum(upperRow, lowerCol - 1)
- _getSum(lowerRow - 1, upperCol)
+ _getSum(lowerRow - 1, lowerCol - 1);
}
/**
* Sets the value of the (row, col) element to newValue.
*
* If row or col is outside the bounds of the initial matrix, undefined
* behavior is invoked.
*/
void update(int row, int col, T newValue) {
_update(row, col, newValue - _matrix[row][col]);
_matrix[row][col] = newValue;
}
private:
std::vector<std::vector<T>> _matrix;
std::vector<std::vector<T>> _sum;
int _rowsCount;
int _colsCount;
int _getPrevious(int index) const {
return (index & (index + 1)) - 1;
}
int _getNext(int index) const {
return index | (index + 1);
}
T _getSum(int upperRow, int upperCol) const {
T res = 0;
for (int row = upperRow; row >= 0; row = _getPrevious(row)) {
for (int col = upperCol; col >= 0; col = _getPrevious(col)) {
res += _sum[row][col];
}
}
return res;
}
void _update(int row, int col, T delta) {
for (int curRow = row; curRow < _rowsCount; curRow = _getNext(curRow)) {
for (int curCol = col; curCol < _colsCount; curCol = _getNext(curCol)) {
_sum[curRow][curCol] += delta;
}
}
}
};

Algorithm for finding smallest number with given number of factors

What's the most efficient algorithm anyone can think of that, given a natural number n, returns the least natural number x with n positive divisors (including 1 and x)? For example, given 4 the algorithm should result in 6 (divisors: 1,2,3,6); i.e. 6 is the smallest number having 4 distinct factors. Similarly, given 6, the algorithm should result in 12 (divisors: 1,2,3,4,6,12); i.e. 12 is the smallest number having 6 distinct factors
In terms of real-world performance, I'm looking for a scalable algorithm which can give answers of the order of 1020 within 2 seconds on a machine which can do 107 computations per second.
http://www.primepuzzles.net/problems/prob_019.htm
b) Jud McCranie, T.W.A. Baumann & Enoch Haga sent basically the same
procedure to find N(d) for a given d:
Factorize d as a product of his prime divisors: d = p1a1 * p2a2 *p3a3 *...
convert this factorization in another arithmetically equivalent factorization, composed of non-powered monotonically decreasing and not
necesarilly prime factors... (uf!...) d = p1a1 * p2a2 *p3a3 *... =
b1 * b2 * b3... such that b1 ≥ b2 ≥ b3...
You must realize that for every given d, there are several
arithmetically equivalent factorizations that can be done: by example:
if d = 16 = 24 then there are 5 equivalent factorizations:
d = 2*2*2*2 = 4*2*2 = 4*4 = 8*2 = 16
N is the minimal number resulting of computing 2b1-1 * 3b2-1 * 5b3-1 * ... for all the equivalent factorizations of d. Working the same example:
N(16) = the minimal of these {2 * 3 * 5 * 7, 23 * 3 * 5, 23 * 33, 27 * 3, 215} = 23 * 3 * 5 = 120
Update: With numbers around 1020, pay attention to the notes by Christian Bau quoted on the same page.
//What is the smallest number with X factors?
function smallestNumberWithThisManyFactors(factorCount) {
Number.prototype.isPrime = function() {
let primeCandidate = this;
if(primeCandidate <= 1 || primeCandidate % 1 !== 0) return false
let i = 2;
while(i <= Math.floor(Math.sqrt(primeCandidate))){
if(primeCandidate%i === 0) return false;
i++;
}
return true;
}
Number.prototype.nextPrime = function() {
let currentPrime = this;
let nextPrimeCandidate = currentPrime + 1
while(nextPrimeCandidate < Infinity) {
if(nextPrimeCandidate.isPrime()){
return nextPrimeCandidate;
} else {
nextPrimeCandidate++;
}
}
}
Number.prototype.primeFactors = function() {
let factorParent = this;
let primeFactors = [];
let primeFactorCandidate = 2;
while(factorParent !== 1){
while(factorParent % primeFactorCandidate === 0){
primeFactors.push(primeFactorCandidate);
factorParent /= primeFactorCandidate;
}
primeFactorCandidate = primeFactorCandidate.nextPrime();
}
return primeFactors;
}
Number.prototype.factors = function() {
let parentNumber = this.valueOf();
let factors = []
let iterator = parentNumber % 2 === 0 ? 1 : 2
let factorCandidate = 1;
for(factorCandidate; factorCandidate <= Math.floor(parentNumber/2); factorCandidate += iterator) {
if(parentNumber % factorCandidate === 0) {
factors.push(factorCandidate)
}
}
factors.push(parentNumber)
return factors
}
Array.prototype.valueSort = function() {
return this.sort(function (a,b){ return a-b })
}
function clone3DArray(arrayOfArrays) {
let cloneArray = arrayOfArrays.map(function(arr) {
return arr.slice();
});
return cloneArray;
}
function does3DArrayContainArray(arrayOfArrays, array){
let aOA = clone3DArray(arrayOfArrays);
let a = array.slice(0);
for(let i=0; i<aOA.length; i++){
if(aOA[i].sort().join(',') === a.sort().join(',')){
return true;
}
}
return false;
}
function removeDuplicateArrays(combinations) {
let uniqueCombinations = []
for(let c = 0; c < combinations.length; c++){
if(!does3DArrayContainArray(uniqueCombinations, combinations[c])){
uniqueCombinations[uniqueCombinations.length] = combinations[c];
}
}
return uniqueCombinations;
}
function generateCombinations(parentArray) {
let generate = function(n, src, got, combinations) {
if(n === 0){
if(got.length > 0){
combinations[combinations.length] = got;
}
return;
}
for (let j=0; j<src.length; j++){
generate(n - 1, src.slice(j + 1), got.concat([src[j]]), combinations);
}
return;
}
let combinations = [];
for(let i=1; i<parentArray.length; i++){
generate(i, parentArray, [], combinations);
}
combinations.push(parentArray);
return combinations;
}
function generateCombinedFactorCombinations(primeFactors, primeFactorCombinations) {
let candidates = [];
for(let p=0; p<primeFactorCombinations.length; p++){
let product = 1;
let primeFactorsCopy = primeFactors.slice(0);
for(let q=0; q<primeFactorCombinations[p].length; q++){
product *= primeFactorCombinations[p][q];
primeFactorsCopy.splice(primeFactorsCopy.indexOf(primeFactorCombinations[p][q]), 1);
}
primeFactorsCopy.push(product);
candidates[candidates.length] = primeFactorsCopy.valueSort().reverse();
}
return candidates;
}
function determineMinimumCobination (candidates){
let minimumValue = Infinity;
let bestFactorCadidate = []
for(let y=0; y<candidates.length; y++){
let currentValue = 1;
let currentPrime = 2;
for(let z=0; z<combinedFactorCandidates[y].length; z++){
currentValue *= Math.pow(currentPrime,(combinedFactorCandidates[y][z])-1);
currentPrime = currentPrime.nextPrime();
}
if(currentValue < minimumValue){
minimumValue = currentValue;
bestFactorCadidate = combinedFactorCandidates[y];
}
}
return minimumValue;
}
let primeFactors = factorCount.primeFactors();
let primeFactorCombinations = removeDuplicateArrays(generateCombinations(primeFactors));
let combinedFactorCandidates = generateCombinedFactorCombinations(primeFactors, primeFactorCombinations);
let smallestNumberWithFactorCount = determineMinimumCobination(combinedFactorCandidates);
console.log('The smallest number with ' + factorCount + ' factors is: ')
console.log(smallestNumberWithFactorCount)
console.log('With these factors being: ')
console.log(smallestNumberWithFactorCount.factors())
return smallestNumberWithFactorCount;
}
smallestNumberWithThisManyFactors(10)

Resources