A number is said to be fine if the first 4 prime numbers i.e 2, 3, 5, or 7 are its only prime factors.
What is the 4000th fine number?
Details and Assumptions :
As an explicit example the first few fine numbers are 2, 3, 4, 5, 6,7, 8, 9, 10, 12.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Perhaps I am being dense, but I don't see where the literal value '28' comes from in your range definition above. Doesn't that mean you will have a list with 28^4 (or ~600k) terms in it? Is that just a value that you are sure MUST be "big enough", because it seems way too big to me. I used an iterative method that kept adding the next Hamming-like sequence to the list and resorting, checking to see if the ordering of the first 4000 elements had changed. My list length was only about 36k when I found the answer.
Log in to reply
It was the value that is "big enough". Note, the product generates the powers of the prime factors. I do not doubt I am checking more things than I need to. It is a bonehead approach,but it works!
Log in to reply
Ok thanks, it might even be more efficient than my method, since you only have to sort your list once. I initially tried solving the problem by just looking at the integer powers for the prime factorization, but I couldn't figure out an easily implementable rule to find the next fine number, given only the prime factorization of the preceding one.
I suspect it may be one of those sneakily hard problems that involves highly non-obvious properties about the recurrences of particular powers along the number-line, kind of like Fermat's last theorem.
I do those things in SQL.
Here is an SQL statement in MS Access
1 2 |
|
My table T contains integers from 0 to 2 8 . I sort the outcome and pick the 4 0 0 1 th element ( the first number is 1 and I exclude it from numbering).
This is cool. I have never seen an SQL solution to a brilliant problem before.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Can you explain in words what the first section of your code is doing?
@Calvin Lin , here's what I'm doing in the first section -
Since brute forcing can be long and tedious, I found a better approach to check if only factors of a number are 2, 3, 5, 7.
I first check if any number n is divisible 2. If it is, I divide it by 2 and I repeat this process again. Once the number is no more divisible by 2, I move on to 3. I essentially do the same thing with 3 and then continue to 5 and 7.
Once I've completed the process, I check if the n is equal to 1. If it is, then it is a fine number. This is because if the number is fine, it's only prime factors are 2, 3, 5, 7. Hence when I divide away all of them, number must be equal to 1.
Producing this code involved making just a slight modification to one of those available at Rosetta code , which in turn is a translation of a Haskell code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
my solution in python using heap and set, not optimized
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Problem Loading...
Note Loading...
Set Loading...
Sleep deprivation hopefully excuses ugly one liners.
Note:
This is not an actual solution. An actual solution would be to notice that a fine number is simply a variation of a Hamming Number . This is a classic CS problem. Clever and efficient algorithms for generating these numbers have been extensively discussed.