Monday, May 27, 2013

Factorial in Java, Recursively and Non-Recursively

FactorialDemo demonstrates how to implement Factorial problem both recursively and non-recursively in Java.

1:  // Program by Luis Gallego Cardona.   

2:  package com.factorialdemo;
3:  import javax.swing.*;
4:  public class FactorialDemo {
5:       public static void main(String[] args) {
6:            String input; // hold the input
7:            int number; // hold a number
8:            // Get the number from the user
9:            input = JOptionPane.showInputDialog("Enter a non-negative number");
10:            number = Integer.parseInt(input);
11:            // Display the users input
12:            JOptionPane.showMessageDialog(null, number + "! is " + factorial(number) + ", using recursion"); // This shows the factorial using recursion
13:            JOptionPane.showMessageDialog(null, number + "! is " + nonrecursivefactorial(number) + ", not using recursion"); // This shows the factorial NOT using recursion
14:            System.exit(0);  
15:            // NOTE: What is the purpose of System.exit(0)? I know I have to call this whenever I use JOptionPane ... but that's about it.
16:       }
17:       private static int nonrecursivefactorial(int n) {
18:            int x; // this holds the number
19:            int fact = 1; // this is a place holder for the result of the factorial
20:            /** The for loops takes in a number as x, then assigns the value as n, which is the counter in the for loop.  
21:             * it then multiplies the number we passed in by 1 (base case), then it keeps multiplying the new number all the way until n is < 1. Thus breaks the loop
22:             */
23:            for (x = n; x > 1; x--)
24:                 fact *= x;
25:            return fact;
26:       }
27:       private static int factorial(int n) {
28:            if(n == 0)
29:                 return 1; // this is the base case , because 0! = 1
30:            else  
31:                 return n * factorial(n - 1);
32:       }
33:  }

Fibonacci sequence in Java, Recursive and Non-Recursive

This is an example in Java of how to implement the Fibonacci Sequence both recursively and Non-Recursively (or using loops).  Any questions please ask, if you don't understand how a method works, make a table!

1:  // This program is written by Luis Gallego Cardona.  

2:  package com.fibonaccidemo; // Comment out if it gives you issues
3:  import java.util.Scanner;
4:  public class FibonacciDemo {
5:       public static void main(String[] args) {
6:            Scanner keyboard = new Scanner(System.in);
7:            int number = 0;      // hold the number
8:            System.out.print("Please enter the nth term of the Fibonacci: ");
9:            number = keyboard.nextInt();
10:            // Recursive call that prints out each number in the sequence up to the desired nth term.
11:            for (int i = 0; i < number; i++)
12:                 System.out.print(fibonacci(i) + " ");
13:            System.out.println();
14:            // Non-Recursive call that simply prints out the nth term in the sequence.
15:            System.out.print(fibonacciNonRecursive(number) + " ");
16:            keyboard.close();
17:       }
18:       /** fibonacciNonRecursive method
19:        * @param n The nth number to calculate
20:        * @return The nth number.
21:        */
22:       private static int fibonacciNonRecursive(int n) {
23:            if(n == 0)
24:                 return 0;
25:            int x = -1, y = 1, z =0;    
26:            for (int i = 1; i <= n; i++){
27:                 z = x + y ;
28:                 x = y;
29:                 y = z;
30:            }
31:            return y;
32:       }
33:       /**
34:        * The fibonnaci method calculates the nth term in the Fibonnaci series
35:        * @param n The nth number to calculate
36:        * @return The nth number.
37:        */
38:       private static int fibonacci(int n) {
39:                 if (n == 0)
40:                      return 0;
41:                 if (n == 1)
42:                      return 1;
43:                 else
44:                      return fibonacci(n-1) + fibonacci(n-2);
45:       }
46:  }

Sunday, April 28, 2013

Dual booting Arch Linux + Windows 7

