Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Blum integers - friendly challenge

mathguy4 days ago

Blum integers are positive integers of the form N = p x q where p < q are prime numbers, both congruent to 3 modulo 4. The smallest such primes are 3, 7, 11 (but not 2, 5 and 13). The smallest Bloom number is 21 = 3 x 7; the next two are 33 = 3 x 11 and 57 = 3 x 19. In particular, the third one is NOT 7 x 11; 57 is less than 77. And 69 = 3 x 23 is also smaller than 77. (I use lower-case x for multiplication, as the automatic formatter on this page does silly things with asterisk.)

Blum integers have nice number-theoretic properties which make them suitable for use in cryptography, but that is irrelevant for the problem I am proposing.

Trivially, the last digit of every Blum integer (in base 10) is 1, 3, 7 or 9. The challenge, stolen shamelessly from rosetta code, is to find out exactly how many of the first 400,000 Blum integers ("first" ordered by value, ascending) have the last digit 1, how many have the last digit 3, how many end in 7 and how many end in 9. To verify your solutions, the correct answer for “last digit = 7” is 99,989.

I can't think of a way to answer this question without actually finding the first 400,000 Blum integers explicitly and then inspecting the last digit. So really the problem is to find the first 400,000 Blum integers; the concluding question about the last digit just makes the required output reasonably small.

There are various ways to approach this - and then, for a set approach, there are various ways to write the code. Both “approach” and “implementation” may have significant effect on the efficiency of the solution. I won't comment on any of these at this time, since I don't want to influence anyone either in favor or against any idea. Working on a solution, I thought of a few things which make sense to me - I'll ask for critique after you get a chance to come up with your own.

Finding all prime numbers up to X (or the first Y prime numbers) may or may not be part of the solution. Assume all prime numbers we may need (let's say up to 20 million) are already stored in a table PRIMES with a single column P designated as primary key. One way to populate such a table in PL/SQL is given on rosetta code under “Sieve of Eratosthenes” where I adapted the solution I proposed at the end of https://forums.oracle.com/ords/apexds/post/sql-puzzle-prime-numbers-1585 However, don't forget that for Blum integers, only primes congruent to 3 modulo 4 are relevant.

This post has been answered by James Su on Jan 8 2026
Jump to Answer
Comments
Post Details
Added 4 days ago
16 comments
220 views