Upside-down numbers: Difference between revisions

no edit summary
(Applesoft BASIC)
No edit summary
 
(13 intermediate revisions by 7 users not shown)
Line 155:
=={{header|Basic}}==
==={{header|Applesoft BASIC}}===
 
Beyond 9 digits, rounding errors occur.
<syntaxhighlight lang="basic"> 1 PRINT "THE FIRST 50 UPSIDE DOWN NUMBERS:": FOR I = 1 TO 50: GOSUB 4: NEXT I: PRINT
2 FOR J = 2 TO 10::I = INT (5 * 10 ^ J): PRINT CHR$ (13)I"TH: ";: GOSUB 4: NEXT J
3 END
4 GOSUB 5: PRINT O$;: RETURN
5 IF I > = 1E9 THEN PRINT "?OVERFLOW" + CHR$ (7): END
6 L$ = "":R$ = "":S = I - 1:F = 1:I$(1) = "5": FOR E = 0 TO 1E38: FOR O = F TO 1:R = S:S = S - INT (9 ^ E):F = 0: IF S > = 0 THEN NEXT O,E
7 IF E THEN R = R + .5: FOR D = E - 1 TO 0 STEP - 1:N = INT (R / 9 ^ D):L$ = L$ + STR$ (N + 1):R$ = STR$ (9 - N) + R$:R = R - N * INT (9 ^ D): NEXT D
8 O$ = L$ + I$(O) + R$ + " "
9 RETURN</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>THE FIRST 50 UPSIDE DOWN NUMBERS:
5 19 28 37 46 55 64 73 82 91 159 258 357 456 555 654 753 852 951 1199 1289 1379 1469 1559 1649 1739 1829 1919 110092198 2288 2378 2468 2558 2648 2738 2828 2918 210083197 3287 3377 3467 3557 3647 3737 3827 3917 310074196 4286 4376 4466
 
500TH: 494616
Line 176 ⟶ 178:
50000000TH: 9285587463255281
500000000TH: 1436368345672474769
5E+09TH: ?OVERFLOW</pre>
</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">
function is_ups( n as uinteger ) as boolean
dim as string m = str(n)
dim as uinteger lm = len(m), i
for i = 1 to int(lm/2.0+0.5)
if val(mid(m,i,1)) + val(mid(m,lm-i+1,1)) <> 10 then return false
next i
return true
end function
 
dim as uinteger count, n=0
 
while count<5000001
if is_ups(n) then
count = count + 1
if count < 51 then
print n,
if count mod 5 = 0 then print
end if
if count = 500 or count = 5000 then
print n
end if
end if
n = n + 1
wend
</syntaxhighlight>
<pre>
5 19 28 37 46
55 64 73 82 91
159 258 357 456 555
654 753 852 951 1199
1289 1379 1469 1559 1649
1739 1829 1919 2198 2288
2378 2468 2558 2648 2738
2828 2918 3197 3287 3377
3467 3557 3647 3737 3827
3917 4196 4286 4376 4466
494616
56546545
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="C++">#include <iostream>
#include <vector>
#include <algorithm>
 
bool isUpsideDown( int n ) {
std::vector<int> digits ;
while ( n != 0 ) {
digits.push_back( n % 10 ) ;
n /= 10 ;
}
if ( std::find ( digits.begin( ) , digits.end( ) , 0 ) != digits.end( ) )
return false ;
int forward = 0 ;
int backward = digits.size( ) - 1 ;
while ( forward <= backward ) {
if ( digits[forward] + digits[backward] != 10 )
return false ;
forward++ ;
if ( backward > 0 ) {
backward-- ;
}
}
return true ;
}
 
