Monday, March 25, 2024

java Timer

A simple program to schedule sonething in the future.
import java.util.TimerTask;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Timer;

class HelloWorld {

	private static class MyTimerTask extends TimerTask {

		String someValue;

		MyTimerTask(String someValue) {
			this.someValue = someValue;
		}

		@Override
		public void run() {
			System.out.println("Timer run " + someValue);
		}
	}

	public static void main(String[] args) {

		HelloWorld me = new HelloWorld();
		LocalDateTime someTimeLater = LocalDateTime.now().plusSeconds(3);

		Date futureDate1 = Date.from(someTimeLater.atZone(ZoneId.systemDefault()).toInstant());

		System.out.println("Scheduling something to be run in future...");

		new Timer().schedule(new MyTimerTask("hello my friend"), futureDate1);

		System.out.println("going on with my life");

	}

}

Saturday, January 27, 2024

How many diagonals does a polygon have?

I came across another problem I never learned how to solve... how many diagonals does a 13-gon have?

Geez, I nerver learned the formula of how many diagonals in a polygon, let's see if I can derive on the fly.. Start with the triangle. nope, no diagonals. The square: 2. The pentagon: 5. The hexagon: 9. It is kinda hard to draw and count bigger ones. So I have a f(3)=0, f(4)=2, f(5)=5, f(6)=9.. and yikes I can't easily obsere a pattern and derive this formula on the fly.

But I kinda observe, something to do with n-2... because can't connect a point to its neighbors and call it diagonal... and sadly I have to give up and look the formula up. Yes I admit.

And it takes I think a bit genius to reach this observation:

The nunber of diagonals is simply number of segments connecting every pair of points, EXCEPT the edges of the polygon.

So it is the Combination n choose 2, minus n: nC2 - n

    n! 
---------  - n
 2! (n-2)!


  n (n-1)
= ------- - n
    2

= n^2-n - 2n
  ----------
     2

= n^2 - 3n
  --------
     2
     
= n(n-3)
  ------
    2
So there are 13(13-3)/2 = 65 diagonals for the 13-gon.

This formula n(n-3)/2 I'd keep it in my treasure chest where I put other not-so-obvious formulas like volumne/surface area of a sphere, volume of a pyramid, etc.

Waita minute, although my initial observation is not going anywhere but somewhat of a good start. For each point on the polygon, do not count the 2 neighbors including the point itself, so n-3 diagonals for each point. Go around each point of the polygon, so it is n(n-3), but by that time you would have counted twice the number of diagonals, so divide by 2. There you are: n(n-3)/2.

Monday, December 18, 2023

Surface Area of a Cone

So I recently encountered a challenging math problem intended for an 8th grader. Find the surface area of a frustum. That is, a circus elephant stand or a bucket. Given the circumference of the top and bottom and the height between.

Wow, that's a tough problem for an 8th grader and I am frustrated at this frustum problem because I never learned the formulas needed. It would be relatively straight forward if I know how to find the surface area of a cone but I don't know. Not that I forgot it but just never learned it, although I know how to find the volume of a cone. That 1/3 base * height formula is actually not exactly straight forward to derive either. I know these formulas are not so useful in life, I know. But the spirit of math should be: if there is an answer find the answer.

Nowadays it is easy to look up the formula. Note the cone has the side and the bottom. The formula looks like this:

That looks a little intimidating. A cone is a paper cup. Surface area is the paper cup plus the bottom circle. The area of the paper cup is the "lateral surface area" and it is π r L, where r is the radius, and L is the slanted height. The slanted height L is also square root of h2 + r2, straight from the Pythagorean Theorem. Neat. So the formula above is area of the paper cup plus the area of the bottom circle. But where did this lateral area formula π r L come from. I looked up a few explanations and none provide satisfactory explanation. Allow me explain.

Now look at the paper cup. Imagine flattening it. It is a section of a circle. What is its radius? L.
What's the circumference of the big circle that the paper cup is part of? 2 π L.
What's its area? π L 2.
But we are interested in the section, the paper cup. Geez I'll need the angle but I don't know.
But I do know its rim's length. That would be the circumference of the bottom of the cone: π r2.
Then the paper cup's area is a fraction of the big circle's area π L 2.
The fraction is rim over entire circumference, which is π r2 over 2 π L. Multiply it out, many things cancel, and then you will get π r L. See, if the cone is entirely flat, then r=L it is just π r2.

Armed with this lateral cone area formula π r L, there is still a bit of calculation to find surface area of a frustum, which is top and bottom circle area. Plus the difference of lateral area of bigger cone and smaller cone. First I'll need the height of the cone which takes a bit of work with similar triangles to find out.

