I wrote prog66 below to calculate a Fibonacci number supplied as a parameter then I tested it by recalculating the 20th term in the sequence:
andrew@UBUNTU:~/Java$ cat prog66.java
import java.math.BigInteger;
public class prog66
{
public static void main (String args[])
{
BigInteger x = BigInteger.valueOf(1);
BigInteger y = BigInteger.valueOf(1);
int required_term = Integer.parseInt(args[0]);
int term = 0;
boolean finished = false;
while (!finished)
{
term++;
if (term == required_term)
{
System.out.println(x);
finished = true;
}
term++;
if (term == required_term)
{
System.out.println(y);
finished = true;
}
x = x.add(y);
y = x.add(y);
}
}
}
andrew@UBUNTU:~/Java$ javac prog66.java
andrew@UBUNTU:~/Java$ java prog66 20
6765
andrew@UBUNTU:~/Java$
Then I ran the UNIX date command to check the time, calculated the 1,000,000th term in the Fibonacci sequence and ran the UNIX date command again. This showed that the program had taken less than 5 minutes to work out the answer:
andrew@UBUNTU:~/Java$ date;java prog66 1000000 > prog66.output;date
Sun Jan 4 19:31:40 GMT 2015
Sun Jan 4 19:36:23 GMT 2015
andrew@UBUNTU:~/Java$
I found a site which had the 1,000,000th Fibonacci number in a file called millionth-fibonacci-number.txt and downloaded it. Then I compared the first few digits in both files and saw that they were the same:
andrew@UBUNTU:~/Java$ cat millionth-fibonacci-number.txt|more
1953282128707757731632014947596256332443542996591873396953405
andrew@UBUNTU:~/Java$ cat prog66.output|more
1953282128707757731632014947596256332443542996591873396953405
Showing posts with label UNIX. Show all posts
Showing posts with label UNIX. Show all posts
Sunday, 4 January 2015
Fibonacci Sequence (Part 3)
Labels:
BigInteger,
date,
Java,
millionth Fibonacci number,
UNIX
Location:
West Sussex, UK
Thursday, 16 May 2013
Java System.out.println and System.out.print
What is the difference between these two?
System.out.println outputs a newline character afterwards:
UNIX > cat prog28.java
public class prog28
{
public static void main (String args[])
{
System.out.println("International DBA");
}
}
UNIX > javac prog28.java
UNIX > java prog28
International DBA
UNIX >
… but System.out.print doesn’t:
UNIX > cat prog29.java
public class prog29
{
public static void main (String args[])
{
System.out.print("International DBA");
}
}
UNIX > javac prog29.java
UNIX > java prog29
International DBAUNIX >
… although you can add one yourself if you wish:
UNIX > cat prog30.java
public class prog30
{
public static void main (String args[])
{
System.out.print("International DBA\n");
}
}
UNIX > javac prog30.java
UNIX > java prog30
International DBA
UNIX >
If you have a Java book on Amazon, which you would like to advertise here for free, please write to me at international_dba@yahoo.co.uk.
If you have a Java book on Amazon, which you would like to advertise here for free, please write to me at international_dba@yahoo.co.uk.
Labels:
Benjamin J Evans,
cat,
Java,
javac,
System.out.print,
System.out.println,
UNIX
Location:
West Sussex, UK
Friday, 10 May 2013
Java Newline and Carriage Return Characters
A newline is represented by \n and a carriage return by \r. UNIX only seems to recognize the newline:
UNIX > cat prog24.java
public class prog24
{
public static void main (String args[])
{
System.out.println("Newline->\n");
System.out.println("Carriage return->\r");
}
}
UNIX > javac prog24.java
UNIX > java prog24
Newline->
Carriage return->
UNIX >
If you copy the program to a Windows environment and run it in a command prompt, both characters are recognized:
C:\Users\J0294094\Java>java prog24
Newline->
Carriage return->
C:\Users\J0294094\Java>
If
you have a Java book on Amazon, which you would like to advertise here
for free, please write to me at international_dba@yahoo.co.uk.
Labels:
\n,
\r,
carriage return,
cat,
Herbert Schildt,
Java,
javac,
newline,
System.out.println,
UNIX
Location:
West Sussex, UK
Java Tab Character
You can use \t to tab across in Java. Tab positions appear to be every 8 characters on the UNIX system below:
UNIX > cat prog25.java
public class prog25
{
public static void main (String args[])
{
System.out.println(" 1 2");
System.out.println("12345678901234567890");
System.out.println("Andrew\tReid");
System.out.println("Andrew\t\tReid");
}
}
UNIX > javac prog25.java
UNIX > java prog25
1 2
12345678901234567890
Andrew Reid
Andrew Reid
UNIX >
If
you have a Java book on Amazon, which you would like to advertise here
for free, please write to me at international_dba@yahoo.co.uk.
Labels:
cat,
Java,
javac,
Paul J. Deitel,
System.out.println,
tab,
UNIX
Location:
West Sussex, UK
Thursday, 9 May 2013
How to Use Hexadecimal Values in Java
You can tell Java that a number is in hexadecimal (base 16) by prefixing it with 0x.
In the example below, the variable hexadecimal_1F is assigned the value 0x1F i.e. hexadecimal 1F. The program then displays the value in decimal (base 10).
In the example below, the variable hexadecimal_1F is assigned the value 0x1F i.e. hexadecimal 1F. The program then displays the value in decimal (base 10).
UNIX > cat prog23.java
public class prog23
{
public static void main (String args[])
{
int hexadecimal_1F = 0x1F;
System.out.println
("hexadecimal 1F = " + hexadecimal_1F);
}
}
UNIX > javac prog23.java
UNIX > java prog23
hexadecimal 1F = 31
UNIX >
You can check the result yourself by converting hexadecimal 1F to decimal (base 10) like this:
Hexadecimal 1F = (in decimal or base 10) (1 x 16) + 15 = 31
Labels:
base 16,
cat,
hexadecimal,
Iducate Learning Technologies,
Java,
javac,
System.out.println,
UNIX
Location:
West Sussex, UK
Tuesday, 7 May 2013
How to Use Octal Values in Java
You can tell Java that a number is in octal (base 8) by prefixing it with a zero.
In the example below, the variable octal_77 is assigned the value 077 i.e. octal 77. The program then displays the value in decimal (base 10).
In the example below, the variable octal_77 is assigned the value 077 i.e. octal 77. The program then displays the value in decimal (base 10).
UNIX > cat prog21.java
public class prog21
{
public static void main (String args[])
{
int octal_77 = 077;
System.out.println
("octal 77 = " + octal_77);
}
}
UNIX > javac prog21.java
UNIX > java prog21
octal 77 = 63
UNIX >
You can check the result yourself by converting octal 77 to decimal (base 10) like this:
octal 77 = (in decimal or base 10) (7 x 8) + 7 = 63
If you have a
Java book on Amazon, which you would like to advertise here for free,
please write to me at international_dba@yahoo.co.uk.
Labels:
Barry Burd,
cat,
Java,
javac,
octal,
System.out.println,
UNIX
Location:
West Sussex, UK
Wednesday, 1 May 2013
Java Escape Character
If you wanted a Java program to display My name is “Andrew”, or whatever your name happens to be, you might try to do it like this:
UNIX > cat prog16.java
public class prog16
{
public static void main (String args[])
{
System.out.println
("My name is "Andrew"");
}
}
UNIX > javac prog16.java
prog16.java:6: ')' expected
("My name is "Andrew"");
^
1 error
UNIX >
… but it does not work as the compiler treats the double quote before Andrew as the end of the literal then sees Andrew as an error. You can get round this by placing an escape character before the double quotes at the start and end of Andrew.
Java then treats these double quotes just like any other character
rather than literal delimiters. The escape character to use is a
backwards slash as shown below:
UNIX > cat prog17.java
public class prog17
{
public static void main (String args[])
{
System.out.println
("My name is \"Andrew\"");
}
}
UNIX > javac prog17.java
UNIX > java prog17
My name is "Andrew"
UNIX >
If you have a Java book on Amazon, which you would like to advertise here for free, please write to me at international_dba@yahoo.co.uk.
Labels:
\,
back slash,
backwards slash,
cat,
Cay S. Horstmann,
escape character,
Java,
javac,
System.out.println,
UNIX
Location:
West Sussex, UK
Subscribe to:
Posts (Atom)