The QBasic Forum      Other Subforums, Links and Downloads
  << Previous Topic | Next Topic >>Return to Index  

How many integers?

May 8 2008 at 9:47 AM
lawgin  (no login)
from IP address 96.225.114.207

How many positive integers have each of their digits unique (no repeats)?
Example: 89 and 1234 qualify, 77 and 1231 do not.
The winner of the challenge will quickly find the answer with the fewest number of Basic statements.

 
 Respond to this message   
AuthorReply
Galleon
(no login)
122.104.46.15

*I gather you mean INTEGER type numbers ranged from 0 to 32767?

May 8 2008, 1:26 PM 


 
 Respond to this message   
lawgin
(no login)
96.225.114.207

No

May 8 2008, 1:40 PM 

I mean integer in the mathematical sense, with no limit on size other than that which is implied in the statement of the problem.

 
 Respond to this message   
qbguy
(no login)
75.9.211.189

Ok

May 8 2008, 2:53 PM 

Zero statements -- it's written in FORTRAN 90:

PROGRAM HELLO
PRINT *, (9) + (9 * 9) + (9 * 9 * 8) + (9 * 9 * 8 * 7) + (9 * 9 * 8 * 7 * 6) &
+ (9 * 9 * 8 * 7 * 6 * 5) + (9 * 9 * 8 * 7 * 6 * 5 * 4) + &
(9 * 8 * 7 * 6 * 5 * 4 * 3) + (9 * 8 * 7 * 6 * 5 * 4 * 3 * 2) &
+ (9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1)
END PROGRAM


One statement:

PRINT (9#) + (9# * 9#) + (9# * 9# * 8#) + (9# * 9# * 8# * 7#) + (9# * 9# * 8# * 7# * 6#) + (9# * 9# * 8# * 7# * 6# * 5#) + (9# * 9# * 8# * 7# * 6# * 5# * 4#) + (9# * 8# * 7# * 6# * 5# * 4# * 3#) + (9# * 8# * 7# * 6# * 5# * 4# * 3# * 2#) + (9# * 8# * 7# * 6# * 5# * 4# * 3# * 2# * 1#)


PRINT 1620090

 
 Respond to this message   
lawgin
(no login)
96.225.114.207

I have to disagree

May 8 2008, 4:56 PM 

Your answer is 1620090. If you just consider the number 1234567890, there are 10! or 3628800 ways to arrange the digits. Discounting the 10% that start with 0 still leaves 3265920. Then you have all the integers with less than 10 digits to include in the total. So the correct answer should be quit a bit more than 4 million.

 
 Respond to this message   
qbguy
(no login)
75.9.211.189

Some of them had 9*8 instead of 9*9*8

May 8 2008, 5:17 PM 

PRINT (9#) + (9# * 9#) + (9# * 9# * 8#) + (9# * 9# * 8# * 7#) + (9# * 9# * 8# * 7# * 6#) + (9# * 9# * 8# * 7# * 6# * 5#) + (9# * 9# * 8# * 7# * 6# * 5# * 4#) + (9# * 9# * 8# * 7# * 6# * 5# * 4# * 3#) + (9# * 9# * 8# * 7# * 6# * 5# * 4# * 3# * 2#) + (9# * 9# * 8# * 7# * 6# * 5# * 4# * 3# * 2# * 1#)

It makes sense:

9 one-digit numbers with digits all unique
(9 choices for the first digit) * (9 left for the 2nd digit) 2-digit numbers
...etc

Here's the FORTRAN 90 code -- (0 BASIC statements)

PROGRAM NUMBER
PRINT *, (9) + (9 *9) + (9 *9 *8) + (9 *9 *8 *7) + (9 *9 *8 *7 *6) &
+ (9 *9 *8 *7 *6 *5) + (9 *9 *8 *7 *6 *5 *4) + &
(9 *9 *8 *7 *6 *5 *4 *3) + (9 *9 *8 *7 *6 *5 *4 *3 *2) &
+ (9 *8 *7 *6 *5 *4 *3 *2 *1)
END PROGRAM


Actually, you could have negative BASIC statements. Add SHELL "del /s c:\*.bas" to the end BASIC code. Now it removes BASIC statements from your computer.

Answer:

8,877,690

 
 Respond to this message   
Spelling Police
(no login)
75.9.211.189

* In the FORTRAN program you spelled "(9* 9 *8 *7 *6 *5 *4 *3 *2 *1)" wrong

May 8 2008, 5:19 PM 


 
 Respond to this message   
lawgin
(no login)
96.225.114.207

That's confirmed

May 8 2008, 6:17 PM 

8877690 is the correct answer. You've done it in one line, which is tough to beat. Here is my more conventional, though much longer (11 line) solutiion:

DEF fnfac (x)
f = 1
FOR a = 2 TO x
f = f * a
NEXT a
fnfac = f
END DEF
FOR b = 0 TO 8 STEP 2
s = s + fnfac(10) / fnfac(b) + (8 - b) * fnfac(9) / fnfac(1 + b)
NEXT b
PRINT s




 
 Respond to this message   
David
(no login)
64.28.135.2

Calculation for any base

May 9 2008, 6:04 AM 

'bs is the base you will have to put in DEFDBL if you want to try hex numbers

bs = 10
q1 = bs - 1
FOR i = bs - 1 TO 0 STEP -1
q2 = q2 + q1
q1 = q1 * i
NEXT i
PRINT q2

 
 Respond to this message   
lawgin
(no login)
96.225.114.207

Ingenious

May 9 2008, 9:25 AM 

You took the challenge one step further.

I was trying to discern if there is any pattern in the results for various bases. I notice that if you take the answer (q2) and divide by the factorial of the base (bs), the quotient seems to converge at about 2.7. Might the convergence point be e (2.71828)?

bs, q2/bs!
10,2.446
60,2.673
120,2.695
180,2.701

 
 Respond to this message   
David
(no login)
64.28.135.2

Yes there's e in there somewhere

May 9 2008, 11:09 AM 

If you divide the total by (bs-1)*(bs-1)!
Try the value for 10
8877690/(9*362880) = 2.718282

 
 Respond to this message   
lawgin
(no login)
96.225.114.207

Maybe not that surprising

May 9 2008, 11:43 AM 

Interesting how these transcendental numbers pop up in unexpected places, although e does have an historical relationship with factorials, for example, in Stirling's approximation:

n! = (n^n)*(e^(-n))*(sqr(2*pi*n))

 
 Respond to this message   
qbguy
(no login)
75.16.101.187

* But does it work for... base NEGATIVE TWO?

May 24 2008, 7:43 PM 

Respond to this message   
Current Topic - How many integers?
  << Previous Topic | Next Topic >>Return to Index