Category Archives: Uncategorized

Clarifications on White Privilege

These are crazy times. 110,000 people have died in the US alone from COVID-19, and people are getting curious about what was done to prepare for it given that we knew about it back in December. And then suddenly the news looks away from that devastation and the fumbled response that caused it because police just caused yet another needless death.

Warning: Opinions Ahead

Before I go any further, let me make clear where I stand on the killing of George Floyd. The video is horrific, and while there may be some extenuating circumstances (e.g. it might be possible that health considerations contributed to the death), Chauvin is certainly not blameless. And of course there have been other recent incidents of questionable police violence in the news. We’re doing something wrong here in the US, and peaceful (!) protests are certainly warranted. Every time we fail to hold people — especially those in law enforcement — accountable for this kind of thing we are just making matters worse.

I wonder why Chauvin thought it necessary to keep his knee on Floyd’s neck all that time. Could it be because bystanders were telling him to get off and he didn’t want to give in to them, looking weak in front of his trainees? Perhaps he thought “I’m in charge here!” and kept on with the knee to show them so. “You can’t tell me what to do!”

And Chauvin didn’t just hurt Floyd. He further tainted everyone in law enforcement, most of them undeservedly.

While she didn’t end up killing anyone, in some ways I am more upset with Amy Cooper, because she knew exactly what she was doing. For all his malice, surely Chauvin didn’t actually intend to kill Floyd — at least not there, in front of all of those witnesses and cameras. In fact, it isn’t clear that the mistreatment was even racially motivated; are we really sure he wouldn’t have been just as aggressive in front of his trainees had Floyd been a big guy of European descent? Cooper, on the other hand, was consciously invoking racial injustice to serve her own purposes. Jaw-dropping.

White Privilege — safety in skin color

I have been hearing commentators talk about the unfairness of being stopped for no apparent reason, which they refer to as “walking while black.” And of being given “the talk” as a kid about the fact that it’s best to cooperate with the cops because, well, you’re black, and if you don’t play nice, things will go worse for you, and you don’t want that. And of white people calling the cops on black people.

So then these commentators go on to explain that white people have no idea what it’s like for black people, that “white privilege” is simply not having to worry about these things. “The world is safe for white people,” one woman says accusingly. “The color of your skin protects you.”

To these people I say this: I agree that there’s a problem here, but you’ve gotten the wrong impression.

Four times (maybe more, but four come to mind) I have had what I consider to be unpleasant encounters with police, and I’m light-skinned, of European heritage. I’m going to describe them to you, but my point is that if you imagine that being white is some kind of magic shield against suspicion, or protection against being shot by a nervous cop, or freedom from having the cops called on you when you are minding your own business, you are about to learn otherwise. Yes, people of color have it worse, at least by some measures, and that is surely due in part to racial bias. Racial bias definitely exists, and it’s bad, and it’s especially bad when it’s in law enforcement and the justice system. But a lot of the problem with police is just the nature of the beast rather than being about racial prejudice. Interactions with the police are nobody’s idea of a picnic, and it is certainly possible to make things harder on yourself by resisting.

“I almost shot you”

I worked my way through college programming computers for small businesses. I had taught myself to program in Basic, and a company in Tucson, AZ paid me $2.50 an hour to program the computers of small businesses (whom they charged $25, keeping 90% — ahem). My clients trusted me, giving me keys to their computer facilities so I could work at night, after my classes. Only one had a security system, and one night, around midnight, I started to open the door without remembering to first turn that security system off. I had only opened the door an eighth of an inch before realizing my mistake, so I thought maybe I got away with it. I had been told to wait for a call if I ever set off the alarm, so I waited a couple of minutes. Nothing.

Convinced that I had dodged a bullet, I began coding. I had forgotten about it when, five minutes later, several vehicles raced into the parking lot, lights flashing. They could see me clearly through the large window in the wall. A loud voice said to open the door. I walked to the door and slowly opened it.

There were three members of the Pima County Sheriff’s Department pointing guns at me. They grabbed me by the arms, took me to a squad car, frisked me, and stuck me in the back. Somebody from the company came out and vouched for me, so they let me go, but as he let me out of the car this one deputy said “Man, you have to be more careful — I almost shot you.” He dragged out the word “almost” to drive home the point that I had come very close to dying that night.

