Why do we only check up to the square root of a prime number to determine if it is prime? Can't we use cube root? - primes

If a number n can be written as axb and m=sqrt(n). Here n=m*m. We say we only need to check upto m because min(a,b)<=m. So cant we take cube roots? Suppose we take n=21, then n=1x3x7. But Cube root is 2. Why does this method fail?

Consider n = 143 = 11 * 13. The cube root of 143 is between 5 and 6. If you only test divisibility by the primes up to 6, you will not find either of the two factors of n and will mistakenly conclude that 143 is prime.

Related

ACM ICPC - Exactly K bridges in Graph

I'm practicing for ACM ICPC and I came on this problem from 2017 ACM ICPC Arab Regionals:
First, let’s define an undirected connected labeled graph, it’s a graph with N nodes with a unique label
for each node and some edges, there’s no specific direction for each edge, also duplicate edges and edges
from a node to itself aren’t allowed, and from any node you can reach any other node.
A bridge in such graph is an edge that if we remove it, the graph will be disconnected (there will
exist nodes which aren’t reachable from each other).
In this problem you are given N and K, and your task is to count the number of different undirected
connected labeled graphs with exactly N nodes and K bridges. Since that number can be huge, print
it modulo M.
An edge is defined using the labels of the nodes it connects, for example we can say (X, Y ) is an
edge between X and Y , also (Y, X) is considered the same edge (since it’s undirected). Two graphs are
considered different, if there’s an edge which exists in one of them but not the other.
Input:
Your program will be tested on one or more test cases. The first line of the input will be a single integer
T (1 ≤ T ≤ 100) representing the number of test cases. Followed by T test cases.
Each test case will be just one line containing 3 integers separated by a space, N (1 ≤ N ≤ 50), K
(0 ≤ K < N) and M (1 ≤ M ≤ 10^9), which are the numbers described in the statement. It’s guaranteed that N will not be more than 25 in 95% of the test cases
Output:
For each test case, print a single line with the number of graphs as described above modulo M.
Sample Input:
4
3 2 10
3 0 10
6 3 10000
6 3 1000
Sample Output
3
1
2160
160
I tried to come up with some formula that would work for all N and K but I had no success. Can someone tell me what should I do to solve it, because I couldn't find any editorial? Thanks in advance

Project Euler # 51 c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to solve problem 51 in project euler.
Here is the problem statement : Project Euler Problem 51
I tried the approach given below.
Generate all the prime numbers between 2 and 10 power 8 using Sieve of Eratosthenes
Gather all n-digit numbers from sieve (i.e .. first i will get all 3 digits, then 4 digits .. so on and compute on it)
check if the number has repeated digit (as per problem statement we have 2 *s of same digit) . If yes , convert into string
if yes, find the position where it repeats and save it as a key (for ex: 12341 will result 04 as key since 1 is the number which is repeated in 0 and 4th position)
now based on the key , insert into the bucket (Bucket is a multimap which contains key as repeated position (04) and value as the prime number)
For each key in bucket, delete the repeated position . If am taking 04 bucket key , then all the prime numbers in that bucket would have the strings repeated in the positions 0 and 4. Am deleting 0th and 4th position of the string which would result my number (12341) as 234 and insert it into a map , which will store the occurence as (234, 8 being the trimmed number and number of occurences).
Now, if the key 234 is repeated 8 times, find the numbers which got trimmed and resulted 234. Those are the answers.
I ran this algorithm in c++ , and for 7 primes 56003, 56113, 56333, 56443, 56663, 56773, and 56993 is resulting less than a second ..
but , for 8 digits , i crossed 10 power 8 primes and still it didnt result any answer. I believe answer is above that limit.
And when i try to generate primes between 2 and 10 power 9 its aborting , since i am storing all the numbers in vector.
My question is ,
Is there any way by which we can fine tune the above mentioned steps and find the answer,
or i need to think some other way to find the answer .
Note: Just for an example i took 12341.
There is atleast one issue in your brute force solution. You are assuming exactly 2 digits are * but question never mentions this. There may be 1 or 3 or more digits which when replaced with the same digits 0-9 still generate primes.
It is impossible to have 8 prime with 1 or 2 * for the following reason:
If you use just 1 *, and let's say replace it with 1 to get a prime(let's call this prime p). Now if p % 3 = 1, you can not replace * with 0, 3 and 6 otherwise the number would become composite(divisible by 3). Throwing away 3 candidates makes it impossible to generate another prime. Next case if p % 3 = 2 you can not replace * with 2, 5 and 8 for the same reason. Making 8-primes with one * impossible for any number of digits.
If you use just 2 *, and let's say replace both with 1 to get a prime(let's call this prime p). Now if p % 3 = 2, you can not replace both * with 0, 3 and 6 otherwise the number would become composite(divisible by 3). Throwing away 3 candidates makes it impossible to generate another prime. Next case if p % 3 = 1 you can not replace * with 2, 5 and 8 for the same reason. Making 8-primes with two * impossible for any number of digits.
This is the reason why your code does not give the required output. You should perhaps try with 3 * characters.

