How can I find all n-bit semiprime numbers?
Ältere Kommentare anzeigen
I know about the primes function but I need to get a list of all n-bit semiprime numbers and all the factors. Thanks.
9 Kommentare
John D'Errico
am 25 Jun. 2022
Yet another question that seems highly likely to be homework, but at the same time, perhaps not. At the same time, you need to remember the list will be a long one if the number of bits is large. However, then I look at your other questions, and it seems you have asked some fairly simple questions, one of which is quite close. That makes it far more likely this is homework.
- Start with a list of all primes less than 2^(n-1).
- Form the product of all pairs of those primes.
- Delete the products that have less than n bits, or more than n bits.
Surely you can handle step 1.
As for step 2, just read the responses to one of the last questions you asked.
Finally, how can you compute the number of bits in a number? What would log2 tell you?
If you want a less memory intensive solution, surely you can do exactly the same thing using a loop, computing products of pairs of primes, saving those that have exactly n bits. Again, log2 would be your friend here, since it will allow you to efficiently know which primes to use in that product. A careful implementation would even allow you to do this efficiently, by computing the log2 of every prime in the initial list.
Finally, If you convinced me this is not homework by explaining why you want this list, AND you showed a reasonably serious effort you made, I would offer more help. I'll bet it is homework though.
Wouldn't you agree that every question asked here is some kind of "homework" ?
John D'Errico
am 25 Jun. 2022
Bearbeitet: John D'Errico
am 25 Jun. 2022
@Torsten, well, often many such questions are. But when a basic question about mathematics like this is asked, with no attempt at solving it, then it fails my smell test. A student needs to make an effort, else they learn nothing. Homework was assigned to the student, not for the student to then go find someone who can answer their question. It is not a problem if a student is unable to answer every homework problem they are given. That just may mean they need to spend more time doing their homework, or it may mean they are in the wrong class. Homework is given to a student to teach them something, here hopefully about both mathematics and about MATLAB.
By the way, for those who don't know what the original question drives at, a semi-prime is an integer that is formed as exactly a product of two primes.
So a number with only two factors, both of which are prime. The factors need not be distinct. 289 is a semi-prime, since it is 17*17. As well, 143 is a semi-prime, as it is 11*13. But 144 is not a semi-prime, since while it has factors of only 2 and 3, they each appear more than once in the list of all prime factors.
The list of n-bit semi-primes would be long for moderately large n. So for n=50, for example, we would have the number 2*p, where p here is the smallest prime just less than 2^49. Using my nextprime utility, that would seem to be:
p = nextprime(2^49,'below')
p =
562949953421231
and therefore 2*p will be a 50 bit semi-prime.
2*sym(p)
ans =
1125899906842462
dec2bin(2*sym(p))
ans =
'11111111111111111111111111111111111111111101011110'
numel(ans)
ans =
50
However, you would seriously not wish to generate the list of all 50 bit semi-primes, as that list would be immense. Remember, there are a gajillion primes less than 2^49 (ok, not quite, but a long list, with approximately this many of them:
2^49/log(2^49)
ans =
16574798083053.1
So roughly 16 trillion such primes. And that means the list of 50 bit semi-primes would be HUGE.
An interesting question is to find twin semi-primes, so number pairs that are both semi-prime, and are adhacent on the number line. We can find such pairs easily enough, thus [9,10], and [38,39]. Regardless, as much as the question is technically interesting, if it is homework, it needs some effort made by the student.
Anyway, IF @Nadatimuj were able to explain why, rationally, they needed such a list of n-bit semi-primes, in a context that was not to just do their current homework assignment, I would be quite happy to offer lots of help. As well, if they made some serious effort, even if it was homework, then I'd be happy to offer at least some help. The problem is, if I just do their homework here, they learn nothing, except there is a sucker born every minute.
Nadatimuj
am 25 Jun. 2022
John D'Errico
am 25 Jun. 2022
Bearbeitet: John D'Errico
am 25 Jun. 2022
Ok, I'll accept that you do have a viable problem. Sorry for being difficult, but you can imagine there are many students who just toss their homework on the site and then expect a response. with no effort made on their part. I'll post an answer to your question, specifically targeted at difficult to solve factoring problems.
Torsten
am 25 Jun. 2022
Thanks for your reply, and just to clarify I am doing reseach, not homeworks.
Research, homeworks - what's the difference ? A winged word in Germany is a wording from our former chancellor, Helmut Kohl, who claimed that "every nation in the EU should try to get its homeworks done properly in order to be convincing towards its partners". So "homework" is open to interpretation.
John D'Errico
am 25 Jun. 2022
Bearbeitet: John D'Errico
am 25 Jun. 2022
I think this is an important distinction. Research, by itself is not sufficient to get my respect. If someone were to ask, "Will you please help me to write the code for my thesis on some obscure topic?". That sort of question deserves no help, since it is just asking for help to solve a problem the poster has been tasked to do. On the other hand, if someone shows some progress, yet has a stumbling block in their attempts to move forwards, as long as they show a reasonable attempt made, they gain my respect. Asking for help is not in itself bad. The problem is in giving up, with no effort shown or made.
Nadatimuj
am 25 Jun. 2022
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