My initial reaction — not shared with them — was WTF?!? Why did you “almost shoot me”? You could see me through the window. I was unarmed. You told me to open the door and I opened it. I did nothing menacing. I cooperated in every way possible.

I thought about that brush with death for quite a while. Had I been black, I might well have thought that it was because of my skin color. But the conclusion I came to is that that deputy was just scared shitless. He had no idea what was on the other side of that door. All he really knew was that somebody had set off the alarm, that I was white, and I had nothing in my hands. But surely the worst ran through his mind. For all he knew there might be three guys with machine guns in there, staying out of sight, prepared to shoot their way out like Butch Cassidy and the Sundance Kid. He was on red alert as he pointed that gun at my chest, safety off, finger pressing lightly against the trigger. In his fear he made himself as ready to kill me as he possibly could — and apparently he almost took that final, tiny little step of applying just a little more force to that trigger.

And clearly, from his perspective it was my fault that he almost killed me!

Let that sink in for a minute. That deputy did not walk away from the experience thinking “Wow, I really need to tone it down a notch before I accidentally kill somebody. I have to learn to control this fear, to always remember that while there is a chance that this person is dangerous, it is much more likely that they are completely innocent. Alarms get set off accidentally all the time.”

No, it was my fault — I needed to be more careful.

“Put your hands on the trunk”

Years later, out of college and living in Tempe, I was walking down the alley toward my house one day. Tempe alleys were roads, big enough for a garbage truck and two rows of garbage bins, and people used them all the time. On that day there was a squad car parked in the alley. I said hello as I passed the officer standing behind the car, and his reply, quite polite, was “Hello. Would you mind coming over and putting your hands on the trunk of the car?”

I understood that it wasn’t really a question, and I did what he asked. As he frisked me, he asked a number of questions — did I live around there, why was I in the alley, etc. — and called in my driver’s license to see if they had anything on me, and to verify that I lived nearby. I checked out, so he let me go. He said only that there had been a burglary in the area. I never did hear anything about a burglary in the area that day, but perhaps it was true. In any case, I didn’t have anything in my hands — no weapon, no tools for forcing entry, no goods I might have stolen. Nothing to make anyone think I might have been involved in a burglary. I don’t have a shifty look. I have no tatoos, no piercings. I don’t dress like a biker. I wasn’t drunk or stoned (I don’t do alcohol or other drugs). I don’t even have an imposing presence — I’m just a regular, clean-cut white guy, and he decided to detain and frisk me.

It’s a little humiliating — especially for guys, I expect — to passively submit to a police officer, allowing yourself to be frisked and manipulated like that. Allowing yourself to be interrogated. Staying where he puts you until he says you can go, as if you were back in kindergarten. Being subject to his judgment, hoping that he doesn’t decide to throw handcuffs on you and take you to the station. It’s demeaning, and it’s understandable to feel angry about that. Did the officer have good reason to do it? I have no way of knowing. If I were black, I might well have thought that I was stopped because of my skin color. But I was just a guy walking in an area where (maybe) some crime was committed, and a cop decided to stop me, frisk me, and detain me while he checked out my story and my ID.

Maybe it isn’t you

The third time I was detained I was walking home late from work — like 2am — in Silicon Valley, and a campus cop detained me for 10 minutes. Why? Who knows — perhaps I was the only person he had seen for an hour, and he wondered what was up. When I asked if there was a problem, he didn’t offer any explanation. Walking while … white?

And the fourth time. Some guy in a truck saw me standing with no shirt on the side of the road at 1am in my quiet neighborhood. He thought that was so strange — he didn’t notice that I was stretching my calves for a run — that he called the cops on me. They caught up with me after my run, three squad cars, lights flashing. Three large armed men surrounded me, one rather menacing, barking questions at me like I had done something wrong and they had caught me. Why don’t you have a shirt on? Because it’s hot, and I’m running. Why are you out so late? Because it’s hot, but less so now than hours ago. You have a cut on your arm — did you get that hiding in the bushes? Neighbors peering through their windows — who is that guy, and what crime did he commit? More questions, and checking of my identity. It was profoundly humiliating. Finally they were satisfied: OK, you can go. Some idiot calls the cops on you for just being there, and when they arrive, you have to keep your cool and put up with it, because resisting is not going to help.

