Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8458735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:09:34+00:00 2026-06-10T13:09:34+00:00

This may not be a programming question but it’s a problem that arised recently

  • 0

This may not be a programming question but it’s a problem that arised recently at work. Some background: big C development with special interest in performance.

I’ve a set of integers and want to test the membership of another given integer. I would love to implement an algorithm that can check it with a minimal set of algebraic functions, using only a integer to represent the whole space of integers contained in the first set.

I’ve tried a composite Cantor pairing function for instance, but with a 30 element set it seems too complicated, and focusing in performance it makes no sense. I played with some operations, like XORing and negating, but it gives me low estimations on membership. Then I tried with successions of additions and finally got lost.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:09:36+00:00Added an answer on June 10, 2026 at 1:09 pm

    For sets of unsigned long of size 30, the following is one fairly obvious way to do it:

    • store each set as a sorted array, 30 * sizeof(unsigned long) bytes per set.
    • to look up an integer, do a few steps of a binary search, followed by a linear search (profile in order to figure out how many steps of binary search is best – my wild guess is 2 steps, but you might find out different, and of course if you test bsearch and it’s fast enough, you can just use it).

    So the next question is why you want a big-maths solution, which will tell me what’s wrong with this solution other than “it is insufficiently pleasing”.

    I suspect that any big-math solution will be slower than this. A single arithmetic operation on an N-digit number takes at least linear time in N. A single number to represent a set can’t be very much smaller than the elements of the set laid end to end with a separator in between. So even a linear search in the set is about as fast as a single arithmetic operation on a big number. With the possible exception of a Goedel representation, which could do it in one division once you’ve found the nth prime number, any clever mathematical representation of sets is going to take multiple arithmetic operations to establish membership.

    Note also that there are two different reasons you might care about the performance of “look up an integer in a set”:

    • You are looking up lots of different integers in a single set, in which case you might be able to go faster by constructing a custom lookup function for that data. Of course in C that means you need either (a) a simple virtual machine to execute that “function”, or (b) runtime code generation, or (c) to know the set at compile time. None of which is necessarily easy.
    • You are looking up the same integer in lots of different sets (to get a sequence of all the sets it belongs to), in which case you might benefit from a combined representation of all the sets you care about, rather than considering each set separately.

    I suppose that very occasionally, you might be looking up lots of different integers, each in a different set, and so neither of the reasons applies. If this is one of them, you can ignore that stuff.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is not a programming question per se but a question that may be
This may not be a programming question, but I don't know where to ask
Sorry, this may or may not be a programming question directly, but I am
This question may not directly relate to programming. I have noticed that the technology
This is may not be a true programming question but deals with geolocation Api,
This question may not be exactly programming related, but this is about development. I
This may or may not be a programming question, but one or two users
Some will object that this is not a programming question. I'm willing to defend
This may not be the kind of question one should ask on StackOverflow, but
This may seem a basic question but my is not currently working. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.