4
\$\begingroup\$

Notice the pattern in the below sequence:

0.1, 0.01, 0.001, 0.0001, 0.00001 and so on, until reaching 0.{one hundred zeros}1

Then, continued:

0.2, 0.02, 0.002, 0.0002, 0.00002 and so on, until reaching 0.{two hundred zeros}2

Continued:

0.3, 0.03, etc, until 0.{three hundred zeros}3

Continued:

0.4, 0.04, etc, until 0.{four hundred zeros}4

Sped up a bit:

0.10, 0.010, etc.  until 0.{one thousand zeros}10

Sped up some more:

0.100, 0.0100...

You get the idea.

The input your code will receive is an integer, the number of terms, and the output is that many number of terms of the sequence.

Input and output formats are unrestricted, and separators are not required.

Trailing zeros are necessary.

Standard loopholes apply.


The outputs of some nth terms are given below:

nth term -> number
-------------------
1        -> 0.1
102      -> 0.2
303      -> 0.3
604      -> 0.4
1005     -> 0.5 
1506     -> 0.6
2107     -> 0.7
2808     -> 0.8
3609     -> 0.9
4510     -> 0.10
\$\endgroup\$
  • 3
    \$\begingroup\$ Are the trailing zeroes necessary? \$\endgroup\$ – Mego Sep 29 '16 at 20:41
  • 1
    \$\begingroup\$ "So, for the number 100, it's 0.100, 0.0100, etc." \$\endgroup\$ – Mego Sep 29 '16 at 20:52
  • 1
    \$\begingroup\$ ^ 0.100 is the same as 0.1, numerically. \$\endgroup\$ – mbomb007 Sep 29 '16 at 20:53
  • 3
    \$\begingroup\$ Then you need to edit the challenge to make that explicitly clear. I'm fairly sure that invalidates existing answers. \$\endgroup\$ – Mego Sep 29 '16 at 20:57
  • \$\begingroup\$ interesting combination of numeric operations and string operations. \$\endgroup\$ – Brian H. Sep 25 '17 at 7:29

13 Answers 13

10
\$\begingroup\$

05AB1E, 23 bytes

Online compiler can't show all terms for high N but it works fine offline.

Lvy2°*>F„0.0N×yJ,¼¾¹Qiq

Explanation

                          # implicit input X
Lv                        # for each y in range [1 .. X]
  y2°*>F                  # for each N in range [0 .. y*100+1)
        „0.               # push the string "0."
           0N×            # push 0 repeated N times
              y           # push current y
               J,         # join and print
                 ¼¾¹Qiq   # quit the program after X terms printed

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Python 2, 73 64 bytes

I think this is pretty short. I'm not sure that I can get it any shorter now that I golfed the if-else away.

s=z=1
exec'print"0.%0*d"%(z,s);q=z>s*100;z+=1-z*q;s+=q;'*input()

Try it online

Less golfed:

n=input()
s=z=1
for i in range(n):
    print"0.%0*d"%(z,s)
    if z>s*100:z=1;s+=1
    else:z+=1
\$\endgroup\$
5
\$\begingroup\$

Haskell, 53 63 bytes

This does the same thing as the 05AB1E answer now

f=(`take`["0."++('0'<$[1..y])++show x|x<-[1..],y<-[0..x*100]])
\$\endgroup\$
  • 2
    \$\begingroup\$ (`take`[.......]) saves two bytes \$\endgroup\$ – Damien Sep 29 '16 at 17:15
5
\$\begingroup\$

Perl 5, 57 52 bytes

(needs -E for say)

for(1..<>){say"0."."0"x$i++.$.;$.++,$i=0if$i>100*$.}

Or readably:

# $. is the input line number, use it to count the
# number at the end. 
# $i counts zeroes in the middle, starts at 0 implicitly                          
for(1..<>){                      # get the number of terms from stdin and loop 
                                 # <> implicitly increases $. (to 1)
    say "0." . "0" x $i++ . $.;  # print the number, add one zero
    $.++, $i=0 if $i > 100 * $.; # increment the number, reset number 
                                 # of zeroes if we've printed enough 
}