My point is that while it is indeed possible that you are being looked at with suspicion … or detained … or frisked … or shot … or brusquely interrogated … because of your skin color, it may not be that. These are ABSOLUTELY NOT things that white people don’t have to put up with. It’s worse if you’re black sometimes, if you get a racist cop, and we need to take a good, hard look at that. But please don’t imagine that the police are all “Yessir, Mr. Whiteperson, let me shine your shoes for you.” If I had told that officer in the alley to fuck off, I would have ended up face down in the dirt in that alley. If I had yanked that computer center door open with an axe to grind about law enforcement, or with something in my hand that looked for a moment like a weapon, or if I were just bigger and meaner-looking, then I would likely have bled out in that doorway. Or maybe if that officer had just finished a drink with caffeine. No black required. That’s reality with cops — they live with the possibility that something violent awaits them, so you have to show them otherwise. And they are by no means perfect. And a few are no doubt even racist. It sucks, but there you go.

And by the way, that talk that black parents give their kids, about being calm and respectful around the cops because it’s safer? I got that talk as well. I was told that if I got pulled over, I was to have the window down and my ID in my hand ready to give to them by the time they reached me. Call them officer. Don’t talk back. Answer their questions. Do what they say. Not because they have earned your respect — they haven’t! — but because they can make your life miserable if they want to. DO NOT give them any reason to do so. Don’t all parents talk to their kids about how to behave around police?

One last time — I’m not saying that racial discrimination isn’t real. It is, and we need to fix it. But being statistically somewhat less likely to get shot, somewhat less likely to get stopped and frisked, and so on isn’t something I really need the word “privilege” to describe. I don’t like the experience any more than you do. I feel humiliated by cops too. My life is in danger around on-edge cops too. Nervous nellies I come across while minding my own business call the cops on me too.

It may make a person feel better to stick her fingers in someone’s face and yell at them about their “white privilege.” And when they predictably don’t like that — you wouldn’t like it either — perhaps she will further demean them about their “white fragility.” But I would like to suggest that these are expressions of anger, and that these terms — deliberately chosen to be inflammatory — do more to hurt the just cause than to advance it. In further dividing people on the basis of skin color — claiming that having white skin makes you complicit, or worthy of contempt, or whatever — this race-based aggression takes us all farther from where we should all really want to be.

In fact, I would suggest that if you find yourself saying “white <anything>” — especially in anger — you should at least consider the possibility that you yourself are being racist. Sure, you’re mad about the several recent killings. You’re mad about the disparity in incarceration rates. You’re mad about food deserts. You’re mad about under-funded schools. You’re mad about the lack of police accountability. And much more. You want to fix these things. But if as a result you look at a person you know nothing about and dislike them for being white, then you are being racist. And your hate pushes them toward a racist response. You are fueling the very problem you want to solve.

So if you have been repeatedly detained, or almost shot, or hurt, or even just felt humiliated by law enforcement, I thoroughly understand. If you didn’t do anything wrong, and you truly did not resist, and you really did receive worse treatment because of the color of your skin, well that’s wrong, I want it fixed, and I’ll vote that way. I’ve already written my state representative, expressing my support for certain police reforms. If, on the other hand, you have convinced yourself that my interactions with the police are a picnic and that you need me to feel guilty about it, well, surely being angry with me based on the color of my skin is not the way out of our racial problems. I’d rather get back to talking about how to fix the problems with law enforcement — together, if you’re up for it.

6 Reasons to Eat Less Meat

Most people seem to think that reducing meat consumption is just about preventing animal suffering, and a lot of people simply can’t (or won’t) empathize with an animal raised in a crowded cage with feces on the floor. But there are many reasons why we would be better off eating less meat:

1. The way we raise the animals — routinely giving them antibiotics — breeds antibiotic-resistant bacteria, leaving us increasingly unable to treat infections.

