Finding a digit in a specific position

Click For Summary

Discussion Overview

The discussion revolves around finding the kth digit of a large prime represented as ##2^t - 1##, where t is a 6-digit decimal number. Participants explore various methods for calculating this digit, including programming approaches and the use of SQL.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests using SQL string functions to find the kth digit but expresses uncertainty about input requirements.
  • Another participant questions how SQL would handle the calculation of ##2^t##, noting that converting binary to decimal is straightforward with efficient algorithms.
  • Several participants identify the number as a Mersenne Prime and mention the existence of only five such primes that meet the specified condition, with significant decimal digit lengths.
  • One participant argues that SQL is not suitable for this task, as it is primarily designed for database information extraction.
  • A later reply proposes that SQL Server's integration with Python and machine learning could potentially enhance the process, although it acknowledges the speculative nature of this idea.
  • Another participant questions the utility of using SQL to invoke Python, suggesting that running Python directly would be more efficient.
  • One participant reflects on the complexity of using advanced tools for simple tasks, likening it to using a high-performance car for mundane errands.
  • A technical approach is presented that involves writing t as a binary number and using two decimal registers to calculate the kth digit through iterative doubling and multiplication.

Areas of Agreement / Disagreement

Participants express differing opinions on the appropriateness of using SQL for this calculation, with some arguing against it while others propose potential integrations. The discussion includes multiple approaches and remains unresolved regarding the best method to find the kth digit.

Contextual Notes

Participants mention the need for Big Integer libraries and the limitations of certain browsers for executing JavaScript solutions. There are also unresolved assumptions about the efficiency and practicality of various proposed methods.

WWGD
Science Advisor
Homework Helper
Messages
7,796
Reaction score
13,095
TL;DR
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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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.
 

Similar threads

Replies
17
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
14
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K