Showing posts with label SageMathCell Server. Show all posts
Showing posts with label SageMathCell Server. Show all posts

Wednesday, 2 September 2020

Factorial Number System

I've titled this post Factorial Number System because I made a post titled Factorial Number Base on 31st March 2018. Strictly speaking, the factorials do not form a number base. This is explained in Wikipedia:

In combinatorics, the factorial number system, also called factoradic, is a mixed radix numeral system adapted to numbering permutations. It is also called factorial base, although factorials do not function as base, but as place value of digits. By converting a number less than n! to factorial representation, one obtains a sequence of \(n\) digits that can be converted to a permutation of \(n\) in a straightforward way, either using them as Lehmer code or as inversion table representation; in the former case the resulting map from integers to permutations of \(n\) lists them in lexicographical order. General mixed radix systems were studied by Georg Cantor. The term "factorial number system" is used by Knuth, while the French equivalent "numération factorielle" was first used in 1888. The term "factoradic", which is a portmanteau of factorial and mixed radix, appears to be of more recent date.

My interest in the subject was rekindled when I came across this OEIS entry for 26085 (I turned 26085 days old today):

A286590: Numbers that are divisible by the product of their factorial base digits (A208575).

In my 2018 post, I treated this number system rather narrowly so I am broadening my scope in this current post. Figure 1 shows an easy way to convert from a decimal number to its factorial representation:


Using this method, the following SageMath code will carry out the conversion (input is shown in blue and output in red):

# -------------------------------------------------------
# decimal to factorial base conversion with trailing zero
# -------------------------------------------------------
number=26085
entry=number
L,n,dividend=[],1,1
while dividend!=0:
    dividend=int(number/n)
    remainder=number%n
    L.append(remainder)
    number=dividend
    n+=1
L.reverse()
print(entry,"has factorial base of",L, "with trailing zero")
# ----------------------------------------------------------
# decimal to factorial base conversion without trailing zero
# ----------------------------------------------------------
number=26085
L,n,dividend=[],2,1
while dividend!=0:
    dividend=int(number/n)
    remainder=number%n
    L.append(remainder)
    number=dividend
    n+=1
L.reverse()
print(entry,"has factorial base of",L, "without trailing zero")  
 
26085 has factorial base of [5, 1, 1, 1, 3, 1, 1, 0] with trailing zero
26085 has factorial base of [5, 1, 1, 1, 3, 1, 1] without trailing zero

Here is the permalink to SageMathCell. Notice the two possible representations: \(51113110_!\) and   \(5111311_!\). This is because "The factorial number system is sometimes defined with the 0! place omitted because it is always zero" (Wikipedia). The conversion from factorial representation to decimal is quite straightforward. Here is the SageMath code (permalink):

# ---------------------------------------------------------
# factorial base (with trailing zero) to decimal conversion 
# ---------------------------------------------------------
number=51113110
D=number.digits()
decimal,n=0,0
for d in D:
    decimal+=d*factorial(n)
    n+=1
print(decimal)
# ------------------------------------------------------------
# factorial base (without trailing zero) to decimal conversion 
# ------------------------------------------------------------
number=5111311
D=number.digits()
decimal,n=0,1
for d in D:
    decimal+=d*factorial(n)
    n+=1
print(decimal)

26085
26085

