logo
down
shadow

Efficient way to find divisibility


Efficient way to find divisibility

By : Osbad44
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)
        fprintf(stderr, "Warning: Your C library does not support your current locale.\n");

    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 : facebook icon twitter icon
Divisibility by 3

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

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)
        lst.add(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

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

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

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:
Related Posts Related Posts :
  • dynamic memory for 2D char array
  • C compiler producing lightweight executeables
  • Cannot Open Shared Object cygmpfr-1.dll
  • How to access a structure member in a function that get it as void* type?
  • Replace for loop with formula
  • how to find if stack increases upwards or downwards?
  • Why does my homespun sizeof operator need a char* cast?
  • Setting exit status when creating core dump
  • Signal Handling in C
  • How to implement B+ Tree for file systems?
  • Stack storage at function call
  • Manipulate the Clipboard in C on Mac OS X
  • Problem with writing a hexadecimal string
  • Why does this code read all ' ' for the anything after the 4th character?
  • GUI for linux c
  • Where do I put all these function-like #defines, in C?
  • Padding in 24-bits rgb bitmap
  • how to acess and change variable of kernel space from user space
  • Can a conforming C implementation #define NULL to be something wacky
  • send() always interrupted by EPIPE
  • Socket Select with empty fd set
  • Coding self similar fractals in computer graphics using C
  • Forking with Pipes
  • sscanf + c99 not working on some platforms?
  • Problem with C function of type char pointer, can someone explain?
  • Macros in C.... please give the solution
  • Unix Piping using Fork and Dup
  • Any difference between these two while loops?
  • Hashtable implementation for C
  • Problems with Linked List in C
  • Stack Allocation in C
  • Random function stuck on one number
  • printf following zeros to a certain degree
  • Measuring CPU clocks consumed by a process
  • How is a CRC32 checksum calculated?
  • Tips on redefining a register bitfield in C
  • Converting a program from c to bash script
  • Static library not included in resulting LLVM executable
  • What is the meaning of "wild pointer" in C?
  • Reading DWORD from binary file
  • Whats the fastest, portable way to hash pointers we know are pointer aligned to a fixed size int?
  • Why does my summation program behaves weird?
  • How to determine values saved on the stack?
  • Thread scheduling C
  • C - converting to 2s complement
  • wireless networks c program
  • From where does the uboot get the information about flash, RTC, RAM in uboot/kernel development
  • printf behaviour in C
  • Measure size and way-order of L1 and L2 caches
  • Efficient copy of entire directory
  • Efficient way to create/unpack large bitfields in C?
  • getnameinfo prototype asks for sockaddr not sockaddr_in?
  • Linked List Sorting with Strings In C
  • Typecasting a floating value or using the math.h floor* functions?
  • Problem implementing sorting algorithm in C with an array of structs
  • How to copy a ram_base file to disk efficiently
  • How to fix casting a pointer to an integer?
  • How to round floating point numbers to the nearest integer in C?
  • PIC C - Sending 200 values over USB, but it only sends 25 or so of them
  • How do you return a string from a function correctly in Dynamic C?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk