-7
\$\begingroup\$

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?

\$\endgroup\$
3
  • 3
    \$\begingroup\$ Maybe 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? \$\endgroup\$
    – ugoren
    Feb 26, 2012 at 21:12
  • 3
    \$\begingroup\$ What's the source of this puzzle? \$\endgroup\$ Feb 27, 2012 at 15:45
  • 8
    \$\begingroup\$ Why the restriction to such a few languages? \$\endgroup\$ Mar 15, 2012 at 3:26

15 Answers 15

3
\$\begingroup\$

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
\$\endgroup\$
3
  • \$\begingroup\$ I need write breakline(\n), but fputs no do this. \$\endgroup\$
    – Maxim
    Feb 26, 2012 at 11:02
  • \$\begingroup\$ fputs works nice, 140 bytes \$\endgroup\$
    – Maxim
    Feb 26, 2012 at 11:45
  • \$\begingroup\$ fputs doesn't add a newline (unlike puts). \$\endgroup\$
    – ugoren
    Feb 27, 2012 at 15:04
2
\$\begingroup\$

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"));
}
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ N may be in range [1, 5*10^7] \$\endgroup\$
    – Maxim
    Mar 2, 2012 at 14:40
  • 2
    \$\begingroup\$ @Maxim: then that should be part of the question. \$\endgroup\$ Mar 2, 2012 at 17:00
  • \$\begingroup\$ 1. 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. \$\endgroup\$
    – ugoren
    Mar 3, 2012 at 21:02
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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 # :-)
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ This is wrong for inputs larger than 9. \$\endgroup\$
    – Etheryte
    May 4, 2018 at 8:03
  • 2
    \$\begingroup\$ Just wondering, why are you answering a bunch of questions that were last active in 2012? \$\endgroup\$
    – Jo King
    May 4, 2018 at 9:20
  • \$\begingroup\$ @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! \$\endgroup\$
    – habs
    May 4, 2018 at 14:58
  • 1
    \$\begingroup\$ @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! \$\endgroup\$
    – habs
    May 4, 2018 at 15:04
1
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

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...

\$\endgroup\$
0
\$\begingroup\$

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){}}}
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

K, 13

Another invalid answer but what the hell.

{a*a:.:x#"1"}
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Japt, 5 bytes

Outputs a digit array.

ì1 ì²

Try it

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

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"));}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Since 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. \$\endgroup\$
    – anatolyg
    Nov 2, 2014 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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