int main( ) {
int current = 0 ;
int sum = 0 ;
std::vector<int> solution ;
while ( sum != 5000 ) {
current++ ;
if ( isUpsideDown( current ) ) {
solution.push_back( current ) ;
sum++ ;
}
}
std::cout << "The first 50 upside-down numbers:\n" ;
std::cout << "(" ;
for ( int i = 0 ; i < 50 ; i++ )
std::cout << solution[ i ] << ' ' ;
std::cout << ")\n" ;
std::cout << "The five hundredth such number: " << solution[499] << '\n' ;
std::cout << "The five thousandth such number: " << solution[4999] << '\n' ;
return 0 ;
}</syntaxhighlight>
{{out}}
<pre>
The first 50 upside-down numbers:
(5 19 28 37 46 55 64 73 82 91 159 258 357 456 555 654 753 852 951 1199 1289 1379 1469 1559 1649 1739 1829 1919 2198 2288 2378 2468 2558 2648 2738 2828 2918 3197 3287 3377 3467 3557 3647 3737 3827 3917 4196 4286 4376 4466 )
The five hundredth such number: 494616
The five thousandth such number: 56546545
</pre>
 
=={{header|Go}}==
{{trans|Python}}
Line 362 ⟶ 462:
</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang="Haskell">import Data.Char ( digitToInt )
import Data.List ( unfoldr , (!!) )
 
findLimits :: (Int , Int) -> [(Int , Int)]
findLimits (st , end ) = unfoldr(\(x , y ) -> if x > y then Nothing else
Just ((x , y ) , (x + 1 , y - 1 ))) (st , end )
 
isUpsideDown :: Int -> Bool
isUpsideDown n
|elem '0' str = False
|otherwise = all (\(a , b ) -> digitToInt( str !! a ) + digitToInt ( str !!
b ) == 10 ) $ findLimits ( 0 , length str - 1 )
where
str = show n
 
main :: IO ( )
main = do
let upsideDowns = take 5000 $ filter isUpsideDown [1..]
putStrLn "The first fifty upside-down numbers!"
print $ take 50 upsideDowns
putStr "The five hundredth such number : "
print $ upsideDowns !! 499
putStr "The five thousandth such number : "
print $ last upsideDowns</syntaxhighlight>
{{out}}
<pre>
The first fifty upside-down numbers!
[5,19,28,37,46,55,64,73,82,91,159,258,357,456,555,654,753,852,951,1199,1289,1379,1469,1559,1649,1739,1829,1919,2198,2288,2378,2468,2558,2648,2738,2828,2918,3197,3287,3377,3467,3557,3647,3737,3827,3917,4196,4286,4376,4466]
The five hundredth such number : 494616
The five thousandth such number : 56546545
</pre>
 
=={{header|jq}}==
Line 509 ⟶ 641:
Five millionth: 82,485,246,852,682
</pre>
 
=={{header|Nim}}==
{{trans|Python}}
<syntaxhighlight lang="Nim">import std/[math, strformat, strutils, sugar]
 
iterator upsideDownNumbers(): (int, int) =
## Generate upside-down numbers (OEIS A299539).
const
Wrappings = [(1, 9), (2, 8), (3, 7), (4, 6),
(5, 5), (6, 4), (7, 3), (8, 2), (9, 1)]
var
evens = @[19, 28, 37, 46, 55, 64, 73, 82, 91]
odds = @[5]
oddIndex, evenIndex = 0
ndigits = 1
count = 0
 
while true:
if ndigits mod 2 == 1:
if odds.len > oddIndex:
inc count
yield (count, odds[oddIndex])
inc oddIndex
else:
# Build next odds, but switch to evens.
odds = collect:
for (hi, lo) in Wrappings:
for i in odds:
hi * 10^(ndigits + 1) + 10 * i + lo
inc ndigits
oddIndex = 0
else:
if evens.len > evenIndex:
inc count
yield (count, evens[evenIndex])
inc evenIndex
else:
# Build next evens, but switch to odds.
evens = collect:
for (hi, lo) in Wrappings:
for i in evens:
hi * 10^(ndigits + 1) + 10 * i + lo
inc ndigits
evenIndex = 0
 