Wikipedia goes on to say:
The factorial number system provides a unique representation for each natural number, with the given restriction on the "digits" used. No number can be represented in more than one way because the sum of consecutive factorials multiplied by their index is always the next factorial minus one:$$\sum_{i=0}^n i \times i!=(n+1)!-1$$
This can be easily proved with mathematical induction, or simply by noticing that :$$\forall i,i \times i!=(i+1-1) \times i!=(i+1)!-i!$$where subsequent terms cancel each other, leaving the first and last term (see Telescoping series).
However, when using Arabic numerals to write the digits (and not including the subscripts as in the above examples), their simple concatenation becomes ambiguous for numbers having a "digit" greater than 9. The smallest such example is the number 10 × 10! = 36,288,00010, which may be written A0000000000!=10:0:0:0:0:0:0:0:0:0:0!, but not 100000000000! = 1:0:0:0:0:0:0:0:0:0:0:0! which denotes 11! = 39,916,80010. Thus using letters A–Z to denote digits 10, 11, 12, ..., 35 as in other base-N make the largest representable number 36 × 36! − 1. 
For arbitrarily greater numbers one has to choose a base for representing individual digits, say decimal, and provide a separating mark between them (for instance by subscripting each digit by its base, also given in decimal, like 24031201, this number also can be written as 2:0:1:0!). In fact the factorial number system itself is not truly a numeral system in the sense of providing a representation for all natural numbers using only a finite alphabet of symbols, as it requires an additional separation mark.

There's a lot more that could be said about this topic and I may add to it in the future. In conclusion, DCODE provides a quick conversion tool from decimal to factorial base and back again. It's an interesting site that I've used before but had forgotten about. It's a site well worth exploring.

Monday, 10 August 2020

Palindromic Cyclops Numbers

The photo pretty well sums up how I'm feeling today and today is a palindromic cyclops number day. I'm currently 26062 days old. My last such day was obviously 25052 and my next will be 27072. Mathematically, there is nothing deeply significant about such numbers. They are a peculiarity of the base ten number system. In other number bases, the number is not palindromic, as can be seen in Figure 1:

Figure 1: source

By contrast, primeness is intrinsic to the number itself and is independent of the base being used to express the number. The base-dependent peculiarities however, still exert a powerful fascination over numberphiles. Figure 2 shows a fancy font representation of 26062. Sometimes it's nice to just enjoy the symmetry of a number and to admire its representation in an artistic font.



In a more mathematically appreciative world, people might celebrate their palindromic cyclic days in the same way that they celebrate their birthdays. They only occur about every three years, so they are rather special. Instead, these days pass by unnoticed for most of humanity. In 600 days time, I'll turn 26662 days old. As well as being palindromic, the central three digits form the number of the beast: 666.

Digit patterns such as 26062 displays are also useful in strengthening numeracy in young children. A simple exercise might be as follows: given the digits 0, 2, 2, 6, 6, arrange them in such a way that the resultant number reads the same from left to right. Two arrangements of course are possible: 26062 and 62026.

From the shallow waters of exercises like the previous one, it's easy to get into deep waters fairly quickly. For example, 26062 has the property that, when it is tripled and 1 is added, the result is also a palindrome: 78187. This tripling and adding one reminded me of the Collatz conjecture that I've written about in earlier posts. However, in the Collatz the rule is that for an even number like 26062, the number is halved and so, in the case of 26062, the result is 13031 (incidentally, also palindromic). I got to thinking: how would a sort of reversed Collatz sequence behave? What is I mean is explained by the following rule for a given number \(n\):
  • if \(n\) is even, \(n \rightarrow 3n+1\)
  • if \(n\) is odd, \( n \rightarrow \dfrac{n-1}{2} \)
Now the Collatz rule is the opposite of this and, as far as is known, always leads to 1. However, for this reversed version, I found that the sequence started to loop once it reached 40 (going back to 121 - shown in blue). Here is the sequence:

26062, 78187, 39093, 19546, 58639, 29319, 14659, 7329, 3664, 10993, 5496, 16489, 8244, 24733, 12366, 37099, 18549, 9274, 27823, 13911, 6955, 3477, 1738, 5215, 2607, 1303, 651, 325, 162, 487, 243, 121, 60, 181, 90, 271, 135, 67, 33, 16, 49, 24, 73, 36, 109, 54, 163, 81, 40

A little experimenting showed that some numbers go to 1. For example, here is the sequence for 1000:

1000, 3001, 1500, 4501, 2250, 6751, 3375, 1687, 843, 421, 210, 631, 315, 157, 78, 235, 117, 58, 175, 87, 43, 21, 10, 31, 15, 7, 3, 1

