0x0000AF56
0x0000AF56

Reputation: 71

Manipulating big number in Powershell

I developed a small script to simulate the Syracuse conjecture in Powershell.

I'm using it with very large number and i'm studying the result in an Excel graph but powershell keeps formatting my number when they are too big :

This is the result i get for the firsts iterations

1.60827282342995E+40 
8.04136411714975E+39 
4.02068205857487E+39 
2.01034102928744E+39 
1.00517051464372E+39 
5.02585257321859E+38 

I would like to have the result without the formatting "E+XX", is there any way to log the entire number to analyse its composition ?

EDIT : The script i wrote :

Remove-Item "E:\syracuse.txt"

$Logfile = "E:\syracuse.txt"
Function LogWrite
{
Param ([string]$logstring)

Add-content $Logfile -value $logstring
}

$chiffre1 = 0
$chiffre2 = 0


$chiffre1 = read-host "chiffre"

write-host Sequence Initiale $chiffre1

$val = 0

while ($val -ne 32132135464664546546545645656454665412321321231321654657987465432132154)

{


$val++

if ([bool]!($chiffre1%2))

{
   Write-Host "Pair"
   $chiffre2=($chiffre1)/2 
   write-host $chiffre2
   LogWrite $chiffre2,$val

}
Else
{
   Write-Host "Impair"
   $chiffre2 = $chiffre1*3+1
   write-host $chiffre2
   LogWrite $chiffre2,$val

}

if ([bool]!($chiffre2%2))

{
   Write-Host "Pair"
   $chiffre1=($chiffre2)/2 
   write-host $chiffre1
   LogWrite $chiffre1,$val
}
Else
{
   Write-Host "Impair"
   $chiffre1 = $chiffre2*3+1
   write-host $chiffre1
   LogWrite $chiffre1,$val

}

}

$val has an arbitrary value

I have Powershell 2.0 only, i'm updating to Powershell v3 right now.

Edit2 : It's very weird now. In the ISE, the script doesn't work, the number is an odd everytime.

I think i managed to find a little solution with the help of bigInt For some reason it doesn't work on ISE but it works on the Powershell v3 cmd.

I modified my LogWrite line for :

LogWrite ([bigint]$chiffre1),$val

or

LogWrite ([bigint]$chiffre2),$val

My log now has the good formatting !

Upvotes: 7

Views: 8408

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174825

In PowerShell 3.0+ you can use the System.Numerics.BigInteger data type to represent arbitrarily large integer values:

PS C:\> 1e15
1E+15
PS C:\> 1e15 -as [System.Numerics.BigInteger]
1000000000000000

There's even a builtin type accelerator for it ([bigint]):

PS C:\> [bigint]1e15
1000000000000000

Upvotes: 8

Related Questions