Finding out the prime factors of a number This finds the prime factors of a number given in a variable. Is it efficient or in need of improvement?

public class PrimeFactorFinder {
    public static void main(String args[]){
        long n = 18;
        long md = 2;
        while(n!=java.lang.Math.sqrt(n)){

            if(n%md==0){
                System.out.print(" "+md);
                n /= md;
            } else {
                md++;
            }
        }
    }
}
The efficiency can be improved. As Simon already said, the condition

n != java.lang.Math.sqrt(n)


is equivalent to n != 1, and therefore not of much use.
Perhaps you meant something different: A composite number \$ n &gt; 1 \$
must have a prime factor \$ p \$ for which  \$ p \le \sqrt n \$. Therefore,
as soon as md &gt; java.lang.Math.sqrt(n) in your iteration, 
n is either equal to 1 or a prime number.

The code then is

    long md = 2;
    while (md &lt;= java.lang.Math.sqrt(n)) {
        if (n % md == 0) {
            System.out.print(" " + md);
            n /= md;
        } else {
            md++;
        }
    }
    if (n &gt; 1) {
        System.out.print(" " + n);
    }


which reduces the number of iterations considerably:
For n = 64_548_621 the loop runs until md = 283 instead of
md = 25343.

Another small improvement is to repeatedly test a possible factor
in a while-loop, in order to save some square root computations
of the (updated) number n:

    long md = 2;
    while (md &lt;= java.lang.Math.sqrt(n)) {
        while (n % md == 0) {
            System.out.print(" " + md);
            n /= md;
        }
        md++;
    }
    if (n &gt; 1) {
        System.out.print(" " + n);
    }


A simple measure to reduce the number of iterations to about 50% is to
test the factor 2 separately, and then only odd numbers starting
at 3:

    while (n % 2 == 0) {
        System.out.print(" " + 2);
        n /= 2;
    }
    long md = 3;
    while (md &lt;= java.lang.Math.sqrt(n)) {
        while (n % md == 0) {
            System.out.print(" " + md);
            n /= md;
        }
        md += 2;
    }
    if (n &gt; 1) {
        System.out.print(" " + n);
    }


As pointed out by @CodesInChaos in a comment,
computing the square root of a long integer via floating point 
arithmetic can suffer from rounding issues.
This would be an issue for numbers greater than 2^52.
From the analysis at How can you easily calculate the square root of an unsigned long long in C?
it follows that the difference between 
(long)java.lang.Math.sqrt(n) and the correct 
value \$ \lfloor \sqrt n \rfloor \$ is at most one.
Since only an upper bound is needed, replace the loop condition by

    while (md &lt;= java.lang.Math.sqrt(n) + 1) {


If you want  reusable code then move the computation of
the prime factors in a separate function which returns the list
of primes. This also separates the computation from the I/O
and keeps the main function short:

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

public class PrimeFactorFinder {

    public static void main(String args[]){
        long n = 64_548_621;
        List&lt;Long&gt; factors = primeFactors(n);
        System.out.println(Arrays.toString(factors.toArray()));
    }

    static List&lt;Long&gt; primeFactors(long n) {
        List&lt;Long&gt; factors = new ArrayList&lt;Long&gt;();
        long md = 2;
        while (n % md == 0) {
            factors.add(md);
            n /= md;
        }
        md = 3;
        while (md &lt;= java.lang.Math.sqrt(n) + 1) {
            while (n % md == 0) {
                factors.add(md);
                n /= md;
            }
            md += 2;
        }
        if (n &gt; 1) {
            factors.add(n);
        }
        return factors;
    }
}