为π(pi)实现Spigot算法

编程入门 行业动态 更新时间:2024-10-28 06:32:23
本文介绍了为π(pi)实现Spigot算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我很难理解此处位于页面底部.

I'm having a hard time understanding the Spigot algorithm for π (pi) found here at the bottom of the page.

在第2部分将A放入常规格式"的底部,我迷路了,我不确定如何在 C (或任何语言)中实现它

I'm getting lost at the bottom of part 2 "Put A into regular form", I'm not exactly sure how to implement this in C (or any language really)

推荐答案

#include <math.h> #include <stdio.h> #define N 100 int len = floor(10 * N/3) + 1; int A[len]; for(int i = 0; i < len; ++i) { A[i] = 2; } int nines = 0; int predigit = 0; for(int j = 1; j < N + 1; ++j) { int q = 0; for(int i = len; i > 0; --i) { int x = 10 * A[i-1] + q*i; A[i-1] = x % (2*i - 1); q = x / (2*i - 1); } A[0] = q%10; q = q/10; if (9 == q) { ++nines; } else if (10 == q) { printf("%d", predigit + 1); for (int k = 0; k < nines; ++k) { printf("%d", 0); } predigit, nines = 0; } else { printf("%d", predigit); predigit = q; if (0 != nines) { for (int k = 0; k < nines; ++k) { printf("%d", 9); } nines = 0; } } } printf("%d", predigit);

更多推荐

为π(pi)实现Spigot算法

本文发布于:2023-11-29 01:11:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644689.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   pi   Spigot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!