Create a sequence which is ordered by bits set

I'm looking for a reversible function unsigned f(unsigned) for which the number of bits set in f(i) increases with i, or at least does not decrease. Obviously, f(0) has to be 0 then, and f(~0) must come last. In between there's more flexibility. After f(0), the next 32* values must be 1U<<0 to 1U<<31, but I don't care a lot about the order (they all have 1 bit set).
I'd like an algorithm which doesn't need to calculate f(0)...f(i-1) in order to calculate f(i), and a complete table is also unworkable.
This is similar to Gray codes, but I can't see a way to reuse that algorithm. I'm trying to use this to label a large data set, and prioritize the order in which I search them. The idea is that I have a key C, and I'll check labels C ^ f(i). Low values of i should give me labels similar to C, i.e. differing in only a few bits.
[*] Bonus points for not assuming that unsigned has 32 bits.
[example]
A valid initial sequence:
0, 1, 2, 4, 16, 8 ... // 16 and 8 both have one bit set, so they compare equal
An invalid initial sequence:
0, 1, 2, 3, 4 ... // 3 has two bits set, so it cannot precede 4 or 2147483648.
Ok, seems like I have a reasonable answer. First let's define binom(n,k) as the number of ways in which we can set k out of n bits. That's the classic Pascal triangle:
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
...
Easily calculated and cached. Note that the sum of each line is 1<<lineNumber.
The next thing we'll need is the partial_sum of that triangle:
1 2
1 3 4
1 4 7 8
1 5 11 15 16
1 6 16 26 31 32
1 7 22 42 57 63 64
1 8 29 64 99 120 127 128
1 9 37 93 163 219 247 255 256
...
Again, this table can be created by summing two values from the previous line, except that the new entry on each line is now 1<<line instead of 1.
Let's use these tables above to construct f(x) for an 8 bits number (it trivially generalizes to any number of bits). f(0) still has to be 0. Looking up the 8th row in the first triangle, we see that next 8 entries are f(1) to f(9), all with one bit set. The next 28 entries (7+6+5+4+3+2+1) all have 2 bits set, so that's f(10) to f(37). The next 56 entries, f(38) to f(93) have 3 bits, and there are 70 entries with 4 bits set. From symmetry we can see that they're centered around f(128), in particular they're f(94) to f(163). And obviously, the only number with 8 bits set sorts last, as f(255).
So, with these tables we can quickly determine how many bits must be set in f(i). Just do a binary search in the last row of your table. But that doesn't answer exactly which bits are set. For that we need the previous rows.
The reason that each value in the table can be created from the previous line is simple. binom(n,k) == binom(k, n-1) + binom(k-1, n-1). There are two sorts of numbers with k bits set: Those that start with a 0... and numbers which start with 1.... In the first case, the next n-1 bits must contain those k bits, in the second case the next n-1 bits must contain only k-1 bits. Special cases are of course 0 out of n and n out of n.
This same stucture can be used to quickly tell us what f(16) must be. We already had established that it must contain 2 bits set, as it falls in the range f(10) - f(37). In particular, it's number 6 with 2 bits set (starting as usual with 0). It's useful to define this as an offset in a range as we'll try to shrink the length this range from 28 down to 1.
We now subdivide that range into 21 values which start with a zero and 7 which start a one. Since 6 < 21, we know that the first digit is a zero. Of the remaining 7 bits, still 2 need to be set, so we move up a line in the triangle and see that 15 values start with two zeroes, and 6 start with 01. Since 6 < 15, f(16) starts with 00. Going further up, 7 <= 10 so it starts with 000. But 6 == 6, so it doesn't start with 0000 but 0001. At this point we change the start of the range, so the new offset becomes 0 (6-6)
We know need can focus only on the numbers that start with 0001 and have one extra bit, which are f(16)...f(19). It should be obvious by know that the range is f(16)=00010001, f(17)=00010010, f(18)=00010100, f(19)=00011000.
So, to calculate each bit, we move one row up in the triangle, compare our "remainder", add a zero or one based on the comparison possibly go left one column. That means the computational complexity of f(x) is O(bits), or O(log N), and the storage needed is O(bits*bits).
For each given number k we know that there are binom(n, k) n-bit integers that have exactly k bits of value one. We can now generate a lookup table of n + 1 integers that store for each k how many numbers have less one bits. This lookup table can then be used to find the number o of one bits of f(i).
Once we know this number we subtract the lookup table value for this number of bits from i which leaves us with the permutation index p for numbers with the given number of one bits. Altough I have not done research in this area I am quite sure that there exists a method for finding the pth permutation of a std::vector<bool> which is initialized with zeros and o ones in the lowest bits.
The reverse function
Again the lookup table comes in handy. We can directly calculate the number of preceding numbers with less one bits by counting the one bits in the input integer and reading in the lookup table. Then you "only" need to determine the permutation index and add it to the looked up value and you are done.
Disclaimer
Of course this is only a rough outline and some parts (especially involving the permutations) might take longer than it sounds.
Addition
You stated yourself
I'm trying to use this to label a large data set, and prioritize the order in which I search them.
Which sounds to me as if you would be going from the low hamming distance to the high hamming distance. In this case it would be enough to have an incremental version which generates the next number from the previous:
unsigned next(unsigned previous)
{
if(std::next_permutation(previous))
return previous;
else
return (1 << (1 + countOneBits(previous))) - 1;
}
Of course std::next_permutation permutation does not work this way but I think it is clear how I mean to use it.