2. The way we raise the animals — in crowded conditions, often with multiple species in proximity — encourages zoonotic transmission, causing human pandemics.

3. The way we raise the animals — in crowded conditions, so that new hosts to infect are always close at hand — breeds hyper-virulent pathogens (pathogens that do not try to minimize harm to their hosts).

4. Raising animals for food is much harder on the environment, in a number of ways.

5. Our health is much the poorer for all the animal flesh we eat (obesity, heart disease, diabetes, and much more).

6. The animals we raise in factory farms have horrific lives, worse than the worst concentration camp.

When I look at this list, I’m sold at #1. Even if none of the rest were true, I would say “Hey, how about we all eat less meat!” Same with #2. These are HUGE problems!

Compiler Explorer and Rust

Well, I just learned something fun. There is a site that makes it easy to see what assembly code is generated for some code in any of dozens of languages, including C and Rust. You get to choose the compiler version and specify the options. Here I wanted to see whether at opt-level 3 the compiler would combine these two calls (it doesn’t):

You can even bring up the code generated by two compiler versions, or two compiler settings, side by side for comparison. Awesome tool!

Rust in the Browser is About More than Performance

I see folks talking about compiling Rust code to WebAssembly (so that it can be run in the browser) as if it were just about performance. They might further judge that most apps are fast enough in Javascript, so Rust in the browser is going to be a very niche thing.

Of course it’s true that Javascript is the de facto language of the browser, and that isn’t going to change anytime soon. However, looking around at existing applications and saying “meh, they’re fine in Javascript” really misses the point.

First, Rust is a far, far better language than Javascript. I don’t want to start a language war, but Javascript is well known to have more warts than a frog. If you don’t like type systems you won’t like Rust, but then you won’t have any use for my blog either. I recognize the value of good type systems, and Rust has a good one. It also does concurrency well, has a good macro system, and much more.

So another reason (besides performance) that you might want to use Rust rather than Javascript for certain tasks is that you want to write higher quality, more robust code.

By the way, even if Javascript performs well enough for your app, you might appreciate not having garbage collection pauses. Rust doesn’t have any.

Nor does Rust come with a runtime. One reason that Rust is especially attractive in the browser is that it is little more than the WebAssembly for your code, and a memory allocator, that is going to get downloaded. Sure, you could use C# or Go or whatever, but if the language is garbage-collected then the downloaded WebAssembly has to include a garbage collector — and whatever else is in that language’s runtime. Rust makes for a leaner download.

Also remember that by virtue of being more performant, Rust uses less battery to do the same task. If your app does something cpu-intensive, then even if Javascript is fast enough to be usable, phone/tablet users will still appreciate you not sucking the battery dry.

And RAM. Given a language that wants all objects on the heap and a header on every object, like Java, you end up using 40 bytes to hold the empty string. That’s not true in Rust. And depending on the type of garbage collector, extra memory may be needed for copying objects around.

And finally, don’t just think about *existing* applications — having a more performant language with no GC pauses opens up new possibilities for things that people would not previously have considered a browser app suitable.

It is still true that most applications present no compelling case for using WebAssembly — they just don’t do anything that significant outside of the GUI and talking to some server. For an app like that, with a team that is already facile with Javascript, and given the gymnastics of getting into and out of WebAssembly modules, why bother with WebAssembly?

So I agree that WebAssembly isn’t something that you will use with every app. However, when you do want a better tool than Javascript, enough to be worth dealing with WebAssembly, why would you choose anything but Rust? Much of the point was to improve performance and/or avoid GC pauses, right? Well, Java/C#/Go/etc. are going to take longer to load, perform worse, have GC pauses, use more RAM, use more battery, and be vulnerable to null pointer exceptions, data races, and other problems. If you are going to go to the trouble of using WebAssembly to improve a browser app, why not use the language that will do the best job of it?

A Thought on the College Admissions Scandal

The world of US university education experienced an earthquake recently when a federal indictment exposed wealthy parents paying to get their underperforming kids accepted into universities.  I’d like to share my personal take on this, channeling my inner Milton Friedman.

