当前位置: 首页 > news >正文

再论自然数全加和 - 质数螺旋的图片和程序

namespace PrimeCross; public partial class FormMain : Form { public FormMain() { InitializeComponent(); } enum Direction : int { Down = 0, Right, Up, Left } Direction Turn(Direction d, bool left = true) => left ? (Direction)(((int)d + 1) % 4) : (d == Direction.Down) ? Direction.Left : ((Direction)((int)d) - 1) ; Point LeftOf(Point p, Direction d) => d switch { Direction.Down => new Point(p.X + 1, p.Y), Direction.Right => new Point(p.X, p.Y - 1), Direction.Up => new Point(p.X - 1, p.Y), Direction.Left => new Point(p.X, p.Y + 1), _ => p, }; Point RightOf(Point p, Direction d) => d switch { Direction.Down => new Point(p.X - 1, p.Y), Direction.Right => new Point(p.X, p.Y + 1), Direction.Up => new Point(p.X + 1, p.Y), Direction.Left => new Point(p.X, p.Y - 1), _ => p, }; Point ForwardOf(Point p, Direction d) => d switch { Direction.Down => new Point(p.X, p.Y + 1), Direction.Right => new Point(p.X + 1, p.Y), Direction.Up => new Point(p.X, p.Y - 1), Direction.Left => new Point(p.X - 1, p.Y), _ => p, }; bool IsPrime(long n) { if (n < 2) return false; if (n == 2) return true; if (n % 2 == 0) return false; for (long i = 3; i <= Math.Sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } const int Black = 0x000000; const int White = 0xffffff; const int Red = 0x0000ff; int length = 0; (int, long, bool)[,]? primes = null; (int, long, bool)[,] BuildPrimesMap(int length) { var map = new (int, long, bool)[length, length]; Point center = new(length >> 1, length >> 1); var direction = Direction.Up; long n = 0; map[center.X, center.Y] = (White, n++, false); Point p = new(center.X + 1, center.Y + 1); map[p.X, p.Y] = (Red, n++, false); p.Y--; map[p.X, p.Y] = (White, n++, true); while (n <= length * length) { n++; p = ForwardOf(p, direction); if (p.X < 0 || p.X >= length - 1 || p.Y < 0 || p.Y >= length - 1) break; var b = IsPrime(n); map[p.X, p.Y] = (b ? White : Red, n, b); var l = LeftOf(p, direction); if (l.X < 0 || l.X >= length - 1 || l.Y < 0 || l.Y >= length - 1) break; var lc = map[l.X, l.Y]; if (lc.Item1 == Black) { direction = Turn(direction); } else { var pt = ForwardOf(p, direction); if (pt.X < 0 || pt.X >= length - 1 || pt.Y < 0 || pt.Y >= length - 1) { continue; } var px = map[pt.X, pt.Y]; if (px.Item1 != Black) { var rp = RightOf(p, direction); if (rp.X < 0 || rp.X >= length - 1 || rp.Y < 0 || rp.Y >= length - 1) break; p = rp; } } } return map; } private void GenerateButton_Click(object sender, EventArgs e) { this.length = Math.Max(PrimesPictureBox.Width, PrimesPictureBox.Height); this.primes = BuildPrimesMap(this.length); var bitmap = new Bitmap(PrimesPictureBox.Width, PrimesPictureBox.Height); for (int y = 0; y < PrimesPictureBox.Height; y++) { for (int x = 0; x < PrimesPictureBox.Width; x++) { var px = x + ((this.length - PrimesPictureBox.Width) >> 1); var py = y + ((this.length - PrimesPictureBox.Height) >> 1); var c = this.primes[px, py]; bitmap.SetPixel(x, y, c.Item3 ? Color.White : Color.Black); } } this.PrimesPictureBox.Image?.Dispose(); this.PrimesPictureBox.Image = bitmap; } private void FormMain_Resize(object sender, EventArgs e) { this.GenerateButton_Click(sender, e); } private void FormMain_Load(object sender, EventArgs e) { this.GenerateButton_Click(sender, e); } private void PrimesPictureBox_MouseMove(object sender, MouseEventArgs e) { if (this.primes != null && PrimesPictureBox.Image != null) { var x = e.X + ((PrimesPictureBox.Image.Width - PrimesPictureBox.Width) >> 1); var y = e.Y + ((PrimesPictureBox.Image.Height - PrimesPictureBox.Height) >> 1); if (x >= 0 && x < PrimesPictureBox.Image.Width && y >= 0 && y < PrimesPictureBox.Image.Height) { PointF cp = new(PrimesPictureBox.Image.Width >> 1, PrimesPictureBox.Image.Height >> 1); PointF dp = new(x - cp.X, cp.Y - y); PointF mp = new(dp.X + (this.length >> 1), dp.Y + (this.length >> 1)); var p = this.primes![(int)mp.X, (int)mp.Y]; long n = p.Item2; bool b = p.Item3; var t = Math.Atan2(dp.Y, dp.X) / Math.PI * 180.0; this.InfoLabel.Text = $"n={n}, x={dp.X}, y={dp.Y}, d={t:N4}°: {(b ? "Prime" : "Composite")}"; } } } }

https://github.com/yyl-20020115/PrimeCross.git

http://www.jsqmd.com/news/391007/

相关文章:

