Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How do i calculate the number of trailing zeros in a factorial of a given number.
N! = 1 * 2 * 3 * 4 ... N
Any Help on this?
Because zeros come from factors 5 and 2 being multiplied together, iterate over all numbers from 1 to the input number, adding to a cumulative count of fives and twos whenever those factors are found. Then, return the smaller of those two counts:
function zeroCount(n) {
let fives = 0;
let twos = 0;
for (let counter = 2; counter <= n; counter++) {
let n = counter;
while (n % 2 === 0) {
n /= 2;
twos++;
}
while (n % 5 === 0) {
n /= 5;
fives++;
}
}
return Math.min(fives, twos);
}
console.log(zeroCount(6)); // 720
console.log(zeroCount(10)); // 3628800
It is very simple, This will help you.
function TrailingZero(n)
{
var c = 0;
for (var i = 5; n / i >= 1; i *= 5)
c += parseInt(n / i);
return c;
}
Let me know if you need help to understand this function.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to make a recursive function for this parameters. Function should determine the nth element of the row
a_0 = 1
a_k = k*a_(k-1) + 1/k
k = 1,2,3...
I am trying really hard to do this but i cant get a result. Please help me to do this
I came up with this but it is not a recursive function. I can not do this as a recursive function
let a = 1
let k = 1
let n = 3
for (let i = 1; i<=n; i++){
a = i*a + 1/i
}
console.log(a)
Here's the recursive function you're looking for, it has two conditions:
k == 0 => 1
k != 0 => k * fn(k - 1) + 1/k
function fn(k) {
if(k <= 0) return 1;
return k * fn(k - 1) + 1/k;
}
console.log(fn(1));
console.log(fn(2));
console.log(fn(3));
console.log(fn(4));
Note: I changed the condition of k == 0 to k <= 0 in the actual function so it won't stuck in an infinite loop if you pass a negative k
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I don't know what is the problem with this code I just need to know
how many number from Start to end that divide by 7 or has 7.
const divideOrHasSeven = (start, end) => {
var x = 0;
for (i = start; i <= end; i++) {
if (i % 7 || i.toString().indexOf("7")) { // if it is divide by 7 or has 7
x += 1;
}
}
return x;
};
The problem is i.toString().indexOf('7') will always return a truthy value EXCEPT (ironically) when 7 is actually in the number (inthe first position - index zero)
Change your conditional to if (i%7===0 || i.toString().indexOf('7')>-1)
You had some problems with your code.
If you compare with this, I think you will understand where.
This i.toString().indexOf('7') will return -1 if 7 is not found in string, which will be evaluated as true.
You also had some syntax errors.
const divideOrHasSeven = (start, end) => {
var x = 0;
for (i = start; i <= end; i++) {
if (i%7 === 0 || i.toString().indexOf('7') >= 0) {
x += 1;
}
}
return x
}
console.log(divideOrHasSeven(1, 200));
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How can I find an extra character between two strings in an optimal way.
Ex1: S1 - 'abcd', S2 - 'abcxd', output - 'x'
Ex2: S1 - '100001', S2 - '1000011', output - '1'
We can do this by traversing linearly and comparing each character in O(n). I want this to be done in much more optimal way, say in O(logn)
Baseline method (O(n)): Just comparing chars and narrowing in on both sides each cycle.
function findDiffChar(base, baseExtraChar) {
let extraLastIndex = base.length;
let lastIndex = extraLastIndex - 1;
for (let i = 0; i < extraLastIndex / 2; i++) {
console.log(`Loop: ${i}`);
if (base[i] !== baseExtraChar[i])
return baseExtraChar[i];
if (base[lastIndex - i] !== baseExtraChar[extraLastIndex - i])
return baseExtraChar[extraLastIndex - i];
}
return false;
}
console.log(findDiffChar('FOOOOOAR', 'FOOOOOBAR')); // B
Improved method using binary search (O(log n)): Compare halves until you've narrowed it down to one character.
function findDiffChar(base, baseExtraChar) {
if (baseExtraChar.length === 1) return baseExtraChar.charAt(0);
let halfBaseLen = Number.parseInt(base.length / 2) || 1;
let halfBase = base.substring(0,halfBaseLen);
let halfBaseExtra = baseExtraChar.substring(0,halfBaseLen);
return (halfBase !== halfBaseExtra)
? findDiffChar(halfBase, halfBaseExtra)
: findDiffChar(base.substring(halfBaseLen),baseExtraChar.substring(halfBaseLen));
}
console.log(findDiffChar('FOOOOAR', 'FOOOOBAR')); // B
console.log(findDiffChar('---------', '--------X')); // X
console.log(findDiffChar('-----------', '-----X-----')); // X
console.log(findDiffChar('------------', '---X--------')); // X
console.log(findDiffChar('----------', '-X--------')); // X
console.log(findDiffChar('----------', 'X---------')); // X
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
I need to know the code built in for the syntax math.pow(x,y). Actually I used the syntax to find exponent of any number... e.g.
var e = Math.pow(-3, 3); yields -27 but couldn't find out the program behind this... Help me please
If you know what power means..
multiplying the number x n times where x is base and n is exponent.
So you just have to repeat the same thing over and over - and that's why loops are for:
var sum = 1; //note that it's not zero!
for (int i=0;i<n;i++) { //loops n times
sum = sum * x; //on each loop multiplies sum by base number
}
Did you mean alternative for Math.pow? Here is one way with simple loop.
function pow(base,power) {
var p = 1;
for (var i=0; i<power; i++) {
p *= base;
}
return p;
}
You can also use recursion to solve this kind of challenge. Beware that recursion has the disadvantage of increasing space complexity as compared to a for-loop.
function pow(base, power) {
if (power === 1) return base * power
return base * pow(base, power - 1)
}
This is a better way to calculate power of a number with recursion:
function power(base, exp) {
if(exp === 0){
return 1;
}
return base * power(base, exp - 1);
}
You can try this:
function pow(n, e) {
let num = n;
for (let i = 1; i < e; i++) {
num *= n;
}
return num;
}
console.log(pow(-3, 3));
It will give you the required result.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Hi found this code on this list and need help converting it to Matlab.
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy)
{
int i, j, c = 0;
for (i = 0, j = nvert-1; i < nvert; j = i++) {
if ( ((verty[i]>testy) != (verty[j]>testy)) &&
(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
c = !c;
}
return c;
}
nvert: Number of vertices in the polygon. Whether to repeat the first vertex at the end.
vertx, verty: Arrays containing the x- and y-coordinates of the polygon's vertices.
testx, testy: X- and y-coordinate of the test point. (This is from another Stack Overflow question: Point in Polygon aka hit test.
JavaScript version:
function insidePoly(poly, pointx, pointy) {
var i, j;
var inside = false;
for (i = 0, j = poly.length - 1; i < poly.length; j = i++) {
if(((poly[i].y > pointy) != (poly[j].y > pointy)) && (pointx < (poly[j].x-poly[i].x) * (pointy-poly[i].y) / (poly[j].y-poly[i].y) + poly[i].x) ) inside = !inside;
}
return inside;
}
How will this translate in Matlab:
function insidePoly = inpoly(poly, pointx, pointy)
% Code
% return inside
Matlab comes with a build-in function inpolygon which seems to do exactly what you are asking for. No need to reimplement it.