Instead of trying to ferret out and prosecute what we currently consider corruption, why not make it legal and visible?  This will surely sound weird at first, but hear me out.

Each university would produce a ranked list of applicants, each with an associated score derived from metrics indicating their likelihood of success — AND NOTHING ELSE — and then let parents sweeten the pot.  If you really want your mathematically mediocre kid in Caltech’s physics department, you can pay extra according to some published schedule to bump their score.

All above board.  By definition it’s not cheating.  The base score is STRICTLY about potential and is subject to review and challenge.  The base score is never bumped because your parents went to the school, or because of your race or religion or anything else.  The only way to bump the score is with money.

Wait — isn’t that bad?  Don’t we want to force colleges to accept the applicants we think are most deserving?

I’m strongly in favor of getting those students with “the right stuff” intellectually a good education so that they are more productive (and pay more taxes).  So given that there is not enough education to go around, how do we get more of it?

Money.  Note that by accepting that bump money from parents whose kids wouldn’t otherwise rank high enough, we are obviously getting that one kid into school who wouldn’t otherwise go, at least not there.  Sure, some of them will fail — at least one of the kids identified in the scandal didn’t even want to go to college — but the more likely the kid is to fail, the more money the parents have to give the school in order to let them try.  And you use that money to provide more/better education the next year.  This isn’t a zero-sum game; more money means that more kids can get educated.  (And even those rich kids who fail will probably learn *something* from the attempt.)

Want to give a break to a particular kid you believe deserves it?  Fine, there’s a way — the same way open to everyone else.  Want to give an entire class of people a break, like poor inner-city kids?  Fine, there’s a way.  It’s all right there in the open, and it works the same for everyone.  Get some money allocated to the project and you’re off to the races.  The costs of the program are obvious.  No crooks getting rich off of bribes.  No encouraging parents who want the best opportunities for their kids to cheat.  No more spending money to investigate that crap.  Who would bribe a crook to cook their kid’s SAT results, knowing that they could be convicted of a felony (and publicly humiliated) for doing so, when they could just pay the university directly to let their kid in, knowing that the money would be used to educate more kids?

OK, I’ll bet now you’re worried that the school will just keep all of the money instead of using it to educate more students.  But that would be tremendously stupid of the school, since they only get the money for the kids they admit and educate.  More kids admitted, more money.  More money, more slots for more kids.  Any school that decided to take only rich parents’ kids would simply be giving up the opportunity to serve the rest, along with the associated revenue.  Some other school will gladly step up and take the money.

And universities would be required to be transparent about the average score of the kids they admitted, the bump money that they accepted, etc.  If a university accepts so much bump money that it drags down the average scores, then maybe you don’t want to go there.  Every university would strike its own balance, not accepting so much bump money that it would significantly degrade the school’s reputation.  Students could see where various universities land on this spectrum and decide whether they want to pay more in order to get a more “elite” experience.

In short, let rich people pay extra to get their kids in — more kids get educated that way.

If right now you’re thinking that people shouldn’t be able to buy *preferential* access to a university education, you’re still thinking that it’s a zero-sum game, that for some reason having more money does not allow us to educate more students.

I actually think we need far more radical reforms to our education system than this, but I don’t have any problem at all with rich people buying their kids an education that they couldn’t get on intellectual promise alone.  And I would argue that you don’t mind either if you think that it’s OK that star athletes are given preference in admissions — you know that schools do that because people donate more to schools with competitive athletic programs, right?  Not to mention ticket sales.  It has nothing to do with the sport being important in any educational sense; in fact you could easily argue that reifying athletes is anti-educational.  Those athletic kids are accepted ahead of others who are intellectually more promising because of the money they bring with them.  The only difference from the current scandal is that the money is not coming from their parents.  If that difference somehow makes it OK in your mind, I can’t relate to your thought process.

So let’s turn this education scandal around, explicitly making a place for money to influence admissions rather than forcing it underground, where it would continue anyway but with the money going to unsavory folks.  Accept the money in broad daylight and use it to educate more kids.  Let’s be completely up front about it with students and parents — the more you look like a good bet academically, the less you pay, but anyone can come here to study.  Once in, you earn your grades on merit, and *that* is where we will be looking for corruption.

