Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How should I solve this question in R? There is a line of 500000 switches, each initially switched to on. One by one, 250000 people

How should I solve this question in R? There is a line of 500000 switches, each initially switched to "on". One by one, 250000 people flip some of the switches. * Person 1 flips off switch 1. * Person 2 flips off switches 4, 6, 8, ..., 499998, 500000 * Person 3 flips off switches 6, 9, 12, ..., 499996, 499999 (any switch already off stays off) * Person 4 flips off switches 8, 12, 16, ...., 499996, 500000 (any switch already off stays off) * etc. * In other words, after person 1, person `i` flips off switch numbers `seq(from=2*i,to=500000,by=i)` Determine which switches remain on after all people have completed their flips. Hint: work with a vector called `switches`. We'll let element `i` represent the status of the `i`-th switch: 1 for on and 0 for off. Define `switches` to be 500000 1s. Left-arrow the first element to be 0 (to represent the first person turning the first switch to off). Use a `for` loop to "turn off" the switches flipped off by person 2, 3, ..., 250000. Sanity checks: `switches[5979:5989]` gives 0 0 1 0 0 0 0 0 1 0 0 and `sum(switches[32943:39999])` will give 672. a) What is the total number of switches that remain on? b) What is the largest switch number (1-500000) that remains on? c) Print to the screen the first 20 or so numerical values of the switch numbers that are on. This sequence of numbers is a very famous one! What is it? ```{r Q5} #switches[5979:5989] #[1] 0 0 1 0 0 0 0 0 1 0 0 #sum(switches[32943:39999]) #672 #a) how many are still on? #b) last switch that is still on (largest switch number) #c) What does the sequence look like ``` Walkthrough if you need it: Step 1: We'll use a vector called `switches` to represent the status of each switch (1 = on, 0 = off). Initialize this vector to be `rep(1,5e5)` ("all on"), then change `switches[1]` to 0 to represent the action of person 1. Step 2: Write a line of code that "left-arrows" elements 4, 6, 8, ... 499998, 500000 of `switches` to 0 to represent what person 2 does to the switches. Step 3: Write a line of code that "left-arrows" elements 6, 9, 12, ... 499996, 499999 of `switches` to 0 to represent what person 3 does to the switches. Step 4: Verify that first 25 elements of `switches` is now `0 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0 1` Step 5: Person 15843 will turn switches 31686 47529 63372 79215 95058 to "off". Left-arrow `i` to be 15843, then verify that `seq(from=2*i,to=100000,by=i)` indeed produces this sequence of numbers. Step 6: Synthesize what you have done in Steps 1 to 5 - initialize `switches` to be a vector of 500000 1s (then change the first element to 0), then write a `for` loop to simulate the flips of persons 2, 3, ..., 250000

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Next Generation Databases NoSQLand Big Data

Authors: Guy Harrison

1st Edition

1484213300, 978-1484213308

More Books

Students also viewed these Databases questions