#1. CIFERNY SUCET #ciferny sucet(odzadu) - 1235 -> 5+3+2+1 def digit_sum_backward(n): dsum = 0 while n != 0: dsum += n % 10 n //= 10 return dsum #ciferny sucet(odzadu) - 1235 -> 1+2+3+4+5 # Co keby som mal pomocnu funkciu ktora vrati k-tu cislicu cisla ''' vrati k-tu(zacinam 0) cislicu z cisla number k_th_digit(123,0)-->3, k_th_digit(123,1)--> 2, k_th_digit(123,2)--> 1 ''' def k_th_digit(number, k): for i in range(k-1): number //= 10 return number % 10 #spocitam pocet cislic cisla def num_digits(number): counter = 0 while number != 0: counter += 1 number //= 10 return counter def digit_sum_forward(n): #zistim si kolko ma cislo cislic num_digs = num_digits(n) dig_sum = 0 #teraz uz mozem vo for cykle - opakujem pre num_digs, num_digs-1,num_digs - 2, ... ,0 for i in range(num_digs,0,-1): dig_sum += k_th_digit(n, i) return dig_sum #print(digit_sum_forward(123)) #2. ROZKLAD naprvocisla #number je (vartime True)/ nie je (False) prvocislom def is_prime(number): for i in range(2, number - 1): if number % i == 0: return False return True #pocet delitelov cisla def num_divisors(number): # zalezi ci chceme do toho zapocitat aj 1 a samotne n (nechceme) counter = 0 for i in range(2, number): counter += 1 return counter #vytlacime prvocisla mensie ako k def print_primes(k): for i in range(2, k): if is_prime(i) == True: # kratsie by sa zapisalo: if is_prime(k): print(i, end='') #vylacennie prvych k prvocisel def print_first_primes(k): counter = 0 number = 2 while counter < k: if is_prime(number): print(number) number += 1 counter += 1 #rozklad NUMBER na prvocisla #Idea: urobit si funkciu(power) ktora zisti kolko krat sa v cisle NUMBER nachadza prvocislo P # Prechadzam postupne cisla p = ... 2, 3, 4, ... # zistim ci p prvocislo a pomocou power zistim jeho mocninu # (vypisem p**mocnina) a p**mocninu odoberiem z number def power(number, p): counter = 0 while number != 0: if (number % p) != 0: break number //= p counter += 1 return counter def factor(number): p = 2 while number != 1: if is_prime(p): # nie je potrebne testovat prvocislenost e = power(number, p) if e > 0: print(p,"^",e,' ', sep='',end='') number //= p**e p += 1 #NUMBER z desiatkovej do 2 kovej # 123 = 1*10^2 + 2*10^1 + 3*10^0 # 1100 = 1*2^4 + 1*2^3 + 0*2^1 + 0*2^0 # (idea viem zistit poslednu cifru): pri 10kovej NUMBER % 10, pri dvojkovej number % 2 # takze v cykle: zistim poslednu a odrezem (//2) ju z NUMBER def decadic_to_binary(number): result = '' while number != 0: digit = number % 2 number //= 2 #print(digit, sep='', end='') - problem mam to odzadu! #alternativa je kumulovat si znaky do string-u - zalezi na poradi ! result = str(digit) + result return result def decadic_to_hex(number): result = '' while number != 0: digit = number % 16 number //= 16 if digit > 9: # ord('A') - zisti poradove cislo 'A' --> 65, (zvysne su 'B' --> 66,... ) # posun o (digit - 10) znakov dalej #chr poradove cislo znaku prevediem na znak digit = chr(ord('A') + (digit - 10)) # #print(digit, sep='', end='') - problem mam to odzadu! result = str(digit) + result return result print(decadic_to_hex(11))