Teach your kids to curse!

“Oh, schmarkle!”

Imagine your son or daughter saying that in class.  Not too upsetting, I’m guessing.

Curse words are upsetting because those within earshot consider them vulgar.  And curse words are satisfyingly cathartic (and potentially even healthy) because of the utterer’s personal culture.  Usually those considerations are aligned, but what if you were to teach your children to curse using words that have no significance to anyone else?  It might sound a little silly, perhaps even humorous, but it wouldn’t sound offensive.  So schmarkle away!

Then later, when friends inevitably introduce your son or daughter to “real” curse words, they will have some degree of immunization against adopting them — the ingrained habit of using an inoffensive word.

O(1) sum_of_multiples() in Rust

I had been working mostly in Scala for a while, then took a diversion into Swift and Objective C.  I wanted to learn another language after that, and had all but decided on Clojure.  But Rust kept nagging at me — there was something about it.  Perhaps I’ll blog more about that later.

So I watched some videos, then read the book, and then started the Rust track at Exercism.io.  Nice site.  One of the exercises is about generating the sum of all multiples under some limit for a given list of factors.  So for example, if the factors are 3, 5, and 7, then the sum of the multiples under 12 is 3 + 5 + 6 + 7 + 9 + 10 = 40.

A naive solution is pretty straightforward:

pub fn naive_sum_of_multiples(limit: u32, factors: &[u32]) -> u32 {
    (1..limit).filter( |&i|
        factors.iter().any(|&j| i % j == 0)
    ).sum()
}

It just iterates through all of the eligible numbers, picking out any that are evenly divisible by any of the factors, and sums them.  Rust’s iterators make that look pretty similar to a lot of other modern languages, like Scala.

But this solution is slow.  I mean really slow — at least compared to what’s possible.  As limit grows, the execution time increases linearly; that is, this is O(n).  (With respect to the limit, that is — we’re ignoring the (very real) impact of the number of factors on execution time. Note that in Exercism’s tests, there are generally two factors, max three, so for simplicity let’s focus on the limit here.)

Well, O(n) doesn’t sound so bad, does it?  But here’s the thing: it could be O(1).

What???  O(1)?  That would mean that you aren’t doing any more work if you increase the limit — and thus the numbers summed — by a factor of 100.  That can’t be right!  But yes, it is.

Here is the key observation.  The instant you see it, your brain will likely jump halfway to the solution.  The sum of (for example)

3 + 6 + 9 + 12 + 15 + 18 + 21

is just

3 * ( 1 + 2 + 3 + 4 + 5 + 6 + 7)

Got it yet?  Remember that the sum of the numbers from 1 to n is simply the value of n * (n + 1) / 2 — actually summing the numbers is unnecessary!

That’s not all there is to it, though. If asked to sum the multiples of 3 and 5 less than 1000, we can use the above technique to sum up the multiples of 3 and the multiples of 5 and then add them together, but we will have counted the multiples of 15 twice; we have to subtract those out.

And that’s a little trickier to do that than it sounds. First, what you really need to subtract out are the multiples of the least common multiple (lcm) of the two numbers, not their product. So, for example, if asked to sum the multiples of 6 and 15, we need to subtract off the multiples of 30 (not 90). The lcm of two numbers is their product divided by their greatest common divisor (gcd).

Also, we need to do this for an arbitrarily long list of numbers, so consider what happens if we are asked to sum the multiples of 4, 6, and 10:

  • First sum the multiples of 4.
  • Then add in the multiples of 6, but subtract the multiples of lcm(4, 6) = 12.
  • Then add in the multiples of 10, but subtract the multiples of lcm(4, 10) = 20 and the multiples of lcm(6, 10) = 30.

But oops, now we have gone the other way, subtracting off the multiples of 20 and 30 in common (60, 120, …) twice, and our result is too low, so we’ll have to add those back in. And if there were multiple corrections at that level (i.e. if we were given a larger list of numbers), we’d have to subtract their elements in common, and so on ad infinitum. At every step we have to take care not to add or subtract the same numbers twice.

