公式:1/81 * (10^(n+1) – 9n – 10) (n=1,2,3…),产生 ” 123···n “。
- 1 * 8 + 1 = 9
- 12 * 8 + 2 = 98
- 123 * 8 + 3 = 987
- 1234 * 8 + 4 = 9876
- 12345 * 8 + 5 = 98765
- 123456 * 8 + 6 = 987654
- 1234567 * 8 + 7 = 9876543
- 12345678 * 8 + 8 = 98765432
- 123456789 * 8 + 9 = 987654321
- 1 * 9 + 2 = 11
- 12 * 9 + 3 = 111
- 123 * 9 + 4 = 1111
- 1234 * 9 + 5 = 11111
- 12345 * 9 + 6 = 111111
- 123456 * 9 + 7 = 1111111
- 1234567 * 9 + 8 = 11111111
- 12345678 * 9 + 9 = 111111111
- 123456789 * 9 + 10 = 1111111111
- 9 * 9 + 7 = 88
- 98 * 9 + 6 = 888
- 987 * 9 + 5 = 8888
- 9876 * 9 + 4 = 88888
- 98765 * 9 + 3 = 888888
- 987654 * 9 + 2 = 8888888
- 9876543 * 9 + 1 = 88888888
- 98765432 * 9 + 0 = 888888888
- 1 * 1 = 1
- 11 * 11 = 121
- 111 * 111 = 12321
- 1111 * 1111 = 1234321
- 11111 * 11111 = 123454321
- 111111 * 111111 = 12345654321
- 1111111 * 1111111 = 1234567654321
- 11111111 * 11111111 = 123456787654321
- 111111111 * 111111111 = 12345678987654321
# awk -vlen=9 'BEGIN{for(i=1;i<=len;i++){for(j=1;j<=i;j++){a=a""j;if(i==j){printf a" * 8 + "i" =" expr a * 8 + i"\n";a=""}}}}'
- 1 * 8 + 1 =9
- 12 * 8 + 2 =98
- 123 * 8 + 3 =987
- 1234 * 8 + 4 =9876
- 12345 * 8 + 5 =98765
- 123456 * 8 + 6 =987654
- 1234567 * 8 + 7 =9876543
- 12345678 * 8 + 8 =98765432
- 123456789 * 8 + 9 =987654321
# awk -vlen=9 'BEGIN{for(i=1;i<=len;i++){for(j=1;j<=i;j++){a=a""j;if(i==j){printf a" * 9 + "expr i + 1" =" expr a * 9 + i + 1"\n";a=""}}}}'
- 1 * 9 + 2 =11
- 12 * 9 + 3 =111
- 123 * 9 + 4 =1111
- 1234 * 9 + 5 =11111
- 12345 * 9 + 6 =111111
- 123456 * 9 + 7 =1111111
- 1234567 * 9 + 8 =11111111
- 12345678 * 9 + 9 =111111111
- 123456789 * 9 + 10 =1111111111
# amazing 的 dc ,太强悍了!
$ dc -e '0 0[1+dstr10*+dn[ * 8 + ]nltn[ = ]nd8*lt+psrltd9>a]dsax' $ dc -e '0 1[d1+str10*+dn[ * 9 + ]nltn[ = ]nd9*lt+psrltd9>a]dsax' $ dc -e '0 7[dstr10*2++dn[ * 9 + ]nltn[ = ]nd9*lt+psrlt1-d0!>a]dsax' $ dc -e '0 0[1+r10*1+dn[ * ]ndn[ = ]ndd*psrrd9>a]dsax'
- 1 * 8 + 1 = 9
- 12 * 8 + 2 = 98
- 123 * 8 + 3 = 987
- 1234 * 8 + 4 = 9876
- 12345 * 8 + 5 = 98765
- 123456 * 8 + 6 = 987654
- 1234567 * 8 + 7 = 9876543
- 12345678 * 8 + 8 = 98765432
- 123456789 * 8 + 9 = 987654321
- 1 * 9 + 2 = 11
- 12 * 9 + 3 = 111
- 123 * 9 + 4 = 1111
- 1234 * 9 + 5 = 11111
- 12345 * 9 + 6 = 111111
- 123456 * 9 + 7 = 1111111
- 1234567 * 9 + 8 = 11111111
- 12345678 * 9 + 9 = 111111111
- 9 * 9 + 7 = 88
- 98 * 9 + 6 = 888
- 987 * 9 + 5 = 8888
- 9876 * 9 + 4 = 88888
- 98765 * 9 + 3 = 888888
- 987654 * 9 + 2 = 8888888
- 9876543 * 9 + 1 = 88888888
- 98765432 * 9 + 0 = 888888888
- 1 * 1 = 1
- 11 * 11 = 121
- 111 * 111 = 12321
- 1111 * 1111 = 1234321
- 11111 * 11111 = 123454321
- 111111 * 111111 = 12345654321
- 1111111 * 1111111 = 1234567654321
- 11111111 * 11111111 = 123456787654321
- 111111111 * 111111111 = 12345678987654321
# awk 'BEGIN{for(i=1;i<=9;i++){print (t=t i),"*",8,"+",i," = ",t*8+i}}'
- 1 * 8 + 1 = 9
- 12 * 8 + 2 = 98
- 123 * 8 + 3 = 987
- 1234 * 8 + 4 = 9876
- 12345 * 8 + 5 = 98765
- 123456 * 8 + 6 = 987654
- 1234567 * 8 + 7 = 9876543
- 12345678 * 8 + 8 = 98765432
- 123456789 * 8 + 9 = 987654321
# awk 'BEGIN{for(i=1;i<=8;i++){print (t=t i),"*",9,"+",i+1," = ",t*9+(i+1)}}'
- 1 * 9 + 2 = 11
- 12 * 9 + 3 = 111
- 123 * 9 + 4 = 1111
- 1234 * 9 + 5 = 11111
- 12345 * 9 + 6 = 111111
- 123456 * 9 + 7 = 1111111
- 1234567 * 9 + 8 = 11111111
- 12345678 * 9 + 9 = 111111111
# awk 'BEGIN{for(i=9;i>1;i--){print (t=t i),"*",9,"+",i-2," = ",t*9+(i-2)}}'
# awk 'BEGIN{for(i=9;i>1;i--){print (t=t 1),"*",(t)," = ",t*t}}'
- 1 * 1 = 1
- 11 * 11 = 121
- 111 * 111 = 12321
- 1111 * 1111 = 1234321
- 11111 * 11111 = 123454321
- 111111 * 111111 = 12345654321
- 1111111 * 1111111 = 1234567654321
- 11111111 * 11111111 = 123456787654321
[seesea@UC ~]$ for((i=1; i<=9; ++i)); do num=$(seq --separator= 1 $i); echo "$num * 8 + $i = $((num * 8 + i))"; done
- 1 * 8 + 1 = 9
- 12 * 8 + 2 = 98
- 123 * 8 + 3 = 987
- 1234 * 8 + 4 = 9876
- 12345 * 8 + 5 = 98765
- 123456 * 8 + 6 = 987654
- 1234567 * 8 + 7 = 9876543
- 12345678 * 8 + 8 = 98765432
- 123456789 * 8 + 9 = 987654321
[seesea@UC ~]$ for((i=1; i<=9; ++i)); do num=$(seq --separator= 1 $i); echo "$num * 8 + $((i+1)) = $((num * 9 + i+1))"; done
- 1 * 8 + 2 = 11
- 12 * 8 + 3 = 111
- 123 * 8 + 4 = 1111
- 1234 * 8 + 5 = 11111
- 12345 * 8 + 6 = 111111
- 123456 * 8 + 7 = 1111111
- 1234567 * 8 + 8 = 11111111
- 12345678 * 8 + 9 = 111111111
- 123456789 * 8 + 10 = 1111111111
[seesea@UC ~]$ for((i=9; i>=2; --i)); do num=$(seq --separator= 9 -1 $i); echo "$num * 9 + $((i-2)) = $((num * 9 + i-2))"; done
- 9 * 9 + 7 = 88
- 98 * 9 + 6 = 888
- 987 * 9 + 5 = 8888
- 9876 * 9 + 4 = 88888
- 98765 * 9 + 3 = 888888
- 987654 * 9 + 2 = 8888888
- 9876543 * 9 + 1 = 88888888
- 98765432 * 9 + 0 = 888888888
[seesea@UC ~]$ for((i=1; i<=9; ++i)); do num=$(seq --separator= $i | sed 's/./1/g'); echo "$num * $num = $((num * num))"; done
- 1 * 1 = 1
- 11 * 11 = 121
- 111 * 111 = 12321
- 1111 * 1111 = 1234321
- 11111 * 11111 = 123454321
- 111111 * 111111 = 12345654321
- 1111111 * 1111111 = 1234567654321
- 11111111 * 11111111 = 123456787654321
- 111111111 * 111111111 = 12345678987654321
# 或者用 yes 来做
[seesea@UC ~]$ for((i=1; i<=9; ++i)); do num=$(yes 1 | head -$i | tr -d '\n'); echo "$num * $num = $((num * num))"; done
- 1 * 1 = 1
- 11 * 11 = 121
- 111 * 111 = 12321
- 1111 * 1111 = 1234321
- 11111 * 11111 = 123454321
- 111111 * 111111 = 12345654321
- 1111111 * 1111111 = 1234567654321
- 11111111 * 11111111 = 123456787654321
- 111111111 * 111111111 = 12345678987654321
说明:以上的各种 shell 和 awk 处理方法来自于热心的CUer的帮忙,万分感谢,让我又有机会学习 shell 的各种用法和技巧!
重新品味无名师心传中的“一行Shell,万行C”,感慨有点深啊!特别是在从之前的blog中看到的时候。