numbers

Javascript + return PrimeNumbers

夙愿已清 提交于 2019-12-10 12:27:04
问题 I am trying to write a function that returns the PrimeNumber. for testing purposes i am just doing a console.log for stages of this function, to try and understand it better. so this line(line:18) in my total function will just return i; as opposed to do a console.log So Basically, 30 will be passed to the function and the function will return every prime number <=30. It is based on this from wiki: This routine consists of dividing n by each integer m that is greater than 1 and less than or

Combining a matrix of numbers, with array of strings

妖精的绣舞 提交于 2019-12-10 12:18:39
问题 I am trying to compute the code seen below, in a more clever way, using MatLAB: f=zeros(5,2) %Values irrelevant. y = {'A1', 'B1'; f(1,1) f(1,2); f(2,1) f(2,2); f(3,1) f(3,2); f(4,1) f(4,2); f(5,1) f(5,2)}; What I get out is the f matrix with the text of A1 and B1 above the two vectors. I suppose there is a simpler way to write this, for more complex purposing, but I've tried any combination of parenthesis, brackets and curly brackets, num2str that I could think of. Any suggestions? 回答1: The

Pseudo Random Number Generator using LFSR in VHDL

廉价感情. 提交于 2019-12-10 12:16:45
问题 I'm having a bit of trouble creating a prng using the lfsr method. Here is my code: library IEEE; use IEEE.STD_LOGIC_1164.ALL; entity pseudorng is Port ( clock : in STD_LOGIC; reset : in STD_LOGIC; Q : out STD_LOGIC_VECTOR (7 downto 0); check: out STD_LOGIC); constant seed: STD_LOGIC_VECTOR(7 downto 0) := "00000001"; end pseudorng; architecture Behavioral of pseudorng is signal temp: STD_LOGIC; signal Qt: STD_LOGIC_VECTOR(7 downto 0); begin PROCESS(clock) BEGIN IF rising_edge(clock) THEN IF

How do I square a number's digits?

雨燕双飞 提交于 2019-12-10 12:13:34
问题 How do I square a number's digits? e.g.: square(21){}; should result in 41 instead of 441 回答1: function sq(n){ var nos = (n + '').split(''); var res=""; for(i in nos){ res+= parseInt(nos[i]) * parseInt(nos[i]); } return parseInt(res); } var result = sq(21); alert(result) 回答2: This is easily done with simple math. No need for the overhead of string processing. var result = []; var n = 21; while (n > 0) { result.push(n%10 * n%10); n = Math.floor(n/10); } document.body.textContent = result

AJAX POST sends numbers as string

家住魔仙堡 提交于 2019-12-10 10:58:30
问题 I my AJAX POST request is sending my numeric data to my server as string for some reason... Here are my data and the AJAX request: var data = { projectId: $("#projectId").val(), startDate: $("#startDate").val(), endDate: $("#endDate").val(), num_auto_tests: Number($("#num_auto_tests").val()), num_manual_tests: Number($("#num_manual_tests").val()), num_passed_tests: Number($("#num_passed_tests").val()), num_failed_tests: Number($("#num_failed_tests").val()), num_unran_tests: Number($("#num

Logarithm-based solution calculates incorrect number of digits in large integers

久未见 提交于 2019-12-10 10:49:27
问题 I don't have enough reputation points yet to leave comments, but saw numerous times when people (incorrectly) suggest using log10 to calculate the number of digits in a positive integer. This is wrong for large numbers! long n = 99999999999999999L; // correct answer: 17 int numberOfDigits = String.valueOf(n).length(); // incorrect answer: 18 int wrongNumberOfDigits = (int) (Math.log10(n) + 1); // also incorrect: double wrongNumberOfDigits2 = Math.floor(Math.log10(n) + 1); The logarithm-based

How to format numbers according to locale in Haskell?

荒凉一梦 提交于 2019-12-10 10:06:28
问题 In Python I can use locale.format to pretty-print numbers according to locale setting: >>> import locale >>> locale.setlocale(locale.LC_ALL, "en_US.UTF-8") 'en_US.UTF-8' >>> locale.format("%.2f",1234567.89,grouping=True) '1,234,567.89' How can I do the same in Haskell? I see that there are localeconv and setlocale bindings, but is there a generic pretty printer which respects Lconv ? 回答1: I would say that if the library in question is missing then you could either write yourself one (obvious

Reversing a Number using bitwise shift

荒凉一梦 提交于 2019-12-10 09:53:16
问题 I am trying to find a way to reverse a number without Converting it to a string to find the length Reversing the string and parsing it back Running a separate loop to compute the Length i am currently doing it this way public static int getReverse(int num){ int revnum =0; for( int i = Integer.toString(num).length() - 1 ; num>0 ; i-- ){ revnum += num % 10 * Math.pow( 10 , i ); num /= 10; } return revnum; } But I would Like to implement the above 3 conditions. I am looking for a way , possibly

Ruby undefined method `+' for nil:NilClass (NoMethodError)

北城余情 提交于 2019-12-10 09:42:56
问题 New to Ruby. Receiving error: undefined method `+' for nil:NilClass (NoMethodError) I do not understand why I am receiving an error for such a simple task of incrementing a value. However, perhaps the error is caused by something else. What is the cause? class LinkedList class Node attr_accessor :data, :nextNode def initialize(data = nil, nextNode = nil) @data = data @nextNode = nextNode end end #member variables @head = nil @size = 0 def initialize @head = Node.new() end def add(val) curr =

How to target e negative number from an array, and get the sum of all positive numbers?

可紊 提交于 2019-12-10 09:37:39
问题 I am trying to figure it out how to target negative numbers in an array. I have this function SummPositive( array ) { } SummPositive( [ 1, 2, 3, 4, 5, -2, 23, -1, -13, 10,-52 ] ); This is an array with negative and positive numbers.How to target all negative numbers from array, when you don't know how many negative numbers are in array? For example, I am trying to...loop to entire array, find the positive numbers, store them in another array, and make a sum (1+2+3+4+5+10+23) . But i am noob,