Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
I have task: On a sheet of paper, a rectangle is drawn along the grid lines. After that, the number of grid nodes located inside, but not on the border of the rectangles, and the number of single grid segments inside the rectangle were counted. Write a program that uses this data to determine the size of a rectangle.
I have this code in Pascal:
var
K,N,i,j:integer;
begin
readln(K,N);
for i:=1 to trunc(sqrt(K)) do
if K mod i = 0 then
begin
if i*(K div i+1)+(K div i)*(i+1)=N then writeln(i+1,' ',K div i+1);
end;
end.
And this my code in JavaScript:
const a = [1000, 2065]
function Sum(K, N) {
for (i = 1; i < Math.trunc(Math.sqrt(K)); i++) {
if (K % i === 0 && i * (Math.floor(K / (i) + 1) + Math.floor(K / i) * (i + 1)) === N) {
break;
}
}
console.log(i + 1, Math.floor(K / (i)) + 1)
}
Sum(a[0], a[1]);
Can you help why my answers in JavaScript are wrong?
Not exactely sure what you're trying to achieve but this javascript code produces the same output (26, 41) as your pascal version does:
See onlinegdb.com!
const K = 1000, N = 2065;
for (let i=1; i<Math.trunc(Math.sqrt(K)); i++)
if (K % i === 0)
if (i*(K / i+1)+(K / i)*(i+1) === N)
console.log(i+1, K / i+1);
I think you have messed something up with the brackets in Math.floor or something similar.
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 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 3 years ago.
Improve this question
I was at internship interview, where I couldn't solve one logical exercise. It was too complicate for me. Could you help me and explain how to solve it? (I didn't pass, but I really want to know how to solve it). Exercise is:
"Given an array arr of strings complete the function landPerimeter by calculating the total perimeter of all the islands. Each piece of land will be marked with 'X' while the water fields are represented as 'O'. Consider each tile being a perfect 1 x 1 piece of land. Some examples for better visualization:
['XOOXO',
'XOOXO',
'OOOXO',
'XXOXO',
'OXOOO']
see image here : https://i.ibb.co/pbDwWSs/pic1.jpg
should return: "Total land perimeter: 24",
while
['XOOO',
'XOXO',
'XOXO',
'OOXX',
'OOOO']
see image here: https://i.ibb.co/Pcdy8Ct/pic2.jpg
should return: "Total land perimeter: 18"
Kindly ask you to show code example how could I solve this exercise.
Thank you very much!
please see this link find-perimeter-shapes-formed-1s-binary-matrix. need some small modifications in your case . see below
<script>
function numOfNeighbour( mat, i, j,rows,cols)
{
var count = 0;
// UP
if (i > 0 && mat[i - 1][j]==='X')
count++;
// LEFT
if (j > 0 && mat[i][j - 1]==='X')
count++;
// DOWN
if (i < rows-1 && mat[i + 1][j]==='X')
count++;
// RIGHT
if (j < cols-1 && mat[i][j + 1]==='X')
count++;
return count;
}
function findPerimeter( mat)
{
var perimeter = 0;
var rows=mat.length;
var cols=mat[0].length;
// Traversing the matrix and finding ones to
// calculate their contribution.
for (var i = 0; i < rows; i++)
for (var j = 0; j < cols; j++)
if (mat[i][j] && mat[i][j]==='X')
perimeter += (4 - numOfNeighbour(mat, i ,j,rows,cols));
return perimeter;
}
console.log("perimeter of ['XOOXO', 'XOOXO', 'OOOXO', 'XXOXO', 'OXOOO'] is ",findPerimeter(['XOOXO', 'XOOXO', 'OOOXO', 'XXOXO', 'OXOOO'])); //24
console.log("perimeter of ['XOOO', 'XOXO', 'XOXO', 'OOXX', 'OOOO'] is " ,findPerimeter(['XOOO', 'XOXO', 'XOXO', 'OOXX', 'OOOO'])); //18
</script>
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this simple javascript code:
function power(base, exponent) {
var result = 1;
for (var count = 0; count < exponent; count++)
result = result * base;
return result;
}
power(2, 10);
but it s hard for me to understand what is the relationship between for loope and the result in the for loop. Can anyone describe the process in here? I know that the answer is 1024 and that it is 2*2*2*2*2*2*...... but it is har for me to understand how for and result is related. result is always 1? Or somehow it grabs the updated version from the loop? Thank you!
Say I pass power(2, 10). Here's how it's going to run, with each iteration:
i | result
--+-------
- | 1
0 | 1 * 2 = 2
1 | 2 * 2 = 4
2 | 4 * 2 = 8
.
.
.
9 | 512 * 2 = 1024
However, the numbers array is redundant. You'll need to check against exponent (i < exponent)
numbers is totally unnecessary. the length of the array [0....x] is x. So your for loop is really for (var i = 0; i < 10; i++). Hope that sorts it out. Also, you don't want to go to 10, but rather to exponent. Try:
for (var i = 0; i < exponent; i++)
why do you not write this?
function power(base, exponent) {
var result = 1;
for (var i = 0; i < exponent; i++)
result = result * base;
return result;
}
or there is also e javascript-integrated way
Math.pow(base, exponent);
when you call a function with power(2,10) you give variable "base" value of 2, and then it multiplies the result (which is 1) by 2 for 10 times, I see no use of that second variable "exponent"