What is the algorithm for calculating the cross-sum (516 = 5 + 1 + 6 = 12) with just a while loop, adding, subtracting, division and mulitply? I would use modolus, but I do not know how to replicate it only with the basic mathematical terms that I spoke about.
Suppose by division you mean float-division; otherwise, you could write your own mod operation using gender separation.
O(n) time, O(1) ( n - ).
O(n)
O(1)
n
JavaScript:
function crossSum(num){ // Find the largest power of ten and number of digits, O(n) var power = 1, numDigits = 1; while (power * 10 <= num){ power = power * 10; numDigits++; } // Calculate cross sum, O(constant * n) = O(n) var sum = 0, digit; while (num > 0){ digit = 0; while ((digit + 1) * power <= num) digit++; console.log(digit) sum = sum + digit; num = num - power * digit; power = power / 10; } return sum; } console.log(crossSum(516));
, , modulo, , - , , , . StackOverflow , , , , . , , .
Source: https://habr.com/ru/post/1671911/More articles:Extract letter from string characters and numbers - javaGetting the remainder without the modulo operator (%) in Javascript, accounting - / + sign - javascriptCombining 3 different condition-based data frames - pythonhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1671909/how-to-load-latest-clojure-lib-from-git-repository&usg=ALkJrhiRw5baBHKP0ibzIWHsLBakLml38AOriginLab - ASCII Intelligent Import - originlabError requesting network on Firebase with ReactJS - javascriptHow to sum integer digits in java? - javaMega menu - slide panels in the direction based on the clicked link? - javascriptinput to forkIO / catch block - haskellHow to configure Google Cloud Datalab to use GPUs for TensorFlow? - google-cloud-platformAll Articles