একটি বড় সংখ্যা স্ট্রিং ফরম্যাটে দেওয়া আছে। এর ডিজিট পারমুটেশন থেকে কি যেকোনোটি ৮ দিয়ে বিভাজ্য তা যাচাই করতে হবে।
উদাহরণ
ইনপুট: input_num = "4696984"
আউটপুট: Divisible by eight
অ্যালগরিদম
- দৈর্ঘ্য < 3 হলে সরাসরি চেক করুন।
- ৩ ডিজিটের সব পারমুটেশন (১০৪ থেকে ৯৯৯, стেপ ৮) যাচাই করুন।
- প্রতিটি ৩-ডিজিট সংখ্যার ডিজিট ফ্রিকোয়েন্সি মূল স্ট্রিংয়ের ফ্রিকোয়েন্সির অধিক নয় কিনা চেক করুন।
উদাহরণ কোড
def solve(input_num):
if len(input_num) < 3:
if int(input_num) % 8 == 0:
return True
if int(input_num[::-1]) % 8 == 0:
return True
return False
temp_arr = [0] * 10
for ch in input_num:
temp_arr[int(ch)] += 1
for count in range(104, 1000, 8):
temp = count
occurrences = [0] * 10
occurrences[temp % 10] += 1
temp //= 10
occurrences[temp % 10] += 1
temp //= 10
occurrences[temp % 10] += 1
temp = count
if occurrences[temp % 10] > temp_arr[temp % 10]:
continue
temp //= 10
if occurrences[temp % 10] > temp_arr[temp % 10]:
continue
temp //= 10
if occurrences[temp % 10] > temp_arr[temp % 10]:
continue
return True
return False
if solve("4696984"):
print("Divisible by eight")
else:
print("Not divisible by eight")
ইনপুট
4696984
আউটপুট
Divisible by eight