Show the (decimal) value of a number of 1s appended with a 3, then squared
- Task
Show here (on this page) the decimal numbers formed by:
- (n 1's appended by the digit 3) and then square the result, where 0 <= n < 8
Ada
<lang Ada>with Ada.Text_Io; with Ada.Numerics.Big_Numbers.Big_Integers;
procedure Ones_Plus_Three is
use Ada.Numerics.Big_Numbers.Big_Integers; use Ada.Text_Io;
Root : Big_Natural := 3; Squared : Big_Natural;
begin
for N in 0 .. 8 loop Squared := Root**2;
Put (To_String (Root, Width => 12)); Put (" "); Put (To_String (Squared, Width => 20)); New_Line;
Root := @ + 10**(N + 1); end loop;
end Ones_Plus_Three;</lang>
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769 111111113 12345679432098769
ALGOL 68
Assuming LONG INT is large enough (as in e.g. ALGOL 68G). <lang algol68>BEGIN
LONG INT n := 0; FOR i TO 8 DO LONG INT n3 = ( n * 10 ) + 3; print( ( whole( n3, 0 ), " ", whole( n3 * n3, 0 ), newline ) ); n *:= 10 +:= 1 OD
END</lang>
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769
Alternative version that shows the values for higher numbers of ones.
<lang algol68>BEGIN
PR precision 250 PR LONG LONG INT n := 0; FOR i FROM 0 TO 111 DO LONG LONG INT n3 = ( n * 10 ) + 3; IF i > 84 THEN STRING v := whole( n3 * n3, 0 ); INT pos := 0; STRING pattern := "123456790"; INT p len := ( UPB pattern - LWB pattern ) + 1; WHILE string in string( pattern, pos, v ) DO v := v[ 1 : pos - 1 ] + "A" + v[ pos + p len : ] OD; pattern := "987654320"; WHILE string in string( pattern, pos, v ) DO v := v[ 1 : pos - 1 ] + "Z" + v[ pos + p len : ] OD; print( ( whole( i, -3 ), " ", v, newline ) ) FI; n *:= 10 +:= 1 OD
END</lang>
- Output:
As the 111...113^2 values get rather large, the code above replaces "123456790" with "A" and "987654320" with "Z". The number of ones is shown on the left.
85 AAAAAAAAA1234ZZZZZZZZZ98769 86 AAAAAAAAA123460ZZZZZZZZZ98769 87 AAAAAAAAA12345720ZZZZZZZZZ98769 88 AAAAAAAAA1234568320ZZZZZZZZZ98769 89 AAAAAAAAA123456794320ZZZZZZZZZ98769 90 AAAAAAAAAA54320ZZZZZZZZZ98769 91 AAAAAAAAAA1654320ZZZZZZZZZ98769 92 AAAAAAAAAA127654320ZZZZZZZZZ98769 93 AAAAAAAAAA12387654320ZZZZZZZZZ98769 94 AAAAAAAAAA1234ZZZZZZZZZZ98769 95 AAAAAAAAAA123460ZZZZZZZZZZ98769 96 AAAAAAAAAA12345720ZZZZZZZZZZ98769 97 AAAAAAAAAA1234568320ZZZZZZZZZZ98769 98 AAAAAAAAAA123456794320ZZZZZZZZZZ98769 99 AAAAAAAAAAA54320ZZZZZZZZZZ98769 100 AAAAAAAAAAA1654320ZZZZZZZZZZ98769 101 AAAAAAAAAAA127654320ZZZZZZZZZZ98769 102 AAAAAAAAAAA12387654320ZZZZZZZZZZ98769 103 AAAAAAAAAAA1234ZZZZZZZZZZZ98769 104 AAAAAAAAAAA123460ZZZZZZZZZZZ98769 105 AAAAAAAAAAA12345720ZZZZZZZZZZZ98769 106 AAAAAAAAAAA1234568320ZZZZZZZZZZZ98769 107 AAAAAAAAAAA123456794320ZZZZZZZZZZZ98769 108 AAAAAAAAAAAA54320ZZZZZZZZZZZ98769 109 AAAAAAAAAAAA1654320ZZZZZZZZZZZ98769 110 AAAAAAAAAAAA127654320ZZZZZZZZZZZ98769 111 AAAAAAAAAAAA12387654320ZZZZZZZZZZZ98769
AWK
<lang AWK>
- syntax: GAWK -f SHOW_THE_DECIMAL_VALUE_OF_A_NUMBER_OF_1S_APPENDED_WITH_A_3_THEN_SQUARED.AWK
- converted from FreeBASIC
BEGIN {
for (i=0; i<=7; i++) { m = make13(i) printf("%1d %9s^2 %'20d\n",i,m,m*m) } exit(0)
} function make13(n, t) {
while (n--) { t = 10 * (t+1) } return(t+3)
} </lang>
- Output:
0 3^2 9 1 13^2 169 2 113^2 12,769 3 1113^2 1,238,769 4 11113^2 123,498,769 5 111113^2 12,346,098,769 6 1111113^2 1,234,572,098,769 7 11111113^2 123,456,832,098,769
C
<lang c>#include <stdio.h>
- include <stdint.h>
uint64_t ones_plus_three(uint64_t ones) {
uint64_t r = 0; while (ones--) r = r*10 + 1; return r*10 + 3;
}
int main() {
uint64_t n; for (n=0; n<8; n++) { uint64_t x = ones_plus_three(n); printf("%8lu^2 = %15lu\n", x, x*x); } return 0;
}</lang>
- Output:
3^2 = 9 13^2 = 169 113^2 = 12769 1113^2 = 1238769 11113^2 = 123498769 111113^2 = 12346098769 1111113^2 = 1234572098769 11111113^2 = 123456832098769
C#
For 0 <= n < 22 <lang csharp>using System; using BI = System.Numerics.BigInteger; class Program { static void Main(string[] args) {
for (BI x = 3; BI.Log10(x) < 22; x = (x - 2) * 10 + 3) Console.WriteLine("{1,43} {0,-20}", x, x * x); } }</lang>
- Output:
9 3 169 13 12769 113 1238769 1113 123498769 11113 12346098769 111113 1234572098769 1111113 123456832098769 11111113 12345679432098769 111111113 1234567905432098769 1111111113 123456790165432098769 11111111113 12345679012765432098769 111111111113 1234567901238765432098769 1111111111113 123456790123498765432098769 11111111111113 12345679012346098765432098769 111111111111113 1234567901234572098765432098769 1111111111111113 123456790123456832098765432098769 11111111111111113 12345679012345679432098765432098769 111111111111111113 1234567901234567905432098765432098769 1111111111111111113 123456790123456790165432098765432098769 11111111111111111113 12345679012345679012765432098765432098769 111111111111111111113 1234567901234567901238765432098765432098769 1111111111111111111113
F#
<lang fsharp> [3L;13L;113L;1113L;11113L;111113L;1111113L;11111113L;111111113L]|>List.iter(fun n->printfn "%10d->%d" n (n*n)) </lang>
- Output:
3->9 13->169 113->12769 1113->1238769 11113->123498769 111113->12346098769 1111113->1234572098769 11111113->123456832098769 111111113->12345679432098769
Factor
a(n) = ((10n+1 - 1) / 9 + 2)2
<lang factor>USING: io kernel math math.functions prettyprint ;
- a ( n -- e m ) 1 + 10^ 1 - 9 / 2 + dup sq ;
8 [ a swap pprint bl . ] each-integer</lang>
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769
Fermat
<lang fermat>Func Make13(n) = m:=0; while n>0 do m:=10*(m+1);n:=n-1; od; m:=3+m; m. for i=0 to 7 do !Make13(i);!' ';!Make13(i)^2;!! od</lang>
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769
FreeBASIC
<lang freebasic>function make13(n as uinteger) as uinteger
dim as uinteger t = 0 while n t = 10*(t+1) n-=1 wend return t+3
end function
dim as ulongint m
for n as uinteger = 0 to 7
m = make13(n)^2 print make13(n), m
next n</lang>
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769
Forth
<lang forth>: 1s+3
0 swap begin dup while swap 10 * 1+ swap 1- repeat drop 10 * 3 +
- sqr dup * ;
- show dup . ." ^2 = " sqr . cr ;
- show-upto
0 swap begin over over < while swap dup 1s+3 show 1+ swap repeat 2drop
8 show-upto bye</lang>
- Output:
3 ^2 = 9 13 ^2 = 169 113 ^2 = 12769 1113 ^2 = 1238769 11113 ^2 = 123498769 111113 ^2 = 12346098769 1111113 ^2 = 1234572098769 11111113 ^2 = 123456832098769
Haskell
<lang haskell>import Text.Printf (printf)
onesPlusThree :: [Integer] onesPlusThree =
map ((3 +) . (10 *)) $ iterate ((1 +) . (10 *)) 0
format :: Integer -> String format = printf "%8lu^2 = %15lu" <*> (^ 2)
main :: IO () main = putStr $ unlines $ take 8 $ map format onesPlusThree</lang>
- Output:
3^2 = 9 13^2 = 169 113^2 = 12769 1113^2 = 1238769 11113^2 = 123498769 111113^2 = 12346098769 1111113^2 = 1234572098769 11111113^2 = 123456832098769
Julia
<lang julia>println("n (10^(n+1) - 1) ÷ 9 + 2) squared") for n in 0:7
println(rpad(n, 14), rpad((big"10"^(n+1) - 1) ÷ 9 + 2, 19), ((big"10"^(n+1) - 1) ÷ 9 + 2)^2)
end
</lang>
- Output:
n (10^(n+1) - 1) ÷ 9 + 2) squared 0 3 9 1 13 169 2 113 12769 3 1113 1238769 4 11113 123498769 5 111113 12346098769 6 1111113 1234572098769 7 11111113 123456832098769
Quackery
<lang Quackery> [ char 1 swap of
char 3 join $->n drop ] is 1's+3 ( n -> n )
8 times [ i^ 1's+3 dup echo say " --> " dup * echo cr ]</lang>
- Output:
3 --> 9 13 --> 169 113 --> 12769 1113 --> 1238769 11113 --> 123498769 111113 --> 12346098769 1111113 --> 1234572098769 11111113 --> 123456832098769
PARI/GP
<lang parigp>Make13(n)=m=0;while(n>0,m=10*(m+1);n=n-1);m=3+m;return(m) for(i=0,7,print(Make13(i)," ",Make13(i)^2))</lang>
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769
Perl
<lang perl>#!/usr/bin/perl
use strict; # https://rosettacode.org/wiki/Show_the_(decimal)_value_of_a_number_of_1s_appended_with_a_3,_then_squared use warnings;
- use bignum; # uncomment for larger than 9 or 32-bit perls
for ( 0 .. 7 )
{ my $number = 1 x $_ . 3; print "$number ", $number ** 2, "\n"; }</lang>
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769
Phix
Perfect opportunity for a little string math, why not...
for n=0 to 37 do string res = repeat('3',n)&'9' for i=1 to n do res = "0" & res integer digit = 3 for j=length(res)-i to 1 by -1 do digit += res[j]-'0' res[j] = remainder(digit,10)+'0' digit = floor(digit/10)+1 end for end for printf(1,"%38s %75s\n",{repeat('1',n)&'3',res}) end for
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769 111111113 12345679432098769 1111111113 1234567905432098769 11111111113 123456790165432098769 111111111113 12345679012765432098769 1111111111113 1234567901238765432098769 11111111111113 123456790123498765432098769 111111111111113 12345679012346098765432098769 1111111111111113 1234567901234572098765432098769 11111111111111113 123456790123456832098765432098769 111111111111111113 12345679012345679432098765432098769 1111111111111111113 1234567901234567905432098765432098769 11111111111111111113 123456790123456790165432098765432098769 111111111111111111113 12345679012345679012765432098765432098769 1111111111111111111113 1234567901234567901238765432098765432098769 11111111111111111111113 123456790123456790123498765432098765432098769 111111111111111111111113 12345679012345679012346098765432098765432098769 1111111111111111111111113 1234567901234567901234572098765432098765432098769 11111111111111111111111113 123456790123456790123456832098765432098765432098769 111111111111111111111111113 12345679012345679012345679432098765432098765432098769 1111111111111111111111111113 1234567901234567901234567905432098765432098765432098769 11111111111111111111111111113 123456790123456790123456790165432098765432098765432098769 111111111111111111111111111113 12345679012345679012345679012765432098765432098765432098769 1111111111111111111111111111113 1234567901234567901234567901238765432098765432098765432098769 11111111111111111111111111111113 123456790123456790123456790123498765432098765432098765432098769 111111111111111111111111111111113 12345679012345679012345679012346098765432098765432098765432098769 1111111111111111111111111111111113 1234567901234567901234567901234572098765432098765432098765432098769 11111111111111111111111111111111113 123456790123456790123456790123456832098765432098765432098765432098769 111111111111111111111111111111111113 12345679012345679012345679012345679432098765432098765432098765432098769 1111111111111111111111111111111111113 1234567901234567901234567901234567905432098765432098765432098765432098769 11111111111111111111111111111111111113 123456790123456790123456790123456790165432098765432098765432098765432098769
Raku
In an attempt to stave of terminal ennui, Find the first 8 where a(n) is semiprime.
<lang perl6>say "$_, {.²}" for (^∞).map({ ( 1 x $_ ~ 3)} ).grep({ .is-prime })[^8]</lang>
- Output:
3, 9 13, 169 113, 12769 11113, 123498769 111111113, 12345679432098769 11111111113, 123456790165432098769 111111111111111111111113, 12345679012345679012346098765432098765432098769 111111111111111111111111111111111111111111111111111111111111111111111111111111111113, 12345679012345679012345679012345679012345679012345679012345679012345679012345679012765432098765432098765432098765432098765432098765432098765432098765432098765432098769
REXX
A little extra code was added to pre-compute the biggest number to find the widths for output alignment. <lang rexx>/*REXX program appends a "3" to a number of "1"s, and then squares that number. */ numeric digits 1000 /*be able to handle huge numbers. */ parse arg n . /*obtain optional argument from the CL.*/ if n== | n=="," then n= 9 /*Not specified? Then use the default.*/ _= copies(1, n)3 /*compute largest index to get width. */ w1= length( commas(_) ) /*get the width of the largest index. */ w2= length( commas(_**2) ) /* " " " " " " number. */
do #=0 to n; _=copies(1, #)3 /*calculate prefix number for output. */ say right( commas(_), w1) right( commas(_**2), w2) /*show prefix, number. */ end /*#*/
exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</lang>
- output when using the input of: 37
(Shown at three-quarter size.)
3 9 13 169 113 12,769 1,113 1,238,769 11,113 123,498,769 111,113 12,346,098,769 1,111,113 1,234,572,098,769 11,111,113 123,456,832,098,769 111,111,113 12,345,679,432,098,769 1,111,111,113 1,234,567,905,432,098,769 11,111,111,113 123,456,790,165,432,098,769 111,111,111,113 12,345,679,012,765,432,098,769 1,111,111,111,113 1,234,567,901,238,765,432,098,769 11,111,111,111,113 123,456,790,123,498,765,432,098,769 111,111,111,111,113 12,345,679,012,346,098,765,432,098,769 1,111,111,111,111,113 1,234,567,901,234,572,098,765,432,098,769 11,111,111,111,111,113 123,456,790,123,456,832,098,765,432,098,769 111,111,111,111,111,113 12,345,679,012,345,679,432,098,765,432,098,769 1,111,111,111,111,111,113 1,234,567,901,234,567,905,432,098,765,432,098,769 11,111,111,111,111,111,113 123,456,790,123,456,790,165,432,098,765,432,098,769 111,111,111,111,111,111,113 12,345,679,012,345,679,012,765,432,098,765,432,098,769 1,111,111,111,111,111,111,113 1,234,567,901,234,567,901,238,765,432,098,765,432,098,769 11,111,111,111,111,111,111,113 123,456,790,123,456,790,123,498,765,432,098,765,432,098,769 111,111,111,111,111,111,111,113 12,345,679,012,345,679,012,346,098,765,432,098,765,432,098,769 1,111,111,111,111,111,111,111,113 1,234,567,901,234,567,901,234,572,098,765,432,098,765,432,098,769 11,111,111,111,111,111,111,111,113 123,456,790,123,456,790,123,456,832,098,765,432,098,765,432,098,769 111,111,111,111,111,111,111,111,113 12,345,679,012,345,679,012,345,679,432,098,765,432,098,765,432,098,769 1,111,111,111,111,111,111,111,111,113 1,234,567,901,234,567,901,234,567,905,432,098,765,432,098,765,432,098,769 11,111,111,111,111,111,111,111,111,113 123,456,790,123,456,790,123,456,790,165,432,098,765,432,098,765,432,098,769 111,111,111,111,111,111,111,111,111,113 12,345,679,012,345,679,012,345,679,012,765,432,098,765,432,098,765,432,098,769 1,111,111,111,111,111,111,111,111,111,113 1,234,567,901,234,567,901,234,567,901,238,765,432,098,765,432,098,765,432,098,769 11,111,111,111,111,111,111,111,111,111,113 123,456,790,123,456,790,123,456,790,123,498,765,432,098,765,432,098,765,432,098,769 111,111,111,111,111,111,111,111,111,111,113 12,345,679,012,345,679,012,345,679,012,346,098,765,432,098,765,432,098,765,432,098,769 1,111,111,111,111,111,111,111,111,111,111,113 1,234,567,901,234,567,901,234,567,901,234,572,098,765,432,098,765,432,098,765,432,098,769 11,111,111,111,111,111,111,111,111,111,111,113 123,456,790,123,456,790,123,456,790,123,456,832,098,765,432,098,765,432,098,765,432,098,769 111,111,111,111,111,111,111,111,111,111,111,113 12,345,679,012,345,679,012,345,679,012,345,679,432,098,765,432,098,765,432,098,765,432,098,769 1,111,111,111,111,111,111,111,111,111,111,111,113 1,234,567,901,234,567,901,234,567,901,234,567,905,432,098,765,432,098,765,432,098,765,432,098,769 11,111,111,111,111,111,111,111,111,111,111,111,113 123,456,790,123,456,790,123,456,790,123,456,790,165,432,098,765,432,098,765,432,098,765,432,098,769
Ring
<lang ring> load "stdlib.ring"
decimals(0)
see "working..." + nl
row = 0 limit = 8
str = "3" for n = 1 to limit
if n = 1 strn = number(str) res = pow(strn,2) see "{" + strn + "," + res + "}" + nl else str = "1" + strn strn = number(str) res = pow(strn,2) see "{" + strn + "," + res + "}" + nl ok
next
see "done..." + nl </lang>
- Output:
working... {3,9} {13,169} {113,12769} {1113,1238769} {11113,123498769} {111113,12346098769} {1111113,1234572098769} {11111113,123456832098769} done...
Wren
<lang ecmascript>import "/fmt" for Fmt
var a = Fn.new { |n|
var s = Num.fromString("1" * n + "3") var t = s * s Fmt.print("$d $d", s, t)
}
for (n in 0..7) a.call(n)</lang>
- Output:
3 9 13 169 113 12769 1113 1238769 11113 123498769 111113 12346098769 1111113 1234572098769 11111113 123456832098769