Square of the number of ones

-5

0

I'm solving problem: in file "a.in" given the number N - length of the number consists of ones. Need to gets square of it, and put this in file "a.out". This is my shortest solution(150 bytes):

char s[1<<27];j;main(i,n){for(fscanf(fopen("a.in","r"),"%d",&n),i=n*=2;--i;j+=i<n/2?i:n-i,s[i-1]=48+j%10,j/=10);fprintf(fopen("a.out","w"),"%s\n",s);}

This is formated copy:

char s[1<<27];j;
main(i,n){
    for(fscanf(fopen("a.in","r"),"%d",&n),i=n*=2;--i;
        j+=i < n/2 ? i: n-i,
        s[i - 1] = 48 + j % 10,
        j /= 10
    );
    fprintf(fopen("a.out","w"),"%s\n",s);
}

The best solution of this problem has size 131 bytes, how? Valid languages: C, C#, C++, Pascal, Java.

Maxim

Posted 2012-02-26T07:14:07.833

Reputation: 1

3Maybe I'm a bit slow, but it took me a long time just to get what the program should do. If the number is 3, you want to print 111*111, which is 12321. Right? – ugoren – 2012-02-26T21:12:28.943

3What's the source of this puzzle? – Peter Taylor – 2012-02-27T15:45:22.417

8Why the restriction to such a few languages? – user unknown – 2012-03-15T03:26:03.300

Answers

3

without changing your logic, you can save 9 characters by using fputs (instead of fprintf).

fputs(s,fopen("a.out","w"));

you can also save one character by not doubling n at first step, thus removing divide by two:

...,i=n*2;--i;
j+=i<n?i:2*n-i

saeedn

Posted 2012-02-26T07:14:07.833

Reputation: 1 241

I need write breakline(\n), but fputs no do this. – Maxim – 2012-02-26T11:02:50.307

fputs works nice, 140 bytes – Maxim – 2012-02-26T11:45:16.137

fputs doesn't add a newline (unlike puts). – ugoren – 2012-02-27T15:04:18.280

2

You can save another character, by doing j/=10 after for - for(...)j/=10;

My best so far, 145 characters:

char s[1<<27];j;
main(i,n){
    for(fscanf(fopen("a.in","r"),"%d",&n),s[i=n*2-1]=10;i;
        j+=i<n?i:n*2-i,
        s[--i]=48+j%10
    )j/=10;
    fputs(s,fopen("a.out","w"));
}

ugoren

Posted 2012-02-26T07:14:07.833

Reputation: 16 527

2

C, 104 chars