Trailing zeroes are printed since the number after the row of zeroes is just an integer concatenated as a string.

Test run:

$ perl -E 'for(1..<>){say"0."."0"x$i++.$.;$.++,$i=0if$i>100*$.}' | tail -3
4511    
0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009
0.10
0.010
\$\endgroup\$
  • \$\begingroup\$ @TheBitByte, yes, added explicit mention of that and a sample output \$\endgroup\$ – ilkkachu Sep 29 '16 at 21:07
  • 1
    \$\begingroup\$ You can win 2 bytes by doing map{...}1..<> instead of for(1..<>){...} ;) \$\endgroup\$ – Dada Sep 29 '16 at 21:14
  • \$\begingroup\$ Always good too see a new Perl competitor :-) Have a +1 \$\endgroup\$ – Ton Hospel Sep 30 '16 at 18:52
3
\$\begingroup\$

QBIC, 73 64 bytes

:#0|{C=!q$D=@0.|[0,q*100|d=d+1?D+$LTRIM$|(C) D=D+A~d=a|_X]]q=q+1

Saved 9 bytes by removing x as upper bound in the FOR loop. Upper bound is now set as q*100. Shame that QBasic automatically adds a space in front of a printed number, that's a costly LTRIM$...

Some explanation: q is used as the current number in the loop. This starts as 1 and gets incremented every 100N turns. This number gets converted to a string and is then appended to the string 0. and the correct number of 0's. In detail:

We use q as our 'base number', this is 1 implicitly 
:                Get the max number of terms from a numeric CMD line param    
#0|              Define string A$ as "0"
{                DO
C=!q$            our base number is cast to the string C$ 
D=@0.|           define D$ as "0."
[0,q*100|        FOR each of the terms we need to do for our base number 
                 (i.e. 101 for 1, 201 for 2 ...)
d=d+1            Keep track of the total # of terms 
                 (i.e. 101 after the 1 cycle, 302 after the 2...)
?D+$LTRIM$|(C)   Print to screen D$ + C$, where D$ = "0.[000]" 
                 and C$ is our base number (1, or 2, or - much later - 100)
                 LTRIM is in there to prevent "0.00 1"
D=D+A            Add A$ to the end of D$ ("0.00" becomes "0.000")
~d=a|_X]         Stop if we've reached the max. # of terms
]                NEXT
q=q+1            If we've done all the terms necessary with this base number
                 then raise base number.
The DO loop gets closed implicitly by QBIC.

Edit: QBIC has seen quite some development over time, and the above can now be solved in 49 bytes, 15 bytes shorter:

{D=@0.`[0,q*100|d=d+1?D+!q$┘D=D+@0`~d=a|_X]]q=q+1
\$\endgroup\$
2
\$\begingroup\$

PowerShell v2+, 89 bytes

param($n)for($j=1;;$j++){for($i=0;$i-le100*$j;$i++){"0.$('0'*$i)$j";if(++$k-ge$n){exit}}}

Straight-up double-for loop. The first, for $j is infinite. The inner, for $i, loops from 0 up to 100*$j, using string concatenation and string multiplication to print out the appropriate item. It also checks total loop counter $k against input $n, and exits the program after we hit that point.

\$\endgroup\$
  • 1
    \$\begingroup\$ @TheBitByte Yes, because each loop the item is being constructed and left on the pipeline as a literal string, so it won't do any truncation. \$\endgroup\$ – AdmBorkBork Sep 30 '16 at 12:21
2
\$\begingroup\$

PHP, 83 Bytes

for($s=1;$i++<$argv[1];){if($r>100*$s){$s++;$r=0;}echo" 0.".str_repeat(0,$r++).$s;}
\$\endgroup\$
  • 1
    \$\begingroup\$ @TheBitByte $s=100;$r=5;echo" 0.".str_repeat(0,$r++).$s; gives back as a string 0.00000100 In my opinion it fits the conditions. We use a string and not a float or a double \$\endgroup\$ – Jörg Hülsermann Sep 29 '16 at 21:13