  • 别再哭求数据恢复!Excel 删错 / 未保存 / 被覆盖,11 招自己就可以搞定深度解析:原理、实战与踩坑记录
  • 实时字幕翻译插件:零代码实现PotPlayer双语字幕无缝切换
  • NCM格式解密与转换完全指南:从原理到实践的音乐自由解决方案
  • 【例 1】二叉苹果树(信息学奥赛一本通- P1575)
  • 原神智能剧情辅助:BetterGenshinImpact的3大突破点与5个实用技巧
  • iOS微信红包自动抢工具:智能监测、自动响应与安全防护的全面解析
  • 如何高效升级DLSS版本?用DLSS Swapper提升游戏画质与帧数的完整方案
  • XUnity.AutoTranslator:Unity游戏多语言实时翻译解决方案
  • 电话号码智能定位:精准高效的地理位置查询解决方案
  • 2026年武汉汉正街中高端潮流女装优质供应链深度盘点 - 2026年企业推荐榜
  • 2026年MVR蒸汽压缩机批发厂家可靠度综合评估报告 - 2026年企业推荐榜
  • 2026湖南全屋定制装修如何避坑?权威评选标准与诚信品牌推荐 - 2026年企业推荐榜
  • 2026年办公室净水器供应厂家综合评测与选型指南 - 2026年企业推荐榜
  • 5个步骤掌握Switch控制器设备适配:开源解决方案从连接痛点到跨平台兼容的实践指南
  • 3大突破如何解决英雄联盟玩家的效率难题:LeagueAkari智能辅助工具深度解析
  • DLSS不生效?3步验证+5个隐藏技巧让画面性能双提升
  • 探索HsMod:从入门到精通的炉石传说增强插件全面解析
  • 3个破局方案:ncmdump让网易云音乐格式自由
  • 2026年国内韩语培训机构深度评测:6家顶尖机构综合解析 - 2026年企业推荐榜
  • Blender3mfFormat:突破3D打印工作流瓶颈的格式处理方案
  • 2026年初无局放试验变压器知名厂商综合评测与选购指南 - 2026年企业推荐榜
  • 2026开年决策:宜昌顶级旅游服务商全景评测与推荐 - 2026年企业推荐榜
  • 如何深度优化NVIDIA显卡性能:Profile Inspector进阶调校指南
  • 深圳净水解决方案优选:这三家企业口碑与技术俱佳 - 2026年企业推荐榜
  • 2026年优质猕猴桃采购指南:核心经销商解析与联系 - 2026年企业推荐榜
  • 2026湖南专业装修连锁品牌全景评估与战略选择指南 - 2026年企业推荐榜
  • 2026年自助棋牌室连锁加盟投资成本分析 - 2026年企业推荐榜
  • 2026年汉正街品牌服饰广场女装批发:高性价比一手货源深度解析 - 2026年企业推荐榜
  • 阴阳师自动化脚本跨模拟器适配与图像识别优化指南
  • 长沙地下室防水补漏服务商深度评测与选择指南 - 2026年企业推荐榜