Ludic numbers: Difference between revisions

3,657 bytes removed ,  7 years ago
Replaced PowerShell with simpler example
m (move a bullet item (a definition) below the stretch goal.)
(Replaced PowerShell with simpler example)
Line 2,152:
 
=={{header|PowerShell}}==
{{works with|PowerShell|2}}
<lang PowerShell>
# Start with a pool large enough to meet the requirements
function Get-Ludic
$Pool = [System.Collections.ArrayList]( 2..22000 )
{
[OutputType([PSObject])]
# Start with 1, because it's grandfathered in
Param
$Ludic = @( 1 ()
# Lower limit 13 (takes that many to make a triplet,) upper limit 1,000,000.
# While the size of the pool is still larger than the next Ludic number...
[Parameter(Mandatory=$false, Position=0)]
While ( $Pool.Count -gt $Pool[0] )
[ValidateRange(13,1000000)]
[int]
$Limit = 25000
)
 
# The 'cheaters' method of creating a blank PSObject
[PSObject]$ludic = "LUDICrous" | Select-Object -Property Numbers, Triplets
 
# Set 1 as the first element in the ludic number list.
[System.Collections.ArrayList]$ludicNumbers = @(1)
 
# Create a list of all remaining numbers within limit.
[System.Collections.ArrayList]$numbers = 2..$Limit
 
# Find the Ludic numbers.
for ($j = 2; $j -lt $Limit; $j++)
{
if ($numbers.Count)
{
$ludicNumbers.Add($numbers[0]) | Out-Null
}
else
{
break
}
 
$i = $numbers[0] - 1
$n = 0
 
while ($n -le $numbers.Count - 1)
{
$numbers.RemoveAt($n)
$n += $i
}
}
 
# Add the Ludic number array to the object.
$ludic.Numbers = $ludicNumbers
 
# Find the Ludic number triplets.
$2dArray = 'Set-Variable -Name ludicTriplets -Value '
$k = 0
 
while ($ludicNumbers[$k] + 6 -le $ludicNumbers.Count)
{
# Add the $nPlus2next =Ludic $ludicNumbers[$k]number +to 2the list
$nPlus6Ludic += $ludicNumbersPool[$k0] + 6
 
# Remove from the pool all entries whose index is a multiple of the next Ludic number
if ($ludicNumbers.IndexOf($nPlus2, 1) -gt 0 -and
[math]::Truncate( ( $Pool.Count - 1 )/ $Pool[0])..0 | ForEach { $Pool.RemoveAt( $_ * $Pool[0] ) }
$ludicNumbers.IndexOf($nPlus6, 1) -gt 0)
{
Set-Variable -Name "array$k" -Value @($ludicNumbers[$k],$nPlus2,$nPlus6)
$2dArray += "`$array$k,"
}
 
$k += 1
}
 
# Add the rest of the numbers in the pool to the list of Ludic numbers
Invoke-Expression ($2dArray.TrimEnd(","))
$Ludic += $Pool.ToArray()
 
# Add the Ludic triplet array to the object.
$ludic.Triplets = $ludicTriplets
 
return $ludic
}
</lang>
The variable $ludic contains an object with all Ludic numbers and triplets within limit range:
<lang PowerShell>
# Display the first 25 Ludic numbers
$ludic = Get-Ludic -Limit 25000
$Ludic[0..24] -join ", "
 
''
$ludic
# Display the count of all Ludic numbers under 1000
$Ludic.Where{ $_ -le 1000 }.Count
''
# Display the 2000th through the 2005th Ludic number
$Ludic[1999..2004] -join ", "
''
# Display all Ludic triplets less than 250
$TripletStart = $Ludic.Where{ $_ -lt 244 -and ( $_ + 2 ) -in $Ludic -and ( $_ + 6 ) -in $Ludic }
$TripletStart.ForEach{ $_, ( $_ + 2 ), ( $_ + 6 ) -join ", " }
</lang>
{{Outout}}
<pre>
1, 2, 3, 5, 7, 11, 13, 17, 23, 25, 29, 37, 41, 43, 47, 53, 61, 67, 71, 77, 83, 89, 91, 97, 107
 
142
Numbers Triplets
 
------- --------
21475, 21481, 21487, 21493, 21503, 21511
{1, 2, 3, 5...} {1 3 7, 5 7 11, 11 13 17, 23 25 29...}
</pre>
<lang PowerShell>
Write-Host "First 25 Ludic numbers : $($ludic.Numbers[0..24] -join ', ')"
Write-Host "Total Ludic numbers <= 1000 : $(($ludic.Numbers | Where-Object {$_ -le 1000}).Count)"
Write-Host "Ludic numbers 2000 to 2005 : $($ludic.Numbers[2000..2005] -join ', ')"
Write-Host "Ludic triplets below 250 :" -NoNewline; for ($i = 0; $i -lt $ludic.Triplets.Count; $i++)
{
if ($ludic.Triplets[$i][2] -lt 250) { $n = $i }
}
$ludic.Triplets[0..$n] |
Format-Wide {$_.ToString().PadLeft(3)} -Column 3 -Force
</lang>
{{Out}}
<pre>
First 25 Ludic numbers : 1, 2, 3, 5, 7, 11, 13, 17, 23, 25, 29, 37, 41, 43, 47, 53, 61, 67, 71, 77, 83, 89, 91, 97, 107
Total Ludic numbers <= 1000 : 142
Ludic numbers 2000 to 2005 : 21481, 21487, 21493, 21503, 21511, 21523
Ludic triplets below 250 :
 
1, 3, 7
1 3 7
5, 7, 11
5 7 11
11, 13, 17
11 13 17
23, 25, 29
23 25 29
41, 43, 47
41 43 47
173, 175, 179
173 175 179
221, 223, 227
221 223 227
233, 235, 239
233 235 239
</pre>