Numbers
Data Types
Integers
Algorithms
Logic
Translation of henninglive's unrolled divison loop for calculating amount of digits to C#
public class Kumite { // Unrolled div loop public static int Digits(ulong n) { var l = 1; while(true) { if (n < 10) return l; if (n < 100) return l + 1; if (n < 1000) return l + 2; if (n < 10000) return l + 3; n /= 10000; l += 4; } } }
- public class Kumite
- {
- // Unrolled div loop
fn digits(mut n: u64) -> usize {let mut l = 1;loop {if n < 10 {return l;- public static int Digits(ulong n)
- {
- var l = 1;
- while(true)
- {
- if (n < 10) return l;
- if (n < 100) return l + 1;
- if (n < 1000) return l + 2;
- if (n < 10000) return l + 3;
- n /= 10000;
- l += 4;
- }
if n < 100 {return l + 1;}if n < 1000 {return l + 2;}if n < 10000 {return l + 3;}n /= 10000;l += 4;- }
- }
using NUnit.Framework; using System; public class NumberOfDigits { [Test] public void RandomTests() { var rand = new Random(); // 10 digits, is there any way to generate random ulongs without implementing lots of logic? for(var i = 1; i < 10; ++i) { // 10 numbers for(var j = 0; i < 10; ++i) { var num = rand.Next((int)Math.Pow(10, i-1), (int)Math.Pow(10, i)); Assert.That(Kumite.Digits((ulong)num), Is.EqualTo(i)); } } } [Test] public void Pow10() { Assert.That(Kumite.Digits(0), Is.EqualTo(1)); ulong n = 1; Assert.That(Kumite.Digits(1), Is.EqualTo(1)); for(var i = n; i < 20; ++i) { n *= 10; Assert.That(Kumite.Digits(n), Is.EqualTo(i + 1)); } Assert.That(Kumite.Digits(ulong.MaxValue), Is.EqualTo(20)); } [Test] public void Pow10_Minus_1() { ulong n = 1; for(var i = 1; i < 20; ++i) { n *= 10; Assert.That(Kumite.Digits(n - 1), Is.EqualTo(i)); } } [Test] public void Pow10_Half() { ulong n = 1; for(var i = n; i < 20; ++i) { n *= 10; Assert.That(Kumite.Digits(n / 2), Is.EqualTo(i)); } } }
#[test]fn pow10() {assert_eq!(digits(0), 1);let mut n = 1;assert_eq!(digits(1), 1);for i in 1..20 {n*=10;assert_eq!(digits(n), i+1);}assert_eq!(digits(std::u64::MAX), 20);}- using NUnit.Framework;
- using System;
#[test]fn pow10_minus_1() {let mut n = 1;for i in 1..20 {n*=10;assert_eq!(digits(n - 1), i);- public class NumberOfDigits
- {
- [Test]
- public void RandomTests()
- {
- var rand = new Random();
- // 10 digits, is there any way to generate random ulongs without implementing lots of logic?
- for(var i = 1; i < 10; ++i)
- {
- // 10 numbers
- for(var j = 0; i < 10; ++i)
- {
- var num = rand.Next((int)Math.Pow(10, i-1), (int)Math.Pow(10, i));
- Assert.That(Kumite.Digits((ulong)num), Is.EqualTo(i));
- }
- }
- }
}#[test]fn pow10_half() {let mut n = 1;for i in 1..20 {n*=10;assert_eq!(digits(n / 2), i);- [Test]
- public void Pow10()
- {
- Assert.That(Kumite.Digits(0), Is.EqualTo(1));
- ulong n = 1;
- Assert.That(Kumite.Digits(1), Is.EqualTo(1));
- for(var i = n; i < 20; ++i)
- {
- n *= 10;
- Assert.That(Kumite.Digits(n), Is.EqualTo(i + 1));
- }
- Assert.That(Kumite.Digits(ulong.MaxValue), Is.EqualTo(20));
- }
- [Test]
- public void Pow10_Minus_1()
- {
- ulong n = 1;
- for(var i = 1; i < 20; ++i)
- {
- n *= 10;
- Assert.That(Kumite.Digits(n - 1), Is.EqualTo(i));
- }
- }
- [Test]
- public void Pow10_Half()
- {
- ulong n = 1;
- for(var i = n; i < 20; ++i)
- {
- n *= 10;
- Assert.That(Kumite.Digits(n / 2), Is.EqualTo(i));
- }
- }
- }