Most elegant way to check if a binary representation of an integer is a palindrome? - bit

What is the most elegant way to check if a binary representation of an integer is a palindrome? Suppose the integer is 32-bit.
Without shifting bits iteratively, can we achieve this? Any code snippet will be highly welcome.
I noticed the post How to check if the binary representation of an integer is a palindrome?, but it is done by bit shifting. Are there any other methods?
Thanks a lot in advance!

This is really just a special case of reversing bit order since any palindrome will be equal to itself once reversed.
One way or another, you have to reverse the order of the bits, which on some platforms is a single instruction. See the linked question and pick the one you find to be the most elegant.

Related

Is it possible to devise a subtraction algorithm that does not first compare the inputs?

Obviously, any competent programming language has built-in subtraction. However, say you needed to implement a custom subtraction algorithm (say, to handle very large numbers). If you are using the borrowing method, you would have to first compare the inputs to determine if the result will be negative, and if so, you switch the order of the subtraction. I am curious if it is possible to write an algorithm that does not need this comparison/operand swapping?
Further, could such an algorithm be more efficient than compare-first, which in the worst case would have to compare every place value?
*I am aware that one could use complements, but this limits the size of permitted inputs (assuming finite representation size).
If you are implementing bignums, then you can combine the comparison and the subtraction so that you only look at each "limb" once, except for one. (The word "limb" comes from the Gnu MP library but it is also used elsewhere to mean the same thing.)
First, you scan from the big end of the longer number until you find a nonzero limb or you reach thr length of the smaller number. In the first case, you know that the longer number is bigger and you also know the position of its most significant limb. (If you have adopted the convention that numbers always have a nonzero most-significant limb then you can skip this step.)
Suppose now that you didn't prove the the longer number is bigger, so you know are at the same position in both numbers. Continue scanning both numbers until you find a difference. At this point, you know which number is bigger and you also know the position of the most significant limb of the larger number.
So now you know which number is bigger, and you can do the normal borrowing algorithm from the low-order ends of the numbers, stopping when you reach the high-order limb which you previously identified.
Since the two scans stop at the same place, you don't look at any limb more than once, exvept the high-order limb which stopped the first scan.
Whether it is actually worthwhile to implement this hack is something you would have to decide. The complication may not be justfiable. But it is possible.

How can I represent a number as a chromosome in a genetic algorithm?

I want to use a genetic algorithm to solve a simple system of two linear equations with two variables. This is mainly to help me get a better understanding of how they work.
Everything seems pretty simple, but I am unsure how to encode possible solutions in the chromosomes for this problem.
I will have two variables which I want to encode in a chromosome to represent a solution. If each variable is can be represented an 8-bit number, would I make a 16-bit binary encoded chromosome (A string of 1's and 0's).
I am just not quite sure how that would work. If two parents are selected for breeding, how would randomly selecting genes from the binary string result in a possibly better solution? This is why I don't think a binary string would work, so any answers would be greatly appreciated!
Why not use the numbers as numbers? You don't have to use binary encoding in a GA. There are mutation and crossover operators working well for real-valued encodings. As you say it's a learning example.. I would recommend you try both approaches, the real-valued encoding should be much quicker to converge.
For binary encoding I would use Single Point Crossover and Bit flip Mutation. For real-valued encoding I would use Blend-Alpha-Beta Crossover (BLX-a-b) or Simulated Binary Crossover (SBX) and Normal Distributed Mutation. You can try some of these and many more operators on the SingleObjectiveTestFunctions in HeuristicLab.

Efficient Means of Implementing Collation & Sorting?

I'm writing lexicography software, which may theoretically need to sort tens of thousands of strings with arbitrary (dictionary-project-specific) collations. There are two means of specifying custom collations:
a map of graphemes to unicode-style multi-level collation keys.
an array of alphabetic graphemes (possibly including digraphs, etc.) in sorting order, which can be internally transformed into a map of collation keys.
The naive method of comparing strings is to check grapheme-by-grapheme until you find a mismatch, and then look up the collation keys for the mismatched graphemes to compare, but I'm hoping there's a more efficient way of doing it.
The best idea I've got so far depends on noticing that strings of equal length can be treated as little-endian base-n numbers, so I can pre-compute an integer key for each string which turns collation into cheap integer comparison. But, this breaks for strings of different length (a big deal when sorting a dictionary), and there's no bound on the size of integers that could be generated.
To account for length differences, I thought I could compute a list of keys for all prefixes of each string, and then just compare the keys for prefixes of length equal to the shorter string being compared. That seems to do pretty well, but key sizes are still unbounded, and storing the keys could use a lot of memory.
Is there a way to improve that approach? Or am I just going about it entirely wrong, and there's a much better means of sorting strings with arbitrary collations?
How about a grapheme-by-grapheme Radix sort? You get Big O n(number of words) * m(length of longest word) sorting. The idea should be fairly simple put all the words that start with A in the A bucket, Bs in the B bucket and so on down the characters in the word.
I'm no expert but I might suggest some kind of hybrid between the naive approach and your approach. Where you look at a fixed number of bytes in each string, treat it as a little-endian number and use a pre-calculated collation. Then if they are the same move to the next set of the same length and do the same. The tricky part is dealing with variable length graphemes (such as UTF-8 or digraphs). The simplest solution would be to use a fixed-width representation in the dictionary, but there might be another, more sophisticated solution, which I can't think of right now.
Once you get to the end of the shorter string you zero extend it to meet the next boundary and then do the comparison.
You could also look at open-source implementations of collations, and see if they do something more sophisticated (for instance the GNU implementation of the strcoll C function).

