Java程序来计算嵌套的偏旁常量(Java programme to compute the nested radical constant)
使用下面的代码,我能够估计它: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的新手)并且不确定这是否适用于此。
谢谢你的帮助!
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'sBigDecimal
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
最满意答案
将输出传递给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 ...
-
Powershell脚本输出到变量 - 捕获写主机输出(Powershell Script output to variable - capture Write-Host output)[2023-10-10]
你不能。 该脚本使用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 ...
-
Powershell参数输出(Powershell parameter output)[2022-07-17]
调用函数时不要使用'()'而不要使用逗号','。 '(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设置变量。 ...
-
powershell写入输出错误与脚本中的多个对象?(powershell write-output bug with multiple objects in a script? [duplicate])[2022-03-01]
我认为这是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.
-
在powershell中写输出(Write output in powershell)[2023-06-09]
将输出传递给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 ...