2
\$\begingroup\$

GNU sed 163 157

+2 included for -rn

s,^,0.1\n,;:;P;s,\.,.0,;s,.$,,;/0{101}/{s,0\.0*(09)?,\1,
s,.9*\n,x&,;h;s,.*x(.*)\n.*,\1,;y,0123456789,1234567890,
G;s,(.*\n)(.*)x.*\n(.*),0.\2\1\3,};/\n./b

Takes input in unary based in this consensus.

Try it online!


An easy way to run this is:

 yes 1 | head -{input} | tr -d '\n' | sed -rnf zeroOne.sed

Explanation

s,^,0.1\n,                   #add 0.1 as the first line of the pattern space
:
P                            #print everything up to the first newline
s,\.,.0,                     #add a 0 after the .
s,.$,,                       #reduce the number left to print by one
/0{100}/{                    #if there is a string of 100 zero do the following
    s,0\.0*(09)?,\1,                 #get rid of all the 0s (except one if the we are adding another digit)
    s,.9*\n,x&,              #put an x before the left most number that will change
    h                        #store in hold space
    s,.*x(.*)\n.*,\1,        #remove everything except the numbers that change
    y,0123456789,1234567890, #replace numbers with the next one up
    G                        #bring the rest of the string back
    s,(.*\n)(.*)x.*\n(.*),0.\2\1\3, 
                             #replace the x and numbers that changed with the new ones
}
/\n./b                   #branch back unless there isn't anything on the last line
\$\endgroup\$
2
\$\begingroup\$

Mathematica, 70 bytes

