Finding a digit in a specific position

In summary, SQL can be used to calculate the k'th digit of a large prime given in the form 2^t-1 , where t is a 6-digit (decimal) number. The algorithm is straightforward and requires only a k * k digit multiply.
  • #1
WWGD
Science Advisor
Gold Member
7,003
10,423
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
  • #2
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
  • #3
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
  • #4
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:
  • #5
  • Like
Likes WWGD
  • #6
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
  • #7
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.
 
  • #8
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.
 

1. How do I find the digit in a specific position in a number?

To find a digit in a specific position in a number, you can use the modulus (%) operator. This operator returns the remainder of a division operation. For example, to find the digit in the ones place in the number 123, you can use 123 % 10, which will return 3.

2. Can I find a digit in a specific position in a decimal or floating-point number?

Yes, you can use the same method as mentioned above for decimal or floating-point numbers. For example, to find the digit in the tenths place in the number 3.14, you can use 3.14 % 1, which will return 0.14.

3. What if I want to find a digit in a specific position in a large number?

If the number is too large to be represented as an integer, you can convert it to a string and then use string indexing to access the specific position. For example, to find the digit in the hundreds place in the number 123456789, you can convert it to a string and then access the character at index 5 (indexes start at 0), which will return 6.

4. Is there a way to find multiple digits in specific positions in a number?

Yes, you can use the same method as mentioned above to find multiple digits in specific positions. For example, to find the digits in the tens and hundreds places in the number 123456789, you can use 123456789 % 100, which will return 89.

5. Can I find a digit in a specific position in a negative number?

Yes, you can use the same method as mentioned above for negative numbers. However, keep in mind that the position of the digit will be counted from the end of the number. For example, to find the digit in the ones place in the number -123, you can use -123 % 10, which will return -3.

Similar threads

Replies
9
Views
1K
Replies
3
Views
1K
Replies
1
Views
1K
Replies
4
Views
384
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Feedback and Announcements
Replies
17
Views
2K
Replies
13
Views
2K
Back
Top