All posts by edwardm

Calculating a square root value

A reader asked a question about how square roots can be calculated, referring to an example in my Volume 1 Introduction to App Inventor e-book. He was confused by the explanation of how it works – so I have written an expanded explanation of the method for calculating a square root of a number.

You could use the built in Math function to find the square root of a number – and generally that is what you should do. But if you would like to learn a method of calculating the square root of a number – then read on!

2,000 years ago, Greek mathematicians identified a method of finding approximate square roots. The method makes an initial guess of a square root (which is often not even close to the right value) and then repeats a sequence of dividing and averaging which gradually refines the initial guess into an accurate square root.  To find the square root of some number n, we set our initial guess to n (which we know is not correct), and then calculate an average of our guess with n divided by our guess.

To find the square root of 30, we initially guess the square root is 30 (which we know is wrong). We then take 30 and divide it by our guess, which in this case is 30. This gives us a value of 1. The average of our original number 30 and our divided value is 15.5. We select 15.5 as our new guess.

This little spreadsheet may help understand how the square root concept works.

To find the square root of 30, we initially guess the square root is 30 (which we know is wrong). We then take 30 and divide it by our initial guess, which is 30. This gives  a value of 1. The average of our original number 30 and 1 is 15.5. We select 15.5 as our new guess on the next row down in the Guess column.

Then we repeat this and take 30 divided by our new guess of 15.5. This gives us 1.93548. We average our 15.5 guess with our new value of 1.9458 giving us 8.717, which becomes our new guess on the next row down.

We repeat again using 8.717 as our new guess and divide 30 by 8.717, giving us 3.44. We average the 8.717 with 3.44 giving us 6.0795, use that as our next guess.

When do we stop this repeating cycle? We could repeat this a set number of times, say six, and then stop. But a better solution is to specify how many digits of accuracy we would like to have in our answer.

Let’s say that 5.4 is accurate enough for our purposes. We could stop as soon as our new guess is within 0.1 of our prior guess. In our spreadsheet above, our generated guesses are:

  • 15.5
  • 8.717742
  • 6.0795
  • 5.507058
  • 5.477306
  • 5.477226

As you can see, the correct answer has converged to 5.477 rather quickly. If we wanted an accuracy of just 1 digit to the right of the decimal point, we can stop once the difference between the current and previous guess is 0.1

The first digits of the last two guesses are the same 5.4 so we could stop with an accuracy of .1. In this particular calculation, we see that we are accurate to the thousandth’s place 5.477 as these values are no longer changing.

We stop the cycle when the difference between guesses becomes very small.

With our definition of small to 0.1, we stop at 5.4. If we set our definition of small difference to 0.001, then we stop at 5.477.

Here is some sample App Inventor code that calculates the square root. It fetches the number for which we find the square root from a user interface Text Box named txtDataEntry. We set NewValue (our initial guess) to this value. Then the code uses a while test loop to repeatedly do the calculation while the difference between our new guess and our previous guess is greater than 0.1 (you can change this to smaller values for greater accuracy).

This square root calculation repeats as long as the difference between our NewValue and OldValue are large.

As long as the difference remains large, the while test condition is “true”, and the code inside the while block continues to execute. However, once the difference becomes small, the test condition evaluates to “false” and the the execution of the “do” section stops. We have found our approximate square root.

Most popular programming languages?

There are many surveys of programming language popularity. Many of the popular surveys have problems with the survey methodology such that they likely produce erroneous estimates of programming language popularity. For example, one survey looks at how many times each programming language is looked up on Internet search systems.

The chart, below, comes from GitHub and bases popularity on the number of “pull requests” that are made to the GitHub software repository. Using this method, JavaScript is the most popular programming language, followed by Python, Java, Ruby and PHP.

Python has become a standard for use by non-computer science students. Whether your college studies be in mechanical engineering or geology, there is a good chance you will learn Python for data analysis projects.

Java is now an old programming language, but still used especially for Android programming. It’s popularity for desktop applications is starting to diminish.

Ruby become popular about ten years ago. Ruby is based on a concept of “frameworks” that provide pre-made program skeletons which you adapt to make your own application. Ruby is very popular for quickly creating web-based applications.

PHP pre-dates Ruby – PHP is a script language that runs on the server side of a web application. PHP is very easy to learn and couples easily with MySQL databases, making the combination a great solution for web-based, database-backed applications.

Finally we get to the “C” derived languages including C, C++ and Microsoft’s cousin C# (a very powerful language with great development tools.). C dates back to about 1970 or so.

C++ was developed in the 1980s and added object oriented programming to C and has since expanded in many ways. C and C++ are commonly “compiled” into machine instructions for each CPU and are used for high performance applications, including operating systems, video games and media applications.

C# has features resembling Java and C++ – but in a more modern design. In some ways, C# is where some wish C++ had gone

Capture

appinventor.pevest.com has moved to learn2c.org

In the spring of 2017, this web site – appinventor.pevest.com – was moved to https://learn2c.org (or an alternate address https://appinventorplus.wordpress.com/)

The move was made for technical and practical reasons. Visit https://learn2c.org for updates made through out 2017 and into 2018.


However, things have changed since then and as of the spring of 2018, this web site may return to here. If that happens, the plan is for the new learn2c.org address to point here as well.

Why return?

Many of the technical issues that caused to migrate a year ago have gone away or been solved.

The learn2c.org web site is hosted at WordPress.com and it appears Google down ranks WordPress web sites in search results. This old appinventor.pevest.com site – even without having been updated in a year, continues to receive many times more visits as a result of searches than does the WordPress.com hosted web site!

Finally, WordPress raised their hosting price last year.

 

Is your Android battery life too short? Some ideas that might fix it

Periodically, my  Nexus 5 phone’s battery life is terrible. On good days, if I don’t use my phone, the battery discharge rate is slow – many hours later, the battery meter says there’s still over 90% of the battery charge remaining. But when it is bad, my unused phone can have its battery drain in 8 hours.

I have found two things to improve improve the battery life on my Google Android Nexus 5.

  • One is to clear the system cache partition – this cache setting is not accessible from Settings.
  • The other is to replace the Android launcher with Nova Launcher, available for free in the Google Play store.

How to Clear the System Cache Partition

To clear the system cache you need to enter a hidden Android start up menu, usually by starting your phone while pressing the power and volume control keys simultaneously. For specific instructions, see this explanation for the Nexus 5 (it might work on your phone too – this does work on my Nexus 5). Another list from a mobile phone service describes how to do this for other phones (I have not tested any of those).

Every few months, the battery life has gotten really bad. But after clearing the system cache partition, the phone returns to normal performance. I hope this works for you.

I noticed this might be related to my installing lots of apps on the phone, over and over again. Some days I’ll install my test app 10 or 20 or 30 or more times. After a few weeks of doing this every day, battery life problems develop. I clear the system cache partition and the phone is fine again.

You can learn more about the Android system partitions by reading this article.

Part 2: Storing and accessing user interface components as variables

Part 1 showed how to reference and store user interface components as a variable. That tutorial used this method to easily change the background colors of six buttons on screen.

In Part 2, we use this technique to simplify a past tutorial about using a Bluetooth link between and Android device and two Arduino devices.

Continue reading Part 2: Storing and accessing user interface components as variables