From e16f34405a943071bb0e6f531ee3dec3bcfbf38e Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Wed, 3 Jan 2018 13:06:23 +0530 Subject: [PATCH] Create geometricProgression.md --- snippets/geometricProgression.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 snippets/geometricProgression.md diff --git a/snippets/geometricProgression.md b/snippets/geometricProgression.md new file mode 100644 index 000000000..8f03a04f2 --- /dev/null +++ b/snippets/geometricProgression.md @@ -0,0 +1,19 @@ +### geometricProgression + +Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive and the ratio between two terms is `step`. + +Use Array(Math.floor(Math.log(end/start)/Math.log(step))+1) to create an array of the desired length, Array.map() to fill with the desired values in a range. +You can omit `start` to use a default value of 1. +You can omit `step` to use a default value of 2. +Returns a error when you try to use `step = 1` +``` js +const geometricProgression = (end, start = 1,step = 2) => + Array.from({ length:Math.floor(Math.log(end/start)/Math.log(step))+1}).map((v, i) => start * (step ** (i)) ) +``` + +```js +geometricProgression(256); // [1, 2, 4, 8, 16, 32, 64, 128, 256] +geometricProgression(256,3); //[3, 6, 12, 24, 48, 96, 192] +geometricProgression(256,1,4); //[1, 4, 16, 64, 256] +geometricProgression(256,2,1); //Gives error +```