Can I bound my solution to Project Euler # 37? - primes

I'm working my way through the Euler project and have come to this one:
The number 3797 has an interesting property. Being prime itself, it is
possible to continuously remove digits from left to right, and remain
prime at each stage: 3797, 797, 97, and 7. Similarly we can work from
right to left: 3797, 379, 37, and 3.
Find the sum of the only eleven primes that are both truncatable from
left to right and right to left.
NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
The solution, given that there are only 11 of these primes, is straightforward enough (although I'm sure you can be as clever as you want about optimization) and I won't give away the answer.
But how do we know there are only 11? That's just given, with no explanation. I haven't found a proof of this after a fair amount of searching, so does anyone know why we can make this assumption?

Looking at this paper By I. 0. Angelí and H. J. Godwin, they have found that . . .
the largest left truncatable prime is 357686312646216567629137 (base 10)
the largest right truncatable prime is 73939133 (base 10)
Using this information, you can examine all numbers between 0-73939133 and find the numbers that are both right and left truncatable.

Related

How to find 9s complement of a given decimal number?

Given decimal number=12
One way to find 9s complement is to simply find 10s complement and subtract 1. By this method I get answer=87.
However can i solve this problem by first finding the equivalent of 12 in base 9 number system which would be 13. Now if I find 9s complement of this number using the formula r^n -N answer would be 81-12.
Please suggest what's wrong ?

Find Numbers Equal to or Greater than 1 using regex

I'm not a developer or scripter. I can't contribute much to this forum but I do use it to get guidance for my staff of developers. That's my disclaimer because the last time I was on this site, someone reamed me out for asking questions and not contributing. For this, I do apologize.
If anyone is willing to assist, or at least give me a kick-start, how would I find the version of a file if the version has #.#.###; i.e., 6.1.3890?
So, my goal is to find a number that is equal to or greater than 1 and equal to or greater than 389. I am only concerned with the digits after the first l'.' and the second '.'
Thanks to any and all.
A regex for a number greater than 389:
(39[0-9]|[4-9][0-9][0-9]|[1-9][0-9][0-9][0-9]+)
A regex for a number greater than 1:
([2-9]|[1-9][0-9]+)
A combined regex for version above 6.1.389:
(6\.1\.(39[0-9]|[4-9][0-9][0-9]|[1-9][0-9][0-9][0-9]+)|6\.([2-9]|[1-9][0-9]+)\.[0-9]+|([7-9]|[1-9][0-9]+)\.[0-9]+\.[0-9]+)
Non zero numbers should not start with a 0.
If the version number format is limited to #.#.### or possibly fewer digits for the last part, the regex can be simplified to:
(6\.1\.(39[0-9]|[4-9][0-9][0-9])|6\.[2-9]\.[0-9]+|[7-9]\.[0-9]\.[0-9]+)

What do the numbers in square brackets mean beside the TAoCP exercises?

Here is an example:
[00] The binary form of 2009...
[05] Which of the letters...
[10] Four-bit quantities -- half-bytes, or hexadecimal digits...
[15] A kilobyte...
[M13] If x is any string of 0s and 1s...
[M20] Prove or disprove...
What do the [00], [05], [10], [15], [M13], [M20] mean?
I have tried:
Googling taocp exercises square brackets
looking for a pattern in the square-bracketed numbers.
they both increase and decrease
they are mostly but not all multiples of five
the ones with an M appear every now and then
M is the only prefix
the codes are non-unique
Googling "the art of computer programming" exercises brackets
Googling "the art of computer programming" M13
Viewing http://dl.acm.org/citation.cfm?id=1312683, which indicates M means milestone.
Googling "the art of computer programming" [00]
Looking for an appendix in the book that explains
Considering the > that is beside some questions too
No luck!
On page xvii in the text there is a summary of the notation used with the exercises
► = recommended
M = Mathematically oriented
HM = Requiring "higher math"
00 = Immediate
10 = Simple (one minute)
20 = Medium (quarter hour)
30 = Moderately Hard
40 = Term Project
50 = Research Problem
It's meant to be a roughly logarithmic scale.
Furthermore "The remainder of the rating number divided by five indicates the amount of detailed work required. Thus , an exercise rated 24 may take longer to solve than an exercise that is rated 25, but the latter will require more creativity" -P. XVI (notes on the exercises)
I think this is mentioned in the introduction to the book somewhere (my copy is in my office now). If I remember correctly, the numbers indicate difficulty, with numbers beginning with 0 being warm-up questions, numbers beginning with 3 indicating problem-set level questions, numbers beginning with 4 indicating very hard problems, and 50 meaning extremely hard (possibly open) questions.
The M means "math," as in "you'll need some tricky math here." The HM means "higher math," meaning "you'll need math beyond what we've covered here to solve this problem."
Hope this helps!

Regex for binary multiple of 3

I would like to know how can I construct a regex to know if a number in base 2 (binary) is multiple of 3. I had read in this thread Check if a number is divisible by 3 but they dont do it with a regex, and the graph someone drew is wrong(because it doesn't accept even numbers). I have tried with: ((1+)(0*)(1+))(0) but it doesn't works for some values. Hope you can help me.
UPDATE:
Ok, thanks all for your help, now I know how to draw the NFA, here I left the graph and the regular expresion:
In the graph, the states are the number in base 10 mod 3.
For example: to go to state 1 you have to have 1, then you can add 1 or 0, if you add 1, you would have 11(3 in base 10), and this number mod 3 is 0 then you draw the arc to the state 0.
((0*)((11)*)((1((00) *)1) *)(101 *(0|((00) *1 *) *0)1) *(1(000)+1*01)*) *
And the other regex works, but this is shorter.
Thanks a lot :)
I know this is an old question, but an efficient answer is yet to be given and this question pops up first for "binary divisible by 3 regex" on Google.
Based on the DFA proposed by the author, a ridiculously short regex can be generated by simplifying the routes a binary string can take through the DFA.
The simplest one, using only state A, is:
0*
Including state B:
0*(11)*0*
Including state C:
0*(1(01*0)*1)*0*
And include the fact that after going back to state A, the whole process can be started again.
0*((1(01*0)*1)*0*)*
Using some basic regex rules, this simplifies to
(1(01*0)*1|0)*
Have a nice day.
If I may plug my solution for this code golf question! It's a piece of JavaScript that generates regexes (probably inefficiently, but does the job) for divisibility for each base.
This is what it generates for divisibility by 3 in base 2:
/^((((0+)?1)(10*1)*0)(0(10*1)*0|1)*(0(10*1)*(1(0+)?))|(((0+)?1)(10*1)*(1(0+)?)|(0(0+)?)))$/
Edit: comparing to Asmor's, probably very inefficient :)
Edit 2: Also, this is a duplicate of this question.
For some who is learning and searching how to do this:
see this video:
https://www.youtube.com/watch?v=SmT1DXLl3f4&t=138s
write state quations and solve them with Axden's Theorem
The way I did is visible in the image-result is the same as pointed out by user #Kert Ojasoo. I hope i did it corretly because i spent 2 days to solve it...
n+2n = 3n. Thus, 2 adjacent bits set to 1 denote a multiple of 3. If there are an odd number of adjacent 1s, that would not be 3.
So I'd propose this regex:
(0*(11)?)+

Prime backwards in order

An emirp (prime spelled backwards) is a pime number whose reversal is also prime. Ex. 17 & 71. I have to write a program that displays the first 100 emirps. It has to display 10 numbers per line and align the numbers properly:
2 3 5 7 11 13 17 31 37 71
73 79 97 101 107 113 131 149 151 157.
I have no cue what I am doing and would love if anyone could dump this down for me.
It sounds like there are two general problems:
Finding the emirps.
Formatting the output as required.
Break down your tasks into smaller parts, and then you'll be able to see more clearly how to get the whole task done.
To find the emirps, first write some helper functions:
is_prime() to determine whether a number is prime or not
reverse_digits() to reverse the digits of any number
Combining these two functions, you can imagine a loop that finds all the numbers that are primes both forward and reversed. Your first task is complete when you can simply generate a list of those numbers, printing them to the console one per line.
Next, work out what format you want to use (it looks like a fixed format of some number of character spaces per number is what you need). You know that you have 100 numbers, 10 per line, so working out how to format the numbers should be straightforward.
Break the problem down into simpler sub-problems:
Firstly, you need to check whether a number is prime. This is such a common task that you can easily Google it - or try a naive implementation yourself, which may be better given that this is homework.
Secondly, you need to reverse the digits of a number. I'd suggest you figure out an algorithm for this on a piece of paper first, then implement it in code.
Put the two together - it shouldn't be that hard.
Format the results properly. Printing 10 numbers per line is something you should be able to figure out easily once the rest is done.
Once you have a simple version working you might be able to optimise it in some way.
A straight forward way of checking if a number is prime is by trying all known primes less than it and seeing if it divides evenly into that number.
Example: To find the first couple of primes
Start off with the number 2, it is prime because the only divisors are itself and 1, meaning the only way to multiple two numbers to get 2 is 2 x 1. Likewise for 3.
So that starts us off with two known primes 2 and 3. To check the next number we can check if 4 modulo 2 equals 0. This means when divide 2 into 4 there is no remainder, which means 2 is a factor of 4. Specifically we know 2 x 2 = 4. Thus 4 is not prime.
Moving on to the next number: 5. To check five we try 5 modulo 2 and 5 modulo 3, both of which equals one. So 5 is prime, add it to our list of known primes and then continue on checking the next number. This rather tedious process is great for a computer.
So on and so forth - check the next number by looping through all previous found primes and check if they divide evenly, if all previously found primes don't divide evenly, you have a new prime. Repeat.
You can speed this up by counting by 2's, since all even numbers are divisible by two. Also, another nice trick is you don't have to check any primes greater than the square root of the number, since anything larger would need a smaller prime factor. Cuts your loops in half.
So that is your algorithm to generate a large list of primes.
Collect a good chunk of them in an array, say the first 10,000 or so. And then loop through them, reverse the numbers and see if the result is in your array. If so you have a emirp, continue until you get the first 100 emirps
If the first 10,000 primes don't return 100 emirps. Move on to the next 10,000. Repeat.
For homework, I would use a fairly simplistic isPrime function, pseudo-code along the lines of:
def isPrime (num):
set testDiv1 to 2
while testDiv1 multiplied by testDiv1 is less than or equal to num:
testDiv2 = integer part of (num divided by testDiv1)
if testDiv1 multiplied by testDiv2 is equal to num:
return true
Add 1 to testDiv1
return false
This basically checks whether the number is evenly divisible by any number between 2 and the square root of the number, a primitive primality check. The reson you stop at the square root is because you would have already found a match below it if there was one above it.
For example 100 is 2 times 50, 4 times 25, 5 time 20 and 10 times 10. The next one after that would be 20 times 5 but you don't need to check 20 since it would have been found when you checked 5. Any positive number can be expressed as a product of two other positive numbers, one below the square root and one above (other than the exact square root case of course).
The next tricky bit is the reversal of digits. C has some nice features which will make this easier for you, the pseudo-code is basically:
def reverseDigits (num):
set newNum to zero
while num is not equal to zero:
multiply newnum by ten
add (num modulo ten) to newnum
set num to the integer part of (num divided by ten)
return newNum
In C, you can use int() for integer parts and % for the modulo operator (what's left over when you divide something by something else - like 47 % 10 is 7, 9 % 4 is 1, 1000 % 10 is 0 and so on).
The isEmirp will be a fairly simplistic:
def isEmirp (num):
if not isPrime (num):
return false
num2 = reverseDigits (num)
if not isPrime (num2):
return false
return true
Then at the top level, your code will look something like:
def mainProg:
create array of twenty emirps
set currEmirp to zero
set tryNum to two
while currEmirp is less than twenty
if isEmirp (tryNum):
put tryNum into emirps array at position currEmirp
add 1 to currEmirp
for currEmirp ranging from 0 to 9:
print formatted emirps array at position currEmirp
print new line
for currEmirp ranging from 10 to 19:
print formatted emirps array at position currEmirp
print new line
Right, you should be able to get some usable code out of that, I hope. If you have any questions of the translation, leave a comment and I'll provide pointers for you, rather than solving it or doing the actual work.
You'll learn a great deal more if you try yourself, even if you have a lot of trouble initially.