echo "First fifty upside-downs:"
for (udcount, udnumber) in upsideDownNumbers():
if udcount <= 50:
stdout.write &"{udnumber:5}"
if udcount mod 10 == 0: echo()
elif udcount == 500:
echo &"\nFive hundredth: {insertSep($udnumber)}"
elif udcount == 5_000:
echo &"Five thousandth: {insertSep($udnumber)}"
elif udcount == 50_000:
echo &"Fifty thousandth: {insertSep($udnumber)}"
elif udcount == 500_000:
echo &"Five hundred thousandth: {insertSep($udnumber):}"
elif udcount == 5_000_000:
echo &"Five millionth: {insertSep($udnumber):}"
break
</syntaxhighlight>
 
{{out}}
<pre>First fifty upside-downs:
5 19 28 37 46 55 64 73 82 91
159 258 357 456 555 654 753 852 951 1199
1289 1379 1469 1559 1649 1739 1829 1919 2198 2288
2378 2468 2558 2648 2738 2828 2918 3197 3287 3377
3467 3557 3647 3737 3827 3917 4196 4286 4376 4466
 
Five hundredth: 494_616
Five thousandth: 56_546_545
Fifty thousandth: 6_441_469_664
Five hundred thousandth: 729_664_644_183
Five millionth: 82_485_246_852_682</pre>
 
=={{header|Pascal}}==
Line 957 ⟶ 1,165:
Five millionth: 82,485,246,852,682
</pre>
 
=={{header|Quackery}}==
 
(Load extensions for the faster merge sort.)
 
'''Method'''
 
Start with generation zero as the empty string and "5". Make the next generation by expanding the previous generation (<code>expand</code>) i.e. wrapping each of the strings in "1" and "9", "2" and "8" … "8" and "2", "9" and "1". Accumulate the generations. The accumulator starts with "5", after one iteration it has "5", "19", "28" … "82", "91", "159", "258" … "852", "951".
 
Note that this does not produce the numbers in numerical order. It's close to numerical order but not quite there.
So once sufficient numbers have been produced to guarantee that all the numbers in the required range have been calculated, convert them from string format to numerical format and sort, then truncate the list of upside down numbers to the required length.
 
