Problem 60948. Check p² = 24k + 1, p > 3, the 'golden prime squares' equation

Historical context
In december 2023, I / Nicolas Douillet was working on prime squares properties and I found* the formula :
which I named the ‘Christmas eve equation'. From it I then wrote :
and I named it the ‘golden prime squares equation’. These two formulas, which are actually the same one, can easily be proven starting from (note this last formula applies not only to prime numbers).
* Apparently, I rediscovered it since I found it a posteriori on this excellent French math website : Site de Gérard Villemin
Problem statement
Your goal here is to check this formula for the first primes less or equal to a given integer m (input). To do so you will simply compute k in a vector. For the two first cases where the formula doesn’t apply -2 and 3- your function should return 0.
Examples
  • If m = 20 then k = [0, 0, 1, 2, 5, 7, 12, 15], since 5^2 = 24x1 + 1, 7^2 = 24x2 + 1, 11^2 = 24x5 + 1, etc…
  • If m = 40 then k = [0, 0, 1, 2, 5, 7, 12, 15, 22, 35, 40, 57] ;
Fobidden functions
  • regexp
  • str2num
  • assignin
See also (my file exchange contributions using this equation) :

Solution Stats

72.0% Correct | 28.0% Incorrect
Last Solution submitted on Aug 18, 2025

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers17

Suggested Problems

More from this Author42

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!