-3

Hypothetic example: There is a number that I will split into x parts (smaller numbers, in a range you see in the example).

I need a way, ideally a one liner for the Linux Bash, to split a large number into unequal smaller numbers like:

 54773147129 / 11 = 11702658340  4215060707  8649154648  8166383273  7721726143  6316805625  5074147699 11938331015 7290181592 10452629111 12235301352
 24335474529 /  5 =  3782894329  7991597003  2435897896  6966444910  3158640391
120358276480 /  9 = 15276312867 11090117156  8997563892 15321924738 10203261894 21007930376 17164309215 13595762399 7701093943
 54773147129 /  8 = 10222423691  6997800027  4434683169  5675683553  1121059020  8522036904  4023455184 13776005581

and so on.

Consider the first example in more detail:

  • The number is 54773147129 and I want to have 11 parts

  • I do not want to simply divide the number by eleven, which would result in 11 equal but non-integer fractions, i.e.

    not 54773147129 / 11 = 4979377011,73

    In general, I don't want to have parts with decimals.

  • I do not want to divide the number into 10 equal parts and one larger part that contains the integer division's modulus, i.e.

    not 4979377011 4979377011 4979377011 4979377011 4979377011 4979377011 4979377011 4979377011 4979377011 4979377011 4979377019

  • I want to have a result like:

    11702658340 4215060707 8649154648 8166383273 7721726143 6316805625 5074147699 11938331015 7290181592 10452629111 12235301352

How to do this in Bash (ideally in a one-liner)?

6
  • 5
    (1) Please provide few examples with way smaller numbers, so it's easier for us to get the general idea. How was 11702658340 or 4215060707 derived from 54773147129 and 11? Was the procedure deterministic or was there randomness? If the latter, what is the desired distribution? The topic of this site is Unix & Linux, not mathematics; we may help you with command-line tools (implementation), but give us a strict description of the problem (specification). (2) Regarding command-line tools: what is your research effort? What have you tried? Commented Jun 21, 2024 at 5:42
  • 5
    24335474529 / 5 could be -2 -1 1 2 24335474529. If that's not an acceptable solution please clarify the exact requirements. Do the numbers have to be strictly positive integers? Do they have to be all different? If so what should be the outcome when it's not possible like for splitting 10 into 5 different strictly positive integers? Commented Jun 21, 2024 at 5:43
  • It's not just you, but what's with this "one-liner" requirement? If a complex answer is given, just drop it in a script Commented Jun 21, 2024 at 14:40
  • @ChrisDavies It said "ideally," so it is what he wants but is not strictly required. Commented Jun 21, 2024 at 22:31
  • @horsey_guy people don't tend to complicate solutions needlessly. So if there's a single line solution that's what'll be offered. If it's a multiline solution it can almost always be crashed into a single line but at the expense of readability (and therefore understanding) Commented Jun 21, 2024 at 22:48

1 Answer 1

3

Could be something like:

$ awk -v x=54773147129 -v n=11 'BEGIN{srand(); while (--n) {i = int(rand() * x / n); print i; x -= i}; print x}'
72148585
1813756228
5944837099
2867160879
1741968982
4361369875
716625589
10973173162
6962424731
15382024656
3937657343

Or to print them space separated on one line rather than one per line:

$ awk -v x=54773147129 -v n=11 'BEGIN{srand(); while (--n) {$++NF = int(rand() * x / n); x -= $NF}; $++NF = x; print}'
2020958615 3686429859 4753778737 1332538908 4429626284 3327568041 627223196 10140012031 1310624106 16318805992 6825581360

Note that there's no guarantee that the numbers will be all different or of uniform distribution.

If the numbers have to be positive integer numbers and all different, there's no possible outcome if x < n*(n-1)/2 anyway.

Beware that in most awk implementations (and that's now a POSIX requirement), srand() seeds the pseudo-random number generator with the integer value of the Unix epoch time, so you'll get the same outcome if the command is run twice within the same second.

2
  • the way with awk is great, that is what i am looking for, thanks a lot! is there a way to bring the numbers not line by line, rather all in one line with separated with a space-character? Commented Jun 21, 2024 at 10:14
  • @user447274, see edit. Commented Jun 21, 2024 at 10:18

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.