Skip to main content
5 of 8
correct typo
barteks2x
  • 281
  • 1
  • 4

Java ( 132 60)

60 characters (works also with negative numbers!):

boolean u(int n){return !(n+"").matches("^.*(.).*\\1.*$");}

132 characters (returns true for negative numbers):

boolean u(int n){int[] p=new int[]{2,3,5,7,11,13,17,19,32,29};double a=9001312320D;while(n>0){a/=p[n%10];n/=10;}return (long)a==a;}

with comments:

boolean unique(int n){
    int[] p=new int[]{2,3,5,7,11,13,17,19,32,29};//list of 10 first primes
    double a=9001312320D;//10 first primes multiplied
    while(n>0){
        a/=p[n%10];//divide by (n%10+1)th prime
        n/=10;//divide n by 10, next digit
    }
    return (long)a==a;//if a is integer then n has all digits unique
}

And answer that is technically correct (character count includes only the function, not global variables), but I think it's cheating, 30 characters:

boolean u(int i){return m[i];}

m[] is boolean array that contains correct answers for all 32-bit integers.

barteks2x
  • 281
  • 1
  • 4