(Assuming ugoren's comment is correct)

main(n,k){fscanf(fopen("a.in","r"),"%d",&n);for(k=1;--n;k=k*10+1);fprintf(fopen("a.out","w"),"%d",k*k);}

Note that I needed to compile it with -m32 on OSX to keep it from crashing. Probably something to do with the implicit prototypes brought about by not including stdio.h.

Keith Randall

Posted 2012-02-26T07:14:07.833

Reputation: 19 865

N may be in range [1, 5*10^7] – Maxim – 2012-03-02T14:40:18.297

2@Maxim: then that should be part of the question. – Keith Randall – 2012-03-02T17:00:35.137

>

  • C's implicit prototype assume functions return int. When they return a pointer, and it's larger than int, things go bad. 2. You can avoid initializing k if you switch it with n (and run the program without parameters). Saves 4 characters (if you also move fscanf into for. Or you can initialize k with fscanf's return value.
  • < – ugoren – 2012-03-03T21:02:36.613

    2

    J, 17 characters

    Invalid answer and not really of any use to the OP, but I was bored and this occupied a few minutes.

    *:10#.1$~".1!:1[1
    

    1!:1[1 take input from the keyboard,

    1$~ creates a list of 1s of the length specified by the input,

    10#. converts to a base 10 number,

    *: squares it.

    Gareth

    Posted 2012-02-26T07:14:07.833

    Reputation: 11 678

    2

    GolfScript 7

    Another answer in an illegal language, for the same reason as the one invoked by Gareth.

    ~1`*~.*
    

    Explanation:

    • takes a number as command line parameter
    • ~ evaluates the number
    • 1` pushes the '1' string on the stack
    • * multiplies the character '1' by the number specified as input (results in a string)
    • ~ evaluates the string of 1s, thus storing the equivalent numeric value on the stack
    • .* squares the existing value

    In order to get the expected output, the program should be called like this:

     more a.in | ruby golfscript.rb program.gs > a.out # :-)
    

    Cristian Lupascu

    Posted 2012-02-26T07:14:07.833

    Reputation: 8 369

    1

    Jelly, 4 bytes (feedback welcome!)

    1xV²
    

    Try it online!

    Repeat '1' a number of times equal to the input (x), concatenate and eValuate the result, and square it.

    Old solution:

    RŒBV
    

    Try it online!

    As per @ugoren's explanation. Make a range from 1 to N, then bounce it (mirror except the last element) with ŒB and use V to concatenate the digits.

    Harry

    Posted 2012-02-26T07:14:07.833

    Reputation: 1 189

    1This is wrong for inputs larger than 9. – Nit – 2018-05-04T08:03:21.067

    2Just wondering, why are you answering a bunch of questions that were last active in 2012? – Jo King – 2018-05-04T09:20:31.597

    @Nit Thank you! I missed that but found another more literal solution in the same amount of bytes. This one should work for larger inputs! – Harry – 2018-05-04T14:58:51.907

    1@JoKing Just trying to practice, and I figure might as well contribute to the site where other people can see. I'm just choosing questions I think that I can answer without considering the date. Hope it helps! – Harry – 2018-05-04T15:04:02.613

    1

    Wren, 55 bytes

    Just do an invalid answer in case file manipulation doesn't work.

    Fn.new{|x|Num.fromString("1"*Num.fromString(x)).pow(2)}
    

    Try it online!

    Explanation

    Takes input as string:

    Fn.new{|x|                                            } // New anonymous function
                                 Num.fromString(x)          // Convert input into a number
              Num.fromString("1"*                 )         // Evaluate 1 with that number times
                                                   .pow(2)  // Square the resulting value
    

    user85052

    Posted 2012-02-26T07:14:07.833

    Reputation:

    1

    Just for fun a 'dc' solution. It read/writes from stdin/stdout, because of its limitations

    $ dc -e '?0sn[lnA*1+sn1-d0<x]dsxxlnd*p' <<< 3
    12321
    

    Dan Andreatta

    Posted 2012-02-26T07:14:07.833

    Reputation: 211

    0

    Keg, -hr, 5 bytes

    1⅍*ℤ²
    

    Try it online!

    Another illegal answer, but hey, as far as I know, there ain't any CGCC jail y'all can throw me into.

    Lyxal

    Posted 2012-02-26T07:14:07.833

    Reputation: 5 253

    0

    C#, 172 chars

    namespace System{class P{static void Main(){var b=Numerics.BigInteger.Parse(new String('1',int.Parse(IO.File.ReadAllText("a.in"))));IO.File.WriteAllText("a.out",b*b+"");}}}
    

    Best I could do with a language with big integers. Unless you're willing to add Python to the language list...

    Keith Randall

    Posted 2012-02-26T07:14:07.833

    Reputation: 19 865

    0

    Java, 198 chars

    Saw you mention Java, thought I'd give it a try. This is the best I can get using the standard runtime:

    import java.io.*;enum F{F;System s;{try{s.setOut(new PrintStream("a.out"));s.out.print((int)Math.pow(1/(9/Math.pow(10,new java.util.Scanner(new File("a.in")).nextInt())),2));}catch(Exception e){}}}
    

    Chad Retz

    Posted 2012-02-26T07:14:07.833

    Reputation: 131

    0

    bash: 69 chars:

    w=$(for i in $(seq 1 `<a.in`)
    do
    echo -n 1
    done)
    echo $((w**2))>a.out
    

    a.out - really? :)

    The only problem: invalid language.

    user unknown

    Posted 2012-02-26T07:14:07.833

    Reputation: 4 210

    0

    K, 13

    Another invalid answer but what the hell.

    {a*a:.:x#"1"}
    

    tmartin

    Posted 2012-02-26T07:14:07.833

    Reputation: 3 917

    -2

    C, 130 chars

    char s[1<<27];main(i,n,j){for(read(open("a.in",0),s),n=atoi(s);j=j/10+n-abs(n-i);s[n*2-++i]=48+j%10);fputs(s,fopen("a.out","w"));}
    

    Maxim

    Posted 2012-02-26T07:14:07.833

    Reputation: 1

    1Since you are actually willing to make the effort, would you please clarify your question? 3 people asked about some fine points; and got no answer. Also, you did some more clarifications in comments; they belong in the question itself. – anatolyg – 2014-11-02T19:08:13.793