Programming Logic: Finding the smallest equation to a large number

I do not know a whole lot about math, so I don't know how to begin to google what I am looking for, so I rely on the intelligence of experts to help me understand what I am after...
I am trying to find the smallest string of equations for a particular large number. For example given the number
"39402006196394479212279040100143613805079739270465446667948293404245721771497210611414266254884915640806627990306816"
The smallest equation is 64^64 (that I know of) . It contains only 5 bytes.
Basically the program would reverse the math, instead of taking an expression and finding an answer, it takes an answer and finds the most simplistic expression. Simplistic is this case means smallest string, not really simple math.
Has this already been created? If so where can I find it? I am looking to take extremely HUGE numbers (10^10000000) and break them down to hopefully expressions that will be like 100 characters in length. Is this even possible? are modern CPUs/GPUs not capable of doing such big calculations?
Edit:
Ok. So finding the smallest equation takes WAY too much time, judging on answers. Is there anyway to bruteforce this and get the smallest found thus far?
For example given a number super super large. Sometimes taking the sqaureroot of number will result in an expression smaller than the number itself.
As far as what expressions it would start off it, well it would naturally try expressions that would the expression the smallest. I am sure there is tons of math things I dont know, but one of the ways to make a number a lot smaller is powers.
Just to throw another keyword in your Google hopper, see Kolmogorov Complexity. The Kolmogorov complexity of a string is the size of the smallest Turing machine that outputs the string, given an empty input. This is one way to formalize what you seem to be after. However, calculating the Kolmogorov complexity of a given string is known to be an undecidable problem :)
Hope this helps,
TJ
There's a good program to do that here:
http://mrob.com/pub/ries/index.html
I asked the question "what's the point of doing this", as I don't know if you're looking at this question from a mathemetics point of view, or a large number factoring point of view.
As other answers have considered the factoring point of view, I'll look at the maths angle. In particular, the problem you are describing is a compressibility problem. This is where you have a number, and want to describe it in the smallest algorithm. Highly random numbers have very poor compressibility, as to describe them you either have to write out all of the digits, or describe a deterministic algorithm which is only slightly smaller than the number itself.
There is currently no general mathemetical theorem which can determine if a representation of a number is the smallest possible for that number (although a lower bound can be discovered by understanding shannon's information theory). (I said general theorem, as special cases do exist).
As you said you don't know a whole lot of math, this is perhaps not a useful answer for you...
You're doing a form of lossless compression, and lossless compression doesn't work on random data. Suppose, to the contrary, that you had a way of compressing N-bit numbers into N-1-bit numbers. In that case, you'd have 2^N values to compress into 2^N-1 designations, which is an average of 2 values per designation, so your average designation couldn't be uncompressed. Lossless compression works well on relatively structured data, where data we're likely to get is compressed small, and data we aren't going to get actually grows some.
It's a little more complicated than that, since you're compressing partly by allowing more information per character. (There are a greater number of N-character sequences involving digits and operators than digits alone.) Still, you're not going to get lossless compression that, on the average, is better than just writing the whole numbers in binary.
It looks like you're basically wanting to do factoring on an arbitrarily large number. That is such a difficult problem that it actually serves as the cornerstone of modern-day cryptography.
This really appears to be a mathematics problem, and not programming or computer science problem. You should ask this on https://math.stackexchange.com/
While your question remains unclear, perhaps integer relation finding is what you are after.
EDIT:
There is some speculation that finding a "short" form is somehow related to the factoring problem. I don't believe that is true unless your definition requires a product as the answer. Consider the following pseudo-algorithm which is just sketch and for which no optimization is attempted.
If "shortest" is a well-defined concept, then in general you get "short" expressions by using small integers to large powers. If N is my integer, then I can find an integer nearby that is 0 mod 4. How close? Within +/- 2. I can find an integer within +/- 4 that is 0 mod 8. And so on. Now that's just the powers of 2. I can perform the same exercise with 3, 5, 7, etc. We can, for example, easily find the nearest integer that is simultaneously the product of powers of 2, 3, 5, 7, 11, 13, and 17, call it N_1. Now compute N-N_1, call it d_1. Maybe d_1 is "short". If so, then N_1 (expressed as power of the prime) + d_1 is the answer. If not, recurse to find a "short" expression for d_1.
We can also pick integers that are maybe farther away than our first choice; even though the difference d_1 is larger, it might have a shorter form.
The existence of an infinite number of primes means that there will always be numbers that cannot be simplified by factoring. What you're asking for is not possible, sorry.

Fastest method implementing number sqare root in string (1000000 digits)

What is fastest algorithm implementing a square root of decimal contained in strings.
This decimal can have 1000000 digits.
Anyone can tell me something about it?
Newton's method should work fine for you: Square Root for Bigint in F# .
Newton's method requires big decimal division. A somewhat simpler method which requires only squaring is just binary search on the square root.
Use 'lsqrt' (Just google for some code) and adjust it for your number type. I used the same approach to deal with big numbers in IronScheme.
Seems to work well.
Edit:
This returns an 'integer' root and a remainder.
BigSquareRoot

Resources