C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Efficient way to find divisibility

Date : November 22 2020, 07:01 PM
To fix the issue you can do Let's say you need to find whether a positive integer K is divisible by a number between 100,000 and 150,000, and it is such a rare operation, that doing precalculations is just not worth the processor time or memory used.
If K < 100,000, it cannot be divisible by a number between 100,000 and 150,000.
code :
``````#include <stdlib.h>
#include <inttypes.h>
#include <limits.h>
#include <locale.h>
#include <ctype.h>
#include <stdio.h>
#include <errno.h>

int is_divisible(const uint64_t  number,
const uint64_t  minimum_divisor,
const uint64_t  maximum_divisor)
{
uint64_t divisor, minimum_result, maximum_result, result;

if (number < minimum_divisor) {
return 0;
}

if (number <= maximum_divisor) {
/* Number itself is a valid divisor. */
return 1;
}

minimum_result = number / maximum_divisor;
if (minimum_result < 2) {
minimum_result = 2;
}

maximum_result = number / minimum_divisor;
if (maximum_result < minimum_result) {
maximum_result = minimum_result;
}

if (maximum_result - minimum_result > maximum_divisor - minimum_divisor) {
/* The number is so large that it is the least amount of work
to check each possible divisor. */
for (divisor = minimum_divisor; divisor <= maximum_divisor; divisor++) {
if (number % divisor == 0) {
return 1;
}
}

return 0;

} else {
/* There are fewer possible results than divisors,
so we check the results instead. */

for (result = minimum_result; result <= maximum_result; result++) {
if (number % result == 0) {
divisor = number / result;
if (divisor >= minimum_divisor && divisor <= maximum_divisor) {
return 1;
}
}
}

return 0;
}
}

int parse_u64(const char *s, uint64_t *to)
{
unsigned long long  value;
const char         *end;

/* Empty strings are not valid. */
if (s == NULL || *s == '\0')
return -1;

/* Parse as unsigned long long. */
end = s;
errno = 0;
value = strtoull(s, (char **)(&end), 0);
if (errno == ERANGE)
return -1;
if (end == s)
return -1;

/* Overflow? */
if (value > UINT64_MAX)
return -1;

/* Skip trailing whitespace. */
while (isspace((unsigned char)(*end)))
end++;

/* If the string does not end here, it has garbage in it. */
if (*end != '\0')
return -1;

if (to)
*to = (uint64_t)value;

return 0;
}

int main(int argc, char *argv[])
{
uint64_t kmin, kmax, dmin, dmax, k, count;

if (argc != 5) {
fprintf(stderr, "\n");
fprintf(stderr, "Usage: %s [ -h | --help | help ]\n", argv[0]);
fprintf(stderr, "       %s MIN MAX MIN_DIVISOR MAX_DIVISOR\n", argv[0]);
fprintf(stderr, "\n");
fprintf(stderr, "This program counts which positive integers between MIN and MAX,\n");
fprintf(stderr, "inclusive, are divisible by MIN_DIVISOR to MAX_DIVISOR, inclusive.\n");
fprintf(stderr, "\n");
return EXIT_SUCCESS;
}

/* Use current locale. This may change which codes isspace() considers whitespace. */
if (setlocale(LC_ALL, "") == NULL)

if (parse_u64(argv[1], &kmin) || kmin < 1) {
fprintf(stderr, "%s: Invalid minimum positive integer to test.\n", argv[1]);
return EXIT_FAILURE;
}
if (parse_u64(argv[2], &kmax) || kmax < kmin || kmax >= UINT64_MAX) {
fprintf(stderr, "%s: Invalid maximum positive integer to test.\n", argv[2]);
return EXIT_FAILURE;
}
if (parse_u64(argv[3], &dmin) || dmin < 2) {
fprintf(stderr, "%s: Invalid minimum divisor to test for.\n", argv[3]);
return EXIT_FAILURE;
}
if (parse_u64(argv[4], &dmax) || dmax < dmin) {
fprintf(stderr, "%s: Invalid maximum divisor to test for.\n", argv[4]);
return EXIT_FAILURE;
}

count = 0;
for (k = kmin; k <= kmax; k++)
count += is_divisible(k, dmin, dmax);

printf("%" PRIu64 "\n", count);
return EXIT_SUCCESS;
}
``````

Share :

## Divisibility by 3

By : Евгений
Date : March 29 2020, 07:55 AM
may help you . The number of operations before recursing is bounded by O(log n), because that is the number of bits required to represent n and subsequently the number of shift operations required to turn any n to zero. The following recursion receives an n that is at most log n / 2, so its loop has the complexity O(log (log n / 2)). Since this term is less than the initial loop complexity (and all following recursions are even less), it can be omitted. To be precise, the sum of all recursions must be less than the initial loop for that to hold, but I'm pretty confident (though I lack the exact proof) that that is the case here.

## Efficient way to find the average of the rank of added scores to a list. Possibly a more efficient way of sorting or a m

By : user2971187
Date : March 29 2020, 07:55 AM
This might help you You should use the SortedList package here, as it provides O(log N) insertion. So, with that package overall complexity is going to be O(NlogN). Read its implementation details here: http://www.grantjenks.com/docs/sortedcontainers/implementation.html
code :
``````from sortedcontainers import SortedList

def solve_sorted_list(numbers):
lst = SortedList()
acc = 0.0
for n in numbers:
pos = lst.bisect(n)
acc += len(lst) - pos
return acc / len(numbers)

print solve_sorted_list([100, 200, 150, 170, 50])
#2.2
``````
``````>>> lst = range(10**5, -1, -1)
>>> %timeit solve_bisect(lst)   #Using NPE's solution
1 loops, best of 3: 1.87 s per loop
>>> %timeit solve_sorted_list(lst)
1 loops, best of 3: 221 ms per loop
>>> lst = range(10**6, -1, -1)
>>> %timeit solve_sorted_list(lst)
1 loops, best of 3: 2.31 s per loop
>>> %timeit solve_bisect(lst)
1 loops, best of 3: 3min 52s per loop
``````

## graphs divisibility

By : xShinichiKudo
Date : March 29 2020, 07:55 AM
it fixes the issue If given graph is transformed to some other graph, where cycles are not allowed, this problem can be solved with Dijkstra's algorithm.
To do this, let's start with string divisibility by 7. Look at this sequence: 1, 10, 100, ... (mod 7). Since 7 is a prime number, 107-1 = 1 (mod 7) because of Fermat's little theorem. Which means 1, 10, 100, ... (mod 7) sequence is periodic and period is 6. This will be used to transform the graph and also this allows to recursively compute Sn (mod 7) using Sn-1 (mod 7): Sn = Sn-1 + 10n%6 * n_th_digit (mod 7).

## Binary divisibility by 10

By : alex_funglee
Date : March 29 2020, 07:55 AM
this will help
First split the number into odd and even bits (I'm calling "even" the bits corresponding to even powers of 2):

## Lisp divisibility

By : Daru.supah.hakah
Date : March 29 2020, 07:55 AM
wish helps you `(disivible '(1 2 3 4 5 6 7)).This is what I've made so far: