integer-arithmetic

Converting a list of digits to a number

纵饮孤独 提交于 2020-01-05 12:17:25
问题 I was wondering if there was a way to take a list of numbers (digits), and truncate the numbers together to be one large number (not addition) in Scheme. For example, I would want (foo '(1 2 3 4)) ;=> 1234 Does Scheme have a built in function to do this? 回答1: There are a number of languages that are in the Scheme family, and there are a few versions of Scheme, too. If you're using one, e.g., Racket, that includes a left associative fold (often called foldl , fold , or reduce , though there

Converting a list of digits to a number

冷暖自知 提交于 2020-01-05 12:17:09
问题 I was wondering if there was a way to take a list of numbers (digits), and truncate the numbers together to be one large number (not addition) in Scheme. For example, I would want (foo '(1 2 3 4)) ;=> 1234 Does Scheme have a built in function to do this? 回答1: There are a number of languages that are in the Scheme family, and there are a few versions of Scheme, too. If you're using one, e.g., Racket, that includes a left associative fold (often called foldl , fold , or reduce , though there

Fast integer solution of x(x-1)/2 = c

杀马特。学长 韩版系。学妹 提交于 2020-01-05 04:25:08
问题 Given a non-negative integer c , I need an efficient algorithm to find the largest integer x such that x*(x-1)/2 <= c Equivalently, I need an efficient and reliably accurate algorithm to compute: x = floor((1 + sqrt(1 + 8*c))/2) (1) For the sake of defineteness I tagged this question C++, so the answer should be a function written in that language. You can assume that c is an unsigned 32 bit int. Also, if you can prove that (1) (or an equivalent expression involving floating-point arithmetic)

Passing arbitrary-sized integers from Prolog to C

独自空忆成欢 提交于 2020-01-03 07:20:29
问题 Right now, I'm learning how to interface SICStus Prolog with C code. I would like to have/use/see a C implementation of "Hamming weight" of arbitrary-sized integers in SICStus Prolog version 4. It seems to me that I need C functions for testing term types (SP_is_integer) and C functions for accessing Prolog terms (SP_get_integer, SP_get_integer_bytes). However, I'm not sure how to use SP_get_integer_bytes in a portable, robust fashion. Could you please point me to some well-crafted solid C

Find the a 4 digit number who's square is 8 digits AND last 4 digits are the original number [closed]

别等时光非礼了梦想. 提交于 2020-01-01 05:02:56
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed last year . From the comments on my answer here, the question was asked (paraphrase): Write a Python program to find a 4 digit whole number, that when multiplied to itself, you get an 8 digit whole number who's last 4 digits are equal to the original number. I will post my answer, but am

Floored integer division

倾然丶 夕夏残阳落幕 提交于 2019-12-31 01:48:13
问题 Is there an easy, efficient and correct (i.e. not involving conversions to/from double) way to do floored integer division (like e.g. Python offers) in C#. In other words, an efficient version of the following, that does not suffer from long/double conversion losses. (long)(Math.Floor((double) a / b)) or does one have to implement it oneself, like e.g. static long FlooredIntDiv(long a, long b) { if (a < 0) { if (b > 0) return (a - b + 1) / b; // if (a == long.MinValue && b == -1) // see *)

Why int plus uint returns uint?

前提是你 提交于 2019-12-29 08:30:06
问题 int plus unsigned int returns an unsigned int. Should it be so? Consider this code: #include <boost/static_assert.hpp> #include <boost/typeof/typeof.hpp> #include <boost/type_traits/is_same.hpp> class test { static const int si = 0; static const unsigned int ui = 0; typedef BOOST_TYPEOF(si + ui) type; BOOST_STATIC_ASSERT( ( boost::is_same<type, int>::value ) ); // fails }; int main() { return 0; } 回答1: If by "should it be" you mean "does my compiler behave according to the standard": yes . C+

How to do 64 bit multiply on 16 bit machine?

青春壹個敷衍的年華 提交于 2019-12-29 08:08:48
问题 I have an embedded 16 bit CPU. On this machine ints are 16 bit wide and it supports longs that are 32 bits wide. I need to do some multiplications that will need to be stored in 64 bits (e.g. multiply a 32 bit number by a 16 bit number). How can I do that with the given constraints? I do not have a math library to do this. 回答1: A suggestion in C. Note that this code probably will be easier to implement with inline assembler as carry detection in C doesn't seem that easy // Change the typedefs

Perfect square and perfect cube

女生的网名这么多〃 提交于 2019-12-29 04:23:25
问题 Is there any predefined function in c++ to check whether the number is square of any number and same for the cube.. 回答1: No, but it's easy to write one: bool is_perfect_square(int n) { if (n < 0) return false; int root(round(sqrt(n))); return n == root * root; } bool is_perfect_cube(int n) { int root(round(cbrt(n))); return n == root * root * root; } 回答2: sqrt(x) , or in general, pow(x, 1./2) or pow(x, 1./3) For example: int n = 9; int a = (int) sqrt((double) n); if(a * a == n || (a+1) * (a+1

Understanding printf() with integers

不羁岁月 提交于 2019-12-25 09:08:11
问题 I have a question regarding how the printf() method prints integers, signed or unsigned. One day, I found myself thinking about how difficult it must be to convert a binary sequence into a sequence of decimal digits that a human can understand, given that a computer has no concept of decimal. Below, I have a printf() method (from here) with its associated methods. I've tried to understand as much as I can about how printi() works, as you can see in the comments: #define PAD_RIGHT 1 #define