# Toilet Paper Mysteries

Today you need to solve a very practical problem: How many loops do you need to have a certain number of sheets on your toilet paper roll? Let's look at some facts:

• The diameter of a bare toilet paper cylinder is 3.8cm
• The length of one sheet of toilet paper is 10cm.
• The thickness of one sheet of toilet paper is 1mm.

Before you wrap around the cylinder the first time, it has a circumference in cm of 3.8*pi. Every time you wrap a sheet around the cylinder its radius increases by .1, therefore its circumference increases by .2*PI. Use this information to find out how many loops it takes to fit n sheets of toilet paper. (Note: Use an approximation of Pi that is at least as accurate as 3.14159).

Test Cases:

n=1:

• 10/(3.8*pi) = .838 loops

n=2:

• (How many full loops can we make?) 1 full loop = 3.8*pi = 11.938.
• (How much do we have left after the 1st loop?) 20 - 11.938 = 8.062
• (How much of a 2nd loop does the remaining piece make?) 8.062/(4*pi) = .642 loops

n=3:

• 1st full loop = 3.8*pi = 11.938, 2nd full loop = 4*pi = 12.566
• 30 - 11.938 - 12.566 = 5.496
• 5.496/(4.2*pi) = .417

n=100 => 40.874

• Phew! 1mm thick? Are you sure you're using toilet paper and not cardboard? Commented Jan 13, 2016 at 19:01
• @DigitalTrauma Clearly you don't know about triple-ply :p Commented Jan 13, 2016 at 19:40
• Under the assumption that the toilet paper does not make steps but continuously increases the radius, you can get a closed form approximation to requested result. Is this good enough? nloops = sqrt(n+11.34)*0.0564189 - 0.19 Commented Jan 13, 2016 at 20:01
• Proposed test case: 100 -> 40.874 Commented Jan 13, 2016 at 22:45
• Triple-ply cardboard?! Now that's thick! Commented Jan 13, 2016 at 22:59

# Pyth, 27 23 bytes

+fg0=-QJc*.n0+18T50)cQJ


### Explanation

                            Q = input number (implicit)
f                 )        increment T from 1, for each T:
*.n0                 multiply by pi to, get half the circumference
c        50           divide by 50, get circumference in sheets
J                      save it to J
=-Q                       decrement Q by it
g0                          use this T if Q is now <= 0
Q        Q (now <= 0)
c J       divided by J (the last circumference)
and print (implicit)

• explanation, please? Commented Jan 13, 2016 at 19:52
• @CᴏɴᴏʀO'Bʀɪᴇɴ Added. Explaining Pyth is always so much fun. Commented Jan 13, 2016 at 20:11
• Your explanation looks like a potential output for Surfin' Word Commented Jan 13, 2016 at 21:24

A scale factor of 5/pi is applied, so that a paper cylinder has a circumference of 19,20,21... cm and a sheet is 50/pi cm.

Saved 2 bytes thanks to xnor, by using an unnamed function.

x!s|s>x=1+(x+1)!(s-x)|1>0=s/x
(19!).(50/pi*)

• A pretty recursive method. Note that unnamed functions are allowed even when you have other lines (despite Haskell not supporting it), so the last line can be pointfree as (19!).(50/pi*).
– xnor
Commented Jan 14, 2016 at 0:23
• Wow, blows my approach out of the water! Commented Jan 14, 2016 at 2:32

# Jelly, 2927 26 bytes

R+18×3.6°µ0;+\³_÷µḞi0©ị+®’


Try it online!

