বুধবার, ২৮ এপ্রিল, ২০১০
fibonacci number
Fibonacci sequence is a sequence of numbers defined by f1 = 1 f2 = 1 fn = fn-1 + fn-2
First ten terms 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
JAVA CODE:public int fib(int n) {
if (n <= 2)
return 1;
else
return fib(n - 1) + fib(n - 2)
}
মঙ্গলবার, ২৭ এপ্রিল, ২০১০
Greatest Common Divisor/LCM
//gcd
public static long gcd(long a, long b) {
if (b==0)
return a;
else
return gcd(b, a % b);
}
//lcm
public long lcm(long a,long b){
return ((a*b)/gcd(a,b));
}
.
isprime
private boolean isPrime (int number)
{
if (number<=1) return false;
if (number==2) return true;
if (number%2==0) return false;
int boundary_no=(int)Math.round(Math.sqrt(number));
for (int i=3; i<= boundary_no; i+=2)
if (n%i==0)
return false;
return true;
}
সোমবার, ২৬ এপ্রিল, ২০১০
swap two variables without temporary variable
x=5;
y=7;
x=x+y=5+7=12;
y=x-y=12-7=5;
x=x-y=12-5=7;
x=7;
y=5;
very silly?
y=7;
x=x+y=5+7=12;
y=x-y=12-7=5;
x=x-y=12-5=7;
x=7;
y=5;
very silly?
এতে সদস্যতা:
পোস্টগুলি (Atom)