LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 863
Default Creating a Macro

Hi, Dana:

I thought it was interesting, too. I knew my code wasn't very efficient, but I
didn't spend any time trying to optimize it. Your code looks interesting.
Thanks for posting it.

Myrna Larson


On Tue, 16 Nov 2004 00:49:15 -0500, "Dana DeLouis"
wrote:

Hi Myrna. I had a old code for this using the 'Collection' object, but I
find that your code is twice as fast on a larger set of numbers. So,
thanks!
I found this interesting, so I played around with it a little. On a large
set N, suppose one is skipping every 100th number. When the remaining
numbers to be found is say 2, the program scans all N numbers 50 times (or
50*2*N) just to get k = 100. I modified your excellent idea to include a
Mod function so that the main loop makes at most 1 pass on all N for each
increment. It looks like this decreases the number of loops by quite a bit
when working with a large set.
In certain math programs, they call this the "Josephus" problem (A
Permutation being the sequence of elimination when every nth member is
"eliminated." One wishes to be the "last man standing"). I've named it by
the same function name.

Sub TestIt()
Dim n As Long
Dim p As Long
Dim v

n = 20: p = 7
v = Josephus(n, p)
Range("A1").Resize(1, n) = v
End Sub

Function Josephus(Num As Long, Nth As Long)

Dim i As Long 'Loop pointer
Dim c As Long 'Counter 1 - n
Dim Remain As Long '# Remaining
Dim n As Long 'Modified Nth
Dim x As Variant 'Input Array
Dim y As Variant 'Output Array


ReDim x(1 To Num)
ReDim y(1 To Num)

For i = 1 To Num
x(i) = i
Next i

Remain = Num
i = 0

Do While Remain 0
n = Nth Mod Remain
If n = 0 Then n = Remain

c = 0
Do Until c = n
i = i + 1
If i Num Then i = 1
c = c + Sgn(x(i))
Loop

y(Num - Remain + 1) = x(i)
x(i) = 0
Remain = Remain - 1
Loop
Josephus = y
'// Note: Last Number or Person "Picked" is # : y(num)
End Function

Thanks for the code ideas. :)


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a New Macro SharonJo Excel Discussion (Misc queries) 1 April 4th 08 01:27 AM
Macro Creating Duster142 New Users to Excel 2 October 7th 05 01:50 AM
Need Help Creating A Macro LJ Owen Excel Worksheet Functions 1 March 2nd 05 01:52 PM
Creating a Macro Tami[_2_] Excel Programming 3 June 11th 04 05:24 PM
Help Creating a Macro milton Excel Programming 3 October 21st 03 09:04 PM


All times are GMT +1. The time now is 09:57 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"