p&((m,x):(n,y):z)|y<p=p&((n,y):z)|1>0=m+(p-x)/(y-x)
t=(&zip[0..](scanl(+)0$map(*pi)[0.38,0.4..]))  Might be able to golf it further by moving the filtering from the & operator into a takeWhile statement, but given that it's not a golfing language this seems relatively competitive. ## Explanation The stream of lengths of toilet paper that comprise full loops are first calculated as scanl (+) 0 (map (* pi) [0.38, 0.4 ..]]. We zip these with the number of full revolutions, which will also pick up the type Double implicitly. We pass this to & with the current number that we want to calculate, call it p. & processes the list of (Double, Double) pairs on its right by (a) skipping forward until snd . head . tail is greater than p, at which point snd . head is less than p. To get the proportion of this row that is filled, it then computes (p - x)/(y - x), and adds it to the overall amount of loops that have been made so far. # C++, 72 bytes float f(float k,int d=19){auto c=d/15.9155;return k<c?k/c:1+f(k-c,d+1);}  I used C++ here because it supports default function arguments, needed here to initialize the radius. Recursion seems to produce shorter code than using a for-loop. Also, auto instead of float - 1 byte less! • You almost fooled me, using d for the radius... Commented Jan 15, 2016 at 12:24 • Not sure if you were aware, but this was the 69,420th post on CGCC! Commented May 15, 2021 at 22:50 • Wow, I had no idea the number 69420 had any special significance! Too bad I cannot relate it to either C++ or toilet paper. Commented May 23, 2021 at 8:26 # Lua, 82 bytes n=... l,c,p=10*n,11.938042,0 while l>c do l,c,p=l-c,c+.628318,p+1 end print(p+l/c)  Not bad for a general-purpose language, but not very competitive against dedicated golfing languages of course. The constants are premultiplied with pi, to the stated precision. • OP wasn't specific about what kind of input to accept, so I left out the initialization of n, but the rest would've run as-is (as-was?). In any case, now it takes n from the command line; e.g. for 3 sheets run it as lua tp.lua 3. Commented Jan 14, 2016 at 14:01 • It's not precisely a rule of this question, but a general policy. Unless the question says otherwise, hardcoding the input makes the submission a snippet, which are not allowed by default. More information about site-wide defaults can be found in the code golf tag wiki. Commented Jan 14, 2016 at 14:47 • I knew about the "whole program or function" part but didn't know that "hardcoding the input makes the submission a snippet". Thanks for clarifying. I think this would actually be longer as a function! Commented Jan 14, 2016 at 18:15 # JavaScript, 77 bytes function w(s,d,c){d=d||3.8;c=d*3.14159;return c>s*10?s*10/c:1+w(s-c/10,d+.2)}  function w(s,d,c){d=d||3.8;c=d*3.14159;return c>s*10?s*10/c:1+w(s-c/10,d+.2)} function wraps(sheets, diameter, circumference) { // Default the value of diameter diameter = diameter || 3.8; circumference = diameter * 3.14159; if (circumference > sheets * 10) return sheets * 10 / circumference; return 1 + wraps(sheets - circumference / 10, diameter + .2); } document.getElementById('text').innerHTML = "0 => " + w(0) + "\n1 => " + w(1) + "\n2 => " + w(2) + "\n3 => " + w(3) + "\n100 => " + w(100) + "\n\n0 => " + wraps(0) + "\n1 => " + wraps(1) + "\n2 => " + wraps(2) + "\n3 => " + wraps(3) + "\n100 => " + wraps(100); <pre id="text"></pre> • Welcome to PPCG! If you'd like, you can use JavaScript ES6, and get this to 55 bytes: w=(s,d=3.8,c=d*3.14159)=>c>s*10?s*10/c:1+w(s-c/10,d+.2) Commented Jan 15, 2016 at 4:22 # C, 87 bytes float d(k){float f=31.831*k,n=round(sqrt(f+342.25)-19);return n+(f-n*(37+n))/(38+2*n);}  Uses an explicit formula for the number of whole loops: floor(sqrt(100 * k / pi + (37/2)^2) - 37/2)  I replaced 100 / pi by 31.831, and replaced floor with round, turning the annoying number -18.5 to a clean -19. The length of these loops is pi * n * (3.7 + 0.1 * n)  After subtracting this length from the whole length, the code divides the remainder by the proper circumference. Just to make it clear - this solution has complexity O(1), unlike many (all?) other solutions. So it's a bit longer than a loop or recursion. ## C#, 113 bytes double s(int n){double c=0,s=0,t=3.8*3.14159;while(n*10>s+t){s+=t;c++;t=(3.8+c*.2)*3.14159;}return c+(n*10-s)/t;}  Ungolfed: double MysteryToiletPaper(int sheetNumber) { double fullLoops = 0, sum = 0, nextLoop = 3.8 * 3.14159; while (sheetNumber * 10 > sum + nextLoop) { sum += nextLoop; fullLoops++; nextLoop = (3.8 + fullLoops * .2) * 3.14159; } return fullLoops + ((sheetNumber * 10 - sum) / nextLoop); }  Results: for 1 sheet 0,837658302760201 for 2 sheets 1,64155077524438 for 3 sheets 2,41650110749198 for 100 sheets 40,8737419532946 ## PHP, 101 bytes <?$p=pi();$r=3.8;$l=$argv[1]*10;$t=0;while($r*$p<$l){$t+=($l-=$r*$p)>0?1:0;$r+=.2;}echo$t+$l/($r*$p);


## Ungolfed

<?
$pi = pi();$radius = 3.8;
$length_left =$argv[1]*10;
$total_rounds = 0; while ($radius * $pi <$length_left) {
$total_rounds += ($length_left -= $radius *$pi) > 0 ? 1 : 0;
;>{$,+n  Expects the number of sheets to be present on the stack at program start. This uses an approximation of pi of 355/113 = 3.14159292..., storing pi/5 in the register. The current iteration's circumference lives on the stack, and pi/5 is added on each iteration. Edit: Refactored to store the circumference directly - previous version stored pi/10 and started the diameter as 38, which was 2 bytes longer. # PHP, 79 bytes function p($s,$d=3.8){$c=$d*pi();return$c>$s*10?$s*10/$c:1+p($s-$c/10,$d+.2);}


Run code in Sandbox

I have pretty much only translated Ross Bradbury's answer for JavaScript into a PHP function, which is also recursive.

• Please don't just copy another answer into another language. Commented May 10, 2016 at 20:37