# Square of the number of ones

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?

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

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

• I need write breakline(\n), but fputs no do this. Feb 26, 2012 at 11:02
• fputs works nice, 140 bytes Feb 26, 2012 at 11:45
• fputs doesn't add a newline (unlike puts). Feb 27, 2012 at 15:04

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"));
}


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

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

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

## 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 # :-)


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

# K, 13

Another invalid answer but what the hell.

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


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

# Japt, 5 bytes

Outputs a digit array.

ì1 ì²


Try it

# 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"));}

• 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. Nov 2, 2014 at 19:08