It is well known that if the square root of a natural number is not an integer, then it is irrational. The decimal expansion of such square roots is infinite without any repeating pattern at all.
The square root of two is 1.41421356237309504880..., and the digital sum of the first one hundred decimal digits is 475.
For the first one hundred natural numbers, find the total of the digital sums of the first one hundred decimal digits for all the irrational square roots.
这个题涉及到高精度开方,像python,haskell等语言原生支持高精度小数,做这个题不成问题,直接使用api即可。我习惯用java,研究BigDecimal发现里面没有开方的方法,所以需要手动实现。可以采用牛顿逼近法解决开方问题,可以用BigDecimal实现高精度。牛顿逼近法参见:http://baike.baidu.com/view/1514354.htm
// 0.0000001是精度,f是待求根的函数,df是f的导数,x0是初值
public static double newtonMehtod(F f, DF df, double x0) {
double x1 = x0 - f.f(x0) / df.df(x0);
while (Math.abs(x1 - x0) > 0.0000001) {
x0 = x1;
x1 = x0 - f.f(x0) / df.df(x0);
}
return x1;
}
//函数f(x)
public interface F {
double f(double x);
}
//f(x)的导数f'(x)
public interface DF {
double df(double x);
}
F和DF的实现类没贴上来。
下面用BigDecimal把上述算法翻译一遍:
static int prec = 100;
static BigDecimal precE;
static {
String e = "-0.";
for (int i = 0; i < prec; i++) {
e += "0";
}
e += "1";
precE = new BigDecimal(e);
}
public static BigDecimal newtonMehtod(F f, DF df, double x00) {
BigDecimal x0 = BigDecimal.valueOf(x00);
BigDecimal x1 = x0.add(f.f(x0)
.divide(df.df(x0), BigDecimal.ROUND_HALF_EVEN).negate());
while (x1.add(x0.negate()).abs().add(precE).compareTo(BigDecimal.ZERO) > 0) {
x0 = x1;
x1 = x0.add(f.f(x0).divide(df.df(x0), BigDecimal.ROUND_HALF_EVEN)
.negate());
}
return x1;
}
public interface F {
BigDecimal f(BigDecimal x);
}
public interface DF {
BigDecimal df(BigDecimal x);
}
当prec取100时,算出来2的平方根是:
1.4142135623730950488016887242096980785696718
753769480731766797379907324784621070388503875
343276415727350138462309122970249248360558507
372126441214970999358314132226659275055927557
999505011527820605714701095599716059702745345
968620147285174186408891986095523.
有了上面的探索,解决80题就不是难事了
另外,上述方法也适合求多项式的根,想知道更多,可以去翻《数值分析》
分享到:
相关推荐
基于openEuler20.03TLS版本编译openGauss源码时需要的软件包: 1. openeuler-lsb-5.0-1.oe2203.src.rpm 2. git-lfs-linux-arm64-v3.3.0.tar.gz 3. flex-2.5.39.tar.bz2
ProjectEuler题1-16题代码,直接引入Eclipse就可以用
GaussDB_100_1.0.1-DATABASE-EULER20SP8-64bit
计算流体力学基础中的二维欧拉方程求解程序
solution to problem 5
Project-Euler-Problem-8
华为欧拉系统 EulerOS-V2.0SP5-x86_64-dvd文件分割成 五个 压缩包,必须集齐 五个 文件后才能一起解压一起使用: EulerOS-V2.0SP5-x86_64-dvd.part5.rar ... EulerOS-V2.0SP5-x86_64-dvd.part4.rar ...
华为欧拉系统 EulerOS-V2.0SP5-x86_64-dvd文件分割成 五个 压缩包,必须集齐 五个 文件后才能一起解压一起使用: EulerOS-V2.0SP5-x86_64-dvd.part5.rar ... EulerOS-V2.0SP5-x86_64-dvd.part4.rar ...
二维欧拉
RKDG-Euler-third-order-2d-accurary.f90
1.openEuler操作系统入门 2.命令行操作基础 3.使用VIM编辑器 4.用户和权限管理 5.安装软件并管理服务 6.管理文件系统及存储 7.日常系统管理 8.使用shell脚本 9.openEuler管理员综合实践——文件共享服务器管理
项目-euler-问题-下载器 将所有 projecteuler.net 问题下载到您的本地存储
euler-search-components-源码.rar
Galerkin method,a numerical approximate method based on,commonly used in ordinary differential equation.
openEuler-22.03-LTS-SP2-x86_64-dvd.iso 适用于x86_64平台服务器,文件使用WinRAR分割成 4 个压缩包,必须一起下载使用: part1:https://download.csdn.net/download/weixin_43800734/88220955 part2:...
openEuler-20.03-LTS-SP2-x86_64-dvd支持鲲鹏及其它多种处理器,文件分割成 5个 压缩包,必须集齐5个 文件后才能一起解压一起使用: openEuler-20.03-LTS-SP2-x86_64-dvd.part5.rar ... openEuler-20.03-LTS-SP2-x86_...
Arduino-PyTeapot-Quaternion-Euler-cube-rotation.zip,从四元数或欧拉角数据可视化旋转立方体pyteapot四元数欧拉立方体旋转,Arduino是一家开源软硬件公司和制造商社区。Arduino始于21世纪初,深受电子制造商的欢迎...
华为欧拉系统 EulerOS-V2.0SP5-x86_64-dvd文件分割成 五个 压缩包,必须集齐 五个 文件后才能一起解压一起使用: EulerOS-V2.0SP5-x86_64-dvd.part5.rar ... EulerOS-V2.0SP5-x86_64-dvd.part4.rar ...
robogirl06-Euler_Solutions-archive-refs-heads-master.zip
华为欧拉系统 EulerOS-V2.0SP5-x86_64-dvd文件分割成 五个 压缩包,必须集齐 五个 文件后才能一起解压一起使用: EulerOS-V2.0SP5-x86_64-dvd.part5.rar ... EulerOS-V2.0SP5-x86_64-dvd.part4.rar ...