用Java模拟芝诺悖论的程序

发布于 2024-09-30 20:49:17 字数 323 浏览 4 评论 0原文

所以我在读一本关于某种悖论的书:假设你在一个房间里,一扇门在你对面。不过,为了离开,您每次必须走一半的距离。因此,第一步你迈出了一半的距离,第二步你又迈出了一半,依此类推(1/2 + 1/4 + 1/8)等。假设如果你继续下去,你将永远无法到达门口像这样。我想使用微积分很容易证明这一点,但是用 Java 或任何其他程序来模拟这一点会很有趣,其中的球从左侧开始,每次移动一半的距离,使其到达右侧,同时显示迄今为止采取的“步骤”数量及其进展。我很想自己做,但我仍然是 Java 初学者,不了解 GUI 编程。谁能模拟这个吗?

(抱歉,如果这不是一个“真正的”问题。我只是很好奇要走多少步才能让球看起来几乎就在那里。)

So I was reading in a book of a certain paradox: Say you are in a room with a door opposite of you. In order to leave though, you must travel half the distance there each time. So the first stride you make half the distance, and the second stride you take half again, and so on (1/2 + 1/4 + 1/8) etc. The claim is if you will never reach the door if you continue like this. This is I guess easy to prove using calculus, but it would be interesting to simulate this in Java or any other program with a ball that starts on the left side, and makes its way to the right side by traveling half the distance each time, while showing the number of 'steps' taken so far and its progress. I would love to do it myself but I'm still a beginner in Java and dont know GUI programming. Can anyone simulate this?

(Sorry if this is not a 'real' question. I'm just really curious how many steps it would take for the ball to even look like it was almost there.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

萌︼了一个春 2024-10-07 20:49:18

这是芝诺悖论之一,这取决于您对“几乎”的定义是什么。从足够远的有利位置来看,无论实际距离是多少,它看起来都几乎就在那里。

至于执行此操作的程序,您的问题太广泛了 - 它可以通过多种方式实现。关键要素是将球到墙壁的距离更新为前一“帧”或迭代中的距离的一半。

This is one of Zeno's Paradoxes It depends on what your definition of "almost" is. From a far enough vantage point, it can look like it is almost there no matter what the actual distance is.

As for a program to do this, your question is far too broad - it could be implemented in a number of ways. The key element is to update the distance of the ball to the wall to be one half of what it was in the previous "frame" or iteration.

热情消退 2024-10-07 20:49:18

如果您想在 C++ 中执行此操作,请使用以下代码:

float Distance;

cout << " Enter distance value :" << endl;
cin >> Distance;

while (Distance > 0){
    cout << Distance << endl;
(Distance = Distance /2);
}


return 0;

}

If you want to do it in C++ here's the code:

float Distance;

cout << " Enter distance value :" << endl;
cin >> Distance;

while (Distance > 0){
    cout << Distance << endl;
(Distance = Distance /2);
}


return 0;

}

中二柚 2024-10-07 20:49:17

请参阅此图,该图形象化了悖论:

http://www.wolframalpha.com/input/?i=1-1/(2^n)+for+0<n<10

See this plot, which visualizes the paradox:

http://www.wolframalpha.com/input/?i=1-1/(2^n)+for+0<n<10
千柳 2024-10-07 20:49:17

在你遇到另一扇门之前,你就会遇到人数限制。抽象起来是:

var Distance=100;
var Traveled=0;
var Remaining=Distance/2

while(Traveled < Distance){
  echo Remaining
  Traveled=Traveled+Remaining
  Remaining=Remaining/2

}

使用 PHP 在我的一台机器上运行它导致..
50
25
12.5
6.25
3.125
1.5625
0.78125
0.390625
0.1953125
0.09765625
0.048828125
0.0244140625
0.01220703125
0.006103515625
0.0030517578125
0.00152587890625
0.000762939453125
0.0003814697265625
0.00019073486328125
9.5367431640625E-5
4.7683715820312E-5
2.3841857910156E-5
1.1920928955078E-5
5.9604644775391E-6
2.9802322387695E-6
1.4901161193848E-6
7.4505805969238E-7
3.7252902984619E-7
1.862645149231E-7
9.3132257461548E-8
4.6566128730774E-8
2.3283064365387E-8
1.1641532182693E-8
5.8207660913467E-9
2.9103830456734E-9
1.4551915228367E-9
7.2759576141834E-10
3.6379788070917E-10
1.8189894035459E-10
9.0949470177293E-11
4.5474735088646E-11
2.2737367544323E-11
1.1368683772162E-11
5.6843418860808E-12
2.8421709430404E-12
1.4210854715202E-12
7.105427357601E-13
3.5527136788005E-13
1.7763568394003E-13
8.8817841970013E-14
4.4408920985006E-14
2.2204460492503E-14
1.1102230246252E-14
5.5511151231258E-15

you will run into numbers limits far before you run into the other door. abstracted it is:

var Distance=100;
var Traveled=0;
var Remaining=Distance/2

while(Traveled < Distance){
  echo Remaining
  Traveled=Traveled+Remaining
  Remaining=Remaining/2

}

running this on one of my boxes using PHP resulted in..
50
25
12.5
6.25
3.125
1.5625
0.78125
0.390625
0.1953125
0.09765625
0.048828125
0.0244140625
0.01220703125
0.006103515625
0.0030517578125
0.00152587890625
0.000762939453125
0.0003814697265625
0.00019073486328125
9.5367431640625E-5
4.7683715820312E-5
2.3841857910156E-5
1.1920928955078E-5
5.9604644775391E-6
2.9802322387695E-6
1.4901161193848E-6
7.4505805969238E-7
3.7252902984619E-7
1.862645149231E-7
9.3132257461548E-8
4.6566128730774E-8
2.3283064365387E-8
1.1641532182693E-8
5.8207660913467E-9
2.9103830456734E-9
1.4551915228367E-9
7.2759576141834E-10
3.6379788070917E-10
1.8189894035459E-10
9.0949470177293E-11
4.5474735088646E-11
2.2737367544323E-11
1.1368683772162E-11
5.6843418860808E-12
2.8421709430404E-12
1.4210854715202E-12
7.105427357601E-13
3.5527136788005E-13
1.7763568394003E-13
8.8817841970013E-14
4.4408920985006E-14
2.2204460492503E-14
1.1102230246252E-14
5.5511151231258E-15

救星 2024-10-07 20:49:17

一些java中的数值分析。看看这个

a bit of numerical analysis in java. have a look at this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文