# A Study on Directionality in the Ulam Square with the Use of the Hough Transform

## Abstract

A version of the Hough transform in which the direction of the line is represented by a pair of co-prime numbers has been used to investigate the directional properties of the Ulam spiral. The method reveals the detailed information on the intensities of the lines which can be found in the square and on the numbers of primes contained in these lines. This makes it possible to make quantitative assessments related to the lines. The analysis, among others, confirms the known observation that one of the diagonal directions is more populated with lines than the other one. The results are compared to those made for a square containing randomly located points with a density close to that for the Ulam square of a corresponding size. Besides its randomness, such square also has a directional structure resulting from the square shape of the pixel lattice. This structure does not depend significantly on the size of the square. The analysis reveals that the directional structure of the Ulam square is both quantitatively and qualitatively different from that of a random square. Larger density of lines in the Ulam square along one of the diagonal directions in comparison to the other one is confirmed.

### Keywords

Ulam Square Spiral Directionality Random Hough transform## 1 Introduction

Since its discovery in 1963 [1], the Ulam spiral [2] called also prime spiral [3] gains much attention as the visual way to get insight into the domain of prime numbers. It seems that the geometric structures which have an important mathematical meaning are, among others, the diagonal lines. One of the observations was that there are more primes on one diagonal than the other. It seems that there are still discoveries to be made about the Ulam spiral (cf. [4], Section *Why It’s Interesting*).

A proper tool for detection and analysis of line segments in an image is the Hough transform (HT) in its version for lines. The Ulam square is not a typical image in which real-world objects are represented by means of a projection onto the camera sensor, which is an approximation in itself. Rather, it is a strictly defined mathematical object. The analysis of lines treated as mathematical objects with the HT was considered by Cyganski, Noel and Orr [7, 8]. They paid attention to the problem of all the digital straight lines possible to be represented in an image, being a digitization of a mathematical straight line crossing the image. Kiryati, Lindenbaum and Bruckstein [6] compared the *digital* and *analog* Hough transforms. They discussed the relation between the digital HT according to [7] and the conventional HTs which are analog in nature.

The lines present in the Ulam square are not the approximations of any actual lines. These lines are, however, the sequences of points having strictly specified ratios of coordinate increments. Therefore, in the case of our interest, the concept of the HT described in [7] is too complex. In the present paper we shall use the version of the Hough transform according to [5] in which the ratios of coordinate increments along the object to be detected have been directly used in the process of accumulating the evidence for the existence of lines.

The HT according to [5] has already been used in [9] to find long contiguous sequences of points which form segments of straight lines. In the present paper, however, we shall investigate the directional structure of the Ulam spiral. We shall make an attempt to answer two questions. First, to what extent in some of the directions there are more linear objects in the Ulam square than in the other ones. Second, what is the difference between the directional structure of the Ulam square and the square of corresponding size with randomly displaced points. The use of the method proposed will enable us to give quantitative answers to these questions.

## 2 Method

*U*is and odd integer, with the coordinate system

*Opq*, \(p,q\in {}[-(U-1)/2,(U-1)/2]\), having its origin in the middle element occupied by the number 1, as shown in Fig. 1. A line in the Ulam square is considered as a sequence of pixels, in which the increments \(\varDelta {}p,\varDelta {}q\) of the coordinates

*p*,

*q*between the subsequent pixels fulfill the condition \(\varDelta {}p/\varDelta {}q=n_1/n_2\), where \(n_1,n_2\) are small integers. Therefore, the slope of the line can be represented by two small integer numbers which form an array \(D_{ij}\), where \(i=n_1\) and \(j=n_2\) (Fig. 2). Thus,

*i*/

*j*is the reduced fraction \(\varDelta {}p/\varDelta {}q\). For the sake of uniqueness it is assumed \(i\ge {}0\). The dimensions of

*D*are restricted to \([0,N]\times [-N,N]\). From all the elements of

*D*only those are used which correspond to reduced fractions.

*D*, the pair is stored in \(D_{ij}\), where

*i*/

*j*is the reduced fraction \(\varDelta {}p/\varDelta {}q\); otherwise it is neglected. The votes are stored along the third dimension

*k*of the accumulator, not shown in Fig. 2. For each such vote, the line offset is also stored, defined as the intercept with the axis

*Op*for horizontal lines and with

*Oq*for the remaining ones.

The relation of the coordinates in the direction table and the angle \(\alpha \) can be seen in Fig. 3. Only some specific angles can be represented. This is in conformity with our interest in sequences of points for which the increments of coordinates are expressed by small integers.

After the accumulation process, in each element of the accumulator, which corresponds to a specific line characterized with the slope and the offset, as previously defined, the following data are stored: the number of voting pairs and the number of primes in this line, and the list of primes which lie on this line. For each prime in this list the following data are stored: its value, its index in the spiral, and its coordinates *p*, *q*. Each list can be easily sorted according to the chosen item in these data.

The accumulator can be analyzed with respect to such aspects like, for example, the directional structure of the Ulam spiral, the existence of lines with large numbers of primes, the contiguous strings of points representing primes in the square, etc.

## 3 Directionality – Selected Results

Before we go on to the results, one more issue needs consideration. In any of our experiments we consider an Ulam square having a specific size, limited by the memory of the computer used and the available time for calculations. Both these requirements do not seem to be crucial, due to that on the one hand very large memories are available now and on the other hand we have to make the calculations only once. However, at this stage we have tried to see if there is a variability in the results with the growing size of the square, or if the results have a tendency to stabilize above some size. As it will be seen further, for some of the characteristics of the directionality, the second case holds. Therefore, we have come to a conclusion that, for the problem of our interest, we can preform the calculations for the square of quite a moderate size of \(1001\times 1001\) points. It contains \(78\,650\) primes, where the largest one is \(1\,001\,989\).

