首页 \ 问答 \ Java程序来计算嵌套的偏旁常量(Java programme to compute the nested radical constant)

Java程序来计算嵌套的偏旁常量(Java programme to compute the nested radical constant)

[来自MathWorld的图片,WolframAlpha

使用下面的代码,我能够估计它:1.7579327566180045

public class Sum {
static int count = 0;
static double a = 0;
public static void main(String[] args) {
    int x = 0;
    System.out.println(sum(x));
}

public static double sum(int x){
    count++;
    x++;
    if (count == 11000){
        return a;
    }
    return a = Math.sqrt(x+sum(x));
}

然而,我无法通过递归获得更准确的答案。 递归调用数从11,000增加给我一个StackOverflowError 。 我也查了解如何在这里使用java的BigDecimal ,但是我无法在这里实现它,老实说不确定这是否有帮助。

问题 : - 我怎样才能让这个程序计算更多的小数位数更准确的常量? 我不确定这是否可以迭代计算或不计算。 我无法计算出iterative定义。

另外,我也研究了Java 8 Streams,但那些对我来说没有多大意义(因为我是编程和Java的新手)并且不确定这是否适用于此。

谢谢你的帮助!


[Image from MathWorld, WolframAlpha

Using the code below I was able to estimate it upto: 1.7579327566180045

public class Sum {
static int count = 0;
static double a = 0;
public static void main(String[] args) {
    int x = 0;
    System.out.println(sum(x));
}

public static double sum(int x){
    count++;
    x++;
    if (count == 11000){
        return a;
    }
    return a = Math.sqrt(x+sum(x));
}

I am however not able to get an answer with more accuracy with the recursion here. Increasing the number of recursive calls from 11,000 gives me a StackOverflowError. I also looked up how I could use the java's BigDecimal here, but I'm unable to implement it here and honestly not sure if that would help.

Question :- how can I make this programme compute the constant to more decimal places and more accurately? I'm not certain if this I can compute it iteratively or not either. I wasn't able to work out an iterative definition.

In addition, I looked into Java 8 Streams too, but those didn't make much sense to me (since i'm new to programming and Java) and not sure if that is applicable here.

Thanks for any help!


原文:https://stackoverflow.com/questions/37084465
更新时间:2024-03-20 08:03

最满意答案

将输出传递给SELECT语句以获取名称,路径和大小,然后传递给Format-Table:

$agh = get-childitem E:\Documents\ -recurse | select Name, Directory, length | format-table

Pipe the output to a SELECT statement to get the name, path, and size, and then to Format-Table:

$agh = get-childitem E:\Documents\ -recurse | select Name, Directory, length | format-table

相关问答

更多
  • 简而言之, Write-Host写入控制台本身。 把它想象成VBScript中的MsgBox。 另一方面,写Write-Output写入流水线,所以下一个命令可以接受它作为输入。 您不需要使用Write-Output来写入对象,因为Write-Output会隐式地为您调用。 PS> Get-Service 将与以下内容相同: PS> Get-Service | Write-Output In a nutshell, Write-Host writes to the console itself. Thi ...
  • 简单地输出东西是PowerShell是一件美丽的事情,也是其最大的优势。 例如,常见的Hello,World! 应用程序减少到一行: "Hello, World!" 它创建一个字符串对象,分配上述值,并且是命令管道上最后一个调用.toString()方法的项,并将结果输出到STDOUT (默认情况下)。 一件美丽的事情 其他Write-*命令特定于将文本输出到其关联的流,并具有它们的位置。 Simply outputting something is PowerShell is a thing of be ...
  • start -FilePath powershell.exe -ArgumentList "-nologo -noninteractive -command {Get-Process}" 会做你要求的,但我有一种可能不是你想要的感觉。 start -FilePath powershell.exe -ArgumentList "-nologo -noninteractive -command {Get-Process}" Will do what you asked for but I have a fe ...
  • 你不能。 该脚本使用Write-Host输出错误。 Write-Host只将文本写入控制台,它不返回任何对象,这意味着没有任何东西可以捕获。 我建议修改脚本以使用其他cmdlet,如Write-Error , Write-Output或任何其他Write-*输出到流(您可以重定向到stdout-stream并保存)。 见http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/ You can't. The script use ...
  • 您可能想要枚举 Get-ChildItem cmdlet的返回值: Get-ChildItem $BetterNet | ForEach-Object { write-host BetterNet found on: Write-Host " "$_ write-host } You probably want to enumerate the return value of the Get-ChildItem cmdlet: Get-ChildItem $Bette ...
  • 调用函数时不要使用'()'而不要使用逗号','。 '(stuff here)'是新代码或数据的标记,应在请求之前收集或一起运行。 正确的方法是.... Get-Something $VariableHere $VariableHere2 要么 Get-Something -VariableHere $VariableHere -VariableHere2 $VariableHere2 when calling the function dont use '()' and dont use commas ...
  • @Mathias R. Jessen提出解决方案,当模块中没有定义时,解决方案可以很好地工作。 但不是,如果你把它放在模块中。 所以,我提供了另一种解决方案,它可以在模块中使用。 在高级函数中(使用[CmdletBinding()]属性的函数),您可以使用$PSCmdlet.SessionState来引用调用者的SessionState 。 因此,您可以使用$PSCmdlet.SessionState.PSVariable.Set('Name' ,'Value')在调用者的SessionState设置变量。 ...
  • 我认为这是Write-Output cmdlet的预期行为。 它正在向管道中发送对象(如果它是管道中的最后一个cmdlet,它将显示给主机。)预计管道中的所有对象都具有相同的属性。 在您的示例中,您有一个具有属性data1的对象和一个具有data2的第二个对象。 如果您使两个对象都使用相同的属性,它将按预期工作: $var1 = New-Object -TypeName PSObject -Prop @{'data1'=6} write-output $var1 $var2 = New-Object -Ty ...
  • 我会提到Start-Transcript作为一种很好的方式。 您还可以使用Get-History来查找您输入的命令。 I was going to mention Start-Transcript as a great way to do it. You can also make use of Get-History to find out what commands you entered.
  • 将输出传递给SELECT语句以获取名称,路径和大小,然后传递给Format-Table: $agh = get-childitem E:\Documents\ -recurse | select Name, Directory, length | format-table Pipe the output to a SELECT statement to get the name, path, and size, and then to Format-Table: $agh = get-childitem ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)