This is assuming that you have Arch installed on its own hdd, and same with windows.  To install windows, just plug in the hard drive to which you intend to install windows 7.   After installation, plug in both hard drives again, making sure you make Arch linux primary and the windows 7 slave.

Boot up arch linux, and install os-probe:

# pacman -S os-probe


Then run the mkconfig command, to rebuild your grub config file:

# grub-mkconfig -o /boot/grub/grub.cfg


WARNING:

If you end up having duplicate entries in your grub menu for Windows 7, check the files in which you can add  custom menu entries. For my computer, I had a manual entry under the file /etc/grub.d/40_custom

All you have to do is open up the file and take out whatever entry is already in there.


# nano /etc/grub.d/40_custom 


The re-reun the mkconfig command above, and you should be good 2 go.

To me it seems like the arch wiki makes this a more complicated problem than it should be.

Wednesday, January 6, 2010

Fix iPod Detection in Ubuntu 9.10 (updated)

This tutorial is the newest and by far the "cleanest" way of getting ubuntu to recognize your iPod. All credit goes to boombox1387 from the Ubuntu Forums.

Step 1: Open up the "Run Application" window by typing Alt + F2 on your keyboard.

Step 2: Type in 'gconf-editor', without the quotes, and hit enter.

Step 3: On the left panel, navigate through the menu to apps > nautilus > preferences.

Step 4: On the right pane locate the name "media_automount" and un-check the box.

Step 5: Close out the gconf-editor window and plug in your iPod.

Step 6: Open what ever other application you are using to manage your iPod, and mount your iPod by going to the desktop and clicking on Places > "Name of your iPod".

All credit goes to boombox1387, a simply outstanding hack to get the job done. Thanks!

Monday, December 21, 2009

Sonbird 1.4 is out!

Well other than me going home for the holidays today, I'm happy to announce that Songbird 1.4 is out! I have been waiting for this release for ages!

Anyhow, if you followed my guide to install songbird, and want to update, all you have to do is type in:

sudo rm -r /opt/Songbird

Then download the new version of Songbird and follow my guide up to step 2.

So run this command inside the folder where you downloaded Songbird:

sudo tar -xvzf Songbird*.tar.gz -C /opt/

and

sudo chown -R username:username /opt/Songbird/

Of Course substitute username, for your actual user name. That should be the basic install of the program, but if you want to add the extra features that I was talking about at the beginning of this tutorial, then go ahead and keep on reading.

This should do it, good luck and have fun!

Monday, December 14, 2009

Ubuntu iPod possible bug

So for the past couple of days I started to loose faith in my Ubuntu install because I thought I had found a perfect application to replace itunes, which was songbird. It all started when I was syncing songs to my iPod through songbird, and all my playlists would be blank on my iPod. I swore up and down that it had to be a problem with songbird. As it turns out to, it's somewhat 1/2 and 1/2 between Ubuntu and Sonbird.

To start off, songbird still can't seem to "Eject" my iPod. To me it's no biggie, I just close down Songbird and right-click on my iPod and select "Safely Remove." As it turns out to be that is what is causing my playlists to be blank on my iPod, selecting "Safely Remove."

The only way I found to fix this little issue was, to instead of selecting "Safely Remove", just select "Eject."

Don't know if this is a bug, but that was my little fix for something I thought was a bigger problem.

Tuesday, December 8, 2009

Fix iPod detection in Ubuntu 9.10

So this guide is intended to be another workaround if you are having trouble getting Ubuntu 9.10 to recognize your iPod.

First of all, make sure that your iPod is disconnected from your computer. Then open up terminal, and type this into the command line:

killall -9 nautilus

After you run that command, plug in your iPod. Wait about 60 seconds, then hit Alt + F2 to bring up the 'Run Application' window. Type in 'nautilus' without quotes, and launch what ever app you are using to manage your iPod. Hopefully now it is recognised.

Hope this works for some of you guys, best of luck!