How to Create a Custom Sequence of Numbers for Printing?

0
4
Asked By CuriousCoder42 On

Hey everyone! I'm trying to generate a specific pattern of numbers incrementally for a project. The pattern I'm starting with is: 4, 1, 2, 3, 8, 5, 6, 7. I need to continue this sequence further to assist in printing a book on A4 paper. My goal is to print 2 pages on each side for a total of 4 pages per sheet when folded. I've got a program that can manage the rearrangement of pages in a PDF, but I need to provide it with this specific sequence first. I can handle simple sequences like `for i in seq -s, 1 1 100` but I'm struggling with the arithmetic parts necessary to repeat the pattern. My approach is to start with 4, then apply the following steps: - subtract 3 for the first number, then add 1 twice, and finally add 5 successively to create the next digits. Could anyone help me out with this? Thanks!

1 Answer

Answered By Ulfnic On

Here's a neat way to achieve that! You don't actually need the `| cat` at the end; it's just there if you want to pipe the output somewhere else. Here's a simple script you can use:

```bash
len=20
num=4
printf '%s ' "$num"
while :; do
for mod in -3 1 1 5; do
(( --len > 0 )) || break 2
num=$(( num + $mod ))
printf '%s ' "$num"
done
done
```

This will give you the output you're looking for! For a one-liner, you can compact it like this:

```bash
len=20;num=4;printf '%s ' "$num";while :;do for mod in -3 1 1 5; do ((--len>0)) || break 2;num=$(( num + $mod ));printf '%s ' "$num";done;done
```

Output will be: `4 1 2 3 8 5 6 7 12 9 10 11 16 13 14 15 20 17 18 19`.
Happy coding!

PrintMaster77 -

Awesome! Thanks a ton, Ulfnic! This is exactly the kind of help I needed. I really appreciate the one-liner at the end along with the breakdown. I figured I was missing something major when I saw the **mod** part, but it all makes sense now. Cheers!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.