Algorithm for short IDs - encoding

I'm looking for an algorithm - or should I better say: encoding? - to compress integer numbers to short string IDs like URL shorteners use: http://goo.gl/0puu
Url safe base 64 comes close to it, but maybe there is something better.
Requirements:
as short as possible
url safe

"yi_H" called base64 "perfect" and after a bit more research I came to the same conclusion, since only the following characters could be used in URLs without worry:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 - _ . ~
Thats 66 characters, whereas base64 only uses 64 characters. The two more possible characters wouldn't be practical because 66 is not based on 2.
Conclusion: URL safe base64 (offered as part of Apache Commons for example) is perfect for short IDs.

Related

Calculating d value in RSA

I saw a couple questions about this but most of them were answered in unhelpful way or didn't get a proper answer at all. I have these variables:
p = 31
q = 23
e - public key exponent = 223
phi - (p-1)*(q-1) = 660
Now I need to calculate d variable (which I know is equal 367). The problem is that I don't know how. I found this equation on the internet but it doesn't work (or I can't use it):
e⋅d=1modϕ(n)
When I see that equation i think that it means this:
d=(1modϕ(n))/e
But apparently it doesn't because 367 (1modϕ(n))/e = 1%660/223 = 1/223 != 367
Maybe I don't understand and I did something wrong - that's why I ask.
I did some more research and I found second equation:
d=1/e mod ϕ(n)
or
d=e^-1 mod ϕ(n)
But in the end it gives the same result:
1/e mod ϕ(n) = 1/223 % 660 = 1/223 != 367
Then I saw some guy saying that to solve that equation you need extended Euclidean algorithm If anyone knows how to use it to solve that problem then I'd be very thankful if you help me.
If you want to calculate something like a / b mod p, you can't just divide it and take division remainder from it. Instead, you have to find such b-1 that b-1 = 1/b mod p (b-1 is a modular multiplicative inverse of b mod p). If p is a prime, you can use Fermat's little theorem. It states that for any prime p, ap = a mod p <=> a(p - 2) = 1/a mod p. So, instead of a / b mod p, you have to compute something like a * b(p - 2) mod p. b(p - 2) can be computed in O(log(p))
using exponentiation by squaring. If p is not a prime, modular multiplicative inverse exists if and only if GCD(b, p) = 1. Here, we can use extended euclidean algorithm to solve equation bx + py = 1 in logarithmic time. When we have bx + py = 1, we can take it mod p and we have bx = 1 mod p <=> x = 1/b mod p, so x is our b-1. If GCD(b, p) ≠ 1, b-1 mod p doesn't exist.
Using either Fermat's theorem or the euclidean algorithm gives us same result in same time complexity, but the euclidean algorithm can also be used when we want to compute something modulo number that's not a prime (but it has to be coprime with numer want to divide by).

Integration with parameters with maple

I would like to compute (formally) some integrals which are just rational fraction and which depends on 3 parameter. It works if I set two parameter to trivial value, else i must stop the computation after 5 min. Does anyone can help me to make it works?
Here is my worksheet:
restart;
omega(x,y):= 1/(1+x^(2)+y^(2))*<2*x,2*y,x^(2)+y^(2)-1>:
Omega(x,y, a,b, l):= simplify(omega(evalc(Re((l*(x+I*y)+a+Ib)/(1-(a-I*b)*(x+I*y))) ),evalc(`&Im;`((l*(x+I*y)+a+I*b)/(1-(a-I*b)*(x+I*y)))) )):
assume(0 < l);
simplify(int(int(Omega(x, y, a, b, l)[1]*(diff(Omega(x, y, a, b, l)[1], x)), x = -infinity .. infinity), y = -infinity .. infinity));
Warning, computation interrupted
simplify(int(int(Omega(x, y, 0, 1, l)[3]*(diff(Omega(x, y, 0, 1, l)[1], x)), x = -infinity .. infinity), y = -infinity .. infinity));
Warning, computation interrupted
simplify(int(int(Omega(x, y, 0, 0, l)[3]*(diff(Omega(x, y, 0, 0, l)[1], x)), x = -infinity .. infinity), y = -infinity .. infinity));
2 Pi
- ----
l
You have Ib where you may intend I*b.
You have &Im where you may intend Im.
You place an assumption on l but place no helpful assumptions on a and b. Are both a and b to be taken as being purely real? I have used such assumptions below.
Below, I utilize assuming rather than assume, as I find it more convenient and it doesn't tend to lead to a muddle with mixes of distinct instances of both unassumed names and assumed names which ostensibly appear equal.
The syntax you used for assigning omega and Omega can produce operators (the apparent intention) when used for 2D Math input, albeit via a diambuguation popup. But here we have plaintext source, and such syntax used as 1D Maple Notation code makes remember table assignments instead of operators. Below I use a syntax valid in both 1D and 2D modes for assigning operators to those two names.
The following results each took anywhere from a few seconds to about a minute on 64bit Linux running Maple 17 on an Intel i5.
restart;
omega:=(x,y)->1/(1+x^(2)+y^(2))*<2*x,2*y,x^(2)+y^(2)-1>:
Omega:=(x,y,a,b,l)->simplify(omega(evalc(Re((l*(x+I*y)+a+I*b)/
(1-(a-I*b)*(x+I*y)))),
evalc(Im((l*(x+I*y)+a+I*b)/
(1-(a-I*b)*(x+I*y)))))):
T31:=simplify(Int(Int(Omega(x,y,a,b,l)[3]
*(diff(Omega(x,y,a,b,l)[1],x)),
x=-infinity..infinity),y=-infinity..infinity),
size) assuming real, l>0:
simplify(value(subs(b=0,T31))) assuming real, l>0;
2
2 Pi (a + l)
- -------------
2 2
a + l
simplify(value(T31)) assuming real, l>0;
4 2 2 4 2 2 2 2 3
2 (a + a l - b + b l + a l - b l + l ) Pi
- -------------------------------------------------
4 2 2 2 2 4 2 2 4
a + 2 a b + 2 a l + b + 2 b l + l
T11:=simplify(Int(Int(Omega(x,y,a,b,l)[1]
*(diff(Omega(x,y,a,b,l)[1],x)),
x=-infinity..infinity),y=-infinity..infinity),
size) assuming real, l>0:
simplify(value(subs(b=0,T11))) assuming real, l>0;
0
simplify(value(T11)) assuming real, l>0;
0

Operations with large numbers

I have some numbers a_i (for i=1 to 10000).
I need to compute exp(a_i)/sum(exp(a_j)) using matlab.
Of course, it is impossible to calculate straight away. I found some tricks, the most interesting being:
"Suppose we want to find exp(7.0873e002). This will be a large number indeed but still just barely within matlab's capability of direct calculation. However, we can find the separate exponent and mantissa without calling on 'exp' as follows;
a = 7.0873e2;
x = a/log(10);
D = floor(x); % D will be an integer
F = 10^(x-D); % F will lie in 1 <= F < 10
Then D will be the power of ten and F the mantissa
F = 6.27376373225551 % The mantissa
D = 307 % The exponent (power of ten)
Compare that with the direct answer:
exp(a) = 6.273763732256170e+307"
I tried something similar, but the result in may case is Inf:
a = 7.0873e5;
x = a/log(10);
D = floor(x);
F = 10^(x-D);
exp(a) = Inf
Anyone has an idea?
Your answer is in F and D. Because your a is much larger than the example a (i.e. e5 vs e2) which they state is just barely within Matlab's range, yours must be well out of the range and thus becomes inf. But it doesn't matter because D and F hold your answer, you aren't supposed to be checkin g it against exp(a), the example only calculates exp(a) to demonstrate the proof of concept. But the whole point of this code is to give you a way to find exp of giant numbers.
In your case you get
D =
307797
and
F =
3.374110424643062 % Use format long
thus your answer is 3.374110424643062e+307797

Extra and misaligned rows when importing data in SAS

I am trying to import data in SAS like:
A B C D E
x y z h i
s1 s2 s3 s4 s5
where A B C D and E are column names.
I have 240 columns in my dataset and the code I am using is:
data INFO;
infile Attdata notab dlm='09'x dsd missover LRECL = 100000000;
length A B C D E $200; (I am importing 240 columns)
input A B C D E;
run;
Whenever I import data, some of the rows of column B, C etc, gets stacked below rows of A:
A B C D E
x h i
s1 s2 s3 s5
y s4
z
Is there a way to fix this? Do I need to do something with lrecl one? My data gets all weird after running this code. Might there be problem with length one?
It may be something to do with missover (rather than LRECL). I have found this site to be useful in the past: http://www2.sas.com/proceedings/sugi26/p009-26.pdf
Are you saying in your question that data from line 1 is appearing on lines 3 and 4 and some data from line 2 is on line 3? I've never seen SAS do this before.
You may want to check your delimiter/end of line characters.

Image shuffling puzzle creation[Modified Question]

I have 3 images E1,E2,E3 of equal size 256*256*3. Now by some any arbitrary rule, i want to create a jig saw puzzle which should be a reversible operation. The resultant image E would then be of unequal size,preferably. The question is explained with a small example to show the objective for simplicity:
How to tackle the unequal sized of the resultant matrix E(image format) and how to achieve this? Please help
Example :
size(E1)=size(E2)=3*3
E1=( 1 2 3
4 5 6
7 8 9 )
E2 = ( a b c
d e f
g h i)
E = ( 1 2 3 a b c
4 5 6 d e f
7 8 9 g h i)
[r c]=size(E);
But the scheme for arrangement should be such that r/c = number of matrices involved in the operation. This however would apply for even dimensioned matrix.
So, the same operation is desired for RGB image.
Modified Question : In the above case, if E=[E1;E2] then how is it possible to extract/get back E1 and E2 from E?
I do not understand what you are doing, but you can accomplish what you have in the example easily using reshape:
E1_reshaped = reshape(e1, 1, []);
E2_reshaped = reshape(e2, 1, []);
E = [E1_reshaped; E2_reshaped];