Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Efficient method to slice an int64 to parts ?

Posted on 2011-09-23
2
Medium Priority
?
406 Views
Last Modified: 2016-09-29
i have a big number that i want to slice into 10 parts for example, each part will be almost equel...

now don't reply with "just devide it by 10", no that's not the point..

so lets say for example i want to devide the number 3,480,352 into 10 parts, it will look like this:

0. 339.9 (kb)
1. 361.1 (kb)
2. 357.2 (kb)
3. 353.9 (kb)
4. 353.9 (kb)
5. 350.4 (kb)
6. 350.4 (kb)
7. 310.4 (kb)
8. 260.2 (kb)
9. 361.1 (kb)

the example i showed was taken from a rea-world scenario.
0
Comment
Question by:rotem156
2 Comments
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 1000 total points
ID: 36586463
Since you don't specify a programming language I'll use pseudo-code. I'll use rand(0,10) to mean a random integer between 0 and 10.

integer num = 3480352;
integer random;
integer array[10];
integer max_rand;

for i from 1 to 9 //do this for the first 9 parts
  max_rand = floor(num/30); //floor means round down, the 30 should be set to whatever you want
  random = rand(0,max_rand*2) - max_rand; //This generates a number from -max_rand to max_rand
  array[i] = num/(10-i+1) + random; //divide by 10 the first time, then by 9, then 8 etc
  num = num - array[i]; Take it out of the old number
end for
array[10] = num; //put the rest in the last one

Open in new window

0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 1000 total points
ID: 36586472
>>i f i devide by 2, it will look like this:

>>0. 346.6 (kb)
>>1. 2.96 (mb)

why?

for one, those aren't close to being equal
and even if they were closer,  why isn't dividing evenly by 2 or 10 acceptable?

are you just looking for semi-random divisions?
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question