Finding a digit in a specific position

AI Thread Summary
Finding the kth digit of a large prime in the form 2^t - 1, where t is a 6-digit decimal number, can be approached by converting t into binary, which involves about 20 bits. The calculation of 2^t is straightforward using efficient algorithms, and Mersenne Primes, which this form represents, are limited in number, with only five satisfying the condition and having between 30,000 to 260,000 decimal digits. While SQL is primarily designed for database queries and not suited for such computations, recent versions of SQL Server incorporate Python and machine learning capabilities, which could enhance processing. However, using Python directly is recommended for simplicity and efficiency. The method involves using two decimal registers to handle powers of two and requires careful multiplication based on the binary representation of t. After computing, the kth digit can be extracted easily.
WWGD
Science Advisor
Homework Helper
Messages
7,700
Reaction score
12,698
TL;DR Summary
We are given a number as a power. How to find the kth digit of a power.
Hi,
In another forum someone asked how to find the kth digit of a large prime given in the form ##2^t-1 ## , where t is a 6-digit (decimal) number. I was thinking maybe using some SQL string function , though I am not sure if input strings must be entered explicitly. Can anyone think of some other way?
 
Last edited by a moderator:
Computer science news on Phys.org
How would SQL calculate ##2^t##? If you have the number in decimal picking the k'th element is easy.

##2^t## in binary is trivial, so you just have to convert it to decimal. There are efficient algorithms for that.
 
  • Like
Likes WWGD
This is called a Mersenne Prime, and there are only 5 of them that satisfy the given condition with approximately 30,000 to 260,000 decimal digits.

Calculating such large integers is simple in any language with a Big Integer library, or you can download a text file from mersenne.org.

SQL is designed for a single purpose: extracting information from a relational database and would not be an appropriate choice.
 
  • Like
Likes WWGD
pbuk said:
This is called a Mersenne Prime, and there are only 5 of them that satisfy the given condition with approximately 30,000 to 260,000 decimal digits.

Calculating such large integers is simple in any language with a Big Integer library, or you can download a text file from mersenne.org.

SQL is designed for a single purpose: extracting information from a relational database and would not be an appropriate choice.
I agree in general but now SQL Server includes Python ( and an ML Server) and I though maybe these could be combined to make the process more effective. But , yes, apologies to all since my post was mostly speculative with no specifics. Edit: I should have said Sql Server instead ( of just Sql), which is no longer just about Sql but it has evolved into a full data platform, including capabilities for Python, R and machine-learning. It seems all this power may be used to do current computations more effective.
 
Last edited:
  • Like
Likes WWGD
Why would you use SQL to call python if you can simply run python?
I don't see how SQL would do anything useful here.
 
  • Like
Likes pbuk
Sorry, I likely fell for the temptations of big machinery at the expense of simplicity and common sense. It's like owning a Ferrari and just using it to go to the corner store. Curious to take it out on the bahn.
 
WWGD said:
In another forum someone asked how to find the kth digit of a large prime given in the form 2^t-1 , where t is a 6-digit (decimal) number.
Write t as a binary number. For 6 digits you will have about 20 bits, and 20 loops to perform later.
You need two decimal registers, each must hold at least k digits. Beyond k, the carry is irrelevant.

The first register starts at 1 and holds progressively larger powers of two. Each loop you double that register by simply adding it to itself.
The other register starts at 1, whenever a bit of t is set, the second register is multiplied by the powers register.
That requires only a k * k digit multiply.

When you have finished the set binary bits of t, subtract one.
Look at the k'th digit.
 
Back
Top