Can anyone offer advice as to what or how the algorithm works when it comes to combining books and enchanting?
I have noticed that when combining books just by switching the books the overall enchantment is cheaper but sometimes when I come to place the final book on my equipment it says "too expensive" although I already have exactly the same enchantments on mine currently
Items repaired or combined on an anvil have a "prior work penalty" which is basically doubled every time and the cost of the specific enchantment operations are added to that, when 2 items are combined the highest "prior work penalty" is used and doubled.
6 operations are the max any item can be combined or repaired.
So, if you think of the combinations as a pyramid you want to keep it as low as possible.
So taking an item and adding enchantments one at a time is a bad idea, instead you want to combine books and items 2 by 2 trying to add items/books that have the same (or close) prior work penalties.
For instance, adding 6 "virgin" books to an item 1 at a time will use up all 6 allowed anvil operations and the item can never be repaired (unless it has Mending).
On the other hand, combining the books 2 by 2 gives you three books that have used up one level of PWP each, then combining 2 of those with each other and the third with the item gives you an item and a book with 2 levels of PWP each and combining those gives you the same enchantments on the item as the first example but you still have something like 3 chances to add more and/or repair the item.