Skip to content

Instantly share code, notes, and snippets.

@zhenyanghua
Created August 6, 2021 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhenyanghua/6de4739b1d4316e2718672fb0dce2f4e to your computer and use it in GitHub Desktop.
Save zhenyanghua/6de4739b1d4316e2718672fb0dce2f4e to your computer and use it in GitHub Desktop.
// source https://jsbin.com
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
/**
* The goal is to make sure the last bike when there is only one bike left,
* it can ride its full distance (100km / 1 (bike)).
* When there are two bikes left, both of them could ride their full distance (100km / 2(bikes))
* This requires the second last bike to give away whatever the amount it has left to make up
* the last bike to have 100km distance. Because the last two bikes must start at the same time when
* both have 100km to go, and the second last bike must give its remaining fuel to the last bike ONLY
* when R(n-1) + R(n) = 100km, where R is the function to represent the remaining fuel. Because they start
* the same time, so we have:
* 100km - R(n-1) = 100km - R(n)
* => R(n) = R(n-1)
* Replace R(n-1) with R(n):
* R(n-1) + R(n) = 100km
* => 2R(n) = 100km
* => R(n) = R(n-1) = 50km
* The same logics apply to any of the previous bikes back to the begining when we still have 50 bikes:
* So the first leg when all 50 bikes start together, we need to stop at a distance where the remaining
* fuel from one bike could be used to fill the rest 49 bikes.
* S(1) = 100 / 50(bikes), where S is the distance to travel for the first leg.
* Combine the above, we could derive the following formular
* S(n) = 100/50 + 100/49 + 100/48 + ... + 100/i + ... + 100/3 + 100/2 + 100/1, where i is the ith leg.
* Use the following iteration we get to know the sum.
*/
let sum = 0;
for (let i = 50; i >=1; i--) {
sum += 100/i;
}
console.log(sum)
</script>
<script id="jsbin-source-javascript" type="text/javascript">/**
* The goal is to make sure the last bike when there is only one bike left,
* it can ride its full distance (100km / 1 (bike)).
* When there are two bikes left, both of them could ride their full distance (100km / 2(bikes))
* This requires the second last bike to give away whatever the amount it has left to make up
* the last bike to have 100km distance. Because the last two bikes must start at the same time when
* both have 100km to go, and the second last bike must give its remaining fuel to the last bike ONLY
* when R(n-1) + R(n) = 100km, where R is the function to represent the remaining fuel. Because they start
* the same time, so we have:
* 100km - R(n-1) = 100km - R(n)
* => R(n) = R(n-1)
* Replace R(n-1) with R(n):
* R(n-1) + R(n) = 100km
* => 2R(n) = 100km
* => R(n) = R(n-1) = 50km
* The same logics apply to any of the previous bikes back to the begining when we still have 50 bikes:
* So the first leg when all 50 bikes start together, we need to stop at a distance where the remaining
* fuel from one bike could be used to fill the rest 49 bikes.
* S(1) = 100 / 50(bikes), where S is the distance to travel for the first leg.
* Combine the above, we could derive the following formular
* S(n) = 100/50 + 100/49 + 100/48 + ... + 100/i + ... + 100/3 + 100/2 + 100/1, where i is the ith leg.
* Use the following iteration we get to know the sum.
*/
let sum = 0;
for (let i = 50; i >=1; i--) {
sum += 100/i;
}
console.log(sum)</script></body>
</html>
/**
* The goal is to make sure the last bike when there is only one bike left,
* it can ride its full distance (100km / 1 (bike)).
* When there are two bikes left, both of them could ride their full distance (100km / 2(bikes))
* This requires the second last bike to give away whatever the amount it has left to make up
* the last bike to have 100km distance. Because the last two bikes must start at the same time when
* both have 100km to go, and the second last bike must give its remaining fuel to the last bike ONLY
* when R(n-1) + R(n) = 100km, where R is the function to represent the remaining fuel. Because they start
* the same time, so we have:
* 100km - R(n-1) = 100km - R(n)
* => R(n) = R(n-1)
* Replace R(n-1) with R(n):
* R(n-1) + R(n) = 100km
* => 2R(n) = 100km
* => R(n) = R(n-1) = 50km
* The same logics apply to any of the previous bikes back to the begining when we still have 50 bikes:
* So the first leg when all 50 bikes start together, we need to stop at a distance where the remaining
* fuel from one bike could be used to fill the rest 49 bikes.
* S(1) = 100 / 50(bikes), where S is the distance to travel for the first leg.
* Combine the above, we could derive the following formular
* S(n) = 100/50 + 100/49 + 100/48 + ... + 100/i + ... + 100/3 + 100/2 + 100/1, where i is the ith leg.
* Use the following iteration we get to know the sum.
*/
let sum = 0;
for (let i = 50; i >=1; i--) {
sum += 100/i;
}
console.log(sum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment