Solved

Efficient method to slice an int64 to parts ?

Posted on 2011-09-23
2
382 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 250 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 73

Assisted Solution

by:sdstuber
sdstuber earned 250 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This algorithm (in C#) will resize any image down to a given size while maintaining the original aspect ratio. The maximum width and max height are both optional but if neither are given, the original image is returned. This example is designed t…
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

815 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now