Qbasic problem! Please help!

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
James
Newbie
Posts: 2
Joined: Sun May 26, 2013 3:48 am

Qbasic problem! Please help!

Post by James »

OK, I think QBasic, is an interesting programming language... I am only just a starter though, so I have some difficulties. Yesterday I saw a really interesting problem: "Number 1634 is really interesting, because it can be expressed as 1634 = 1^4 + 6^4 + 3^4 + 4^4. Write the program that can find other numbers like 1634"
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Sounds like homework to me. I'll let it slide though if you post some attempts at a solution.

You will need to scan the digits as text and convert them to numerical values to work with the exponent values then convert that value back to see if they match.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
James
Newbie
Posts: 2
Joined: Sun May 26, 2013 3:48 am

Post by James »

burger2227 wrote:Sounds like homework to me. I'll let it slide though if you post some attempts at a solution.

You will need to scan the digits as text and convert them to numerical values to work with the exponent values then convert that value back to see if they match.
well I tried this...
IF a*1000 + b*100 + c*10 + d = a*a*a*a + b*b*b*b + c*c*c*c + d*d*d*d THEN
PRINT a*1000+b*100+c+10+d
END IF
But, I just get 0 as an answer!
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

I'd assume that the exponent is based on the number of digits in the number. Four digit numbers would be an exponent of 4.

You can use ^ to create an actual exponential value such as a = 4 ^ 4

First you need to convert the test number to a string to find each digit.

testnumber$ = LTRIM$(STR$(number)) 'removes leading space

exponent = LEN(testnumber$)

FOR digit = 1 TO exponent
'use MID$ to find each digit of the test number, VAL it and make the exponent value and add it to a total.
NEXT

Then compare the total to the original value. You would need to test a range of numbers like from 1000 to 9999.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Jack002
Newbie
Posts: 5
Joined: Wed Jul 10, 2013 1:28 pm

Re: Qbasic problem! Please help!

Post by Jack002 »

Hi, I'm Jack T. New here, not to QuickBasic.

I see this post was from last MAY, so warning, its a reply to an old post.

I see this might be from a homework assignment, given the age of the question, I think we're ok now. Its safe.

I wanted to try out this puzzle, how many cases are true from 1000 to 9999 where the first digit^4 + second^4 + third^4 + fourth^4 = itself? (Ignoring the cases of numbers not of 4 dec places) This is what I came up with

Code: Select all

'test to see if a number's digits in the form
' a^4 + b^4 + c^4 + d^4 = itself.
'in the range from 1000-9999

FOR a = 1000 TO 9999
    t1 = VAL(MID$(STR$(a), 2, 1))
    t2 = VAL(MID$(STR$(a), 3, 1))
    t3 = VAL(MID$(STR$(a), 4, 1))
    t4 = VAL(MID$(STR$(a), 5, 1))
    IF t1 ^ 4 + t2 ^ 4 + t3 ^ 4 + t4 ^ 4 = a THEN PRINT a
NEXT a
Only three numbers qualify.

Jack T.

PS: If you ever saw "Numberphile" in YouTube, this is right up their alley. Check it out.
Post Reply