999 on the other hand enters a different loop to that of 26062. It would be interesting to explore the different results in a future post but here I'm just illustrating how there can be complexity hiding in apparent simplicity when exploring the properties of a number.

While on the subject of 26062, it can be noted that it has the interesting property that the sum of its prime divisors is also a palindrome (242). It is in fact a member of OEIS A046354:


A046354

Composite palindromes whose sum of prime factors is palindromic (counted with multiplicity).


The sequence runs 4, 6, 8, 9, 121, 292, 444, 575, 717, 828, 989, 1331, 2002, 4884, 5445, 8668, 9559, 10201, 11211, 11811, 13231, 14241, 14541, 14641, 15251, 15751, 16261, 16761, 18281, 19291, 19591, 20002, 21112, 21312, 22022, 22922, 23832, 26062, ...

It can be generated using the following SageMath code (permalink):

P=[]
for number in [1..26062]:
    N=number.digits()
    s=""
    for n in N:
        s+=str(n)
    if number==int(s):
        P.append(number)
L=[]
for p in P:
    if is_prime(p)==0:
        sum=0
        M=list(factor(p))
        for m in M:
            sum+=m[0]*m[1]
        if sum in P:
            L.append(p)
print(L)

Monday, 10 June 2019

Lotto Loser

Figure 1
Figure 1 shows my Australian Gold Lotto results for Saturday night, the 1st June 2019. Once again, a prize of any sort eluded me but I was naturally struck by the fact that across my four games, I had all the winning numbers: 6, 9, 20, 25, 27 and 31 with 9 being chosen twice and each of the other numbers once.

Immediately, I wondered what was the probability of such an occurrence. Specifically, what were my chances of choosing all six winning numbers across four games but not have more than three winning numbers in any one game (prizes are awarded for four or more winning numbers). I'm going to ignore supplementary numbers in this analysis and only consider the red winning numbers.

I've pondered Lotto probabilities before in an earlier post titled Losing at Lotto (17th March 2018) in which I investigated the probability of not getting any numbers (red or blue) in four games. That turned out to be about 0.0066 or 0.66%. I made other posts about Lotto including Lotto Simulations (20th October 2018), The Mersenne Twister (20th January 2019 )and Oz Lotto (31st May 2017).

Anyway, back to problem under consideration. In no game can I have more than three winning numbers or else I'd win a prize. So a constant for each game is that I need to choose 3 losing numbers out of the 39 available. There are 39 x 38 x 37 ways of doing this. What I've done in Figure 2 is to show the range of minimum possible configurations (where there is no repetition of winning numbers across the four games). From those configurations I've worked out the remaining possibilities, given the constraints that there cannot be more than three winning numbers in any game and there cannot be a repeated number in any game.

Figure 2

We'll tackle each in turn:

Configuration 1, top left, multiplying rows: 

6 x 5 x 4 x 39 x 38 x 37
3 x 2 x 1 x 39 x 38 x 37
45 x 44 x 43 x 39 x 38 x 37
45 x 44 x 43 x 39 x 38 x 37   

Configuration 2, top middle, multiplying rows: 

6 x 5 x 43 x 39 x 38 x 37 
4 x 3 x 43 x 39 x 38 x 37
2 x 1 x 43 x 39 x 38 x 37
45 x 44 x 43 x 39 x 38 x 37   

Configuration 3, top right, multiplying rows: 

6 x 5 x 4 x 39 x 38 x 37
3 x 2 x 43 x 39 x 38 x 37
1 x 44 x 43 x 39 x 38 x 37 
45 x 44 x 43 x 39 x 38 x 37 

Configuration 4, bottom left, multiplying rows:

6 x 5 x 43 x 39 x 38 x 37
4 x 3 x 43 x 39 x 38 x 37
2 x 44 x 43 x 39 x 38 x 37
1 x 44 x 43 x 39 x 38 x 37

Configuration 5, bottom middle, multiplying rows:

6 x 5 x 4 x 39 x 38 x 37
3 x 44 x 43 x 39 x 38 x 37
2 x 44 x 43 x 39 x 38 x 37
1 x 44 x 43 x 39 x 38 x 37   

