-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Count numbers, coprime with a given one #151
Comments
Here is a draft implementation plan for anyone willing to take a stab:
|
Well, here is a trivial, but I think probably reasonably efficient, implementation of foo:
While this is correct and fulfills the bonus property, it is a little dull on infinite input lists. The following implementation is only slightly more complex and has the additional property that any given subsequence of an infinite list will appear in finite time:
Still thinking about how to meet the extra bonus property. It feels like the sort of thing which has a blindingly simple and efficient implementation, but every one which comes to mind fails to meet one or two of those criteria. |
@Bodigrim isn't this Euler's totient function?
Isn't this an infinite quantity unless we bound it by e.g. asking only for numbers up to the given one, thereby arriving at the definition of Euler's totient function? |
@rockbmb |
I happened to need something like this for another problem, so I constructed a foo which also fulfills the extra bonus property
Note that foo3 takes a single argument, a potentially infinite list. Its result is a list of list of subsets of its argument. In other words, rather than Note also that the elements in the subsets appear in reverse order from the original list, but that ensures maximum sharing while at the same time still allowing infinite input lists. If this is a problem and you are dealing with finite lists, the best way is to reverse the input list before passing it to foo. Note finally that each of the list of subsets is already sorted in order of increasing value if by value of a subset you define the integer value of the bit pattern corresponding to the subset (interpreted little-endian). That is useful for my task. I knew there was a semi-elegant way of getting this! |
Sounds amazing! Sorry, I am a bit overloaded at the moment, will take a look next week. |
On of my OCDs is that once I get set on a simple Haskell problem which ought to be encapsulated in a reusable function, my mind just gnaws at it until it has achieved its most pure form. In that spirit, I offer
For
or
or
|
@CarlEdman for |
@rockbmb Yes, the third is my favorite too. It is clean and simple and performs just a single integer addition to generate each element, while the first one looks messier and does a multiply and a divide for every element. And yet, when I benchmarked them against each other, the first one was marginally, but consistently faster. Otherwise I would not even have included it. The difference is too small to decide between the versions, but was a useful, repeat lessons for old hackers like me that the hardware our code runs on these days does really fast integer and floating-point multiplies and divides and hence the habit of trying to avoid them has become obsolete. Meanwhile, harder-to-predict factors, like cache hits and locality of reference, are huge for performance. |
@CarlEdman a nice alternative would be to use the first, but add a comment above it that says: |
This is really beautiful! The choice between first and third implementations of |
Very nice! And I should have used binomial. My near-identical homebrew pascalTriangle is a leftover from before arithmoi had the function. I'm slowly replacing the functions in my own library with usually superior arithmoi functions, but had missed that. Two points:
|
binomial :: Num a => [[a]]
binomialTransposed :: Num a => [[a]]
binomialLine :: Integral a => a -> [a]
binomialColumn :: Integral a => a -> [a] + #152 to compute individual binomial coefficients efficiently.
|
It would be desirable to have an efficient implementation of the following function:
or even better this one:
emphasing the fact that only prime factors of
n
matter.The text was updated successfully, but these errors were encountered: