Jump to content

How to count a number as a geometric sequence/progression


Winston

Recommended Posts

I apologize for posting in VBS, I was in a hurry. I searched for hours for a simple code to count "1, 2, 3, 4, 5, 6, 7, 8" as "1, 2, 4, 8, 16, 32, 64, 128" in JS or HTML or CSS; Multiplying the same number with itself didn't work. I want simple code and Google would not show anything. I found "Math.pow()" and "Math.LOG()", but can't find a simple code with an example of "Math.GMS" if Geometric Sequence, or "Math.GMP" if Geometric Progression. What is the simplest I can write? Do I have to combine two or more versions of "Math.xxx()" with my number? The formula is confusing, so I could not write it using that code. Not to be rude, but PLEASE ANSWER, my last question never got an answer. If you don't even answer this one as well, what's the point of this site? If I can't ask something without getting an answer, I will have to ask elsewhere. I SIGN UP TO GET AN ANSWER, not to just quit and go elsewhere. So if you have any help, PLEASE HELP! I would like to have it count "1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096..." when entering "1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13...".

This is the closest I got;


<input type="number" id="exmp" value="1">
<button onclick="myExample()">Convert</button>

<p id="exmpone1"></p>

<script>
function myExample() {
  var no = document.getElementById("exmp").value;
  document.getElementById("exmpone1").innerHTML = Math.pow(no, no);
}
</script>

 

Edited by Winston
Link to comment
Share on other sites

Here is an update. I was continuing to try to find it, https://www.geeksforgeeks.org/program-to-print-gp-geometric-progression/ was similar, but I want it to be variable, and show final number without all numbers from 1 up. When I tried to type 5 in the code, and have the ratio of 2 (if I am not mistaken), it shows, "1, 2, 4, 8, 16", but I just want the "16" when I change it.

 

I tried this, https://www.30secondsofcode.org/js/s/geometric-progression and again, set the ratio to 2, but it only shows numbers that match in the code, where if I write 8, it would read, "1,2,4,8".

 

I'm still not having any luck, so please respond and help. I don't know why they just make "Math.GP", which would be easier.

Link to comment
Share on other sites

The reason you're finding code that prints all of the terms up to N is because that is what a progression is: a sequence of terms.

Math.pow() already provides everything you need to find any one term in a geometric progression, which is why browsers will never implement a more specific function to do it. If browsers had a dedicated built-in function for every single little problem somebody wanted to solve there would be millions of them.

The formula for a term is right on the page you linked to:

TN = a1 * r(n-1)

The code is simple:

function geometricProgression(a1, r, n) {
  return a1 * Math.pow(r, n-1);
}

Putting into the HTML with the inputs and outputs it would look like this:

Progression factor: <input type="number" id="factor" value="2"><br>
Term number: <input type="number" id="term" value="1">
<button onclick="myExample()">Convert</button>

<p id="exmpone1"></p>

<script>
function myExample() {
  var factor = Number(document.getElementById("factor").value);
  var term = Number(document.getElementById("term").value);
  document.getElementById("exmpone1").innerHTML = geometricProgression(factor, factor, term);
}

function geometricProgression(a1, r, n) {
  return a1 * Math.pow(r, n-1);
}
</script>

 

Link to comment
Share on other sites

Regarding answers to your topics, this is a forum where ordinary people sign up to ask or answer questions. The W3Schools staff are not actually directly involved, answers come from regular users like you. If nobody knows the answer to your question you may not get any answers.

Link to comment
Share on other sites

