views:

821

answers:

6

I'm currently beginning to program with Java. I tried to code the sequence in the title as an output in Java, but I'm stuck! I'm experimenting with the for function, any help is welcomed ;)

+1  A: 

Or did your teacher ask you to? Two nested for loops will do the trick.

Or in another way in another language (Ruby):

4.times {|n| print 10**(n+1)}
Jonas Elfström
I was also wondering if this was a school project.....
Eddie
thanks... I kind of feel lame asking such simple things.. I'm a complete beginner to JAVA...
El Toro
+1 for the Ruby code. :)
Bombe
+16  A: 
System.out.println("1 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0");

But seriously, folks, this is an untested first pass:

for(int i=1; i<100; i++){
    System.out.print("1 ");
    for(int j=0; j<i; j++){
        System.out.print("0 ");
    }
}

If you looking for basic info on how to get started, Google is your friend. For example, try googling for "for loop java" and you'll get a lot of good examples. Also, to learn basic things in any language, a google search for "<language> hello world" is very reliable.

Michael Haren
+1 for funny AND right.
paxdiablo
+1 for what Pax said. :)
Bombe
+4  A: 

You could store the number 10 in a variable, then in a loop print the number, multiply it by 10 (which appends a zero to its decimal representation), and repeat.

yjerem
Clever--I like that idea
Michael Haren
Right up until the point where you overflow :-)
paxdiablo
This is a good idea, but I think he needs to print the spaces in between each digit. @Pax: This won't overflow. He's not trying to print out the entire number, just chunks of it at a time.
Bill the Lizard
Depends on when the sequence runs out, @Bill, once you get beyond 2^31 with your multiplications, you're toasted. If Q'er is stopping at 100,000 then it's okay.
paxdiablo
It will overflow when you multiply 10^19 by 10 (assuming numbers are 64-bit). Tim's solution would last a bit longer.
yjerem
I see what you mean now. I'm assuming the string in the question is the full output.
Bill the Lizard
A: 

This is not really a question of fors but rather of very rudimentary algorithmic thinking. You have a sequence that consists of a "1", and then something else that grows over time", another "1", another something, etc. You can think of these as two different series that are interleaved.

Hence, overall structure would be something like:

while(... infinity?)
{
   System.out.print("1");
   doSomething(); 
}

Now the something clearly is correlated to the number of iterations ("stages") of the outer loop or the count of 1, so you need something like:

int stage=0;
while(...infinity?)
{
   ++stage;
   System.out.print("1");
   for(int i=0; i<stage; ++i) System.out.print("0");
}

If you know how many cycles you need to go through, use a for loop instead of a while, and increase stage through it.

Uri
+2  A: 
for (int i = 2; i < 64; i <<= 1)
    //System.out.print(Integer.toString(i, 2));
    System.out.print(Integer.toString(i, 2).replaceAll("[01]", "$0 "));
Tim
Clever. And the spaces between chars? :)
JMD
Fixed it: With regular expressions ofcourse! Lets make this the best answer ever! ;)
Tim
+15  A: 

Why two loops?

(converted from C#, pardon any syntax errors)

String s = "1 ";
for (int i = 0; i < 5; ++i)
{
  s = s + "0 ";
  System.out.print(s);
}

Self-critique:

  • two for loops (like Michael Haren's solution) would negate the string copying
  • a StringBuffer/StringBuilder would negate the string copying
JMD
nice - i like it.
Michael Haren
I had to think about 5 seconds before I got it ;)
VVS
Huh? Won't this just print a single "1" followed by a growing number of zeroes? I spent more than 5 seconds reading it ...
unwind
bad karma for using String + String in loop. this algorithm has a O(n²) runtime due to object allocation/deacllocation, compared to the other solutions.
Andreas Petersson
@Andreas, you're right. But I did note, myself, that the string copying was not ideal. I think we can all agree this was a somewhat contrived requirement. I just liked this as an alternative because it uses only one for loop and the others earlier on all used two. :)@unwind, if you doubt it debug it. :) It performs exactly to his requirements because s starts with "1 ", and s is both appended to and printed inside the for loop.So what's worse in this case, a known-quantity of String + String copying, or using regular expressions, or a StringBuffer, or...?
JMD