Menu Close

Find-the-sum-of-all-positive-numbers-less-than-400-and-not-divisible-by-6-




Question Number 179574 by Ar Brandon last updated on 30/Oct/22
Find the sum of all positive numbers   less than 400 and not divisible by 6.
$$\mathrm{Find}\:\mathrm{the}\:\mathrm{sum}\:\mathrm{of}\:\mathrm{all}\:\mathrm{positive}\:\mathrm{numbers} \\ $$$$\:\mathrm{less}\:\mathrm{than}\:\mathrm{400}\:\mathrm{and}\:\mathrm{not}\:\mathrm{divisible}\:\mathrm{by}\:\mathrm{6}. \\ $$
Commented by Ar Brandon last updated on 30/Oct/22
#include <stdio.h> int main(void) { int sum = 0; for (short i = 1; i < 400; i++) if (i % 6 != 0) sum += i; printf("%d", sum); return 0; }
Commented by Ar Brandon last updated on 30/Oct/22
Output: 66534 Any mathematical solution?
Commented by mr W last updated on 31/Oct/22
Ar Brandon sir:  can you please calculate the answer to  Q179420 with a program?  it is total number of numbers between  10 and  9876543211 which are divisible  by 9 and have different digits.  the first one is 18 and the last one is  9876543210.
$${Ar}\:{Brandon}\:{sir}: \\ $$$${can}\:{you}\:{please}\:{calculate}\:{the}\:{answer}\:{to} \\ $$$${Q}\mathrm{179420}\:{with}\:{a}\:{program}? \\ $$$${it}\:{is}\:{total}\:{number}\:{of}\:{numbers}\:{between} \\ $$$$\mathrm{10}\:{and}\:\:\mathrm{9876543211}\:{which}\:{are}\:{divisible} \\ $$$${by}\:\mathrm{9}\:{and}\:{have}\:{different}\:{digits}. \\ $$$${the}\:{first}\:{one}\:{is}\:\mathrm{18}\:{and}\:{the}\:{last}\:{one}\:{is} \\ $$$$\mathrm{9876543210}. \\ $$
Commented by Ar Brandon last updated on 31/Oct/22
OK Sir, I'll try it this evening. We are going to start from 18 and increment by 9 then store the remainders when divided by 10, 100, 1000, etc in arrays then use the nested for loop to compare each value stored in the arrays by the others. If the values are all different they pass the test, else they're ignored. See you in the evening, Sir ���� Thanks for mentioning me for this task.
Commented by Ar Brandon last updated on 05/Nov/22
#include <stdbool.h> #include <stdio.h> int main(void) { bool flag; int num[10]; long int count = 0; for (long int i = 18; i < 9876543211; i += 9) { num[0] = -1; num[1] = -2; num[2] = -3; num[3] = -4; num[4] = -5; num[5] = -6; num[6] = -7; num[7] = -8; num[8] = -9; num[9] = -10; num[0] = i % 10; num[1] = i % 100 / 10; if (i > 99) num[2] = i % 1000 / 100; else goto compare; if (i > 999) num[3] = i % 10000 / 1000; else goto compare; if (i > 9999) num[4] = i % 100000 / 10000; else goto compare; if (i > 99999) num[5] = i % 1000000 / 100000; else goto compare; if (i > 999999) num[6] = i % 10000000 / 1000000; else goto compare; if (i > 9999999) num[7] = i % 100000000 / 10000000; else goto compare; if (i > 99999999) num[8] = i % 1000000000 / 100000000; else goto compare; if (i > 999999999) num[9] = i / 1000000000; compare: for (short j = 0; j < 10; j++) { for (short k = j+1; k < 10; k++) { flag = true; if (num[j] == num[k]) { flag = false; goto loop_end; } } } loop_end: if (flag) count++; } printf("%ld\n", count); return 0; }
Commented by Ar Brandon last updated on 05/Nov/22
I tried this, Sir! But got no output. It seems the figures are too large for my 32 bit compiler. Or perhaps I've missed something.
Answered by manxsol last updated on 30/Oct/22
Σ_1 ^(399) −6Σ_1 ^(66)          Σ_1 ^n =n((n+1)/2)  integer(((400)/6))=66  399×((400)/2)−6×66×((67)/2)=66534
$$\sum_{\mathrm{1}} ^{\mathrm{399}} −\mathrm{6}\sum_{\mathrm{1}} ^{\mathrm{66}} \:\:\:\:\:\:\:\:\:\sum_{\mathrm{1}} ^{{n}} ={n}\frac{{n}+\mathrm{1}}{\mathrm{2}} \\ $$$${integer}\left(\frac{\mathrm{400}}{\mathrm{6}}\right)=\mathrm{66} \\ $$$$\mathrm{399}×\frac{\mathrm{400}}{\mathrm{2}}−\mathrm{6}×\mathrm{66}×\frac{\mathrm{67}}{\mathrm{2}}=\mathrm{66534} \\ $$

Leave a Reply

Your email address will not be published. Required fields are marked *