That sounds like a pain, but using recursion it’s actually fairly straightforward.  In the following Rust code, I’ve changed the API a bit from the Exercism problem.  First, the integers are u64, so that we can use much bigger limits.  And secondly, in this case we’ll sum all multiples up to and including limit.  It’s an arbitrary choice anyway, and doing it this way will save us a small step for clarity.

pub fn fast_sum_of_multiples(limit: u64, factors: &[u64]) -> u64 {
  fn lcm(a: u64, b: u64) -> u64 { a*b / gcd(a,b) }
  fn gcd(a: u64, b: u64) -> u64 { if b == 0 {a} else { gcd(b, a%b) } }
  fn sum_from_ix(i: usize, limit: u64, factors: &[u64]) -> u64 {
    if i == factors.len() {  // we've processed all factors
      0
    } else {
      let factor = factors[i];
      let n = limit / factor;  // # of multiples of factor to sum
      let sum_of_multiples_of_factor = factor * (n*(n+1)/2);
      let new_factors: Vec<_> = factors[..i].iter()
        .map(|&prev_factor| lcm(prev_factor, factor))
        .filter(|&factor| factor <= limit)
        .collect();
      let sum_of_previously_seen_multiples_of_factor =
        sum_from_ix(0, limit, &new_factors[..]);
      let sum_of_multiples_of_rest_of_factors =
        sum_from_ix(i+1, limit, factors);
      sum_of_multiples_of_factor
        - sum_of_previously_seen_multiples_of_factor
        + sum_of_multiples_of_rest_of_factors
    }
  };
  sum_from_ix(0, limit, factors)
}

This is not too far from what a Scala solution would look like, although I think the Scala solution is a bit more readable.  In part this owes to Scala’s persistent List data structure, which was a little cleaner to work with here than a Rust Vec.  Also, Scala’s implicits make it unnecessary to make two calls that are explicit in the Rust version: iter() and collect().  Oh, and in Rust functions cannot close over variables; only closures can do that, but we couldn’t use a closure since in Rust closures cannot be recursive.  That forced us to explicitly include limit in the argument list of sum_from_ix().  Ampersands here and there in closures.  Semicolons.  These are little things, but collectively noticeable.

def fastSumOfMultiples(limit: Long, factors: List[Long]): Long = {
  def lcm(a: Long, b: Long) = a*b / gcd(a,b)
  def gcd(a: Long, b: Long): Long = if (b == 0) a else gcd(b, a%b)
  def sumOfMults(factors: List[Long], prevFactors: List[Long] = Nil): Long =
    factors match {
      case Nil => 0
      case factor::rest =>
        val n = limit / factor  // # of multiples of factor to sum
        val sum_of_multiples_of_factor = factor * (n*(n+1)/2)
        val sum_of_previously_seen_multiples_of_factor =
          sumOfMults(prevFactors.map(lcm(_, factor)).filter(_ <= limit))
        val sum_of_multiples_of_rest_of_factors =
          sumOfMults(rest, factor::prevFactors)
        sum_of_multiples_of_factor -
           sum_of_previously_seen_multiples_of_factor +
           sum_of_multiples_of_rest_of_factors
    }
  sumOfMults(factors)
}

But really, the difference is not so stark.  And the Rust version does not need a garbage collector — amazing!

On to the next exercise….

Thoughts on “Self-Serving Bias”

If someone were to ask you and your roommate what percent of the work around the house you each do, the answers would almost surely total to more than 100%; you each likely overestimate your contribution. This is an oft-mentioned example of “self-serving bias.”

While I have no doubt that this bias exists, there is one confounding factor that you have to be careful about in attributing such a discrepancy in assessment to self-serving bias, something I have never seen discussed.  That confounding factor is that we value things differently.

My wife, when I was married, used to iron my undershirts after they came out of the dryer.  I asked her not to do it: not only was it a waste of her time because the wrinkles would not be visible, it was also a waste of energy both to iron them and then for the AC to remove the heat from the house.  She saw things differently and continued to iron them.

So when thinking about how much work she did around the house, there were those twenty minutes of ironing she did for me.  But I counted that activity more as an annoyance than as a contribution.

