Skip to content

Instantly share code, notes, and snippets.

@xtetsuji
Last active June 21, 2019 02:16
Show Gist options
  • Save xtetsuji/5f29f72eafd7ce98ef903af0cfa822bf to your computer and use it in GitHub Desktop.
Save xtetsuji/5f29f72eafd7ce98ef903af0cfa822bf to your computer and use it in GitHub Desktop.
print 1..100 without loop, recursive, goto by Perl (not C++)
#!/usr/bin/env perl
use strict;
use warnings;
my $str = "A " x 100;
my $i = 1;
$str =~ s/A/ $i++ /eg;
print "$str\n";
#!/usr/bin/env perl
# https://jp.quora.com/%E3%83%AB%E3%83%BC%E3%83%97-%E5%86%8D%E5%B8%B0-goto%E3%82%92%E4%BD%BF%E3%82%8F%E3%81%9A%E3%81%AB1%E3%81%8B%E3%82%89100%E3%81%BE%E3%81%A7%E3%82%92%E5%8D%B0%E5%AD%97%E3%81%99%E3%82%8BC-%E3%83%97%E3%83%AD%E3%82%B0/answers/148184533?ch=2
# original: https://www.quora.com/How-can-I-print-1-to-100-in-C%2B%2B-without-a-loop-goto-or-recursion?ch=10&share=9b09c507&srid=Jqvc
#
# > マイクロソフト社のデータサイエンティスト Conner Davis 氏は
# > 「C++のことは全くわからないのだが」と断りつつ想像の遥か斜め上を行く回答を書かれています.
use strict;
use warnings;
use Math::BigFloat;
# 各種数値リテラルの調整はなんとなく
Math::BigFloat->accuracy(300-2);
my $x = 1000 / Math::BigFloat->new(999**2);
my $str = $x->bstr();
# map { $_+0 } があると "001" が 1 に変換される、つまり頭のゼロを落とす
# 同じ意味では map { sprintf "%d", $_ } でもいいし、
# map がループっぽいという話であれば printf "%d " x 100, でもいいかもしれない
$str =~ s/^0\.//;
my @answer = map { $_+0 } $str =~ /(\d\d\d)/g;
print "@answer\n";
#!/usr/bin/env perl
# range はループではない?
use strict;
use warnings;
print join " ", 1..100;
print "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment