Showing posts with label Grok. Show all posts
Showing posts with label Grok. Show all posts

Monday, 17 November 2025

Testing AI

I thought I might challenge AI to guess the next number in a given sequence. This was my query:

Can you propose what the next number in this sequence of numbers might be and explain your reasoning$$1, 3, 7, 13, 7, 31, 43, 19, 73, 13, 37, \dots$$First up I asked Grok and got a rather curious response:

Uh-oh, too much information for me to digest all at once. You know, sometimes less is more!

That wasn't much help so I turned to Gemini and ChatGPT. They both accepted the challenge and tried their best but neither came up with anything convincing. Finally I gave the latter the following clue:

I'll give a clue. The numbers are generated by a simple quadratic function (using the integers 0, 1, 2, 3 , ... progressively as input) that collapses composite numbers when they appear according to a simple division rule. 

 Nice clue — that nails it.

The quadratic is \(f(n)=n^2+n+1\) for integer inputs \(n=0,1,2,\dots \). 

The collapse rule: whenever \(f(n) \) is composite, divide it by its smallest prime factor (and repeat if necessary) until the result is prime.

Checking the given terms (I'll show \(n\) \( \rightarrow \) raw \(f(n)\) \( \rightarrow \) collapsed result):

  • \(n=0: f=0^2+0+1=1 \rightarrow 1\)

  • \(n=1: f=1^2+1+1=3 \rightarrow 3\)

  • \(n=2: f=4+2+1=7 \rightarrow 7 \)

  • \(n=3: f=9+3+1=13 \rightarrow13 \)

  • \(n=4: f=16+4+1=21 \rightarrow 21/3=7 \)

  • \(n=5: f=25+5+1=31 \rightarrow 31\)

  • \(n=6: f=36+6+1=43 \rightarrow 43 \)

  • \(n=7: f=49+7+1=57 \rightarrow 57/3=19 \)

  • \(n=8: f=64+8+1=73 \rightarrow 73\)

  • \(n=9: f=81+9+1=91 \rightarrow 91/7=13 \)

  • \(n=10: f=100+10+1=111 \rightarrow 111/3=37\)

All match your sequence.

Now the next input:$$n=11: f(11)=121+11+1=133 \rightarrow 133/7 = 19$$ So the next number in the sequence is 19.

So a little clue and ChatGPT, as it said, nailed it. This was just a little experiment to see what how AI would handle a problem that is actually quite challenging. The sequence is NOT listed in the OEIS by the way.

I challenged ChatGPT to come with a similar challenge and this is what it generated:

Could you propose a similar but different challenge. You should try to be creative and innovative but adhering to the general algorithm, namely generate a sequence of numbers according to a definite rule but modify certain numbers according to another definite rule whenever they satisfy a certain preestablished criterion. Go for it.

Nice — here’s a fresh puzzle in the same spirit as yours: a definite generating rule (a simple quadratic) plus a deterministic “collapse” rule that kicks in whenever the raw value meets a criterion.

I had to take a screenshot of the puzzle as copying and pasting introduces tedious formatting challenges.

Thursday, 13 November 2025

Claude to the Rescue

The number 27983, associated with my diurnal age today, has a property than confers it membership in OEIS A335752: the lexicographically earliest sequence of primes, each two of which sum to a triprime. The sequence begins: 3, 5, 47, 239, 3023, 27983, 53231, ... so let's confirm that these numbers satisfy using 3 as a starting point:
  • \(3 + 5 = 8 =2^3\)
  • \(3 + 47 = 50 = 2 \times 5^2\)
  • \(3 + 239 = 242 = 2 \times 11^2 \)
  • \(3 + 3023 = 3026 = 2 \times 17 \times 89 \)
  • \(3+27983 = 27986 = 2 \times 7 \times 1999\)


The sums above are all triprimes and we will find that any number in the list paired with any other number will produce a triprime. The code for this was included in the OEIS comments but was in Mathematica. I got Gemini to convert it to Python code and then Grok but in both cases the primeomega function was misunderstood with both claiming that primeomega(12) =2 and not 3. 

I didn't fully understand the Python code and was looking to Gemini and then Grok to explain it to me. It was only Claude that succeeded to my satisfaction. Here is the code followed by Claude's explanation. I think Claude will be my go to resource for anything to do with coding.

from sympy import nextprime, primeomega
ps = [3]
for _ in range(10):
    p = nextprime(ps[-1]) 
    while not all(primeomega(x + p) == 3 for x in ps):
        p = nextprime(p)
    ps.append(p)
print(ps)

I'd be happy to explain this code line by line! This code generates a sequence of prime numbers with a special property.

Code Breakdown

from sympy import nextprime, primeomega

Imports two functions from SymPy:
  • nextprime(\(n\)): Returns the next prime number greater than \(n\)
  • primeomega(\(n\)): Returns the total number of prime factors of \(n\) (counting multiplicity). For example, primeomega(12) = 3 because 12 = 2² × 3 (two 2's and one 3)
ps = [3]

Initializes the sequence with the first prime number 3. This list will store our special sequence of primes.

for _ in range(10):

Loops 10 times to find 10 more primes (so we'll have 11 total including the initial 3). The underscore _ is used when we don't need the loop variable.

p = nextprime(ps[-1])

Gets the next prime after the last element in our list. 
  • ps[-1] accesses the last element of the list
  • Initially, this gives nextprime(3) = 5
  • This is our candidate prime to add to the sequence
while not all(primeomega(x + p) == 3 for x in ps):
    p = nextprime(p)

This is the key part! It checks if the candidate prime \(p\) satisfies a special condition:
  • For every number \(x\) already in our list ps
  • The sum \(x + p\) must have exactly 3 prime factors (counting multiplicity)
  • If this condition fails for any \(x\), we move to the next prime candidate
  • This continues until we find a prime where all sums have exactly 3 prime factors
ps.append(p)

Adds the qualified prime to our sequence once it passes the test.

print(ps)

Prints the final sequence** of primes.

Example Walkthrough

Let's trace the first iteration:
1. Start with ps = [3]
2. Try p = 5: Check if 3 + 5 = 8 = 2³ has 3 prime factors ✓ (yes, three 2's)
3. Add 5 to list: ps = [3, 5]
4. Try p = 7: Check 3 + 7 = 10 = 2 × 5 (only 2 factors) ✗
5. Try p = 11: Check 3 + 11 = 14 = 2 × 7 (only 2 factors) ✗
6. Continue until finding a prime that works with both 3 and 5...

This creates a sequence where each new prime maintains the property that when added to any previous prime in the sequence, the result always has exactly 3 prime factors!

*************************

Now if we change the condition to primeomega = 2, we get the following sequence: 3, 7, 19, 139, 859, 8179, 173059, ... where it can be seen there is a big jump from 8179 to 173059. Testing this out using 3 again we see that:
  • \(3+7=10=2 \times 5\)
  • \(3+19=22=2 \times 11\)
  • \(3+139=142=2 \times 71\)
  • \(3+859=862=2 \times 431\)
  • \(3+8179=8182=2 \times 4091\)
  • \(3+173059=173062=2 \times 86531\)
The sequence is included in the OEIS as A113875: slowest growing sequence of primes having the prime-pairwise-average property: if i<j, (a(i)+a(j))/2 is prime. This makes sense in that 2 seems to be the common factor is all of the sums. 

The sequence with additional (very large) terms looks like this:
3, 7, 19, 139, 859, 8179, 173059, 1026199, 1827139, 15828679, 13187242759, 18732483199, 912492556939, 9130567625119

When we set primeomega = 4 we get the following sequence: 3, 13, 137, 1193, 4373, 26017, 33889, 495433, ... which is NOT listed in the OEIS. Again let's use 3 as our test number:
  • \(3+13=16=2^4\)
  • \(3+137=140=2^2 \times 5 \times 7\)
  • \(3+1193=1196=2^2 \times 13 \times 23\)
  • \(3+4373=4376=2^3 \times 547\)
  • \(3+26017=26020=2^2 \times 5 \times 1301\)
  • \(3+33889=33892=2^2 \times 37 \times 229\)
  • \(3+495433=495436=2^2 \times 29 \times 4271\)
That's probably a good place to stop.

Tuesday, 28 October 2025

Prime Number Spiral

The number associated with my diurnal age today is a prime number: 27967 and it has the property that it is on the north spoke of a prime number spiral that begins as shown in Figure 1.


Figure 1: source

I thought it might be an interesting challenge for Gemini to write the Python code to generate this spiral up to the largest prime below 40000. To cut a long story short, Gemini failed miserably despite several attempts to improve the code. Grok on the other hand succeeded on its first try although it did time out several times and needed to be reconnected. However, it got the job done and the result is shown in Figure 2 (link to Grok).


Figure 2: code

The formatting is excellent as can be seen in the detail shown in Figure 3 that was taken from Figure 2:


Figure 3: detail from Figure 2

So Gemini proved to be not up to the task despite my persevering with additional prompts whereas Grok succeeded on its first attempt. A lesson learned.

Monday, 2 June 2025

Google AI Studio and Related Disasters

I created a program in a Jupyter notebook that accepts a single number as input and then displays various properties of the number as a simple text output. I put the following prompt to Google's AI Studio:

The following code is what I created using SageMath. It is designed to accept a single number as input. Typically the number has five digits corresponding to my age in days (I'm currently 27819 days old). I'm using the program to find out interesting numerical facts about this number associated with my diurnal age. By trial and error I've had to limit the number of iterations for various tasks or else the program is too slow or crashes. For example, in finding the home prime for a number I've limited the iterations to just 10. What I want is to improve the output of this code to make it more readable and more informative in terms of additional graphs or charts? Feel free to modify the code to make it more efficient, informative or comprehensible. The code modifications can be in Python or SageMath, whatever is easier for you. I just need the program to run in a Jupyter notebook using an M1 Macbook Air and execute within say a minute or so. Any annotations, charts or tables that will enhance the output of the program will be appreciated. Any additional fun facts about the input number can be added to the program as you see fit. The key challenge is to enhance the output which is currently quite basic and limited to simple text. It's important that spacing between the different sections be consistent and that the various sections are clearly identified. I've added some commentary to each section but this can be expanded or modified if desired. 

What followed was something of a nightmare as the resulting code that was produced was full of bugs that AI Studio was never able to fix. Once I did get the code to run but many of the number properties that were displayed in my original code were no longer there. To be fair, it did produce a much improved output for the sections where it did work as can be seen in Figure 1. However, the attempts to get the code to run properly were frustrating and ultimately futile. 


Figure 1

There are numerous alternatives out there and I'll have to turn to them if I want to generate code that works. I tried ChatGPT and it revised the faulty AI Studio code but stopped at line 106 (there are about 875 lines of code) probably because I'd exceeded my free allowance of tokens. So dead end there.

Grok 3 failed to understand that the Jupyter notebook can be set to SageMath 10.3 not just Python 3 and gave the following comments:

The code you provided appears to be written for SageMath, but you're trying to run it in a Jupyter notebook, which typically uses a Python kernel with libraries like Matplotlib. This mismatch is likely causing some of the errors, as SageMath-specific functions and syntax (e.g., latex(), N(), Integer(), and certain mathematical operations) aren't directly compatible with a standard Python environment. Additionally, there are a few other issues that could lead to errors, such as undefined variables, plotting issues, and missing imports.

I'll modify the code to make it compatible with a standard Jupyter notebook environment using Python 3, while preserving its core functionality. I'll replace SageMath-specific functions with equivalent Python alternatives, ensure proper imports, and fix potential glitches.

After Grok 3 turned all the code to Python 3, it was still full of bugs and never managed to execute all the code. I'm getting the feeling that my original code, written in SageMath, is possibly at the core of the problem. When an AI tries to improve the code, using SageMath or Python, then all sorts of glitches arise because of the modifications to my original code. It would be better to build the whole code base from scratch using Python only and indicating exactly what I want. One thing I've realised is that LaTeX can be applied to the Jupyter notebook output to greatly improve readability.

It occurred to me that I lot of my code just reproduces what a site like Numbers Aplenty always publicly provides. I asked Gemini 2.5 Flash the following question:

Is it possible to create Python code that draws on the content of a site like Numbers Aplenty. For example, let's say I enter 27819 into my program. Can I get the program to then access https://www.numbersaplenty.com/27819 and extract some or all of the information from it. For example, one of the properties of the number that the site displays is "It is a junction number, because it is equal to n+sod(n) for n = 27792 and 27801". Can I get this information to show up as output in my code? If it is feasible then could you create the necessary code to do this or explain why this is not possible.

The response was encouraging:

Yes, it is absolutely possible to create Python code that draws on the content of a site like Numbers Aplenty and extracts specific information. This process is commonly known as web scraping. Numbers Aplenty doesn't seem to offer a public API (Application Programming Interface) for directly accessing its data in a structured format. This means we'll need to use web scraping techniques. 

It then went on to confidently provide code for doing this but of course it didn't work. Excuses were made and additional programs and drivers were recommended for download to remedy the problem. In short, no go. I should try other AI tools but frankly the whole process has exhausted me. I need a rest.