Home » SQL & PL/SQL » SQL & PL/SQL » Armstrong numbers
Armstrong numbers [message #249275] Tue, 03 July 2007 21:20 Go to next message
sridhar.sangubhatla
Messages: 2
Registered: June 2007
Location: Madison
Junior Member
Hello,

Can one of you clarify as to why this problem arises?
Here is the code:

DECLARE
v_number number;
v_a int;
v_b1 int;
v_b int;
v_c int;

BEGIN
FOR v_number in 1..999
LOOP
v_a := v_number/100;
v_b1 := v_number/10;
v_b := MOD(v_b1,10);
v_c := MOD(v_number,10);
if (v_a**3 + v_b**3 + v_c**3 = v_a*100 + v_b*10 + v_c*1) then
dbms_output.put_line(v_a||v_b||v_c||'is an Armstrong number');
END IF;
END LOOP;
END;

Now, this code works perfectly and gives the desired results (1,153,
370, 371, 407 being the answers).
But, when the range of the numbers is changed :
FOR v_number in 100..999
one of the results (153) is not displayed.

More surprisingly, the code works when the first number is within the
range 1 to 53, i.e.,
FOR v_number in 1..999 gives 153 as one of the results,
FOR v_number in 53..999 gives 153 as one of the results, but
FOR v_number in 54..999 doesn't give 153 as the result.

Please help.
Thnx in advance.
Sridhar
Re: Armstrong numbers [message #249277 is a reply to message #249275] Tue, 03 July 2007 21:43 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>Please help.
Please help with what?
This has NOTHING to do with Oracle.
You could produce the same results on a programmable calculator; or with any scripting language.
You are NOT display numbers.
You are displaying concatenated STRINGS!
Re: Armstrong numbers [message #249278 is a reply to message #249275] Tue, 03 July 2007 21:44 Go to previous messageGo to next message
rleishman
Messages: 3728
Registered: October 2005
Location: Melbourne, Australia
Senior Member
153 is not an Armstrong number. The line is printing out for '53', not 153.

When v_number = 53:
- v_a = 1
- v_b = 5
- v_b1 = 5
- v_c = 3
- v_a**3 + v_b**3 + v_c**3 = 153
- v_a*100 + v_b*10 + v_c*1 = 153

Since the last two expressions are equal, a line is printed. But you don't print v_number (which would show 53), you print v_a**3 + v_b**3 + v_c**3. ie. 153

According to Wikipedia, you should actually be using
(v_a + v_b + v_3)**3.

Ross Leishman
Re: Armstrong numbers [message #249287 is a reply to message #249278] Tue, 03 July 2007 23:33 Go to previous messageGo to next message
caliguardo
Messages: 107
Registered: February 2007
Location: Chennai
Senior Member

Quote:
153 is not an Armstrong number.



Sum of cubes of 1,5 and 3 is 153. It is quite obvious that 153 is an armstrong number
Re: Armstrong numbers [message #249292 is a reply to message #249275] Wed, 04 July 2007 00:11 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
yes 153 is an Armstrong number
SQL> !cat an.sql
DECLARE
n1 int;
n2 int;
n3 int;

BEGIN
DBMS_OUTPUT.ENABLE(100000);
FOR n1 in 0..9 LOOP
   FOR n2 in 0..9 LOOP
      FOR n3 in 0..9 LOOP
         if ((n1*100 + n2*10 +n3) = (n1**3 + n2**3 + n3**3)) then
            dbms_output.put_line(n1||n2||n3||' is an Armstrong number');
         END IF;
      END LOOP;
   END LOOP;
END LOOP;
END;
/
Re: Armstrong numbers [message #249531 is a reply to message #249292] Wed, 04 July 2007 22:05 Go to previous messageGo to next message
rleishman
Messages: 3728
Registered: October 2005
Location: Melbourne, Australia
Senior Member
I stand corrected. What I should have said is that 153 is not a solution to the OPs program.

v_a := v_number/100;
=> 153/100
=> 1.53
=> 2 (cast to an integer)

[Updated on: Wed, 04 July 2007 22:10]

Report message to a moderator

Re: Armstrong numbers [message #250121 is a reply to message #249275] Sun, 08 July 2007 02:58 Go to previous messageGo to next message
Volder
Messages: 38
Registered: April 2007
Location: Russia
Member
we can get armstrong numbers without PL/SQL:

SQL> select num armstrong_numbers from
  2  (select * from dual connect by level<=1000
  3   model
  4    partition by (rownum rn)
  5    dimension by(0 dim)
  6    measures(rownum-1 num, 0 n)
  7     rules iterate(10) until (iteration_number+1=length(num[0]))
  8     (n[0]=n[0]+power(substr(num[0],iteration_number+1,1),length(num[0]))
  9      ))
 10      where num=n
 11      order by 1
 12  /

ARMSTRONG_NUMBERS
-----------------
                0
                1
                2
                3
                4
                5
                6
                7
                8
                9
              153
              370
              371
              407

14 rows selected

SQL>
Re: Armstrong numbers [message #250164 is a reply to message #250121] Sun, 08 July 2007 21:49 Go to previous messageGo to next message
rleishman
Messages: 3728
Registered: October 2005
Location: Melbourne, Australia
Senior Member
An excellent example of SQL gone mad.

This is exactly what you should NOT do, because noone who comes after you will ever understand what was intended.
Re: Armstrong numbers [message #663324 is a reply to message #249292] Tue, 30 May 2017 06:51 Go to previous messageGo to next message
ynkr999
Messages: 7
Registered: May 2017
Junior Member
hi Blackswan,

what you have written i am not understanding in this code,
if possible could you explain in detail please.

Regards
-------
Naveen
Re: Armstrong numbers [message #663325 is a reply to message #663324] Tue, 30 May 2017 07:19 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
ynkr999 wrote on Tue, 30 May 2017 04:51
hi Blackswan,

what you have written i am not understanding in this code,
if possible could you explain in detail please.

Regards
-------
Naveen
WOW!
This is a blast from the past.
To me the code is self-explanatory.
If you don't understand it, I won't waste my time trying to explain what is intuitively obvious to anyone who knows PL/SQL
Re: Armstrong numbers [message #663326 is a reply to message #249292] Tue, 30 May 2017 07:48 Go to previous messageGo to next message
ynkr999
Messages: 7
Registered: May 2017
Junior Member
hi Blackswan,

actually i am cnfused this ((n1**3 + n2**3 + n3**3) one only.I never seen before this type of code.
what it will returning i dont know.because i am just learning plsql.
regrads
----------
Naveen
Re: Armstrong numbers [message #663327 is a reply to message #663326] Tue, 30 May 2017 07:59 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
(n1**3) means that n1 is multiplied by itself 3 times
It is spoken as "n1 cubed" in English.
Re: Armstrong numbers [message #663328 is a reply to message #249292] Tue, 30 May 2017 08:20 Go to previous message
ynkr999
Messages: 7
Registered: May 2017
Junior Member

hi Blackswan,

Thankyou so much for taking the time to help with my question.

Regards
--------
Naveen
Previous Topic: Dependent Objects recursively
Next Topic: Query Db table with Key Value
Goto Forum:
  


Current Time: Fri Mar 29 03:53:38 CDT 2024