I had another partner who had a curio cabinet with shelves of little glass unicorns and other trinkets on display.  We lived in Phoenix, so all those little pieces needed frequent dusting; in her mind that effort was part of the housework.  But that curio cabinet did nothing for me; it was strictly for her pleasure.  Whatever dusting she did on those unicorns was maintenance on her hobby, as far as I was concerned, comparable to me keeping the tires inflated on my bike.

There can even be discrepancies for work that is valued by both.  For my partner, keeping the kitchen tidy may involve putting on a shelf in the pantry some things that I would prefer to see left on the counter, like the cinnamon and jars of nuts.  Or imagine living with someone who wants the carpets vacuumed every week, when you are fine with once a month.

None of this is to say that humans don’t engage in self-serving bias — just that such discrepancies may also owe in part to discrepancies in what people consider the goal.

Wanted: DNA for Smart Brains

OK, smart people, you may be in a position to help science! There is important research underway to help determine the genetic factors of intelligence; our genes have a large influence on how are brains are built, and this effort will try to determine what is different about the genes of highly intelligent people from those of the general population. This research is likely to be ground-breaking, and the project is looking for test subjects. If your test scores meet any of the following criteria, you qualify:

– a pre-1995 SAT score of at least 780 Math and 700 Verbal
– a post-1995 SAT score of at least 800 Math and 760 Verbal
– a GRE score of at least  800 Quantitative and 700 Verbal
– an ACT score of at least 35

Sound like you? If you still have the documentation, apply at

https://www.cog-genomics.org/

If selected, they will send you a little kit for you to collect saliva. You’ll be able to see the results of your own DNA analysis.

Here’s a great talk on the subject:

Sounds interesting!

Oxytocin and relationships

Oxytocin is all in the news these days, and people seem to want a better understanding of the role the hormone plays in relationships. Here is an analogy based on my take on it.

Have you ever made bread? I had a breadmaker for a long time, a gift from a good friend. It eventually died of old age, after making literally thousands of loaves. I experimented with the ingredients all the time. Every once in a while I would forget to add an ingredient, and the results were, as a rule, disastrous.

I made whole-wheat loaves, which are a bit touchy. If I forgot to add an egg — or gluten or something else to increase the protein content — everything would still go fine in the beginning. The yeast would eat the sugars in the honey or molasses and release CO2, causing the loaf to rise beautifully.

At that point the heat was supposed to chemically change the protein in the gluten or egg, to denature it, making it firm so that the loaf would hold its shape. With too little protein, the bread would rise and then, as the yeast started to die from the heat, fall in the center. As that network of tiny holes collapsed, the moisture could no longer evaporate out — the result was a dense, moist brick of firm dough with a nearly bulletproof crust.

For an adult whose brain doesn’t manufacture much oxytocin — perhaps through some misfortune of genetic inheritance, or a lack of parental love as a child — trying to have an intimate relationship may be a bit like baking that doomed loaf of bread. In the early stages of a relationship the brain is swimming in hormones like dopamine and serotonin and adrenalin, and all goes well. Like yeast happy in their sugar-rich environment, the relationship feeds on the resulting euphoria and grows by leaps and bounds.

Eventually, though, the high wears off. Oxytocin is supposed to be in play at that point, though, keeping you firmly attached to your beloved. Ideally you feel as though your life and your partner’s life are more or less one life shared by the two of you, in a large emotional space opened up by passion and given permanence by oxytocin. You still have your own separate interests, of course, but the core of your life is not just in a space shared through some act of will but rather is the same as the core of your partner’s life.

But if your brain doesn’t produce enough oxytocin, you might still feel like a person with his/her own separate life, sharing an intimate world with somebody you enjoy and care deeply about but who is nevertheless, in some sense, ever so subtly intruding on your life.  You make compromises for the benefit of the relationship, but those compromises feel different than they would if you were really bonded to your partner. Disappointments and inconveniences accumulate because goals are not truly shared.  You begin to secretly wish that your life were your own again.

Eventually the relationship collapses into a disagreeable blob that is impossible to get out of the pan without a hacksaw.

Whole wheat, anyone?