Each product on each line is divided by \(^{45}p_5\) or 45 x 44 x 43 x 42 x 41 x 40 = 5,864,443,200 and all lines are multiplied together within a configuration. The probabilities for all configurations are then added because they are mutually exclusive. The results are shown in Figure 2 (a spreadsheet snapshot).

Figure 3
The chances work out to be slightly less than two in ten million (1.89), whereas the probability of getting all six numbers in one game is slightly more than one in ten million (1.23). To me, this result seems far too low. 

Another approach involves the use of combinations, which I tried initially but dismissed because the probability seemed far too high. However, I'll revisit that approach here and see what I come up with a second time around. The essential points in this approach are:

  • there are four games
  • there are 45C6 ways of choosing the six numbers in each game
  • up to three winning numbers (6C3) can be chosen in each game
  • conversely, there must be three losing numbers (38C3) in each game
  • all six winning numbers must be chosen across the four games

The configurations shown in Figure 3 can be reused but this time with combinations. See Figure 4 below.

Figure 4

Each of the line above needs to be divided by 45C6 to determine the probabilities which turn out to be:
  • 0.00638281628563513 for configuration 1 
  • 0.321868017603801 for configuration 2 
  • 0.0548922200564621 for configuration 3 
  • 0.472073092485574 for configuration 4 
  • 0.161017178832289 for configuration 5
  • 1.01623332526376 overall
Clearly something is terribly wrong as probability cannot exceed 1. Below is the SageMath code I used for the calculation:
C11=binomial(6,3) #6C3 x 39C3 C12=binomial(3,3) #3C3 x 39C3 C13=binomial(45,3) #45C3 x 39C3
C14=binomial(45,3) #45C3 x 39C3
C21=binomial(6,2)*binomial(43,1) #6C2 x 43C1 x 39C3
C22=binomial(4,2)*binomial(43,1) #4C2 x 43C1 x 39C3
C23=binomial(2,1)*binomial(43,1) #2C1 x 43C1 x 39C3 C24=binomial(45,3) #45C3 x 39C3 C31=binomial(6,3) #6C3 x 39C3
C32=binomial(3,2)*binomial(43,1) #3C2 x 43C1 x 39C3
C33=binomial(1,1)*binomial(44,2) #1C1 x 44C2 x 39C3
C34=binomial(45,3) #45C3 x 39C3 C41=binomial(6,2)*binomial(43,1) #6C2 x 43C1 x 39C3
C42=binomial(4,2)*binomial(43,1) #4C2 x 43C1 x 39C3 C43=binomial(2,1)*binomial(44,2) #2C1 x 44C2 x 39C3
C44=binomial(1,1)*binomial(44,2) #1C1 x 44C2 x 39C3
C51=binomial(6,3) #6C3 x 39C3 C52=binomial(3,1)*binomial(44,2) #3C1 x 44C2 x 39C3
C53=binomial(2,1)*binomial(44,2) #2C1 x 44C2 x 39C3 C54=binomial(1,1)*binomial(44,2) #1C1 x 44C2 x 39C3
C1=n(C11*C12*C13*C14*(binomial(39,3)/binomial(45,6))^4)
C2=n(C21*C22*C23*C24*(binomial(39,3)/binomial(45,6))^4)
C3=n(C31*C32*C33*C34*(binomial(39,3)/binomial(45,6))^4)
C4=n(C41*C42*C43*C44*(binomial(39,3)/binomial(45,6))^4)
C5=n(C51*C52*C53*C54)*((binomial(39,3)/binomial(45,6))^4)
print C1, C2, C3, C4, C5
p=C1+C2+C3+C4+C5
print p
I'll need to return to this and try to resolve the problem. My first result is vanishingly small and my second is impossibly high!

Tuesday, 1 January 2019

Sphenic Numbers Revisited

After this post, I discovered that I'd already made an earlier post about sphenic numbers. No matter but it alerted me to the fact that I've made so many posts to this mathematics blog that I'm losing track of what I've posted.

