Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* file: pandigital_division.c */
- /* author: Stan van der Bend (email: stanvdbend@gmail.com) */
- /* date: 25-10-2018 */
- /* version: 1.0 */
- /* Description: prints all combinations of numbers ABCDE/FGHIJ
- * that yield n. The letters represents the digits.
- * All digits 0..9 must be used. */
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- // template for a dynamic array
- typedef struct { int *array; size_t used; size_t size; } DynamicArray;
- // initialise a DynamicArray, allocate resources.
- void initDA(DynamicArray *a, size_t initialSize) {
- a->array = (int *)malloc(initialSize * sizeof(int));
- a->used = 0;
- a->size = initialSize;
- }
- // insert an integer element into the argued DA
- void insertInDynamicArray(DynamicArray *a, int element) {
- if (a->used == a->size) {
- a->size *= 2;
- a->array = (int *)realloc(a->array, a->size * sizeof(int));
- }
- a->array[a->used++] = element;
- }
- DynamicArray combine(DynamicArray a, DynamicArray b){
- DynamicArray combined;
- initDA(&combined, a.used + b.used);
- for (int i = 0; i < a.used; ++i) {
- insertInDynamicArray(&combined, a.array[i]);
- }
- for (int i = 0; i < b.used; ++i) {
- insertInDynamicArray(&combined, b.array[i]);
- }
- return combined;
- }
- /**
- * @param a an array of length > 8, possibly containing duplicates.
- * @return {@code true}(1) if the array contains a duplicate integer value, {@code false}(0) if not.
- */
- int containsDuplicate(DynamicArray a){
- for (int i = 0; i < a.used; ++i) {
- for (int j = i + 1; j < a.used; ++j) {
- if(a.array[i] == a.array[j]){
- return 1;
- }
- }
- }
- return 0;
- }
- /**
- * Checks if a given two dynamic arrays contain the digits 0..9
- *count = 246
- * @param a array one
- * @param b array two
- * @return {@code true}(1) if the digits 0..9 are present in arrays a and b, {@code false}(0) if not.
- */
- int containsPangramNumber(DynamicArray a, DynamicArray b){
- int panProduct = 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9; // 362.880
- int panSum = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9; // 45
- /*
- * Determine both the sum and the product of the digits in the two given arrays.
- */
- int product = 1;
- int sum = 0;
- for (int i = 0; i < a.used; ++i) {
- if(a.array[i] == 0) // leave out zero
- continue;
- product *= a.array[i];
- sum += a.array[i];
- }
- for (int j = 0; j < b.used; ++j) {
- if(b.array[j] == 0) // leave out zero
- continue;
- product *= b.array[j];
- sum += b.array[j];
- }
- return panSum == sum && panProduct == product;
- }
- /**
- * @param n an integer n of length 4/5
- * @return convert the given integer n into an array of digits
- */
- DynamicArray getDigits(int n){
- DynamicArray digits;
- initDA(&digits, 5);
- while (n > 0){
- int remainder = n%10;
- insertInDynamicArray(&digits, remainder);
- n /= 10;
- }
- return digits;
- }
- int countPanDiv2(int n){
- // we declare this variable to count the results, if none we should print "NONE"
- int count = 0;
- // iterate the number from 98765..01234 (in ascending order)
- for(int i = 98765; i >= 01234; i--){
- // find the remainder of the product of the given input and the iteration number
- int remainder = i % n;
- // check if the division product is going to be a whole number, remainder is 0
- if(remainder == 0){
- // get the product of the division by our iteration value and the input number
- int product = i / n;
- // check whether both the input number and the product contain a leading zero
- int bothHaveLeadingZero = i < 10000 && product < 10000;
- // if both have, that means we know it does not contain the digits 0..9
- if(!bothHaveLeadingZero){
- /*
- * store the digits of both the input number and the product in an array
- */
- DynamicArray a = getDigits(i);
- DynamicArray b = getDigits(product);
- DynamicArray combined = combine(a, b);
- // if the combined length of both of our arrays is smaller than 9,
- // we know the combined digits of these arrays cannot contain the digits 0..9
- if(combined.used < 9)
- continue;
- // check if the digits of the combined array contains any duplicates
- if(containsDuplicate(combined))
- continue;
- // check if the combined digits of both arrays are the digits 0..9
- if(containsPangramNumber(a, b)){
- /*
- * check which array (if any) uses up 4 out of 5 slots,
- * if any such array exists, add a 0 in front of the number that this array represents.
- */
- if(a.used == 4)
- printf("0%d/%d", i, product);
- else if(b.used == 4)
- printf("%d/0%d", i, product);
- else
- printf("%d/%d", i, product);
- printf("\n");
- // printf(" - length = %d %d \n", (int) a.used, (int) b.used);
- count++;
- }
- }
- }
- }
- return count;
- }
- int main() {
- int n; //n integers
- scanf("%d", &n);
- int count = countPanDiv2(n);
- if(count == 0)
- printf("NONE");
- return 0;
- }
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement