Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 20th, 2003, 04:36 PM
DansZs DansZs is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 3 DansZs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile Division Algorithm

Hi all! I'm new to the board, but I browsed it some today and this place looks great! On of the reasons I hit on it was because I was looking for help on a homework problem I have for my Algorithms class and figured this would be the place to find it!

Basically, I have to design a Division Algorithm DIVIDE(n, m) (n and m being positive integers) using only addition, multiplication, and subtraction and the running time for the algorithm must be better than Big Theta(n/m). If the numerator won't divide evenly, the algorithm rounds down.

I've already had this problem once in class on my last test, but it was a bonus problem, and although my algorithm was correct, it ran in Big Theta(n/m) time, so I only received 1 out of the 3 bonus points on the test. Now it's on my homework assignment and I'd like to finally beat this thing!

Thanks in advance for your help!

Reply With Quote
  #2  
Old February 20th, 2003, 05:11 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
posted twice for some reason...
__________________
hmmm...

Last edited by andy3109 : February 20th, 2003 at 05:42 PM.

Reply With Quote
  #3  
Old February 20th, 2003, 05:12 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
ou tof curiosity, what was your answer on the bonus?

Reply With Quote
  #4  
Old February 21st, 2003, 04:47 AM
tony825 tony825 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: Greece
Posts: 63 tony825 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
How can you use "some" digits of number n to make the division?

1236 | 6
.........|__
.........|
.........|
12/6 =2
0
36/6 =6

thats the easy part;
but how the heck can someone "find" the 12 w/o dividing it first with 10 (123) then with 100 (12) or backwards firts with 1000 (1) then with 100 (12) ?

i have i clue in using data-structs like queues but this will make the running time toooooooooo big.

out.............................. in
..|..................................|
.V.................................V
|1| --> |2| --> |3| --> |6| --> NULL

=1236

btw : had to edit cause the blank spaces were deleted so i changed them with dots
__________________
No sign

Last edited by tony825 : February 21st, 2003 at 04:52 AM.

Reply With Quote
  #5  
Old February 21st, 2003, 07:30 AM
DansZs DansZs is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 3 DansZs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

Hey! I appreciate all the help so far! You guys are great. I should have posted the code I wrote so far to begin with. The idea I came up with was that you can keep a count of how many times you add a number to itself until you get to the numerator. For example, 9/2: 2+2+2+2, so the count would equal 4 which is the floor division of 9/2. Unfortunately, this runs in Theta(n/m) time, so it won't get me full credit on the assignment.

My code:

DIVIDE(n,m)
if m>n
..return 0
else if m<=n
..while m<=n
......count++
......m=m+m
......if m<=n
.........n=n-m
.........count++

return count


This was my pseudocode on the test which the professor replied: "Correct, but slow"

Thanks again!

Last edited by DansZs : February 21st, 2003 at 07:34 AM.

Reply With Quote
  #6  
Old February 22nd, 2003, 06:45 AM
ygfperson ygfperson is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 10 ygfperson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I would start your code at the 'while' command, and skip the first 'if' command entirely. It's not necessary.

Reply With Quote
  #7  
Old February 22nd, 2003, 07:05 PM
Mats Mats is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Stockholm, Sweden
Posts: 2 Mats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
This should do the trick...

public loops as integer

divide(m,n)
loops = 0
return iterator(0, m, n)
end

iterator(startval, m, n)
val = n
times = 1
target = m - startval

While val < target
loops = loops + 1
times = times * 2
val = val * 2
Wend

If times = 1 Then
return 0
Else
return (times / 2) + iterator(startval + val / 2, m, n)
End If
End

it doubles the value of n for each loop until it hits m
then it restarts at the previous value by adding doubles of
n to that value.

I tested it and it works fine and fast.

Hope you like it...
/Mats

Reply With Quote
  #8  
Old February 22nd, 2003, 07:12 PM
Mats Mats is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Stockholm, Sweden
Posts: 2 Mats User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sorry...

Realised that I used a couple of divisions (/2)
but store the previous values in temporary variables and you'll get rid of them...

/Mats

Reply With Quote
  #9  
Old February 23rd, 2003, 08:05 PM
DansZs DansZs is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 3 DansZs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hey Thanks for the help Mats! Your algorithm looks good!!!!!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > Division Algorithm


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT