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

New Free Tools on Dev Shed!

#1
February 20th, 2003, 05:36 PM
 DansZs
Junior Member

Join Date: Feb 2003
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
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!

#2
February 20th, 2003, 06:11 PM
 andy3109
Contributing User

Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 12
posted twice for some reason...
__________________
hmmm...

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

#3
February 20th, 2003, 06:12 PM
 andy3109
Contributing User

Join Date: Nov 2002
Posts: 421
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 12

#4
February 21st, 2003, 05:47 AM
 tony825
Contributing User

Join Date: Jan 2003
Location: Greece
Posts: 63
Time spent in forums: < 1 sec
Reputation Power: 11
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 05:52 AM.

#5
February 21st, 2003, 08:30 AM
 DansZs
Junior Member

Join Date: Feb 2003
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0

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 08:34 AM.

#6
February 22nd, 2003, 07:45 AM
 ygfperson
Junior Member

Join Date: Feb 2003
Posts: 10
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.

#7
February 22nd, 2003, 08:05 PM
 Mats
Junior Member

Join Date: Feb 2003
Location: Stockholm, Sweden
Posts: 2
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

#8
February 22nd, 2003, 08:12 PM
 Mats
Junior Member

Join Date: Feb 2003
Location: Stockholm, Sweden
Posts: 2
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

#9
February 23rd, 2003, 09:05 PM
 DansZs
Junior Member

Join Date: Feb 2003
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
Hey Thanks for the help Mats! Your algorithm looks good!!!!!

 Viewing: Dev Shed Forums > Programming Languages - More > Software Design > Division Algorithm

## Developer Shed Advertisers and Affiliates

 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 Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

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