How to calculate the number of daffodils in various programming languages (non-precision)

Source: Internet
Author: User
Tags mul

In various programming languages, the method for obtaining the number of daffodils (non-precision) is realized ).

PHP "Daffodils" implementation code <? Phpfor ($ I = 100; $ I <1000; $ I ++) {$ M = floor ($ I/100 ); // split hundreds of BITs $ n = floor ($ I/10) % 10; // split ten bits $ k = floor ($ I % 10 ); // split the single-digit if ($ I ==( $ M * $ m + $ N * $ n + $ K * $ k )) {echo $ I. "<br/>" ;}}?> Theoretical output of all PHP bits/*** the number of daffodils is a number no less than three digits. The sum of the N power of each digit is equal to that number. n is the number of digits * @ name daffodilsnum daffodils * @ Param $ places daffodils> = 3 */function daffodilsnum ($ places = 3) {// set_time_limit (0 ); // set the script timeout value to no limit. If the prompt is 30 seconds, you can enable this control // $ begin = microtime (); // script start time // define the number of digits if (! Defined ('places') define ('places', is_numeric ($ places )? $ Places: 3); If (places> = 3) {$ min = POW (10, places-1); // select the starting position of the number range $ max = POW (10, places); // The end position of the number range. // The start value. For ($ I = $ min, $ out = ''; $ I <$ Max; $ I ++) {$ sum = 0; // The sum of the power values of the current number and $ arr = str_split ($ I); // string-based split ($ J = 0; $ j <places; $ J ++) {// perform a power operation on each number and accumulate $ sum + = POW ($ arr [$ J], places ); if ($ sum> $ I) {// if the current accumulate value is greater than the selected number, the loop break will pop out;} if ($ sum = $ I) {// Add this number to the output queue $ out if it meets the definition. = $ I. '<br/>' ;}}// output queue echo $ out; // echo "<br/> ". (microtime ()-$ begin); // time consumed by the output script. valid when the script start time is enabled} else {// $ this-> error ('wrong bits '); // Number of wrong digits} the language's "Daffodils" implementation code # include <stdio. h>
Int main ()

{

Int I, D1, Hub, transit;

For (I = 100; I <= 9999; ++ I)

{

For (hub = 0, transit = I, d1 = 123; transit! = 0 ;)

{

D1 = (transit % 10) * (transit % 10) * (transit % 10 ); /* You need to add a loop for Multiple Digits. I am too lazy to change it. */hub = hub + D1;

Transit = transit/10;

}

If (hub = I) printf ("daffodils % d \ n", I );

}

Return 0;

} Pascal implements the code program shuixianhuashu; var A, B, C: integer; begin for a: = 1 to 9 do for B: = 0 to 9 do for C: = 0 to 9 do if a * A + B * B * B + C * c = 100 * A + 10 * B + C then writeln (100 * A + 10 * B + C ); end. or: Program sxh; var A, B, C, D: integer; begin for a: = 100 to 999 do begin B: = a mod 10; C: = a mod 100 Div 10; D: = a div 100; if B * B * B + C * C + D * D = A then writeln (); end; end. or program ABCD; var A, B, C, I, T: integer; begin I: = 100; Repeat A: = trunc (I/100); B: = trunc (I/10)-A * 10; C: = I-trunc (I/10) * 10; t: = A * a * A + B * B * B + C * C; if I = t then writeln (I, '=',, '^ 3 +', B, '^ 3 +', C, '^ 3'); I: = I + 1 until I> 999 end. the "Daffodils" implementation code of Visual Basic is private sub command2_click () dim I as integer, A as integer, B as integer, c As integer for I = 100 to 999 Step 1 A = I \ 100 B = (I-100 *) \ 10 C = I-100 * A-10 * B if a ^ 3 + B ^ 3 + C ^ 3 = I then PRI Nt I next I end sub Fortran's "Daffodils" implementation code write (*, 30) do 10 k = 100,999 IA = K/100 IB = Mod (K, 100) /10 Ic = Mod (K, 10) if (K. ne. IA ** 3 + IB ** 3 + IC ** 3) goto 10 write (*, 20) K, IA, IB, IC 10 continue 20 format (5x, 4i4) 30 format (5x, 18hn = I ** 3 + J ** 3 + k ** 3) stop end C ++ Compiler's daffodils implementation code # include <iostream> using namespace STD; int main () {int A, Q, W, E; for (A = 100; A <1000; ++ A) {q = A/100; W = (a-Q * 100)/10; E = (a-Q * 100-w * 10); if (a = Q * q + W * W + E * E) cout <A <"is the number of daffodils" <Endl ;}; return 0 ;} code implemented in Python: for I in range (): For J in range (): For k in range ): if I * 100 + J * 10 + k = I * I + J * j + K * K: code implemented in print I * 100 + J * 10 + k Java public class numberofdaffodils {public static void main (string [] ARGs) {list <long> numbers = new arraylist <long> (); For (long I = 100; I <long. max_value; I ++) {If (MATCH (I) {numbers. add (I) ;}} System. Out. println (numbers);} Private Static Boolean match (long in) {// count int COUNT = (in + ""). length (); // record each number long temp = 0; // The number of records stored in the remainder long num = in; // The total number long sum = 0; for (INT I = count; I> 0; I --) {temp = num/(long) math. pow (10, I-1); num = num % (long) math. pow (10, I-1); sum + = math. pow (temp, count);} return in = sum;} C # ASP. implementation Code in N for (INT I = 100; I <1000; I ++) {int Bai = 0; int Shi = 0; int Ge = 0; int baiyushu = 0; Bai = I/100; baiyushu = I % 100; Shi = baiyushu/10; GE = baiyushu % 10; if (I = Bai * BAI + Shi * Shi + ge * Ge) {console. writeline ("daffodils:" + I + "<br>") ;}} add C # daffodils implementation code (indefinite digits) /// <summary> /// determine whether a number is a daffodils number // </Summary> /// <Param name = "num"> number to be determined </Param >/// <returns> judgment result: true-Yes, false-No </returns> bool iswaterflower (INT num) {If (Num <= 0) {return false;} int temp = num; // the values to be judged. Separate the numbers and place them in the set. arraylist list = new arraylist (); While (temp> 0) {list. add (TEMP % 10); temp/= 10;} // determine whether the number is equal to the number after the power of the number, if yes, int sum = 0; foreach (int I in list) {int Mul = 1; for (Int J = 0; j <list. count; j ++) {Mul * = I;} sum + = Mul;} return sum = num;} JavaScript + HTML implement variable-digit Operations <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" The bottom value of the array. Num is the number of compliant daffodils. var a = 0, num = 0; // traverses all the numbers that match the number of parameter bits for (I = lownum; I <= highnum; I ++) {// RES is the sum of the N power of each digit of the N-bit number, the preset value is 0 var res = 0; // The number of each digit of the current I is decomposed, as shown in figure
Array var new_ I = I. tostring (). Split (""); for (a = 0; A <= valnum-1; A ++) {// get the number of daffodils
Rule value res = math. pow (parseint (new_ I [a]), valnum) + Res;} // determines the number of daffodils, if yes, the daffodils will be merged into the output queue if (RES = I) {num ++; res_str = res_str + "<br>" + Res ;}} // output queue if (valnum <3) {output = "you entered invalid digits! ";}Else {output = output + num + res_str ;}// output document. getelementbyid ("divnum "). innerhtml = output ;}</SCRIPT> Form form1 and add
Text Box text1 and
Command button command1 (2) modify the caption attribute of command1 to "Calculate and display" (3) add method sxh for form1 (4) modify the sxh Code as follows: para x X1 = int (X % 10) x2 = int (X/10) % 10x3 = int (x/100) % 100 if x = x1 ^ 3 + x2 ^ 3 + X3 ^ 3 return. t. else return. f. if endif (5) is command1
Click the event to write the following Event code: thisform. currentx = thisform. width/2 thisform. currenty = thisform. height/2 thisform. print ("the number of daffodils is:") for M = 100 to 999 thisform. text1.value = m sure = thisform. sxh (m) if sure =. t. thisform. print (STR (m, 4) + space (3) inkey (0.5) endif for latency = 1 to 20000 Yiru = 2008610029 endfor this. enabled =. f. QBASICNumbers of daffodils between 1-999999 CLs for I = 1 to 999999 e $ = STR $ (I) A $ = mid (E $, 1, 1) B $ = mid (E $, 2, 1) C $ = mid (E $, 3, 1) d $ = mid (E $, 4, 1) A = Val (A $) and B = Val (B $) and c = Val (C $) and D = Val (d $) if I = a ^ 4 + B ^ 4 + C ^ 4 + d ^ 4 then print I; next I end Pb implementation method (only 3 digits) int S,, b, C for S = 100 to 999 A = INTEGER (S/100) B = INTEGER (S-A * 100)/10) C = s-INTEGER (S/10) * 10 if S = a ^ 3 + B ^ 3 + C ^ 3 then MessageBox ("", S) E Method for implementing nd if next ActionScript (only three digits are counted) var N: int; var M: int; For (var I: Int = 1; I <= 9; I ++) {for (var j: Int = 1; I <= 9; j ++) {for (var k: Int = 1; I <= 9; k ++) {M = I * 100 + J * 10 + k; If (M = I * I + J * j + K * K) {n ++; trace (m) ;}}} method implemented by Delphi (100-999) var A, B, C, D: integer; begin for: = 100 to 999 do begin B: = a div 100; C: = A Div 10 mod 10; D: = a mod 10; if B * B * B + C * C + D * D = A then memo1.lines. add (inttostr (A) End End; methods implemented in MATLAB (100-999) for M = 100: 999 M1 = fix (M/100); M2 = REM (fix (M/10 ), 10); M3 = REM (M, 10); If M = m1 ^ 3 + m2 ^ 3 + m3 ^ 3 disp (m) end or: mathematica implementation method (uncertain number of digits) n = input ["enter a natural number greater than 2 N:"]; for [I = 10 ^ (n-1 ), I <10 ^ n-1, I ++, if [total [integerdigits ^ integerlength] = I, print] Add a C ++ algorithm # include <iostream> # include <cmath> using namespace STD; void main () {int A, B, C, E, f, G, double D; B = 1; F = 0; A = 100; E = 0; C = G = A;: Do {A/= 10; B ++;} while (A> 10); do {d = g % 10; g/= 10; E + = POW (D, b); F ++;} while (F! = B + 1); If (E = c) {cout <C <"\ n"; C ++; A = G = C; B = 1; f = 0; E = 0; goto a;} else {C ++; A = G = C; B = 1; F = 0; E = 0; goto A ;}} bash script to calculate the number within 100-999 #! /Bin/bash for (a = 1; A <10; A ++) do for (B = 0; B <10; B ++ )) do for (C = 0; C <10; C ++) do number1 = $ (A * 100 + B * 10 + C )) number2 = $ (A ** 3 + B ** 3 + C ** 3) If [$ number1-EQ $ number2]; then ECHO "found number $ number1" fi done easy language code to find the number of daffodils in a specified range. version 2. the subroutine extracts the numbers of daffodils, integers, and returns the numbers of daffodils in the range. If the range is too large, it will take a lot of time. the starting Number of the parameter, which is an integer. From this number, determine whether it is a daffodils. the end number of the parameter. It must be an integer greater than the start number. the parameter is saved to obtain the daffodils array, which is an integer and can be an empty array. number array of local variables, text type, "0 ". local variable daffodils, integer, "0 ". sum of local variables, integer type. local variable count, integer type. local variable count 2, integer type. if (start number> end number) is used to clear the Array (Save the array to get the number of daffodils), return (0 ). if it really ends. variable loop first (start number, end number, 1, count ). first of the count loop (take the text length (to the text (count), Count 2) join the member (number array, take the text in the middle (to the text (count), Count 2, 1 )) process event (). the end of the count loop (). first of the count loop (taking the number of members of the array (number array), Count 2) sum = sum + evaluate the power (to the value (number array [Count 2]), take the text length (to the text (count) to process the event (). the end of the count loop (). if you really (sum = count) join the member (daffodils, count ). if the processing event is completed (). variable loop tail () Save to get the number of daffodils array = return the number of daffodils (take the number of elements of the array (the number of daffodils) VB Code judge the number of daffodils dim () as integer n = inputbox ("enter an N-bit positive integer" & CHR (10) & "n greater than or equal to 3", "Daffodils", 153) dim I as integer m = Len (n) redim A (1 to m) as integer for I = 1 to m a (I) = Val (mid (n, I, 1) Next I for I = 1 to m s = a (I) ^ m + s next I if S = Val (N) then msgbox "is the number of daffodils" else msgbox "is not the number of daffodils" end if Add a C language method:// Daffodils # include <stdio. h> # include <math. h> int main () {unsigned long FR, to, n, m, K, sum; int nlog, numlen, brpoint = 1; // enter the value 100 ~ Printf ("input number range ('from' to '):"); scanf ("% lu", & FR, & to); For (n = fr; n <= to; n ++) {// calculate the number of bits of N nlog = log10 (n); numlen = floor (nlog) + 1; // The Power of the digits of N and M = N; sum = 0; while (M> 0) {k = m % 10; sum + = POW (K, numlen); M = m/10;} // if it is the number of daffodils, output. Five if (sum = N) {printf ("% 12lu", n); If (brpoint % 5 = 0) {printf ("\ n "); brpoint = 0;} brpoint ++;} return 0;} freebasic implement 100 ~ Daffodils between 999 for I = 100 to 999 A = I \ 100 B = I \ 10 mod 10 C = I mod 10 if a * A + B * B + C * c = I then print I: S = S + 1 next I print s sleep end Bat computing 100 ~ The number of daffodils between 999@ Echo off & setlocal enabledelayedexpansion for/L % A in (, 9) Do (for/L % B in (, 9) do (for/L % C in (0, 1, 9) Do (Set/A ver1 = % A % B % C, ver2 = % A * % A + % B * % B + % C * % C if! Ver1! =! Ver2! Echo! Ver1 !))) Pause> NUL Output daffodils in assembly language Stack segment StackDWP 256 DUP (?) Stack ends Data Segment Buf DB 3 DUP (?) Data ends code segment assume Cs: code, DS: data, SS: Stack FJ proc near push CX push DX push Si mov Si, 0 mov CX, 10 fj1: mov dx, 0 Div CX mov Buf [Si], DL Inc Si or ax, ax jnz fj1 mov ax, si pop DX pop cx ret fj endp print proc near push CX push DX mov dx,-1 Push DX mov CX, 10 P1: mov dx, 0 Div CX push DX or ax, ax jnz P1 P2: Pop dx cmp dx,-1 je P9 add DL, 30 h mov ah, 2 int 21 h JMP P2 P9: moV DL, 9 mov ah, 2 int 21 h pop DX pop cx ret print endp start: mov ax, data mov ds, ax mov BX, 100 S0: mov ax, BX call FJ mov CX, ax mov Si, 0 mov dx, 0 S1: mov Al, Buf [Si] Mul Buf [Si] Mul Buf [Si] add dx, ax Inc Si loop S1 CMP dx, bx jne S2 mov ax, DX call print S2: Inc bx cmp bx, 1000 JB S0 mov ah, 4ch int 21 H code ends end start

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.