Thank you, it helped me make the file work, and helped move to the correct topic (don't know how to do that manually)... But now I face a new problem. Though I did convert the HTML to EXE, I've been trying for a few hours now to convert this HTML to APK. I tried converting the HTML straight over to the XML in an APK app, and I tried converting an EXE to APK. One app gave me (and others, frustratingly,) a message; "Wrong! Select a destination folder correctly...". One site, trying to convert HTML to APK, I could not import pictures. I looked for a way to convert from CSS and other things to it like APK, no luck, "You can't convert xxx to yyy. Meant for a different platform." Well, could you try to convert this HTML to APK for me, or show me how to rewrite the commands?

 

<!DOCTYPE html>
<html>
<title>Son Of A Pitch 2! - Sound Frequency Converter</title>
<style>
body {
  background-image: url("BackGround.png");
  background-repeat: repeat;
  background-attachment: fixed;
  background-position: center;
  background-size: 100% 100%;
}
</style>
<body>
<audio id="tappingone">
  <source src="ButtonClick.WAV" type="audio/wav">
</audio>
<audio id="tappingtwo">
  <source src="ButtonHover.WAV" type="audio/wav">
</audio>
<h2>Winston presents...</h2>
<img src="Logo.png" width="500" height="500"><br>
Type the octave <input type="number" id="term" value="0"> and
<button onclick="myExample();myTapClickAud();" onmouseover="myOverClickAud();" onmouseleave="myClickOffClickAud();">convert</button> to the units...

<p id="exmpone1">With this page, you can convert sound frequencies to other compatible units.</p>
<p id="exmpone2">We will be using 1 Hertz to represent note C0. The higher the number, the higher the frequency, and lower or negative numbers will lower the frequency.</p>
<p id="exmpone3">This is an updated version of the original S.O.A.P. which could barely do Hertz, now upgraded to work better thanks to a user helping and giving me programming code.</p>
<p id="exmpone4">So? What are you waiting for? Enter a number and convert!</p>
<p id="exmpone5"><br>Last updated on Thursday, July 15, 2021, 12:57 PM</p>
<p id="exmpone6"><br><br><b>Missing a frequency and&#x002F;or pitch unit? Report incorrect information? Errors and&#x002F;or technical difficulties? Don't scrub your cassette tape off, but instead, please contact me and tell me what is wrong...</b></p>
<p id="exmpone7"><u>CENSORED-ADDRESS</u></p>
<p id="exmpone8"></p>
<p id="exmpone9"></p>
<p id="exmpone10"></p>
<p id="exmpone11"></p>
<p id="exmpone12"></p>
<p id="exmpone13"></p>

<script>
var one = document.getElementById("tappingone");  
var two = document.getElementById("tappingtwo");  
function myTapClickAud() {
  one.play();
  one.currentTime = 0;
  two.pause();
}
function myOverClickAud() {
  one.pause();
  two.play();
  two.currentTime = 0;
}
function myClickOffClickAud() {
  two.pause();
}
function myOffClickAud() {
  one.pause();
  two.pause();
}
function myExample() {
  var term = Number(document.getElementById("term").value);
  document.getElementById("exmpone1").innerHTML = geometricProgression(1, 2, term)*2 + " Hertz";
  document.getElementById("exmpone2").innerHTML = geometricProgression(1, 2, term)/1000*2 + " Kilohertz";
  document.getElementById("exmpone3").innerHTML = term + " Octaves";
  document.getElementById("exmpone4").innerHTML = term*1200 + " Cents";
  document.getElementById("exmpone5").innerHTML = term*12 + " Semitones";
  document.getElementById("exmpone6").innerHTML = term*5 + " Sharp&#x002F;Flat Notes (Black Keys)";
  document.getElementById("exmpone7").innerHTML = term*7 + " Natural Notes (White Keys)";
  document.getElementById("exmpone8").innerHTML = geometricProgression(1, 2, term)/1000000000*2 + " Gigahertz";
  document.getElementById("exmpone9").innerHTML = geometricProgression(1, 2, term)/1000000*2 + " Megahertz";
  document.getElementById("exmpone10").innerHTML = geometricProgression(1, 2, term)/1000000000000*2 + " Terahertz";
  document.getElementById("exmpone11").innerHTML = geometricProgression(1, 2, term)/10*2 + " Decahertz";
  document.getElementById("exmpone12").innerHTML = geometricProgression(1, 2, term)/100*2 + " Hectohertz";
  document.getElementById("exmpone13").innerHTML = 1 / geometricProgression(1, 2, term)/2 + " Second Cycles";
}
function geometricProgression(a1, r, n) {
  return a1 * Math.pow(r, n-1);
}
</script>
</body>
</html>

I have the pictures and sounds, but that probably would be stored in "Resources/drawable". What app could I use to turn it into an APK Android Application?

Link to comment
Share on other sites

I've never made an app for Android so I can't answer that.

If I intended to build an app, I would download Android Studio and build it there.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...