3
\$\begingroup\$

A company is arranged in a heirarchical structure, with a layer of workers at the bottom. Each worker is managed by a manager. Consequently, each manager is managed by another manager until there is a company "boss". Each manager is restricted to managing, at most, x workers. For a company of size y calculate the number of managers required (including the boss)

Function should take two inputs. Eg non-golfed definition: calc_num_managers(num_workers, num_workers_per_manager)

You can assume that the number of workers per manager will be greater than 1.

Examples:

  • A company with 0 workers needs 0 managers
  • If the company has 4 workers, and each manager can manage 8 workers, then there is 1 manager.
  • If the company has 4 workers, and each manager can manage 4 workers, then you need 1 manager
  • If the company has 12 workers, and each manager can manage 8 workers, then there are 3 managers: enter image description here
\$\endgroup\$
15
  • 3
    \$\begingroup\$ "two team leaders and a boss" - This needs to be expanded on. \$\endgroup\$
    – Shaggy
    Commented Jul 27, 2017 at 10:28
  • 1
    \$\begingroup\$ Hello and Welcome to PPCG. You should consider posting in the sandbox first. Your examples make no sense. I´d say 7 or 5 managers for the last example, depending on wether the managers are workers too or not. \$\endgroup\$
    – Titus
    Commented Jul 27, 2017 at 10:33
  • 1
    \$\begingroup\$ At first I thought, you were asking for ceiling(y/x) but now I'm confused.. \$\endgroup\$ Commented Jul 27, 2017 at 10:39
  • 4
    \$\begingroup\$ Still not clear, why 3 managers are needed.. here \$\endgroup\$ Commented Jul 27, 2017 at 11:02
  • 1
    \$\begingroup\$ The second manager is the one boss in Wheat Wizard's organogram. \$\endgroup\$ Commented Jul 27, 2017 at 19:41

5 Answers 5

6
\$\begingroup\$

C, C++, Java, C#, D : 73 69 bytes

-4 bytes thanks to Zacharý

Due to the similarities between theses languages, this answer works for each.
Maybe it works in processing too, but i can't test

int c(int w,int p){int r=0;do{w=(w+p-1)/p;r+=w;}while(w>1);return r;}

Here is an optimized version in D : 62 bytes thanks to Zacharý

T c(T)(T w,T p){T r;do{w=(w+p-1)/p;r+=w;}while(w>1);return r;}

T here is a type for template metaprogramming. This function is callable with c(4,8);

\$\endgroup\$
13
  • \$\begingroup\$ Amazing, one code for 3 langs)) \$\endgroup\$ Commented Jul 29, 2017 at 20:17
  • \$\begingroup\$ @ЕвгенийНовиков 4* languages. Just edited to test for C :) \$\endgroup\$ Commented Jul 29, 2017 at 20:19
  • \$\begingroup\$ And how about includes ? codegolf.meta.stackexchange.com/questions/7515/… \$\endgroup\$ Commented Jul 29, 2017 at 20:20
  • 1
    \$\begingroup\$ @ЕвгенийНовиков Well, the question just says to have a function, not a full program \$\endgroup\$ Commented Jul 29, 2017 at 20:22
  • \$\begingroup\$ I think this works in D (wow, actually a decent D answer) and Processing as well. \$\endgroup\$
    – Adalynn
    Commented Jul 30, 2017 at 20:19
1
\$\begingroup\$

Jelly, 14 bytes

Ṃ÷⁴Ċ0Ṃ’$?µÐĿḊS

Try it online!

\$\endgroup\$
1
\$\begingroup\$

JavaScript, 46 43 bytes

If scheme isn't solvable, there will be stack exeeded error

-3 bytes ceil trick inspired by Cows quack

f=(w,m)=>(r=0|(w+m-1)/m,r>1&&(r+=f(r,m)),r)

//test
;[[0,1],[4,8],[4,4],[12,8],[8,3],[10,3]]//0;1;1;3;4;7
.map(e=>console.log("w="+e[0]+" m="+e[1]+" result="+f(e[0],e[1])))

Requirements

w count of workers, non-negative integer

m count of max workers per manager, positive integer

\$\endgroup\$
2
  • \$\begingroup\$ I believe |1 is shorter than Math.ceil \$\endgroup\$
    – user41805
    Commented Jul 29, 2017 at 19:29
  • \$\begingroup\$ @Cowsquack (19/10)|1 is 1, but ceil 2 \$\endgroup\$ Commented Jul 29, 2017 at 19:31
1
\$\begingroup\$

Perl 5, 44 + 1 (-n) = 45bytes

$t=<>;$m+=$_=1+int$_/$t while($_>1);say$m|$_

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Husk, 7 bytes

ΣtU¡(⌈/

Try it online!

Repeatedly (¡) takes the ceiling () of the division (/) of the number of workers by the number of workers per manager and sums (Σ) all the unique results (U).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.