Today I turned 25474 days old. This number factors to 2 * 47 * 271. Yesterday's number, 25473, factors to 3 * 7 * 1213. Both are sphenic numbers, described by Numbers Aplenty as follows:
A number \(n\) is called sphenic if it is the product of 3 distinct primes. For example, 370 is a sphenic number because it is the product of the 3 primes 2, 5 and 37. Sphenic numbers are quite common: up to \(10^8\) there are 20710806 sphenic numbers (that's about 20%). 
The sum of the reciprocals of the sphenic numbers diverges, while the sum of the reciprocal of their squares converges to \(0.003696244...\), which can be expressed as: $$ \frac{(P(2)^3-3 \, P(2) \, P(4)+2 \,P(6))}{6}\\ \text { where }P(s)=\sum_{p\mathrm{\ prime}}\frac{1}{p^s}$$ is the so-called prime Zeta function. 
The first sphenic numbers are 30, 42, 66, 70, 78, 102, 105, 110, 114, 130, 138, 154, 165, 170, 174, 182, 186, 190, 195, 222, 230, 231, 238, 246, 255, 258, 266, 273, 282, 285, 286, 290, 310
Wikipedia adds that:
All sphenic numbers have exactly eight divisors. If we express the sphenic number as \( n = p \cdot q \cdot r\) where \(p\), \(q\), and \(r\) are distinct primes, then the set of divisors of \(n\) will be \({1, p, q, r, pq, pr, qr, n}\). The converse does not hold. For example, 24 is not a sphenic number, but it has exactly eight divisors. 
All sphenic numbers are by definition squarefree, because the prime factors must be distinct. 
The first case of two consecutive sphenic integers is 230 = 2×5×23 and 231 = 3×7×11. The first case of three is 1309 = 7×11×17, 1310 = 2×5×131, and 1311 = 3×19×23. There is no case of more than three, because every fourth consecutive positive integer is divisible by 4 = 2×2 and therefore not squarefree. 
The numbers 2013 (3×11×61), 2014 (2×19×53), and 2015 (5×13×31) are all sphenic. It's interesting that these very recent calendar years formed a sphenic triplet, although I didn't know it at the time I was living through them. The next three consecutive sphenic years will be 2665 (5×13×41), 2666 (2×31×43) and 2667 (3×7×127) (see OEIS A248202 for a list of the central number of such triples). 
Sphenic Brick
In terms of geometry, each sphenic number can be considered to represent the volume of a unique and "primitive" rectangular prism (sometimes called a sphenic brick) whose dimensions are given by its three prime factors. I'm using primitive here in the same sense as "primitive Pythagorean triad" such as 3, 4 and 5 (as opposed to 6, 8 and 10). By its definition however, a sphenic number can never represent the volume of a cube or a rectangular prism with a square cross-section.

Each sphenic number \( n = p \cdot q \cdot r\) can be associated with another number, namely the surface area of the rectangular prism  \( 2 \, (p \cdot q + p \cdot r+q \cdot r) \). For example, the sphenic number  \( 7429 = 17 \cdot 19 \cdot 23\) can be viewed as a rectangular prism with an associated surface area of \(2302\) square units. The ratio between area and volume can then be explored. The table below shows the values of such ratios for sphenic numbers between 25400 and 25500:


It is possible for the volume and surface area to be equal. In a range of numbers between 1 and 1000, the only such dimensions that produce this are:
  • 3 x 7 x 42   --> 882 
  • 3 x 8 x 24   --> 576
  • 3 x 9 x 18   --> 486
  • 3 x 10 x 15 --> 450
  • 4 x 5 x 20   --> 400
  • 4 x 6 x 12   --> 288
Whether these are the only values with this property I don't know but none of the above numbers (288, 400, 450, 486, 576 and 882) are sphenic so it's likely that there are no sphenic numbers with this property.

To determine the sphenic numbers within a given range, this SageMath code (link to SageMathCell server) can be used or the box below (sometimes temperamental) can be experimented with:


Friday, 4 May 2018

Polygonal Number Generating Function and Formula

Today my diurnal age is 25233, a number that happens to be a polygonal number, specifically a 32-gonal number. A WolframAlpha article states that the generating function for the n-gonal numbers is given by: $$G_n(x)= \frac{x \, [(n-3)x+1]}{(1-x)^3} $$ This means that for the 32-gonal numbers, the formula becomes: $$G_{32}(x)= \frac{x \, (33x+1)}{(1-x)^3}$$In WolframAlpha, the coefficients can then be identified using the series command:


In Sage, the Taylor series is generated using the code:
g(x)=x*(33*x+1)/(1-x)^3
g.taylor(x,0,15).coefficients()
When run, this code produces the following output:


In the case of the 36-gonal numbers, the formula for the n-th term is given by:$$a(n)=n(17n-16) $$The general formula for the n-th polygonal number is given by:$$a(n)=(P-2) \frac{n(n+1)}{2}-(P-3)n$$where \(P\) is the number of vertices of the polygon.

In the case of \(P=36 \) (our 36-gon), the formula becomes:$$34\frac{n(n+1)}{2}-33n=n(17n-16) $$Here's a video describing how the formula is derived:

Thursday, 19 April 2018

Sage Reference Manual

I first made a reference to SageMath in a previous blogpost on the 4th January 2017. At that time I installed it but, after tinkering with it a little, I forgot about it until recently. However, my interest has been rekindled and I've installed the latest version (8.1) on my laptop running macOS High Sierra 10.13.3. The online reference manual is located here but instead of a single PDF file, there is an assortment of 70+ PDF files. Because I'm travelling overseas shortly and may not always have Internet access, I wanted to download all the PDF files to have on my laptop for reference.

Fortunately, there is an extension for Chrome called Batch File Downloader that lets you download many links from a  website easily. Here is what the interface looks like:


As the name says, the extension created a batch file that allowed me to download all the *.pdf files in the online directory that it was pointed at. The total size of all files is a little over 80 MB with plotting.pdf (44.9 MB) and plot3d.pdf (20.8 MB) being the two largest. Most of the others are around 1 MB in size. 

Since SageMath uses Python, I also installed the Python  programming language on my laptop. However, my focus will be on getting more proficient at using SageMath. It's tedious to find in Python that most of the basic mathematical functions are missing. For example, if in SageMath one inputs factor(25217), then 151 * 167 is received as output. However, in Python the code must be created:

x, y = 25217, int(25217/2)
for i in range(2, y):
     if x % i == 0:
     print(i)

So the adventure begins. In the meantime, I've come across an interesting site created by a mathematics professor called Gregory V. Bard who writes:
The beauty of Sage is that it works through the internet. There is almost never any reason to do a local install of Sage on your laptop or home computer. This is good news, because it saves a lot of headaches and hassles (especially for students), that you would have to suffer if you were using Mathematica, Maple, Matlab, or Magma. The exception is if you have limited or no internet access, such as in rural areas. 
Internet access involves using the SageMathCell Server which as Professor Bard says is a competitor to WolframAlpha and up until recently was called Sage-Aleph. This is the interface:

Professor Bard has written a book called Sage for Undergraduates that is available as a zipped pdf file by clicking on the link. His site is up-to-date but is "preserving the look-and-feel of the World Wide Web as it was, in 1998".

Using SageMath, I'll be able to wean myself off WolframAlpha which, up until now, I've relied on completely for the factorisation of the number corresponding to my diurnal age and, if the factors are supportive, determining how this number can be expressed as a sum of two squares. Now, using SageMathCell or my own notebook, I can determine both.

Code that is created in SageMathCell can be shared via temporary or permanent links and also by a QR code which conveniently encodes the permalink. The permalink can be quite long and ugly because it encodes the entire block of code. Here is QR code that I generated:

If the QR code is scanned with a QR code reader, you will be taken to the SageMathCell Server where the following code should appear: