module.exports=function getZerosCount(number, base) {
let result = 0;
let delitel = base;
let mass = [1, 2, 3, 5, 7, 9, 11, 13];
let mass2 = [];
let num=0;
let obj = {};
let schetchik = 1;
let itog = [];
for (let i = 1; i < mass.length; i++) {
if (base / mass[i] == Math.floor(base / mass[i])) {
base /= mass[i];
mass2.push(mass[i]);
i = 0;
}
else if (i == mass.length - 1) {
mass2.push(base);
}
else if (base == delitel && i == mass.length - 1)
break;
else if (base == 1) {
break;
}
}
if (mass2.length == 1) {
obj[mass2[0]] = 1;
}
else
for (let i = 1; i < mass2.length + 1; i++) {
if (mass2[num] == mass2[i]) {
schetchik++;
continue;
}
else {
obj[mass2[num]] = schetchik;
num = i;
schetchik = 1;
}
}
for (key in obj) {
delitel=+key;
while (true) {
let dop = number / delitel;
dop = Math.floor(dop);
result += dop;
delitel *=+key;
if (delitel>number) {
break;
}
}
itog.push(Math.floor(result/obj[key]));
result=0;
}
if(itog.length==1)
{return itog[0]}
else
for(let i=0; i<itog.length-1;i++){
if(itog[i]<itog[i+1])
result=itog[i];
else result=itog[i+1];
}
return result;
}
module.exports = function getZerosCount(number, base) {
var multiplier = 2,
simpleMultipliers = [],
balance, zeroNum = [],
obj = {},
zeros = [];
while(multiplier <= base){
balance = base % multiplier;
if(!balance){
simpleMultipliers.push(multiplier);
base /= multiplier;
}
else multiplier++;
}
for(var i = 0; i < simpleMultipliers.length; i++){
var number1 = number;
zeroNum[i] = 0;
while(number1){
number1 = Math.floor(number1/simpleMultipliers[i]);
zeroNum[i] += number1;
}
}
zeroNum.forEach(function(a){
if (obj[a] != undefined)
obj[a]++;
else
obj[a] = 1;
});
for(var key in obj){
zeros.push(Math.floor(+key/obj[key]));
}
return Math.min.apply(null, zeros);
}
module.exports = function getZerosCount(number, base) {
const getMaxPower = (p, n) => {
let power = 0;
while (n / p > 0) {
power += Math.floor(n / p);
n /= p;
}
return power;
};
const getMaxPowerByQ = (p, n, q) => {
return Math.floor(getMaxPower(p, n) / q);
};
const processBase = (currentBase, iterBase) => {
let q = 0;
while (currentBase % iterBase == 0) {
q++;
currentBase /= iterBase;
}
return {q, b: currentBase};
};
let result = number;
let j = base;
for (let p = 2; p <= base; p++) {
if (j % p != 0) continue;
const {q, b} = processBase(j, p);
j = b;
result = Math.min(result, getMaxPowerByQ(p, number, q));
}
return result;
};
module.exports = function solveSudoku(matrix) {
let solution = matrix.map( arr => arr.slice() );
if(solveHelper(solution) == true){
return solution;
} else {
return false;
}
function solveHelper(solution){
let minPossibleCountCell = false;
while(true){
minPossibleCountCell = false;
for(let i = 0; i < solution.length; i++){
for(let j = 0; j < solution.length; j++){
if(solution[i][j] != 0) continue;
let possibleValues = findPossibleValues(i, j, solution);
let possibleValuesCount = possibleValues.length;
if(possibleValuesCount == 0){
return false;
}
if(possibleValuesCount == 1){
solution[i][j] = possibleValues[0];
continue;
}
if(minPossibleCountCell == false || possibleValuesCount < minPossibleCountCell[1].length){
minPossibleCountCell = [[i, j], possibleValues];
}
}
}
if(minPossibleCountCell == false){
return true;
} else if(minPossibleCountCell[1].length > 1){
if(findPossibleValues(minPossibleCountCell[0][0], minPossibleCountCell[0][1], solution).length == minPossibleCountCell[1].length){
break;
}
}
}
//Из вариативных решений пробуем каждое пока не получится
let row = minPossibleCountCell[0][0];
let column = minPossibleCountCell[0][1];
for(let iter = 0; iter < minPossibleCountCell[1].length; iter++){
let solutionCopy = solution.map( arr => arr.slice() );
solutionCopy[row][column] = minPossibleCountCell[1][iter];
if( solveHelper( solutionCopy ) ){
for(let j = 0; j < 9; j++){
for(let k = 0; k < 9; k++){
solution[j][k] = solutionCopy[j][k];
}
}
return true;
}
}
return false;
}
function findPossibleValues( rowIndex, columnIndex, matrix ){
let sourceNum = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let rowValues = getRowValues(rowIndex, matrix);
let columnValues = getColumnValues(columnIndex, matrix);
let blockValues = getBlockValues(rowIndex, columnIndex, matrix);
let complete = rowValues.concat( columnValues.concat(blockValues) );
complete.forEach(function(numCheck){
if( sourceNum.indexOf(numCheck) != -1){
sourceNum.splice( sourceNum.indexOf(numCheck), 1 );
}
})
return sourceNum;
}
function getRowValues( rowIndex, matrix ){
return matrix[rowIndex].filter( element => element > 0 );
}
function getColumnValues( columnIndex, matrix ){
let result = [];
for(let n = 0; n < matrix.length; n++){
if( matrix[n][columnIndex] == 0 ) continue;
result.push( matrix[n][columnIndex] );
}
return result;
}
function getBlockValues( rowIndex, columnIndex, matrix ){
let blockRowStart = 3 * Math.floor(rowIndex / 3);
let blockColumnStart = 3 * Math.floor(columnIndex / 3);
let result = [];
for(let m = blockRowStart; m < blockRowStart + 3; m++){
for(let l = blockColumnStart; l < blockColumnStart + 3; l++){
if( matrix[m][l] > 0 ) result.push (matrix[m][l]);
}
}
return result;
}
}
module.exports = function solveSudoku(matrix) {
var matxpro = matrix;
function passes(matrix) {
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] == 0) {
matrix[i][j] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
}
}
}
return matrix;
}
function forLine(matx) {
var arrPos = [];
for (let i = 0; i < matx.length; i++) {
for (let j = 0; j < matx[i].length; j++) {
if (typeof matx[i][j] == 'object') {
arrPos.push(j);
}
}
for (let j = 0; j < arrPos.length; j++) {
for (let l = 0; l < matx[i][arrPos[j]].length; l++) {
for (let k = 0; k < matx[i].length; k++) {
if (matx[i][arrPos[j]][l] == matx[i][k]) {
matx[i][arrPos[j]].splice(l, 1);
if (matx[i][arrPos[j]].length == 1) {
matx[i][arrPos[j]] = matx[i][arrPos[j]][0];
}
}
}
}
}
}
return matx;
}
function forColumn(matx) {
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
if (typeof matx[i][j] == "object") {
for (let g = 0; g < 9; g++) {
for (let k = 0; k < matx[i][j].length; k++) {
if (matx[i][j][k] == matx[g][j] && typeof matx[g][j] != "object") {
matx[i][j].splice(k, 1);
if (matx[i][j].length == 1) {
matx[i][j] = matx[i][j][0];
}
}
}
}
}
}
}
return matx;
}
return forColumn(forLine(passes(matxpro)));
}
module.exports = sudoku;
function sudoku(matrix) {
var sudo = [...matrix];
if (sudokuSolver(sudo, 0, 0)) {
return sudo;
}
}
function filled(matrix, i, j, val) {
for (var row = 0; row < 9; row++) {
if (matrix[row][j] === val) {
return false;
}
}
for (var col = 0; col < 9; col++) {
if (matrix[i][col] === val) {
return false;
}
}
var row = Math.floor(i / 3) * 3;
var col = Math.floor(j / 3) * 3;
for (var a = row; a < row + 3; a++) {
for (var b = col; b < col + 3; b++) {
if (matrix[a][b] === val) {
return false;
}
}
}
return true;
}
function sudokuSolver(matrix, i, j) {
if (i === 9) {
i = 0;
j = j + 1;
if (j === 9) {
return true;
}
}
if (matrix[i][j] != 0) {
return sudokuSolver(matrix, i+1, j);
}
for (var val = 1; val <= 9; val++) {
if (filled(matrix, i, j, val)) {
matrix[i][j] = val;
if (sudokuSolver(matrix, i+1, j)) {
return true;
}
}
}
matrix[i][j] = 0;
return false;
}
module.exports = function longestConsecutiveLength(array) {
if (array.length == 0) {
return 0;
} else if (array.length == 1) {
return 1;
}
var count = 1;
var set = [];
var newSet = [];
if (array.length > 1) {
set = sortNum(array);
for (let i = 0; i < set.length; i++) {
if (set[i] + 1 == set[i+1]) {
count++;
} else if (set[i+1] != set[i]) {
newSet.push(count);
count = 1;
}
}
return Math.max.apply(null, newSet);
}
function sortNum(array) {
var sorted = [];
var middle = Math.floor(array.length/2);
var left = array.slice(0, middle);
var right = array.slice(middle);
while (left.length > 0 && right.length > 0) {
if (left[0] < right[0]) {
sorted.push(left.shift());
} else {
sorted.push(right.shift());
}
}
return sorted.concat(left).concat(right);
}/**/
}
module.exports = function longestConsecutiveLength(array) {
let cout = 1;
let result = [];
array = array.sort(function (a, b) {
return a - b;
});
if (array.length == 0)
return 0;
if (array.length == 1)
return 1;
for (let j = 0; j < array.length - 1; j++) {
if (array[j] == array[j + 1]) {
continue;
} else if (array[j] == array[j + 1] - 1) {
cout++;
} else {
result.push(cout);
cout = 1;
}
}
if (result.length == 1) {
return result[0];
}
result.sort(function (a, b) {
return a - b;
})
return result[result.length - 1];
};
module.exports = function longestConsecutiveLength(array) {
if (!array || array.length == 0) return 0;
const numbers = new Set(array);
let max = 1;
array.forEach(it => {
if (numbers.has(it + 1) && !numbers.has(it - 1)) {
let length = 1;
while (numbers.has(it + length)) length++;
if (length > max) max = length;
}
});
return max;
};
class SmartCalculator {
constructor(initialValue) {
this.values = [initialValue];
this.operations = [];
}
add(number) {
this.values.push(number);
this.operations.push("+");
return this;
}
subtract(number) {
this.values.push(number);
this.operations.push("-");
return this;
}
multiply(number) {
this.values.push(number);
this.operations.push("*");
return this;
}
devide(number) {
this.values.push(number);
this.operations.push("/");
return this;
}
pow(number) {
this.values.push(number);
this.operations.push("^");
return this;
}
execute_high_priority(){
for (let i = this.operations.length; i >= 0; i--)
if(this.operations[i] == "^") this.execute_pow(i);
this.clear();
}
execute_middle_priority(){
for (let i = 0; i < this.operations.length; i++)
if(this.operations[i] == "*") this.execute_multiply(i);
else if(this.operations[i] == "/")this.execute_devide(i);
this.clear();
}
execute_low_priority(){
for (let i = 0; i < this.operations.length; i++)
if(this.operations[i] == "+") this.execute_add(i);
else if(this.operations[i] == "-")this.execute_subtract(i);
this.clear();
}
execute_add(i){
this.values[i+1] = this.values[i]+this.values[i+1];
this.values[i] = null;
this.operations[i] = null;
}
execute_subtract(i){
this.values[i+1] = this.values[i]-this.values[i+1];
this.values[i] = null;
this.operations[i] = null;
}
execute_multiply(i){
this.values[i+1] = this.values[i]*this.values[i+1];
this.values[i] = null;
this.operations[i] = null;
}
execute_devide(i){
this.values[i+1] = this.values[i]/this.values[i+1];
this.values[i] = null;
this.operations[i] = null;
}
execute_pow(i){
this.values[i] = Math.pow(this.values[i],this.values[i+1]);
this.values[i+1] = null;
this.operations[i] = null;
}
clear(){
let new_values = [];
let k = 0;
for (let i = 0; i < this.values.length; i++) {
if(this.values[i] != null)new_values[k++] = this.values[i];
}
this.values = new_values;
let new_operations = [];
k = 0;
for (let i = 0; i < this.operations.length; i++) {
if(this.operations[i] != null)new_operations[k++] = this.operations[i];
}
this.operations = new_operations;
}
valueOf(){
this.execute_high_priority();
this.execute_middle_priority();
this.execute_low_priority();
return this.values[0];
}
}
module.exports = SmartCalculator;
class SmartCalculator {
constructor(initialValue) {
this._expression = initialValue;
}
valueOf() {
let array = this._expression.split(' '),
index = array.length,
result = 0;
while ( (array.length >= 3) &&
( array.includes('*') || array.includes('/') || array.includes('^') ) ) {
const operation = array[index - 2],
x = parseInt(array[index - 3]),
y = parseInt(array[index - 1]);
let step = null;
if (array.includes('^')) {
if (operation == '^') {
step = Math.pow(x, y);
}
}
else if (array.includes('*') || array.includes('/')) {
if (operation == '*') {
step = x * y;
}
else if (operation == '/') {
step = Math.floor(x / y);
}
}
if (array.length >= 3) {
if (step != null) {
array.splice(index - 3, 3, step);
index = array.length;
result = step;
}
else {
index -= 2;
}
}
else {
result = step;
break;
}
}
index = 0;
while (array.length >= 3) {
const operation = array[index + 1],
x = parseInt(array[index]),
y = parseInt(array[index + 2]);
let step = null;
if (operation == '+') {
step = x + y;
}
else if (operation == '-') {
step = x - y;
}
if (step == null) {
break;
}
result = step;
array.splice(index, 3, step);
}
return result;
}
add(number) {
this._expression += ' + ' + number;
return this;
}
subtract(number) {
this._expression += ' - ' + number;
return this;
}
multiply(number) {
this._expression += ' * ' + number;
return this;
}
devide(number) {
this._expression += ' / ' + number;
return this;
}
pow(number) {
this._expression += ' ^ ' + number;
return this;
}
}
module.exports = SmartCalculator;
class SmartCalculator {
constructor(initialValue) {
this.value = initialValue;
this.expression = '' + initialValue + '';
}
toString() {
return this.value;
}
add(number) {
this.expression += '+' + number;
this.value = eval(this.expression);
return this;
}
subtract(number) {
this.expression += '-' + number;
this.value = eval(this.expression);
return this;
}
multiply(number) {
this.expression += '*' + number;
this.value = eval(this.expression);
return this;
}
devide(number) {
this.expression += '/' + number;
this.value = eval(this.expression);
return this;
}
pow(number) {
const val = this.expression.match(/(\d+)(?=\)*$)/)[0];
let expr = 'Math.pow(' + val + ',' + number + ')';
this.expression = this.expression.replace(/(\d+)(?=\)*$)/, expr);
this.value = eval(this.expression);
return this;
}
}
module.exports = SmartCalculator;