2-3-5-7 wheel factorization seems to skip prime number 331

When following the procedure on wikipedia for wheel factorization, I seem to have stumbled into a problem where the prime number 331 is treated as a composite number if I try to build a 2-3-5-7 wheel.
With 2-3-5-7 wheel, 2*3*5*7=210. So I setup a circle with 210 slots and go through steps 1-7 without any issues. Then I get to step 8 and strike off the spokes of all multiples of prime numbers, I eventually strike off the spoke rooted at 121, which is a multiple of 11, which is a prime. For the spoke rooted at 121, 121 + 210 = 331. Unfortunately, 331 is a prime number.
Is the procedure on Wikipedia incorrect?
Or did I misunderstand the procedure, and should have only struck out spokes that are multiples of 2, 3, 5, and 7, but not any of the other primes less than 210?
Wikipedia is correct.
331 is in the 1 spoke of the wheel. The spoke is not shaded, so 331 is potentially prime. And in fact, it is prime.
121 is also in the 1 spoke of the wheel, so 121 is potentially prime. That is, it is not eliminated as a prime by the wheel. However, it is not prime.
The wheel doesn't allow you to make any inference about the primality of 331 based on the non-primality of 121. Sorry.
I have an implementation of wheel factorization at my blog, if you want to look at it.
Yes, you are only allowed to strike off the spokes that are multiples of 2, 3, 5 and 7. In fact, 121 which is a multiple of 11, is relatively prime to 210. So the numbers on the 121 spoke can be either prime or composite.

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.