### 3.1 Ulam Square

Let us check if the number of primes on lines in each direction reveals some information on the directionality. This is shown in Fig. 5. It can be seen that the directional structure of the square is not revealed in this plot.

In the plots of Figs. 6 and 7 is is clearly see that the horizontal, vertical and diagonal lines are the most populated in the Ulam square. It can also be seen that one of the diagonals has more representation that the other in both graphs. This is in conformity with the observations found in literature [1]. These two plots will be used in the further analysis.

### 3.2 Random Square

*canalli*on Mars, see e.g. [10]). The other side of the problem is that the image of our interest is formed by square pixels, which can be of importance as far as the directions are concerned, due to that vertical, horizontal and diagonal lines are the most naturally represented in the square grid. Therefore, in the two random dot images of Fig. 8 the human eye tends to see some regularities, but they are different in each one.

In Figs. 9 and 10 the results for ten different realizations of the random square are compared to those of the Ulam square. Most of these realizations were taken for \(1001\times 1001\) pixels, as this size was considered reasonable for the case of the primes square. However, to see whether the properties depend on the size of the random square, two realizations were taken for size \(501\times 501\) and two for size \(2001\times 2001\) pixels. For each of the ten realizations, the results are very similar. This means that the directional structure of the random square is stable, besides that any particular realization differs in its details and that the size of the image changes.

In the plots of Figs. 9 and 10 it can be seen that the random squares exhibit the directional structure in which the horizontal, vertical and both diagonal lines are dominating above the lines in other directions and that the directions are represented by different numbers of voting pairs. This phenomenon does not significantly depend on the realization of the random process which gave rise to a particular image. As it could be expected, the horizontal direction is not favored versus the vertical one; similarly, one diagonal direction is not favored versus the other one.

On the contrary, in the Ulam square one diagonal direction is dominating over the other one. This is the most conspicuous difference. The pattern of other points for subsequent directions is also different from that for the random square.

## 4 Summary and Prospect

A version of the Hough transform proposed previously has been used to investigate the directional properties of the Ulam spiral. The method makes it possible to assess the difference in the intensities of lines related to subsequent directions in the quantitative way, due to that the data on the points related to the angles are available.

The measures of line intensities used here allowed us to show that one of the diagonal directions is more populated with lines than the other one. Such a phenomenon is absent in the random square, although in that square also some directionality is observed.

The proposed method gives the possibility to study other quantitative characteristics of the Ulam square [11].

### References

- 1.Wikipedia: Ulam spiral — Wikipedia, The Free Encyclopedia (2016). https://en.wikipedia.org/w/index.php?title=Ulam_spiral&oldid=726734495. Accessed 20 June 2016
- 2.Stein, M., Ulam, S., Wells, M.: A visual display of some properties of the distribution of primes. Am. Math. Mon.
**71**(5), 516–520 (1964). doi:10.2307/2312588 MathSciNetCrossRefMATHGoogle Scholar - 3.Weisstein, E.W.: Prime spiral. From MathWorld–A Wolfram Web Resource (2016). http://mathworld.wolfram.com/PrimeSpiral.html. Accessed 20 June 2016
- 4.Authors of the
*Prime Spiral*chapter: Prime spiral (Ulam spiral). The Math Images Project (2016). http://mathforum.org/mathimages/index.php/Prime_spiral_(Ulam_spiral). Accessed 20 June 2016 - 5.Chmielewski, L.J., Orłowski, A.: Hough transform for lines with slope defined by a pair of co-primes. Mach. Graph. Vis.
**22**(1/4), 17–25 (2013)Google Scholar - 6.Kiryati, N., Lindenbaum, M., Bruckstein, A.: Digital or analog Hough transform? Pattern Recogn. Lett.
**12**(5), 291–297 (1991). doi:10.1016/0167-8655(91)90412-F CrossRefGoogle Scholar - 7.Cyganski, D., Noel, W.F., Orr, J.A.: Analytic Hough transform. In: Proceedings of SPIE. Sensing and Reconstruction of Three-Dimensional Objects and Scenes, vol. 1260, pp. 148–159 (1990). doi:10.1117/12.20013
- 8.Liu, Y., Cyganski, D., Vaz, R.F.: Efficient implementation of the analytic Hough transform for exact linear feature extraction. In: Proceedings of SPIE. Intelligent Robots and Computer Vision X: Algorithms and Techniques, vol. 1607, pp. 298–309 (1992). doi:10.1117/12.57109
- 9.Chmielewski, L.J., Orłowski, A.: Finding line segments in the Ulam square with the Hough transform. In: Chmielewski, L.J., Datta, A., Kozera, R., Wojciechowski, K. (eds.) ICCVG 2016. LNCS, vol. 9972, pp. 617–626. Springer, Heidelberg (2016). doi:10.1007/978-3-319-46418-3_55
- 10.Evans, J.E., Maunder, E.W.: Experiments as to the actuality of the “canals” observed on Mars. Mon. Not. R. Astron. Soc.
**63**, 488–499 (1903). doi:10.1093/mnras/63.8.488 CrossRefGoogle Scholar - 11.Chmielewski, L.J., Orwski, A.: Prime numbers in the Ulam dsquare (2016). www.lchmiel.pl/primes. Accessed 01 Aug 2016