<code>necessary</code> computes a safe number of items to include in the list to guarantee that none are missing from the truncated list - i.e. the length of the list after each iteration of <code>expand</code>. It is <code>(9^n - 5) / 4</code>, where <code>n</code> is the number of iterations. ([https://oeis.org/A211866 OEIS A211866])
 
<code>^</code> in <code>necessary</code> is bitwise XOR, not exponentiation.
 
<syntaxhighlight lang="Quackery"> [ 0
[ 2dup > while
1 ^ 9 * 1+ again ]
nip ] is necessary ( n --> n )
 
[ [] swap
witheach
[ ' [ [ char 1 char 9 ]
[ char 2 char 8 ]
[ char 3 char 7 ]
[ char 4 char 6 ]
[ char 5 char 5 ]
[ char 6 char 4 ]
[ char 7 char 3 ]
[ char 8 char 2 ]
[ char 9 char 1 ] ]
witheach
[ over dip do
dip swap join
swap join nested
rot swap join
swap ]
drop ] ] is expand ( [ --> [ )
 
[ dup necessary temp put
' [ [ char 5 ] ]
' [ [ ] [ char 5 ] ]
[ expand tuck join swap
over size
temp share < not
until ]
drop
temp release
[] swap
witheach
[ $->n drop join ]
sort
swap split drop ] is upsidedowns ( n --> [ )
 
5000 upsidedowns
say "First 50 upside down numbers:"
dup 50 split drop
[] swap witheach
[ number$ nested join ]
45 wrap$
cr cr
say "500th upside down number: "
dup 499 peek echo
cr
say "5000th upside down number: "
4999 peek echo</syntaxhighlight>
 
{{out}}
 
<pre>First 50 upside down numbers:
5 19 28 37 46 55 64 73 82 91 159 258 357 456
555 654 753 852 951 1199 1289 1379 1469 1559
1649 1739 1829 1919 2198 2288 2378 2468 2558
2648 2738 2828 2918 3197 3287 3377 3467 3557
3647 3737 3827 3917 4196 4286 4376 4466
 
500th upside down number: 494616
5000th upside down number: 56546545</pre>
 
=={{header|Raku}}==
Line 1,059 ⟶ 1,346:
2: "82485246852682"
1: "269222738456273888148"
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">DIGITS =(1..9).to_a
 
updowns = Enumerator.new do |y|
y << 5
(1..).each do |s|
perms = DIGITS.repeated_permutation(s)
perms.each{|perm| y << (perm + perm.reverse.map{|n| 10-n}).join.to_i }
perms.each{|perm| y << (perm + [5] + perm.reverse.map{|n| 10-n}).join.to_i }
end
end
 
res = updowns.take(5000000)
res.first(50).each_slice(10){|slice| puts "%6d"*slice.size % slice}
 
puts
n = 500
5.times do
puts "%8d: %-10d" % [n, res[n-1]]
n *= 10
end
</syntaxhighlight>
{{out}}
<pre> 5 19 28 37 46 55 64 73 82 91
159 258 357 456 555 654 753 852 951 1199
1289 1379 1469 1559 1649 1739 1829 1919 2198 2288
2378 2468 2558 2648 2738 2828 2918 3197 3287 3377
3467 3557 3647 3737 3827 3917 4196 4286 4376 4466
 
500th : 494616
5000th : 56546545
50000th : 6441469664
500000th : 729664644183
5000000th : 82485246852682
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="Rust">fn is_upside_down( num : u32 ) -> bool {
let numberstring : String = num.to_string( ) ;
let len = numberstring.len( ) ;
let numberstr = numberstring.as_str( ) ;
if numberstr.contains( "0" ) {
return false ;
}
if len % 2 == 1 && numberstr.chars( ).nth( len / 2 ).unwrap( ) != '5' {
return false ;
}
let mut forward : usize = 0 ;
let mut backward : usize = len - 1 ;
while forward <= backward {
let first = numberstr.chars( ).nth( forward ).expect("No digit!").
to_digit( 10 ).unwrap( ) ;
let second = numberstr.chars( ).nth( backward ).expect("No digit!").
to_digit( 10 ).unwrap( ) ;
if first + second != 10 {
return false ;
}
forward += 1 ;
if backward != 0 {
backward -= 1 ;
}
}
true
}
 
fn main() {
let mut solution : Vec<u32> = Vec::new( ) ;
let mut sum : u32 = 0 ;
let mut current : u32 = 0 ;
while sum < 50 {
current += 1 ;
if is_upside_down( current ) {
solution.push( current ) ;
sum += 1 ;
}
}
let five_hundr : u32 ;
while sum != 500 {
current += 1 ;
if is_upside_down( current ) {
sum += 1 ;
}
}
five_hundr = current ;
let five_thous : u32 ;
while sum != 5000 {
current += 1 ;
if is_upside_down( current ) {
sum += 1 ;
}
}
five_thous = current ;
println!("The first 50 upside-down numbers:") ;
println!("{:?}" , solution ) ;
println!("The five hundredth such number : {}" , five_hundr) ;
println!("The five thousandth such number : {}" , five_thous ) ;
}</syntaxhighlight>
{{out}}
<pre>
The first 50 upside-down numbers:
[5, 19, 28, 37, 46, 55, 64, 73, 82, 91, 159, 258, 357, 456, 555, 654, 753, 852, 951, 1199, 1289, 1379, 1469, 1559, 1649, 1739, 1829, 1919, 2198, 2288, 2378, 2468, 2558, 2648, 2738, 2828, 2918, 3197, 3287, 3377, 3467, 3557, 3647, 3737, 3827, 3917, 4196, 4286, 4376, 4466]
The five hundredth such number : 494616
The five thousandth such number : 56546545
</pre>
 
Line 1,064 ⟶ 1,456:
{{trans|Python}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var genUpsideDown = Fiber.new {
258

edits