Solved

Efficient method to slice an int64 to parts ?

Posted on 2011-09-23
2
384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 74

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Recursive algorithm 8 242
mangling a text file 4 308
Odds of Duplication 20 149
Trigger Event from a dynamic DLL to a WPF UserControl 7 96
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…
Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
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…

749 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