Take[Join@@Table["0."<>Array["0"&,k]<>ToString@m,{m,#},{k,0,100m}],#]&

Unnamed function that takes the desired length as its argument and returns a list of strings (using strings instead of numbers makes the trailing zeros easy to include).

This version is slow as hell! because if you want, for example, the first 10 terms, it actually computes the sequence all the way through the 10th segment (101 + 201 + ... + 1001 = 5510 terms) and then retains only the first 10. So already at input 303, it's computing nearly five million terms and discarding almost all of them. The 81-byte version

Take[Join@@Table["0."<>Array["0"&,k]<>ToString@m,{m,Sqrt[#/50]+1},{k,0,100m}],#]&

doesn't have this flaw, and in fact calculates nearly the fewest segments possible.

\$\endgroup\$
  • 1
    \$\begingroup\$ You can save 1 byte by avoiding Table: Take[Join@@("0."<>#&/@NestList["0"<>#&,ToString@#,100#]&~Array~#),#]& \$\endgroup\$ – JungHwan Min Sep 30 '16 at 0:01
2
\$\begingroup\$

JavaScript (ES6), 69

n=>[...Array(n)].map(_=>(w=l--?z:(l=++x*100,z='0.'),z+=0,w+x),l=x=0)

Unnamed function with desired length in input and returning an array of strings.

Test

f=
n=>[...Array(n)].map(_=>(w=l--?z:(l=++x*100,z='0.'),z+=0,w+x),l=x=0)

var tid=0
function exec()
{
    var n=+I.value; 
    O.textContent=f(n).map((r,i)=>i+1+' '+r).join`\n`;
}

function update()
{
  clearTimeout(tid);
  tid=setTimeout(exec, 1000);
}

exec()
<input id=I value=305 type=number oninput='update()'><pre id=O></pre>

\$\endgroup\$
1
\$\begingroup\$

dc, 71 bytes

sn1sj1si1[lj1+dddsjZ1-dkAr^/li1+si]sJ[0nK1+kA/pljA0*K=Jlnlid1+si<M]dsMx

Try it online!

Honestly, I thought taking advantage of dc's arbitrary precision would make this easy, but I'm pretty sure just printing a ton of zeroes will be golfier. It feels really suboptimal, but I still think the use of arbitrary precision is interesting.

sn1sj1si1 We're taking top-of-stack as our nth-term target, so we store it in n, initialize i and j at 1, and leave a 1 on the stack.

[lj1+dddsjZ1-dkAr^/li1+si]sJ Macro J handles what to do when we've hit the 100, 200, &c. mark. Counter j keeps track of this, so we increment it, we set our precision to the number of digits it has less one, and then we leave the first digit of it (Z1-Ar^/; divide by 10^(number of digits less one)) on the stack since this is our starting point for the next big long chain.

[0nK1+kA/pljA0*K=Jlnlid1+si<M]dsMx Our main macro. First we print a leading 0 with 0n. Then K1+k increments our precision by one place. A/ divides by ten, pushing us a place over to the right.p prints, and then the rest is housekeeping. We can test our current precision to determine whether or not we need to jump into macro J, so we multiply counter j by a hundred (note: A0 here may need to be replaced with 100 on some implementations (cygwin)) and do the comparison. We load n, our target nth term, increment our main counter, i, and then compare the two. If we haven't yet hit the target, we keep running M.

\$\endgroup\$
1
\$\begingroup\$

Excel VBA, 63 Bytes

Anonymous VBE immediate window function that takes input from range [A1] and outputs to the VBE immediate window

n=[A1]:While n>i*100:n=n-1-i*100:i=i+1:Wend:?"0."String(n,48)&i
\$\endgroup\$
0
\$\begingroup\$

Java 8, 120 bytes

n->{String r="";a:for(int i=1,j,k,c=n;;i++)for(j=0;j<=i*100;r+=i+"\n"){for(r+="0.",k=j++;k-->0;r+=0);if(c--<1)break a;}}

Explanation and TIO uses the one below however. The difference: the one above creates one big String and then returns it; the one below prints every line separately (which is much better for performance).

130 bytes:

n->{String r;a:for(int i=1,j,k,c=n;;i++)for(j=0;j<=i*100;System.out.println(r+i)){for(r="0.",k=j++;k-->0;r+=0);if(c--<1)break a;}}

Explanation:

Try it here. (Will time out or exceed output limit for test cases above 604, but locally test case 4510 works in below 2 seconds.)

n->{                  // Method with integer parameter and no return-type
  String r;           //  Row String
  a:for(int i=1,      //  Index integer `i` starting at 1
        j,k,          //  Some other index integers
        c=n           //  Counter integer starting at the input
        ;;i++)        //  Loop (1) indefinitely
      for(j=0;        //   Reset `j` to 0
          j<=i*100;   //   Loop (2) from 0 to `i*100` (inclusive)
          System.out.println(r+i)){
                      //     After every iteration: print `r+i` + new-line
        for(r="0.",   //    Reset row-String to "0."
            k=j++;    //    Reset `k` to `j`
            k-->0;    //    Loop (3) from `j` down to `0`
          r+=0        //     And append the row-String with "0"
        );            //    End of loop (3)
        if(c--<1)     //    As soon as we've outputted `n` items
          break a;    //     Break loop `a` (1)
      }               //   End of loop (2) (implicit / single-line body)
                      //  End of loop (1) (implicit / single-line body)
}                     // End of method
\$\endgroup\$
  • \$\begingroup\$ I notice you often post explanations with every line commented. I think this tends to be counterproductive, since comments like "initialize _ to _" and "end of _" crowd out more informative comments. Have you considered paring down the comments, or providing a high-level overview instead? \$\endgroup\$ – Jakob Dec 17 '17 at 19:48
  • \$\begingroup\$ @Jakob It's mainly because Java answers are usually pretty big I think personally. If everything would fit on a single line without scrollbar, I could put the loops and ifs as a whole including closing bracket. Anyway, I'll try to leave out some things next time, like the "End of" parts, to see how it looks. Thanks for the feedback! \$\endgroup\$ – Kevin Cruijssen Dec 18 '17 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.