But the point is this: Do not ever just tell kids to use formulas, derive them! Deriving formula is learning math. Using the formula is applying math. Plugging in numbers in a formula which you don't know where it came is lowest form of math.

Wednesday, April 19, 2023

"Teen Takeover"

Dozens of teens terrorized downtown, beat and robbed a terrified woman. https://news.yahoo.com/disturbing-video-shows-terrified-woman-161836419.html

This is the street where I used to walk by everyday to work, and to school. Not too long ago there was endless looting and every store was boarded up and I don't know know if those looters were ever get caught (probably not).

The mayor-elect's response was "It was unacceptable", and "but don't demonize youth" https://abc7chicago.com/chicago-violence-brandon-johnson-teens-new-mayor/13143044.

People debate whether this is adequate response or not. What do you think? Well I think it was adequate, at least he didn't say it was acceptable. What did the police say? "... won't be tolerated" and "We strongly encourage parents to accompany their teens or have them remain under the supervision of a responsible adult. " Like their parents (if not null) still able to control these teenagers.

Waita minute just what is this "Yay we got active" thing on the video? These teenagers take joy of torturing the terrified victim!

Not too long ago some clueless traveler thing crowned Chicago as the "Best Big City in the United States" https://www.nbcchicago.com/news/local/chicago-voted-best-big-city-in-the-us-by-conde-nast-traveler-for-6th-year-in-a-row-heres-why/2957641/. I am not sure if crime was ever in its clueless magazine's cluessless consideration.

Look, it was unacceptable, and shameful and some strong response is needed as well as addressing the root cause.

Just why it happened in the first place? Do these teenagers have any demands? Were they protesting against something or demanding something I have no clue. Is it because they rob and terrorize, "because they can" because cops are so useless? Since even looting was fine what else is not fine? There are already dozens of gun violence just about everyday. Police basically can't put crimes in check.

Besides criticizing, what can you do about it?

Move out of Chicago is definitely a good move but that isn't the solution because that's avoiding the problem.

Why can't these kids just study hard, find work, live a good life and not terrorize others? Read the rest of Johnson's response: "...not constructive to demonize youth who have otherwise been starved of opportunities in their own communities". Yes, youth is not to be demonized, but waita minute, these youths were demonic!

Problem 1: is lack of opportunites a reason to terrorize downtown? Problem 2: why is there lack of opportunities in their communities?

Solution 1: I already do see flashing police cars on most intersections of the magnificent mile Michigan Avenenue. There needs to be the same in the less magnificent mile State Street. Every intersection needs one. The police need to keep its words: catch them put them on jail, although I have some doubt their ability to do so.

Now the bigger problem (#2) is why isn't there opportunities in their communities? I believe if there are any business in their communities they get robbed everyday and that's why nobody open business in there, and vicious cycle of poor beget violence, and violence beget more poor, etc.

This means: someone must invest in their communities (not my money please). Those who are super well to do (like top Hollywood, sports stars etc) that they admire, descend down to talk to these people, tell them what is proper and what is not. Create jobs. Promote Peace.

Now talk is easy to "create jobs". Decent paying jobs now have ridiculously high requirements and people prefer to loot instead of doing difficult jobs that pays little. Bump up the minimum wage to even higher does not solve that either.

Walmart had to close money-losing stores https://www.cnn.com/2023/04/12/business/walmart-chicago-stores-closing/index.html. Yes theft was one reason why they lose money.

So the long term solution is first the police must be fearful enough, must be capable enough. Stronger effective punishment needed. Then the city offer incentive to create business (for example low tax) and promise safety for business (security cameras, cops patroling etc) Jobs: there must be attainable jobs out there for those willing to work. Talk is easy, but who is willing to hire these teens and change their lives?

The ultimate solution is actually education. Brandon Johnson was a teacher he should have a better idea than me that education is actually the best solution here. But do the schools even talk about this after this incident? They go back to their endless (and useless) curriculum: alliteration, onomatopoeia, simile and metaphor, endless boring math drills and native Americans history (they don't even teach modern US history nor any world history and forever obsessed with explorers era). Schools should talk about this! and focus less on other (controversial) social issues.

Clueless travel magazine: do you still think Chicago is the best big city?

Wednesday, April 5, 2023

Chicago's new mayor

Brandon Johnson has won the mayor race in a tight race: 51% over 49%, in the second round against Paul Vallas. First round had 10 candidates and Vallas had 32% over Johnson's 21%. Voter turnout was just 33%. There are people dying to get right to vote and there are people who don't bother to vote.

Chicago is a mess. Can this new mayor fix all the problems? We will wait and see.