Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 6 123 ... LastLast
Results 1 to 25 of 133

Thread: 500 Ways to Print 1 to 10

  1. #1
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 500 Ways to Print 1 to 10

    OK, so every forum has one, and it's something to get your hacking skills out on!

    Lets see if we can list 500 ways to print the numbers 1 to 10.

    a) There are no language boundaries
    b) Try not to repeat the same things
    c) number your solution so it follows on
    d) name your language
    e) Make your code compilable not matter what language it is

    ok, good luck every one

    #1 Java
    public class Loop1to10 {
    	public static void main(String[] args) {
    		for(int i = 1; i < 11; i++)
    			System.out.print(i + " ");
    	}
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: 500 Ways to Print 1 to 10

    Nice idea Chris.

    I'll be amazed if we ever make it to 500!!

    Heres my first contribution:

    #2 Java
    public class Print1to10 {
        public static void main(String[] args) {        
            int array[] = {1,2,3,4,5,6,7,8,9,10};
            for(int s : array){
                System.out.print(s + " ");
            }
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: 500 Ways to Print 1 to 10

    Haha ye we shall see, here's a messy long winded one lol.

    #3 Java
    public class Loop1to10 {
    	public static void main(String[] args) {
    		new Loop1to10();
    	}
     
    	public Loop1to10(){
    		class Insanity implements Madness{
    			void print(int x){
    				System.out.println(x);
    			}
    			private int i;
    			public Insanity(int i){
    				set(i);
    			}
    			private void set(int i){
    				this.i = i;
    			}
    			public int value() {
    				return i;
    			}
    		}
    		Insanity[] PrintingOneToTen = new Insanity[10];
    		for(int i = 1; i < 11; i++){
    			PrintingOneToTen[i-1] = new Insanity(i);
    			PrintingOneToTen[i-1].print(PrintingOneToTen[i-1].value());
    		}
     
    	}
    }

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: 500 Ways to Print 1 to 10

    #4 Java
    public class Print1to10 {
        public static void main(String[] args) {
            int count = 1;
            while (count < 11) {
                System.out.print(count + " ");
                count++;
            }
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: 500 Ways to Print 1 to 10

    lol this is going to get very messy

    #5 Java
    public class Print1to10 {
     
        public static void main(String[] args) {
     
            int a = (11 - 1), b = (10 - 1), c = (9 - 1), d = (8 - 1),
            e = (7 - 1), f = (6 - 1), g = (5 - 1), h = (4 - 1), i = (3 - 1),
            j = (2 - 1);
     
            System.out.print(j + " ");
            System.out.print(i + " ");
            System.out.print(h + " ");
            System.out.print(g + " ");
            System.out.print(f + " ");
            System.out.print(e + " ");
            System.out.print(d + " ");
            System.out.print(c + " ");
            System.out.print(b + " ");
            System.out.print(a + " ");
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 500 Ways to Print 1 to 10

    don't know if this counts.

    6. Java
    public class OneToTen
    {
    public static void main(String[] args)
    {
         System.out.println("1 2 3 4 5 6 7 8 9 10");
    }
    }
    Last edited by helloworld922; March 31st, 2010 at 12:17 PM.

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 500 Ways to Print 1 to 10

    Pretty sure I didn't see a recursive solution yet...

    7. Java

    public class OneToTen
    {
    public static void main(String[] args)
    {
         recurse(1);
    }
     
    public static void recurse(int num)
    {
         System.out.print(num+" ");
         if (num > 10)
         {
              return;
         }
         recurse(num+1);
    }
    }
    Last edited by helloworld922; March 31st, 2010 at 12:19 PM.

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: 500 Ways to Print 1 to 10

    Quote Originally Posted by helloworld922 View Post
    don't know if this counts.

    6. Java
    public class OneToTen
    {
    public static void main(String[] args)
    {
         System.out.println("1 2 3 4 5 6 7 8 9 10");
    }
    }
    Haha that totally counts! Every method counts if we are ever going to get to 500!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  9. #9
    Junior Member
    Join Date
    Feb 2010
    Location
    Canada
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: 500 Ways to Print 1 to 10

    8. Java

    public class OneToTen {
     
    public static void main(String[] args) {
     
    int c = 0;
     
    while(c<10) {
    	if(c==0) {
    		System.out.println(1);
    	}
    	else if(c==1) {
    		System.out.println(2);
    	}
    	else if(c==2) {
    		System.out.println(3);
    	}
    	else if(c==3) {
    		System.out.println(4);
    	}
    	else if(c==4) {
    		System.out.println(5);
    	}
    	else if(c==5) {
    		System.out.println(6);
    	}
    	else if(c==6) {
    		System.out.println(7);
    	}
    	else if(c==7) {
    		System.out.println(8);
    	}
    	else if(c==8) {
    		System.out.println(9);
    	}
    	else if(c==9) {
    		System.out.println(10);
    		break;
    	}
    	c++;	
    }
     
    }
    }

  10. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 500 Ways to Print 1 to 10

    I'm pretty sure this works
    9. Brainf***
    ++++++++
    [>++++++++>++++<<-]
    >+.+>.<.+>.<.+>.<.+>.<.+>.<.+>.<.+>.<.+>.<.>.<--------.-.>.<

  11. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 500 Ways to Print 1 to 10

    10. Java

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.Timer;
     
    public class OneToTen implements ActionListener
    {
    	private int num;
    	public static Thread main;
     
    	public static void main(String[] args)
    	{
    		OneToTen.main = Thread.currentThread();
    		Timer timer = new Timer(10, new OneToTen());
    		timer.setRepeats(false);
    		timer.start();
    		try
    		{
    			Thread.sleep(1000);
    		}
    		catch (InterruptedException exception)
    		{
    			// TODO Auto-generated catch block
    			System.exit(0);
    		}
    	}
     
    	public OneToTen()
    	{
    		this.num = 1;
    	}
     
    	/**
    	 * @param e
    	 */
    	@Override
    	public void actionPerformed(ActionEvent e)
    	{
    		System.out.print(this.num + " ");
    		this.num++;
    		if (this.num == 11)
    		{
    			OneToTen.main.interrupt();
    		}
    		else
    		{
    			Timer timer = new Timer(10, this);
    			timer.setRepeats(false);
    			timer.start();
    		}
    	}
    }

  12. #12
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: 500 Ways to Print 1 to 10

    omg helloworld you stole brainf*** from me, i was going to do that one later you gay lol.

  13. #13
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: 500 Ways to Print 1 to 10

    LOL @ the brainf*** example. Never even heard of it!!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  14. #14
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 500 Ways to Print 1 to 10

    you snooze you lose Besides, there are multiple ways to print out 1-10 in brainf***. Just find another one. Here's another one

    11. Brainf***

    ++++++++[>++++++>++++>++++++>++++>++++++>++++>++++++>++++>++++++>++++>+++++>++++>++++++>++++>++++++>++++>++++++>++++>++++++>++++++>++++<<<<<<<<<<<<<<<<<<<<<-]
    >>>>>>>>>>>>>>>>>>>+<<+++++++++<<++++++++<<+++++++<<++++++<<+++++<<++++<<+++<<++<<+.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.>.

  15. #15
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: 500 Ways to Print 1 to 10

    Java is an object oriented language

    12. Java
    public class NumbersOO {
     
      int number;
     
      public static void main(String[] args) {
        System.out.println(new NumbersOO(10));
      }
     
      public NumbersOO(int number) {
        this.number = number;
      }
     
      @Override
      public String toString() {
        StringBuffer sb = new StringBuffer(" ");
        for (int i = 1; i <= number; i++) {
          sb.append(String.valueOf(i));
          sb.append(' ');
        }
        return sb.toString().trim();
      }
    }
    db

  16. #16
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: 500 Ways to Print 1 to 10

    And how about a recursive non-OOPS solution?

    13. Java
    public class NumbersRecursive {
     
      public static void main(String[] args) {
        printNumber(10);
      }
     
      private static void printNumber(int number) {
        if (number == 0) {
          return;
        }
        printNumber(--number);
        System.out.print(number + 1 + " ");
      }
    }
    db
    Last edited by Darryl.Burke; April 2nd, 2010 at 12:03 AM.

  17. #17
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 500 Ways to Print 1 to 10

    14. Java

    public class OneToTen
    {
    	public enum Num {
    		ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN;
     
    		@Override
    		public String toString()
    		{
    			if (this.equals(ONE))
    			{
    				return "1";
    			}
    			else if (this.equals(TWO))
    			{
    				return "2";
    			}
    			else if (this.equals(THREE))
    			{
    				return "3";
    			}
    			else if (this.equals(FOUR))
    			{
    				return "4";
    			}
    			else if (this.equals(FIVE))
    			{
    				return "5";
    			}
    			else if (this.equals(SIX))
    			{
    				return "6";
    			}
    			else if (this.equals(SEVEN))
    			{
    				return "7";
    			}
    			else if (this.equals(EIGHT))
    			{
    				return "8";
    			}
    			else if (this.equals(NINE))
    			{
    				return "9";
    			}
    			else
    			{
    				return "10";
    			}
    		}
    	}
     
    	public static void main(String[] args)
    	{
    		Num number = Num.valueOf("ONE");
    		System.out.print(number + " ");
    		number = Num.valueOf("TWO");
    		System.out.print(number + " ");
    		number = Num.valueOf("THREE");
    		System.out.print(number + " ");
    		number = Num.valueOf("FOUR");
    		System.out.print(number + " ");
    		number = Num.valueOf("FIVE");
    		System.out.print(number + " ");
    		number = Num.valueOf("SIX");
    		System.out.print(number + " ");
    		number = Num.valueOf("SEVEN");
    		System.out.print(number + " ");
    		number = Num.valueOf("EIGHT");
    		System.out.print(number + " ");
    		number = Num.valueOf("NINE");
    		System.out.print(number + " ");
    		number = Num.valueOf("TEN");
    		System.out.print(number + " ");
    	}
    }

  18. #18
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 500 Ways to Print 1 to 10

    15. Java

    public class OneToTen
    {
    	public static void main(String[] args)
    	{
    		int num = 3628800;
    		OneToTen.tenFactors(num);
    	}
     
    	public static void tenFactors(int num)
    	{
     
    		int count = 0;
    		for (int j = 1; j < num && count < 10; j++)
    		{
    			if (num % j == 0)
    			{
    				System.out.print(j + " ");
    				count++;
    			}
    		}
     
    	}
    }

  19. #19
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 500 Ways to Print 1 to 10

    Technically these are similar methods, but they use different algorithms

    16. Java

    import java.io.FileNotFoundException;
     
    /**
     * Solves a linear system of equations Ax = b using 3 different iterative
     * algorithms.
     * 
     * @author Andrew Ho
     * 
     */
    public class CopyOfOneToTen
    {
    	/**
    	 * Calculates the residual matrix of Ax = B, where x is a guess vector.
    	 * residual = B - Ax
    	 * 
    	 * @param a
    	 * @param guessX
    	 * @param b
    	 * @return
    	 */
    	private static double[][] calculateResidual(double[][] a, double[] guessX, double[] b)
    	{
    		// convert x into a matrix to allow us to multiply
    		double[][] matX = new double[guessX.length][1];
    		for (int i = 0; i < matX.length; i++)
    		{
    			matX[i][0] = guessX[i];
    		}
    		// calculate the residual
    		double[][] resid = CopyOfOneToTen.mMult(a, matX);
    		for (int i = 0; i < b.length; i++)
    		{
    			resid[i][0] = b[i] - resid[i][0];
    		}
    		return resid;
    	}
     
    	/**
    	 * Gets the dot product of two vectors.
    	 * 
    	 * @param mat1
    	 * @param mat2
    	 * @return mat1 dot mat2
    	 */
    	public static double dot(double[] v1, double[] v2)
    	{
    		if (v1.length != v2.length)
    		{
    			throw new RuntimeException("Error! lengths don't match");
    		}
    		double dot = 0;
    		for (int i = 0; i < v1.length; i++)
    		{
    			dot += v1[i] * v2[i];
    		}
    		return dot;
    	}
     
    	/**
    	 * Performs the matrix multiplication mat1*mat2. Throws a runtime exception
    	 * if col(mat1) != row(mat2). It is assumed that both matrices are
    	 * non-jagged.
    	 * 
    	 * @param mat1
    	 * @param mat2
    	 * @return
    	 */
    	public static double[][] mMult(double[][] mat1, double[][] mat2)
    	{
    		if (mat1[0].length != mat2.length)
    		{
    			throw new RuntimeException("error! col{mat1) != col(mat2)");
    		}
    		double[][] result = new double[mat1.length][mat2[0].length];
    		// simplify the process of getting the correct row
    		double[][] temp = CopyOfOneToTen.transpose(mat2);
    		for (int i = 0; i < mat1.length; i++)
    		{
    			for (int j = 0; j < mat2[0].length; j++)
    			{
    				result[i][j] = CopyOfOneToTen.dot(mat1[i], temp[j]);
    			}
    		}
    		return result;
    	}
     
    	/**
    	 * Transposes the matrix. Assumes a non-jagged matrix.
    	 * 
    	 * @param mat
    	 * @return
    	 */
    	public static double[][] transpose(double[][] mat)
    	{
    		double[][] transposed = new double[mat[0].length][mat.length];
    		for (int i = 0; i < transposed.length; i++)
    		{
    			for (int j = 0; j < mat.length; j++)
    			{
    				transposed[i][j] = mat[j][i];
    			}
    		}
    		return transposed;
    	}
     
    	/**
    	 * Calculated the Frobenius Norm (2-norm) of a matrix.
    	 * 
    	 * norm(mat) = sqrt(sum(mat[i][j]^2))
    	 * 
    	 * @param mat
    	 * @return
    	 */
    	public static double fNorm(double[][] mat)
    	{
    		double sum = 0;
    		for (int i = 0; i < mat.length; i++)
    		{
    			for (int j = 0; j < mat[i].length; j++)
    			{
    				sum += mat[i][j] * mat[i][j];
    			}
    		}
    		return Math.sqrt(sum);
    	}
     
    	/**
    	 * Uses the jacobi iterative algorithm to calculate the solution vector of
    	 * Ax = B. Iterates until the residual norm is less than the tolerance
    	 * value, or the max iterations has been reached.
    	 * 
    	 * @param a
    	 * @param b
    	 * @param max
    	 * @param tol
    	 * @return
    	 */
    	public static double[] jacobi(double[][] a, double[] b, int max, double tol)
    	{
    		double[] oldX = new double[b.length];
    		double[] newX = new double[b.length];
    		// initial guesses: b
    		for (int i = 0; i < b.length; i++)
    		{
    			oldX[i] = b[i];
    		}
    		for (int count = 0; count < max; count++)
    		{
    			newX = new double[b.length];
    			for (int i = 0; i < b.length; i++)
    			{
    				// temporary value for calculating new x values
    				double temp = 0;
    				for (int j = 0; j < b.length; j++)
    				{
    					if (j != i)
    					{
    						temp = temp + a[i][j] * oldX[j];
    					}
    				}
    				// new x value
    				newX[i] = (b[i] - temp) / a[i][i];
    				if (Double.isInfinite(newX[i]) || Double.isNaN(newX[i]))
    				{
    					// won't converge, somehow got to infinity
    					System.out.println("Won't converge!");
    					return null;
    				}
    			}
    			oldX = newX;
    			// calculate the residual
    			if (CopyOfOneToTen.fNorm(CopyOfOneToTen.calculateResidual(a, oldX, b)) <= tol)
    			{
    				// within tolerance, stop
    				return newX;
    			}
    		}
    		return newX;
    	}
     
    	public static void main(String[] args) throws FileNotFoundException
    	{
    		double[][] a = { { -16, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 4, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
    				{ -16, -8, 1, 0, 0, 0, 0, 0, 0, 0 }, { 24, 13, -2, 1, 0, 0, 0, 0, 0, 0 },
    				{ -37, -11, 2, 0, 1, 0, 0, 0, 0, 0 }, { 28, 20, -4, -1, 0, 1, 0, 0, 0, 0 },
    				{ 22, 30, -5, -5, -2, 0, 1, 0, 0, 0 }, { -75, -41, 9, 0, 3, -1, 1, 1, 0, 0 },
    				{ -42, -39, 6, 8, 4, 1, -2, 0, 1, 0 }, { 17, -27, 6, 6, 11, 4, 0, 2, 0, 1 } };
    		double[] b = { -16, 6, -29, 48, -48, 58, 44, -106, -49, 110 };
    		double[] vals = CopyOfOneToTen.jacobi(a, b, 100000, 1e-5);
    		for (int i = 0; i < vals.length; i++)
    		{
    			System.out.printf("%d ", (int) vals[i]);
    		}
    	}
     
    }

    17. Java

    import java.io.FileNotFoundException;
     
    /**
     * Solves a linear system of equations Ax = b using 3 different iterative
     * algorithms.
     * 
     * @author Andrew Ho
     * 
     */
    public class OneToTen
    {
    	/**
    	 * Calculates the residual matrix of Ax = B, where x is a guess vector.
    	 * residual = B - Ax
    	 * 
    	 * @param a
    	 * @param guessX
    	 * @param b
    	 * @return
    	 */
    	private static double[][] calculateResidual(double[][] a, double[] guessX, double[] b)
    	{
    		// convert x into a matrix to allow us to multiply
    		double[][] matX = new double[guessX.length][1];
    		for (int i = 0; i < matX.length; i++)
    		{
    			matX[i][0] = guessX[i];
    		}
    		// calculate the residual
    		double[][] resid = OneToTen.mMult(a, matX);
    		for (int i = 0; i < b.length; i++)
    		{
    			resid[i][0] = b[i] - resid[i][0];
    		}
    		return resid;
    	}
     
    	/**
    	 * Gets the dot product of two vectors.
    	 * 
    	 * @param mat1
    	 * @param mat2
    	 * @return mat1 dot mat2
    	 */
    	public static double dot(double[] v1, double[] v2)
    	{
    		if (v1.length != v2.length)
    		{
    			throw new RuntimeException("Error! lengths don't match");
    		}
    		double dot = 0;
    		for (int i = 0; i < v1.length; i++)
    		{
    			dot += v1[i] * v2[i];
    		}
    		return dot;
    	}
     
    	/**
    	 * Calculated the Frobenius Norm (2-norm) of a matrix.
    	 * 
    	 * norm(mat) = sqrt(sum(mat[i][j]^2))
    	 * 
    	 * @param mat
    	 * @return
    	 */
    	public static double fNorm(double[][] mat)
    	{
    		double sum = 0;
    		for (int i = 0; i < mat.length; i++)
    		{
    			for (int j = 0; j < mat[i].length; j++)
    			{
    				sum += mat[i][j] * mat[i][j];
    			}
    		}
    		return Math.sqrt(sum);
    	}
     
    	/**
    	 * Uses the Gauss-Seidel method for calculating the solution to Ax = b.
    	 * Stops when the tolerance is met or the max iterations is reached.
    	 * 
    	 * @param a
    	 * @param b
    	 * @param max
    	 * @param tol
    	 * @return
    	 */
    	public static double[] gaussSeidel(double[][] a, double[] b, int max, double tol)
    	{
    		double[] x = new double[b.length];
    		// intial guesses: 0
    		for (int count = 0; count < max; count++)
    		{
    			// calculate the residual
    			if (OneToTen.fNorm(OneToTen.calculateResidual(a, x, b)) <= tol)
    			{
    				// within tolerance, stop
    				break;
    			}
    			// outer loop
    			for (int i = 0; i < b.length; i++)
    			{
    				// temporary value for calculating new x values
    				double temp = 0;
    				for (int j = 0; j < b.length; j++)
    				{
    					if (i != j)
    					{
    						temp = temp + a[i][j] * x[j];
    					}
    				}
    				if (a[i][i] == 0)
    				{
    					// won't converge, divide by 0
    					System.out.println("Won't converge!");
    					return null;
    				}
    				x[i] = (b[i] - temp) / a[i][i];
    			}
    		}
    		return x;
    	}
     
    	public static void main(String[] args) throws FileNotFoundException
    	{
    		double[][] a = { { -16, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 4, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
    				{ -16, -8, 1, 0, 0, 0, 0, 0, 0, 0 }, { 24, 13, -2, 1, 0, 0, 0, 0, 0, 0 },
    				{ -37, -11, 2, 0, 1, 0, 0, 0, 0, 0 }, { 28, 20, -4, -1, 0, 1, 0, 0, 0, 0 },
    				{ 22, 30, -5, -5, -2, 0, 1, 0, 0, 0 }, { -75, -41, 9, 0, 3, -1, 1, 1, 0, 0 },
    				{ -42, -39, 6, 8, 4, 1, -2, 0, 1, 0 }, { 17, -27, 6, 6, 11, 4, 0, 2, 0, 1 } };
    		double[] b = { -16, 6, -29, 48, -48, 58, 44, -106, -49, 110 };
    		double[] vals = OneToTen.gaussSeidel(a, b, 100000, 1e-5);
    		for (int i = 0; i < vals.length; i++)
    		{
    			System.out.print((int) vals[i] + " ");
    		}
    	}
     
    	/**
    	 * Performs the matrix multiplication mat1*mat2. Throws a runtime exception
    	 * if col(mat1) != row(mat2). It is assumed that both matrices are
    	 * non-jagged.
    	 * 
    	 * @param mat1
    	 * @param mat2
    	 * @return
    	 */
    	public static double[][] mMult(double[][] mat1, double[][] mat2)
    	{
    		if (mat1[0].length != mat2.length)
    		{
    			throw new RuntimeException("error! col{mat1) != col(mat2)");
    		}
    		double[][] result = new double[mat1.length][mat2[0].length];
    		// simplify the process of getting the correct row
    		double[][] temp = OneToTen.transpose(mat2);
    		for (int i = 0; i < mat1.length; i++)
    		{
    			for (int j = 0; j < mat2[0].length; j++)
    			{
    				result[i][j] = OneToTen.dot(mat1[i], temp[j]);
    			}
    		}
    		return result;
    	}
     
    	/**
    	 * Transposes the matrix. Assumes a non-jagged matrix.
    	 * 
    	 * @param mat
    	 * @return
    	 */
    	public static double[][] transpose(double[][] mat)
    	{
    		double[][] transposed = new double[mat[0].length][mat.length];
    		for (int i = 0; i < transposed.length; i++)
    		{
    			for (int j = 0; j < mat.length; j++)
    			{
    				transposed[i][j] = mat[j][i];
    			}
    		}
    		return transposed;
    	}
    }

  20. #20
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 500 Ways to Print 1 to 10

    18. C++

    #include <iostream>
     
    int main()
    {
         int* a = new int[10];
         for (int i = 0; i < 10; i++)
         {
              *a = 10 - 1;
              a++;
         }
         for (int i = 0; i < 10; i++)
         {
              cout << a* << " ";
              a--;
         }
         delete[] a;
         return 0;
    }

  21. #21
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 500 Ways to Print 1 to 10

    19. C++
    #include <iostream>
    #include <math.h>
     
    int main()
    {
         double a= 10;
         for (int i = 0; i < 10; i++)
         {
              cout << (int) log10(a) << " ";
              a *= 10;
         }
         return 0;
    }

  22. #22
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 500 Ways to Print 1 to 10

    cheating

    19. Matlab

    1:10

  23. #23
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: 500 Ways to Print 1 to 10

    20. Java:
    int i = 1;
    while (2520 % i == 0) {
      System.out.println(i++);
    }
    db

  24. #24
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: 500 Ways to Print 1 to 10

    Any rules against Rube Goldberg solutions? haha

    # 21: java
    class BitSolution 
    {
    	public static final int[] COUNT_BITS = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 
    	3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 
    	3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 
    	5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 
    	3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 
    	4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 
    	5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};
    	public static void main(String[] args){		
    		int number = 1;
    		int bits = 0;
    		while (  (bits = countBits(number))  < 11 ){
    			System.out.println(" " + bits );
    			number |= ( bits << bits );
    		}
     
    	}
     
    	public static int countBits(int value){
    			return COUNT_BITS[ (value >> 24 ) & 0xff ] + 
    					COUNT_BITS[ (value >> 16 ) & 0xff ] + 
    					 COUNT_BITS[ (value >> 8 ) & 0xff ] +
    					  COUNT_BITS[ (value ) & 0xff ];
    	}
    }
    Last edited by copeg; April 4th, 2010 at 10:03 PM.

  25. #25
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: 500 Ways to Print 1 to 10

    Similar to Matlab
    #22: R
    c(1:10)

Page 1 of 6 123 ... LastLast

Similar Threads

  1. print code to browser
    By bookface in forum Java Theory & Questions
    Replies: 4
    Last Post: April 21st, 2010, 01:09 AM
  2. print space
    By brainTuner in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 1st, 2010, 06:09 PM
  3. How to print results to screen
    By NinjaLink in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 19th, 2010, 01:46 PM
  4. Can someone please tell me why my code doesn't print out anything?
    By deeerek in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 6th, 2010, 08:35 AM
  5. print hex decimal value
    By ran830421 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 25th, 2009, 07:23 PM