Finding Prime Numbers with Excel, and using Array Formulas

Download Primes.zip.

Several of the puzzles at Project Euler (see previous post) require the finding of prime numbers, for instance what is the 10,001th prime number?  Searching the Web for some ready made solutions I found this array formula at Chip Pearson’s site:

=IF(OR(C8=2,C8=3),”prime”,IF(AND((MOD(C8,ROW(INDIRECT(“2:”&INT(SQRT(C8)))))<>0)),”prime”,”not prime”)) *

* This formula is slightly amended from the one on Chip’s site, which treated 1 as a prime, and divided by all numbers up to C8-1, rather than SQRT(C8)

Quoting from Chip’s site:

“This is an array formula, so you must press CTRL SHIFT ENTER rather than just ENTER when you first enter the formula and whenever you edit it later. If you do this properly, Excel will display the formula enclosed in curly braces { }. See the array formulas pagefor much more detail about array formulas. Note that the formula will not work properly if you do not enter it with CTRL SHIFT ENTER. This formula will work with numbers up to 268,435,455, after which Excel’s ability to handle the intermediate array fails. “

Now it may not be immediately obvious to you how that formula works, certainly it was not at all obvious to me,so let’s break it down into small chunks:

Breakdown of an array formula

Breakdown of an array formula

Entering 25 in cell C8:
=”2:”&INT(SQRT(C8)) returns 2:5

=ROW(INDIRECT(“2:”&INT(SQRT(C8)))) returns a column array with the numbers {2;3;4,5}

=MOD(C8,ROW(INDIRECT(“2:”&INT(SQRT(C8))))) returns a column array with the remainder of 25 divided by the members of the array above {1;1;1,0}

=AND((MOD(C8,ROW(INDIRECT(“2:”&INT(SQRT(C8)))))<>0)) returns TRUE if all the remainders are greater than zero, or FALSE if there are one or more zeroes, and finally

=IF(AND((MOD(C8,ROW(INDIRECT(“2:”&INT(SQRT(C8)))))<>0)),”prime”,”not prime”) returns “prime” if the statement above is TRUE, or “not prime” if it is FALSE.

The initial: =IF(OR(C8=2,C8=3),”prime”,… is required because the main formula does not work for values less than 4, so it is necessary to check for entered values of 2 or 3 (but not 1, which is not defined as a prime number).

The reason why this formula must be entered as an array formula is that it is necessary to check for all the divisions in the array generated by the ROW statement. If the formula is enetered without pressing “Ctrl-Shift-Enter” the AND function only checks the first member of the array, and finds that all odd numbers are prime numbers.

This is an excellent example of the power of an array formula, allowing complex multi-stage computations to be carried out in a single cell; it is however not of much use for Project Euler purposes, which required finding the 10,001st prime for instance. For that purpose I have written my own prime UDF – download here.

A UDF to generate a sequence of primes

A UDF to generate a sequence of primes

=Prime(MaxVal, First, Last)

This function will find all the prime numbers in a given range (e.g. from the 99,995th to 100,001st prime as shown above).  If “First” is not entered or is zero the function checks if MaxVal is prime.

For checking if one number is prime the UDF uses a similar approach to the array function, except that it only checks odd numbers, since we know that even numbers other than 2 cannot be prime.

For generating a range of primes it uses the “Sieve of Eratosthenes” method:

  • Set up an array from 1 to MaxVal
  • Enter a 1 for each number in the array divisible by 2 or 3
  • For each odd number from 5 to MaxVal:
  • If its array value = 1, it is not prime, check the next odd number
  • If its array value = 0, it is prime, add it to the prime list and enter 1 for each number in the array that is an odd multiple of this prime number.
  • Stop when the last prime in the required sequence has been found, or MaxVal has been reached.

More refinements on the Sieve of Eratosthenes method are given here: Fun With Prime Numbers

Posted in Arrays, Excel, Maths, Newton, UDFs, VBA | Tagged , , , , | 28 Comments

Project Euler

Whilst tag-surfing I discovered Project Euler, which looks like fun for people who enjoy mathematical puzzles.  Here’s what they say about themselves:

What is Project Euler?

Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

The motivation for starting Project Euler, and its continuation, is to provide a platform for the inquiring mind to delve into unfamiliar areas and learn new concepts in a fun and recreational context.”

After you register and log in you can post an answer to the questions, and they will tell you if it is right or not.

Posted in Maths, Newton | Tagged , | Leave a comment

Drawing in Excel 7 – Creating drawings from coordinates

Previous Post, Drawing in Excel 6

The file PlotXYcontains routines to plot scale drawings in Excel from a list of XY coordinates of node points, node points for each shape, and formatting details for each shape type.  The plot is scaled to fit within the selected plot range and, unlike Excel charts, is scaled equally in the X and Y directions, so circles will stay circular.  Shapes may either be defined as polylines, with a series of points, or by using any of the built in Excel shapes.

Examples of the output are shown below, and data for the first three is included in the download.  The final example was plotted from about 250,000 node points, using over 12,000 polyline shapes (using Excel 2007), illustrating that this tool is capable of producing drawings from very large data sets.

Super T bridge beam cross section

Super T bridge beam cross section

mesh

Finite Element Mesh for a Buried Precast Arch Structure

illusion

Concentric squares, or a square spiral?

Detail of a very large finite element model of a bat skull

Detail of a very large finite element model of a bat skull

Any comments or requests for additional features?  Please let me know.

Posted in Drawing, Excel, VBA | Tagged , , , | 25 Comments

Concrete 09 Call for Abstracts – Closes 14th November

Concrete 09, the bi-annual conference of the Concrete Institute of Australia will be held from 17th to 19th September 2009 at Luna Park, Sydney.  The theme of the conference is “adding value in changing climates”.  The closing date for submission of abstracts is Friday 14th November 2008; just 7 days left!.

More details here

logo

Posted in Concrete, Newton | Tagged , , | Leave a comment

Reinforced Concrete Section Analysis 7 – rectangular sections

Previous post

The spreadsheets presented in earlier posts in this series have been simplified for use with rectangular sections with two layers of reinforcement.  The functions EStress (for elastic analysis of stresses, strains and crack widths) and UMom (for the ULS analysis of bending, axial load and shear capacity) can be found in the spreadsheet RC Design Functions7.

Examples of the output from these functions are shown below:

Interaction Diagram generated by UMom

Interaction Diagram generated by UMom

Estress and UMom entered as array functions

Estress and UMom entered as array functions

Posted in Beam Bending, Concrete, Newton, UDFs | Tagged , , , , , | 3 Comments