<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-9129544825020018355</id><updated>2012-02-10T04:59:52.876-08:00</updated><title type='text'>Algorithms and Java Technology</title><subtitle type='html'>This blog was dedicated to everyone that want to learn Algorithms and Java Technology. Hope you will enjoy learning and thanks for visiting my blog.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://algojava.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://algojava.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>algojava</name><uri>http://www.blogger.com/profile/17930158193083809015</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>13</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-9129544825020018355.post-8770823196114488389</id><published>2009-01-09T23:28:00.000-08:00</published><updated>2010-12-19T02:25:05.389-08:00</updated><title type='text'>Generate Prime Numbers With "Dynamic Programming"</title><content type='html'>Here I present one technique to generate the first N prime numbers with dynamic programming. Dynamic Programming is a paradigm for designing algorithms where the next solution was taken from previous solution.&lt;br /&gt;&lt;br /&gt;The memory complexity is O(N).&lt;br /&gt;The time complexity is O(N*C), where C is the number of prime between 2 and sqrt(N) (inclusive).&lt;br /&gt;&lt;br /&gt;Advantages:&lt;br /&gt;* flexibility (generate first N prime numbers).&lt;br /&gt;* can check prime until N^2 with complexity O(C).&lt;br /&gt;* in my computer can generate first 200,000 prime numbers below 1 secs.&lt;br /&gt;&lt;br /&gt;Disadvantages:&lt;br /&gt;* slow when N is too large.&lt;br /&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;&lt;pre class="prettyprint"&gt;public class PrimeDP {&lt;br /&gt;&lt;br /&gt; int primes[];&lt;br /&gt; public boolean isPrime(int x) {&lt;br /&gt;  if(x&lt;2) return false;&lt;br /&gt;  for(int k=0;k &lt; primes.length &amp;&amp; primes[k]*primes[k] &lt;= x;k++)&lt;br /&gt;   if(x%primes[k]==0) return false;&lt;br /&gt;  return true;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public int getPrime(int index) {&lt;br /&gt;  return primes[index];&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public PrimeDP(int n) {&lt;br /&gt;  primes = new int[n];&lt;br /&gt;  primes[0] = 2;&lt;br /&gt;  primes[1] = 3;&lt;br /&gt;  for(int k=2,i=6;k &lt; n;i+=6) {&lt;br /&gt;   if(isPrime(i-1)) primes[k++]=i-1;&lt;br /&gt;   if(k &lt; n &amp;&amp; isPrime(i+1))&lt;br /&gt;   primes[k++]=i+1;&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public static void main(String []args) {&lt;br /&gt;  long a = System.currentTimeMillis();&lt;br /&gt;  int n = 200000;&lt;br /&gt;  PrimeDP p = new PrimeDP(n);&lt;br /&gt;  long b = System.currentTimeMillis();&lt;br /&gt;&lt;br /&gt;  System.out.println("time = " + (b-a) + " ms.");&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9129544825020018355-8770823196114488389?l=algojava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://algojava.blogspot.com/feeds/8770823196114488389/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://algojava.blogspot.com/2009/01/generate-prime-number-with-dynamic.html#comment-form' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/8770823196114488389'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/8770823196114488389'/><link rel='alternate' type='text/html' href='http://algojava.blogspot.com/2009/01/generate-prime-number-with-dynamic.html' title='Generate Prime Numbers With &quot;Dynamic Programming&quot;'/><author><name>algojava</name><uri>http://www.blogger.com/profile/17930158193083809015</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9129544825020018355.post-8765829573051848334</id><published>2009-01-04T22:25:00.000-08:00</published><updated>2009-01-04T22:26:10.908-08:00</updated><title type='text'>Sieve Of Erasthothenes (Optimize Memory Version)</title><content type='html'>&lt;pre&gt;&lt;br /&gt;&lt;br /&gt;public class Sieve {&lt;br /&gt; &lt;br /&gt; private int prime[]; &lt;br /&gt; &lt;br /&gt; public boolean isPrime(int x) {  &lt;br /&gt;  if(x==2) return true;&lt;br /&gt;  if(x&lt;2 || x%2==0) return false;&lt;br /&gt;  x&gt;&gt;=1;&lt;br /&gt;  return (prime[x&gt;&gt;5] &amp; (1&lt;&lt;(x&amp;31))) == 0;&lt;br /&gt; }  &lt;br /&gt; &lt;br /&gt; public Sieve(int n) {&lt;br /&gt;  prime = new int[(n&gt;&gt;6)+1];&lt;br /&gt;  for(int i=3;i*i&lt;=n;i+=2) {&lt;br /&gt;   int hi = i&gt;&gt;1;&lt;br /&gt;   if( (prime[hi&gt;&gt;5] &amp; (1&lt;&lt;(hi&amp;31))) == 0) {&lt;br /&gt;    for(int j=i*i;j&lt;=n;j+=i)&lt;br /&gt;     if((j&amp;1)!=0) {&lt;br /&gt;      int hj = j&gt;&gt;1;&lt;br /&gt;      prime[hj&gt;&gt;5] |= (1&lt;&lt;(hj&amp;31));&lt;br /&gt;     }&lt;br /&gt;   }&lt;br /&gt;  }  &lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public static void main(String []args) {&lt;br /&gt;  &lt;br /&gt;  long start = System.currentTimeMillis();  &lt;br /&gt;  Sieve sieve = new Sieve(10000000);&lt;br /&gt;  long finish = System.currentTimeMillis();&lt;br /&gt;  &lt;br /&gt;  for(int i=0;i&lt;100;i++)&lt;br /&gt;   if(sieve.isPrime(i)) System.out.print(i + " ");&lt;br /&gt;  System.out.println("");&lt;br /&gt;      &lt;br /&gt;  System.out.println("Time : " + (finish-start)/1000.0 + " secs");  &lt;br /&gt; }&lt;br /&gt; &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9129544825020018355-8765829573051848334?l=algojava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://algojava.blogspot.com/feeds/8765829573051848334/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://algojava.blogspot.com/2009/01/sieve-of-erasthothenes-optimize-memory.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/8765829573051848334'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/8765829573051848334'/><link rel='alternate' type='text/html' href='http://algojava.blogspot.com/2009/01/sieve-of-erasthothenes-optimize-memory.html' title='Sieve Of Erasthothenes (Optimize Memory Version)'/><author><name>algojava</name><uri>http://www.blogger.com/profile/17930158193083809015</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9129544825020018355.post-6600479114612166489</id><published>2009-01-04T18:29:00.000-08:00</published><updated>2009-01-04T18:44:04.752-08:00</updated><title type='text'>3: Bit Manipulation (check the bit and flip the bit)</title><content type='html'>In the previous post, we already learn how to set the bit and clear the bit. To check whether the k-th bit is 0 or 1 we can use &amp; operator. To flip(change from 0 to 1 or 1 to 0) the bit simply use ^ (XOR) operator. This is the last series from "Trilogy Of Bit Manipulation".&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;int x = 10; // 00001010&lt;br /&gt;&lt;br /&gt;// check the bit&lt;br /&gt;if( (x &amp; (1&lt;&lt;3)) != 0 )&lt;br /&gt;   printf("3-th bit is 1");&lt;br /&gt;else&lt;br /&gt;   printf("3-th bit is 0");&lt;br /&gt;&lt;br /&gt;// flip the bit&lt;br /&gt;x = x ^ (1&lt;&lt;3); // long version&lt;br /&gt;printf("%d\n",x); // x = 2 = 00000010&lt;br /&gt;&lt;br /&gt;// flip the bit again&lt;br /&gt;x ^= (1&lt;&lt;3); // short version&lt;br /&gt;printf("%d\n",x); // x = 10 = 00001010&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9129544825020018355-6600479114612166489?l=algojava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://algojava.blogspot.com/feeds/6600479114612166489/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://algojava.blogspot.com/2009/01/3-bit-manipulation-check-bit-and-flip.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/6600479114612166489'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/6600479114612166489'/><link rel='alternate' type='text/html' href='http://algojava.blogspot.com/2009/01/3-bit-manipulation-check-bit-and-flip.html' title='3: Bit Manipulation (check the bit and flip the bit)'/><author><name>algojava</name><uri>http://www.blogger.com/profile/17930158193083809015</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9129544825020018355.post-7635102108677244854</id><published>2009-01-03T23:13:00.001-08:00</published><updated>2009-01-03T23:32:30.332-08:00</updated><title type='text'>2: Bit Manipulation (clear the bit)</title><content type='html'>In the first series you have learn how to set the bit, now you will learn how to clear the bit.&lt;br /&gt;This is the second series from "Trilogy Of Bit Manipulation".&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;int x = 0;&lt;br /&gt;x |= (1&lt;&lt;2); // x = 00000100&lt;br /&gt;x |= (1&lt;&lt;4);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;To clear the k-th bit, we can use &amp;amp; and ~ operator.&lt;br /&gt;x = x &amp; ~(1&lt;&lt;2); // I clear the 2-th bit.&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;How does it work ?&lt;/span&gt;&lt;br /&gt;&amp;amp; is AND operation:&lt;br /&gt;1 &amp;amp; 1 = 1&lt;br /&gt;1 &amp;amp; 0 = 0&lt;br /&gt;0 &amp;amp; 1 = 0&lt;br /&gt;0 &amp;amp; 0 = 0&lt;br /&gt;&lt;br /&gt;~ is NEGATE operation:&lt;br /&gt;~ 1 = 0&lt;br /&gt;~ 0 = 1&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;x = x &amp; ~(1&lt;&lt;2); // long version&lt;br /&gt;x &amp;= ~(1&lt;&lt;2); // short version&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;initial x = 17 = 00010001&lt;br /&gt;and I want to clear the 0-th bit.&lt;br /&gt;&lt;br /&gt;(1&lt;&lt;0)  = 00000001&lt;br /&gt;~(1&lt;&lt;0) = 11111110&lt;br /&gt;&lt;br /&gt;00010001&lt;br /&gt;11111110&lt;br /&gt;-------- AND operation&lt;br /&gt;00010000 (the 0-th bit is clear)&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9129544825020018355-7635102108677244854?l=algojava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://algojava.blogspot.com/feeds/7635102108677244854/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://algojava.blogspot.com/2009/01/2-bit-manipulation-clear-bit.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/7635102108677244854'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/7635102108677244854'/><link rel='alternate' type='text/html' href='http://algojava.blogspot.com/2009/01/2-bit-manipulation-clear-bit.html' title='2: Bit Manipulation (clear the bit)'/><author><name>algojava</name><uri>http://www.blogger.com/profile/17930158193083809015</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9129544825020018355.post-1169801855694984707</id><published>2009-01-03T22:42:00.000-08:00</published><updated>2009-01-03T23:27:27.930-08:00</updated><title type='text'>1: Bit Manipulation (set the bit)</title><content type='html'>In 32-bit Compiler, int size is 4 byte (32 bit). &lt;br /&gt;Today we will play with bit ( I will type the last 8 bit rather than 32 bit to simplify my explanation).&lt;br /&gt;This is the first series from "Trilogy Of Bit Manipulation". &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;int x = 0; &lt;br /&gt;//The bit value is : 00000000 (all the bit is 0).&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;&lt;pre&gt; &lt;br /&gt;x = x | (1&lt;&lt;0); // set the 0-th bit from right. &lt;br /&gt;// The bit value is : 00000001 (the last bit is 0).&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;pre&gt; &lt;br /&gt;x = x | (1&lt;&lt;2); // set the 2-th bit from right. &lt;br /&gt;// The bit value is : 00000101.&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;How does it work ?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;| is OR operation :&lt;br /&gt;1 or 1 = 1&lt;br /&gt;1 or 0 = 1&lt;br /&gt;0 or 1 = 1&lt;br /&gt;0 or 0 = 0&lt;br /&gt;&lt;br /&gt;&lt;&lt; is Shift Left operation :&lt;br /&gt;1&lt;&lt;0 = 1 = 00000001&lt;br /&gt;1&lt;&lt;1 = 2 = 00000010&lt;br /&gt;1&lt;&lt;2 = 4 = 00000100&lt;br /&gt;1&lt;&lt;3 = 8 = 00001000&lt;br /&gt;&lt;br /&gt;So when I have x = 10 = 00001010&lt;br /&gt;&lt;br /&gt;And I want to set the 5-th bit: &lt;br /&gt;x = x | (1&lt;&lt;5); // long version&lt;br /&gt;x |= (1&lt;&lt;5);  // short version&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;x    = 10   = 00001010&lt;br /&gt;1&lt;&lt;5 = 32   = 00100000&lt;br /&gt;OR operation-----------&lt;br /&gt;              00101010&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9129544825020018355-1169801855694984707?l=algojava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://algojava.blogspot.com/feeds/1169801855694984707/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://algojava.blogspot.com/2009/01/bit-manipulation-set-bit.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/1169801855694984707'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/1169801855694984707'/><link rel='alternate' type='text/html' href='http://algojava.blogspot.com/2009/01/bit-manipulation-set-bit.html' title='1: Bit Manipulation (set the bit)'/><author><name>algojava</name><uri>http://www.blogger.com/profile/17930158193083809015</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9129544825020018355.post-6488328085906826740</id><published>2009-01-03T07:26:00.000-08:00</published><updated>2009-01-04T07:06:33.051-08:00</updated><title type='text'>Sudoku Solver (C++)</title><content type='html'>What is Sudoku ? &lt;br /&gt;Sudoku is a logic-based number-placement puzzle. The objective is to fill a 9×9 grid so that each column, each row, and each of the nine 3×3 boxes (also called blocks or regions) contains the digits from 1 to 9 only one time each. The puzzle setter provides a partially completed grid.&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;Here I present a solution to solve sudoku with a simple backtracking. I use bitmask to mark the number already used in each row, column and block. I can't estimate what is the time complexity of my code, but I guarantee it's fast enough (&lt; 1 secs on my laptop). There is another efficient solution using dancing link but more harder to code.&lt;br /&gt;&lt;br /&gt;Here's the full c++ code:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#include "stdio.h"&lt;br /&gt;&lt;br /&gt;int board[9][9];&lt;br /&gt;&lt;br /&gt;int fi[9]; // forbidden row&lt;br /&gt;int fj[9]; // forbidden column&lt;br /&gt;int fk[9]; // forbidden block&lt;br /&gt;&lt;br /&gt;int solve(int p) { &lt;br /&gt;&lt;br /&gt;if(p==0)&lt;br /&gt;{&lt;br /&gt;for(int a=0;a&lt;9;a++) fi[a]=fj[a]=fk[a]=0;&lt;br /&gt;for(int i=0;i&lt;9;i++)&lt;br /&gt; for(int j=0;j&lt;9;j++)&lt;br /&gt;  if(board[i][j]!=0)&lt;br /&gt;  {&lt;br /&gt;   int x = 1&lt;&lt;(board[i][j]-1);&lt;br /&gt;   int k = i / 3 * 3 + (j / 3);&lt;br /&gt;   fi[i] |= x;&lt;br /&gt;   fj[j] |= x;&lt;br /&gt;   fk[k] |= x;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt; if(p==81) return 1;&lt;br /&gt;&lt;br /&gt; int i = p / 9; // calculate row position&lt;br /&gt; int j = p % 9; // calculate column position&lt;br /&gt; int k = i / 3 * 3 + (j / 3); // calculate block position&lt;br /&gt; &lt;br /&gt; if(board[i][j]!=0) return solve(p+1);&lt;br /&gt; for(int no=1;no&lt;=9;no++) &lt;br /&gt; {&lt;br /&gt;  int x = 1&lt;&lt;(no-1);&lt;br /&gt;&lt;br /&gt;  // check the bit&lt;br /&gt;  if(fi[i] &amp; x) continue;&lt;br /&gt;  if(fj[j] &amp; x) continue;&lt;br /&gt;  if(fk[k] &amp; x) continue;&lt;br /&gt;   &lt;br /&gt;  // mark the bit&lt;br /&gt;  fi[i] |= x; &lt;br /&gt;  fj[j] |= x; &lt;br /&gt;  fk[k] |= x;&lt;br /&gt;   &lt;br /&gt;  board[i][j] = no;&lt;br /&gt;  // recurse the next level&lt;br /&gt;  if(solve(p+1)==1) return 1;&lt;br /&gt;  board[i][j] = 0;&lt;br /&gt;   &lt;br /&gt;  // clear the bit&lt;br /&gt;  fi[i] &amp;= ~x; &lt;br /&gt;  fj[j] &amp;= ~x; &lt;br /&gt;  fk[k] &amp;= ~x;&lt;br /&gt; }&lt;br /&gt; return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;&lt;br /&gt; // fill the board in here&lt;br /&gt;&lt;br /&gt; // end of fill the board &lt;br /&gt;&lt;br /&gt; // print the result&lt;br /&gt; if(solve(0)==0) puts("no solution");&lt;br /&gt; else {&lt;br /&gt;  for(int i=0;i&lt;9;i++,puts(""))&lt;br /&gt;   for(int j=0;j&lt;9;j++)&lt;br /&gt;    printf("%d",board[i][j]);&lt;br /&gt; }&lt;br /&gt; return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9129544825020018355-6488328085906826740?l=algojava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://algojava.blogspot.com/feeds/6488328085906826740/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://algojava.blogspot.com/2009/01/sudoku-solver-c.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/6488328085906826740'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/6488328085906826740'/><link rel='alternate' type='text/html' href='http://algojava.blogspot.com/2009/01/sudoku-solver-c.html' title='Sudoku Solver (C++)'/><author><name>algojava</name><uri>http://www.blogger.com/profile/17930158193083809015</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9129544825020018355.post-646251637547996218</id><published>2009-01-02T11:25:00.001-08:00</published><updated>2009-01-03T06:47:04.209-08:00</updated><title type='text'>How to create TextField that only accept letter (Java)</title><content type='html'>If you use a normal JTextField then all character(s) can be accepted. But sometimes we need a textfield that only accept letter ('a'-'z' and 'A'-'Z'). &lt;br /&gt;I Hope this JLetterField.java can solve your problem.&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;import javax.swing.JTextField;&lt;br /&gt;import javax.swing.text.AttributeSet;&lt;br /&gt;import javax.swing.text.BadLocationException;&lt;br /&gt;import javax.swing.text.Document;&lt;br /&gt;import javax.swing.text.PlainDocument;&lt;br /&gt;&lt;br /&gt;public class JLetterField&lt;br /&gt;extends JTextField&lt;br /&gt;{&lt;br /&gt;    public JLetterField(int cols) {&lt;br /&gt;        super("",cols);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    protected Document createDefaultModel() {&lt;br /&gt;        return new LetterDocument();&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    class LetterDocument extends PlainDocument {&lt;br /&gt;        public void insertString(int offs,String str,AttributeSet a)&lt;br /&gt;        throws BadLocationException&lt;br /&gt;        {&lt;br /&gt;            if(str==null) return;&lt;br /&gt;            char [] before = str.toCharArray();&lt;br /&gt;            String after = "";&lt;br /&gt;            for(int i=0;i &lt; before.length;i++)&lt;br /&gt;            {&lt;br /&gt;                if( (before[i]&gt;='a' &amp;&amp; before[i]&lt;='z') || (before[i]&gt;='A' &amp;&amp; before[i]&lt;='Z') )&lt;br /&gt;                    after += before[i];&lt;br /&gt;            }&lt;br /&gt;            super.insertString(offs,after,a);&lt;br /&gt;        }&lt;br /&gt;    }    &lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9129544825020018355-646251637547996218?l=algojava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://algojava.blogspot.com/feeds/646251637547996218/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://algojava.blogspot.com/2009/01/how-to-create-textfield-that-only.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/646251637547996218'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/646251637547996218'/><link rel='alternate' type='text/html' href='http://algojava.blogspot.com/2009/01/how-to-create-textfield-that-only.html' title='How to create TextField that only accept letter (Java)'/><author><name>algojava</name><uri>http://www.blogger.com/profile/17930158193083809015</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9129544825020018355.post-3478712829195851620</id><published>2009-01-02T00:29:00.000-08:00</published><updated>2009-01-02T00:43:32.888-08:00</updated><title type='text'>Sieve Of Eratosthenes (C++)</title><content type='html'>In mathematics, the Sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to a specified integer.&lt;br /&gt;&lt;br /&gt;The Algorithm:&lt;br /&gt;   1. Create a contiguous list of numbers from two to some highest number n.&lt;br /&gt;   2. Strike out from the list all multiples of two (4, 6, 8 etc.).&lt;br /&gt;   3. The list's next number that has not been struck out is a prime number.&lt;br /&gt;   4. Strike out from the list all multiples of the number you identified in the previous step.&lt;br /&gt;   5. Repeat steps 3 and 4 until you reach a number that is greater than the square root of n (the highest number in the list).&lt;br /&gt;   6. All the remaining numbers in the list are prime.&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;The C++ code:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#include "stdio.h"&lt;br /&gt;#include "conio.h"&lt;br /&gt;&lt;br /&gt;const int SIZE = 10000;&lt;br /&gt;char f[SIZE + 1];&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt;    &lt;br /&gt;    // 0 and 1 is not prime&lt;br /&gt;    f[0]=f[1]=1;&lt;br /&gt;    &lt;br /&gt;    // every even number (except 2) is not prime&lt;br /&gt;    for(int i=4;i&lt;=SIZE;i+=2) f[i]=1;&lt;br /&gt;    &lt;br /&gt;    // iterate every odd number (start from 3) &lt;br /&gt;    for(int i=3;i*i&lt;=SIZE;i+=2)&lt;br /&gt;    {&lt;br /&gt;        // if the current number is prime then the multiple is not prime &lt;br /&gt;        if(f[i]==0)&lt;br /&gt;        {&lt;br /&gt;            for(int j=i*i;j&lt;=SIZE;j+=i)&lt;br /&gt;                f[j]=1;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    // display prime numbers between 0 and 100&lt;br /&gt;    for(int i=0;i&lt;=100;i++)&lt;br /&gt;        if(f[i]==0) printf("%d\n",i);&lt;br /&gt;             &lt;br /&gt;    getch();&lt;br /&gt;                    &lt;br /&gt;    return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9129544825020018355-3478712829195851620?l=algojava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://algojava.blogspot.com/feeds/3478712829195851620/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://algojava.blogspot.com/2009/01/sieve-of-eratosthenes-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/3478712829195851620'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/3478712829195851620'/><link rel='alternate' type='text/html' href='http://algojava.blogspot.com/2009/01/sieve-of-eratosthenes-c.html' title='Sieve Of Eratosthenes (C++)'/><author><name>algojava</name><uri>http://www.blogger.com/profile/17930158193083809015</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9129544825020018355.post-7653506770870321345</id><published>2009-01-01T09:54:00.000-08:00</published><updated>2009-01-01T10:05:58.742-08:00</updated><title type='text'>How to create replaceAll() (C++) ?</title><content type='html'>Happy New Year World, this is my first post in 2009. This time I will tell about how to create replaceAll() function. This function are used to replace part of original string that contains old value to new value. The idea just iterate the string you want to replace and take the substring from each index then compare to the value you want to replace.&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;You can see the fullcode below :&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;#include "iostream"&lt;br /&gt;#include "string"&lt;br /&gt;using namespace std;&lt;br /&gt;&lt;br /&gt;string replaceAll(string text,string from,string to) {&lt;br /&gt; string result = "";&lt;br /&gt; int i = 0;&lt;br /&gt; while(i &lt; text.size()) &lt;br /&gt; {&lt;br /&gt;  if(i+from.size() &lt;= text.size() &amp;&amp; text.substr(i,from.size())==from )&lt;br /&gt;  {&lt;br /&gt;   result += to;&lt;br /&gt;   i += from.size();&lt;br /&gt;  }&lt;br /&gt;  else &lt;br /&gt;  {&lt;br /&gt;   result.push_back(text[i]);&lt;br /&gt;   i++;&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt; return result;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main() {&lt;br /&gt; string a = "C++ is easy";&lt;br /&gt; cout &lt;&lt; a &lt;&lt; endl;&lt;br /&gt; a = replaceAll(a,"C++ is","C++ and Java are");&lt;br /&gt; cout &lt;&lt; a &lt;&lt; endl; &lt;br /&gt; return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9129544825020018355-7653506770870321345?l=algojava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://algojava.blogspot.com/feeds/7653506770870321345/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://algojava.blogspot.com/2009/01/how-to-create-replaceall-c.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/7653506770870321345'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/7653506770870321345'/><link rel='alternate' type='text/html' href='http://algojava.blogspot.com/2009/01/how-to-create-replaceall-c.html' title='How to create replaceAll() (C++) ?'/><author><name>algojava</name><uri>http://www.blogger.com/profile/17930158193083809015</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9129544825020018355.post-6155006324083413685</id><published>2008-12-30T01:23:00.000-08:00</published><updated>2008-12-30T22:54:10.781-08:00</updated><title type='text'>Terbilang (Java)</title><content type='html'>Here's Terbilang.java, it's to convert decimal notation into word (Indonesia).&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;br /&gt;100 ==&gt; one hundred (seratus)&lt;br /&gt;1020 ==&gt; one thousand and twenty (seribu dua puluh)&lt;br /&gt;&lt;br /&gt;So it's very useful for people that want to learning Indonesia ^^.&lt;br /&gt;&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;public class Terbilang {    &lt;br /&gt;    public static String bilang(long uang) {&lt;br /&gt;        String nama[]={"nol","satu","dua","tiga","empat","lima","enam","tujuh","delapan","sembilan"};&lt;br /&gt;        String besar[]={"triliun","milyar","juta","ribu",""};                &lt;br /&gt;        if(uang==0) return nama[0];&lt;br /&gt;        long p = 1000000000000l;&lt;br /&gt;        String hasil = "";&lt;br /&gt;        for(int i=0;i &lt; besar.length;i++,p /= 1000)&lt;br /&gt;        {&lt;br /&gt;            if(uang &lt; p) continue;&lt;br /&gt;            long temp = uang / p;                &lt;br /&gt;            boolean seribu = p==1000;&lt;br /&gt;            if(temp&gt;=100) {&lt;br /&gt;                hasil += nama[(int)temp/100] + " ratus ";&lt;br /&gt;                temp %= 100;&lt;br /&gt;                seribu = false;&lt;br /&gt;            }&lt;br /&gt;            if(temp&gt;=11 &amp;&amp; temp&lt;=19) {&lt;br /&gt;                hasil += nama[(int)temp-10] + " belas ";&lt;br /&gt;                temp = 0;&lt;br /&gt;                seribu = false;&lt;br /&gt;            }&lt;br /&gt;            if(temp&gt;=10) {&lt;br /&gt;                hasil += nama[(int)temp/10] + " puluh ";&lt;br /&gt;                temp %= 10;&lt;br /&gt;            }&lt;br /&gt;            if(temp &gt; 0) {&lt;br /&gt;             if(seribu &amp;&amp; temp==1)&lt;br /&gt;              hasil += "se";&lt;br /&gt;             else&lt;br /&gt;              hasil += nama[(int)temp] + " ";  &lt;br /&gt;            }&lt;br /&gt;            uang %= p;&lt;br /&gt;            hasil += besar[i] + " ";&lt;br /&gt;        }&lt;br /&gt;        hasil=hasil.replaceAll("satu ratus", "seratus");&lt;br /&gt;        hasil=hasil.replaceAll("satu belas", "sebelas");&lt;br /&gt;        hasil=hasil.replaceAll("satu puluh", "sepuluh");&lt;br /&gt;        return hasil.trim();&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9129544825020018355-6155006324083413685?l=algojava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://algojava.blogspot.com/feeds/6155006324083413685/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://algojava.blogspot.com/2008/12/terbilang-java.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/6155006324083413685'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/6155006324083413685'/><link rel='alternate' type='text/html' href='http://algojava.blogspot.com/2008/12/terbilang-java.html' title='Terbilang (Java)'/><author><name>algojava</name><uri>http://www.blogger.com/profile/17930158193083809015</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9129544825020018355.post-4647239424440912573</id><published>2008-12-29T21:21:00.000-08:00</published><updated>2008-12-29T22:02:47.819-08:00</updated><title type='text'>How To Create Tic Tac Toe AI (Java)</title><content type='html'>What is TicTacToe ?&lt;br /&gt;&lt;br /&gt;Tic-tac-toe, also spelled tick tack toe, and alternatively called noughts and crosses, hugs and kisses, and many other names, is a pencil-and-paper game for two players, O and X, who take turns marking the spaces in a 3×3 grid, usually X going first. (source: &lt;a href="http://en.wikipedia.org/wiki/Tic-Tac-Toe"&gt;http://en.wikipedia.org/wiki/Tic-Tac-Toe&lt;/a&gt;)&lt;br /&gt;&lt;br /&gt;Now I will present a simple code to create Tic Tac Toe AI.&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;&lt;br /&gt;I will create one java file: TicTacToeAI.java&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;The Variable&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;    /* the board */&lt;br /&gt;    private int board[][];    &lt;br /&gt;    /* empty */&lt;br /&gt;    public static final int EMPTY = 0;&lt;br /&gt;    /* player one */&lt;br /&gt;    public static final int ONE = 1;&lt;br /&gt;    /* player two */&lt;br /&gt;    public static final int TWO = 2;   &lt;br /&gt;&lt;/pre&gt; &lt;br /&gt;    &lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Simple Constructor&lt;/span&gt;&lt;br /&gt;it will initialize all value in board to 0.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;    public TicTacToeAI() {&lt;br /&gt;        board = new int[3][3];&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;    &lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Method&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;if position (i,j) is outside the boundary it will return EMPTY&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;    /* get the board value for position (i,j) */&lt;br /&gt;    public int getBoardValue(int i,int j) {&lt;br /&gt;        if(i &lt; 0 || i &gt;= 3) return EMPTY;&lt;br /&gt;        if(j &lt; 0 || j &gt;= 3) return EMPTY;&lt;br /&gt;        return board[i][j];&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;    &lt;br /&gt;My winning move definition is one single move that lead to victory.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;    /* calculate the winning move for current token */&lt;br /&gt;    public int []nextWinningMove(int token) {&lt;br /&gt;        for(int i=0;i&lt;3;i++)&lt;br /&gt;            for(int j=0;j&lt;3;j++)            &lt;br /&gt;                if(getBoardValue(i, j)==EMPTY) {&lt;br /&gt;                    board[i][j] = token;&lt;br /&gt;                    boolean win = isWin(token);&lt;br /&gt;                    board[i][j] = EMPTY;&lt;br /&gt;                    if(win) return new int[]{i,j};&lt;br /&gt;                }&lt;br /&gt;        return null;&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt; &lt;br /&gt;I calculate the best move based on this priority:&lt;br /&gt;1. lead to win&lt;br /&gt;2. prevent enemy to win&lt;br /&gt;3. center position&lt;br /&gt;4. first available position   &lt;br /&gt;&lt;pre&gt;&lt;br /&gt;    /* calculate the best move for current token */&lt;br /&gt;    public int []nextMove(int token) {&lt;br /&gt;                &lt;br /&gt;        /* if we can move on the next turn */&lt;br /&gt;        int winMove[] = nextWinningMove(token);&lt;br /&gt;        if(winMove!=null) return winMove;&lt;br /&gt;&lt;br /&gt;        /* choose the move that prevent enemy to win */&lt;br /&gt;        for(int i=0;i&lt;3;i++) &lt;br /&gt;            for(int j=0;j&lt;3;j++)&lt;br /&gt;                if(getBoardValue(i, j)==EMPTY)&lt;br /&gt;                {&lt;br /&gt;                    board[i][j] = token;&lt;br /&gt;            boolean ok = nextWinningMove(token==ONE ? TWO : ONE ) == null;                    &lt;br /&gt;                    board[i][j] = EMPTY;&lt;br /&gt;                    if(ok) return new int[]{i,j};&lt;br /&gt;                }&lt;br /&gt;        &lt;br /&gt;        /* lucky position in the center of board*/&lt;br /&gt;        if(getBoardValue(1, 1)==EMPTY) return new int[]{1,1};&lt;br /&gt;        &lt;br /&gt;        /* choose available move */&lt;br /&gt;        for(int i=0;i&lt;3;i++) &lt;br /&gt;            for(int j=0;j&lt;3;j++)&lt;br /&gt;                if(getBoardValue(i, j)==EMPTY)&lt;br /&gt;                    return new int[]{i,j};&lt;br /&gt;        &lt;br /&gt;        /* no move is available */&lt;br /&gt;        return null;&lt;br /&gt;    }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;    &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;    /* determine if current token is win or not win */&lt;br /&gt;    public boolean isWin(int token) {&lt;br /&gt;        final int DI[]={-1,0,1,1};&lt;br /&gt;        final int DJ[]={1,1,1,0};&lt;br /&gt;        for(int i=0;i&lt;3;i++)&lt;br /&gt;            for(int j=0;j&lt;3;j++) {    &lt;br /&gt;                &lt;br /&gt;                /* we skip if the token in position(i,j) not equal current token */&lt;br /&gt;                if(getBoardValue(i, j)!=token) continue;&lt;br /&gt;                &lt;br /&gt;                for(int k=0;k&lt;4;k++) {&lt;br /&gt;                    int ctr = 0;&lt;br /&gt;                while(getBoardValue(i+DI[k]*ctr, j+DJ[k]*ctr)==token) ctr++;&lt;br /&gt;                    if(ctr==3) return true;&lt;br /&gt;                }&lt;br /&gt;            }&lt;br /&gt;        return false;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9129544825020018355-4647239424440912573?l=algojava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://algojava.blogspot.com/feeds/4647239424440912573/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://algojava.blogspot.com/2008/12/how-to-create-tic-tac-toe-ai-java.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/4647239424440912573'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/4647239424440912573'/><link rel='alternate' type='text/html' href='http://algojava.blogspot.com/2008/12/how-to-create-tic-tac-toe-ai-java.html' title='How To Create Tic Tac Toe AI (Java)'/><author><name>algojava</name><uri>http://www.blogger.com/profile/17930158193083809015</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9129544825020018355.post-9171049206122867169</id><published>2008-12-29T08:03:00.000-08:00</published><updated>2008-12-29T18:35:07.849-08:00</updated><title type='text'>Count number of word ( C )</title><content type='html'>Word in my definition is sequence of non-space character.&lt;br /&gt;Count number of word in sentence was an easy task if you already know the trick.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;br /&gt;My name is Timo   ==&gt; counted as four word&lt;br /&gt;     I        like   to solve      problems ==&gt; counted as five word&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;&lt;br /&gt;So to create a computer program that count number of word, we can count based on how many space that separate the word (trailing space will be counted once).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;int count(char sentence[]) {&lt;br /&gt;  int i;&lt;br /&gt;  int ret = 0;&lt;br /&gt;  int x = 0;&lt;br /&gt;  for(i=0;sentence[i];i++)&lt;br /&gt;  {&lt;br /&gt;      if(sentence[i]!=' ')&lt;br /&gt;      {&lt;br /&gt;          x = 1;&lt;br /&gt;      }&lt;br /&gt;      else&lt;br /&gt;      {&lt;br /&gt;          ret += x;&lt;br /&gt;          x = 0;&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;  return ret + x;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Hope this tutorial will be useful :D.&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9129544825020018355-9171049206122867169?l=algojava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://algojava.blogspot.com/feeds/9171049206122867169/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://algojava.blogspot.com/2008/12/menghitung-banyak-kata-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/9171049206122867169'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/9171049206122867169'/><link rel='alternate' type='text/html' href='http://algojava.blogspot.com/2008/12/menghitung-banyak-kata-c.html' title='Count number of word ( C )'/><author><name>algojava</name><uri>http://www.blogger.com/profile/17930158193083809015</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-9129544825020018355.post-3681402558222113484</id><published>2008-12-28T08:07:00.000-08:00</published><updated>2008-12-29T18:35:57.883-08:00</updated><title type='text'>The future of Java - Closures</title><content type='html'>With Java SE 6 recently released, the discussion about what features should be in Dolphin is raging hotter than ever. Perhaps the most intereresting – and invasive – proposal is the one for closures. But this is complicated by there actually being two proposals. One is called Closures for the Java Programming Language, by Gilad Brach, Neal Gafter, James Gosling and Peter van der Ahé (the BGGA proposal). The other one is called Concise Instance Creation Expressions by Joshua Bloch, Bob Lee and Doug Lea (the CICE proposal). Neither of these two have been finished enough to actually create a JSR for them.&lt;br /&gt;&lt;span class="fullpost"&gt;&lt;br /&gt;So, what’s the difference and what does it mean for Java programmers all over the world? Well, the CICE version is really just some syntactic sugar for anonymous interface implementations. To get closer to the closure ideal, there is some allowance for making it possible to setting variables in the enclosing scope, but there isn’t really anything in here you couldn’t do with a few clever hacks in the Java compiler. As such, it’s better than nothing, but I will be disappointed if this is all we get.&lt;br /&gt;&lt;br /&gt;The BGGA proposal then; this is more ambitious. There is new syntax, new function types, two new reference types (Null and Unreachable). It allows some neat things with using the for keyword in new ways, and will also allow closing over break, return and continue (but not in the general way, since that would equal implementing continuations). This is all good. The new syntax have a shortcut, the looks very much like Ruby code blocks, but there are ways of chaining them that looks actually useful (just look at the with-method in the proposal).&lt;br /&gt;&lt;br /&gt;Of course, there are problems with the BGGA proposal too. Not so much in the lack of power, but possibly because it will involve to many new concepts (or new for Java programmers at least), to much new syntax, and it is possible that this isn’t even enough to make the closures really useful.&lt;br /&gt;&lt;br /&gt;And this is the real problem with adding closures to Java at this point. I commend what they’re trying to achieve, but I believe it’s to late. Having closures from the beginning will shape the API in ways it’s not possible to do right now. The only way we can get the full benefits of all these new toys would be for Sun to revamp the standard library, adding new versions of most classes and making the old ones softly deprecated. Changing the interfaces will be impossible, so we will haft to get used to referencing java.util.CCollection to get at the new each() internal iterator. And that each is needed. Java needs internal iterators. But that’s another rant.&lt;br /&gt;&lt;br /&gt;In conclusion; I’m looking forward to see the end result of these proposals, but I’m very wary of what they will bring too. It is very double edged and it sometimes feels like the Java class library should be ripped out and restarted from scratch. This would remove much cruft and create a new, leaner language. And that language should have full closures and have a class library designed to maxime closure goodness.&lt;br /&gt;&lt;br /&gt;Resource: http://java.apress.com/article/407/the-future-of-java-closures&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9129544825020018355-3681402558222113484?l=algojava.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://algojava.blogspot.com/feeds/3681402558222113484/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://algojava.blogspot.com/2008/12/future-of-java-closures.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/3681402558222113484'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/9129544825020018355/posts/default/3681402558222113484'/><link rel='alternate' type='text/html' href='http://algojava.blogspot.com/2008/12/future-of-java-closures.html' title='The future of Java - Closures'/><author><name>algojava</name><uri>http://www.blogger.com/profile/17930158193083809015</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry></feed>
