Project Name : prgmNUMSORT

Description : I'm looking for someone who would want to do a little coding. I want to make a program and need some help. Basically, you enter a number and it will tell you which of the following groups is falls into (Odious #'s Happy #'s Unhappy #'s Evil #'s Lucky #'s Economical #'s Wasteful #'s) Anyone reading this thread probably has no idea what those numbers are. Look them up cause that is what I did. anyways, I think to do this we have the program run the number through the bounds of the number groups and display that number group when the number falls in it. Hopefully, it is as simple as it sounds and if you want to help just reply.

Why : I compete in math competitions, and believe it or not they ask questions about what number groups a given number falls under. I also want to learn more about programming.

---------------------------------------------------------------------------------------------------------------------
Edit:
The first bit of code completed. This will knock out the Evil and Odious categories. Please Reply with improvements if you see anything that isn't very efficient. Thanks to everyone who helps me. This is a great learning opportunity.


Code:

Disp "Enter Number"
Input "To Sort",A
If A>0
Then
   16->D
   0->dim(L1
   While A>0
      A/2->B
      If int(B)=B
      0->C
      If int(B)!=B
      1->C
      iPart(B)->A
      C->L1(dim(L1)+1)
   End
   sum(L1)->E
   If remainder(E,2)=0
   Then
      Disp "Evil"
   Else
      Disp "Odious"
   End
End
Something like numbersaplenty.com.

Plenty of structural supports go into a program like this one. They need to be erected separately to some extent, so pick a couple that seem like easier lifting, start building those up, and if something gets snagged or misaligned along the way, describe what was being handled, where you wanted it, and point out the hitch. This helps us identify what tool(s) to lend, in lieu of offering an entire supply warehouse, which you've already got but might not know precisely what to do with.
I just finished making the second part which would detect happy and unhappy numbers but there is a mistake in it somewhere. I will post code after school.
Jasonpm wrote:
I just finished making the second part which would detect happy and unhappy numbers but there is a mistake in it somewhere. I will post code after school.

Just wondering, what is the point behind happy and unhappy numbers? It seems a bit weird
Remember, if something in an "If" statement equals 0, use not(condition) instead of condition=0, to save a byte of memory in your program. Also, ClrList [list 1] saves 1 byte rather than 0->dim(list 1).
As i mentioned before, I am working on coding the happy and unhappy number segment of my project. A quick explanation of happy and unhappy numbers follows.

Happy/Unhappy Numbers : Take the number and find the sum of the squares of each digit. Take this sum and repeat by finding the sum of the square of the digit. If this number eventually reachs one it is happy if it doesnt reach one it is unhappy.

Examples
Happy # = 7
7^2=49
4^2+9^2=97
9^2+7^2=130
1^2+3^2+0^2=10
1^2+0^2= 1 (Remember once we hit 1 we can call the number happy)

Unhappy # = 215
Based on the definition of an unhappy number, it seems hard to know when you should stop calculating since it never reaches one. What I have noticed is when a sum of the squares repeats, we can consider the number unlucky.
2^2+1^2+5^2=30
3^2+0^2=9
9^2=81
8^2+1^2=65
6^2+5^2=61
6^2+1^2=37
3^2+7^2=58
5^2+8^2=89
8^2+9^2=145
1^2+4^2+5^2=42
4^2+2^2=20
2^2+0^2=4
4^2=16
1^2+6^2=37 (Finally we have a repeat in the number 37)

I have found a few grey areas in my code that I could use some help on. My code will be posted below and I will try to make comments so that you may follow the code easily.

Code:

0->dim(L1
1+int(log(A->1 //Digits in A
For(J,1,I
   remainder(A,10)->L1(dim(L1)+1 //Sending each digit of A into a seperate list spot 
   int(A/10
End
0->dim(L2
For(K,1,20) //The loop repeats 20 times see reasing below at (A)
   sum(L1^2)->F
   1+int(log(F)->G //storing Sums of squares of digits and repeating see (B)
   0->dim(L1
   F->L2(dim(L2)+1)
   For(H,1,G)
      remainder(F,10)->L1(dim(L1)+1)
      int(F/10)->F
   End
End
If max(L2=1
Then
   Disp "Happy" //Cause The "1" Appeared
Else
   Disp "Unhappy" //Cause no "1" Appeared
   

(A) I don't know how to make a loop where it stops on one of the following events :
Stops when a 1 is stored in list2 or when a number is stored for the second time in list2
(B)Sums of Squares of digits stored in list2. in list 2 we are looking for repeats or the # "1"

Please help me out especially with making the loop constraints for when a number repeats or =1
Checking for 1 is easy. For repeats you have two options:

* Store all previously seen values in a list; if the new value is in the list you have a repeat. A list L2 contains a value N if max(L2=N.

* Use the Pollard cycle finding algorithm, which would be a good challenge to code. It's more complicated but only needs to store 2 values.
lirtosiast wrote:

* Store all previously seen values in a list; if the new value is in the list you have a repeat. A list L2 contains a value N if max(L2=N.


Thanks for the advice I will try this out. I assume you do one as follows: max(L2=1
Finished next bit of code. If anyone wants to optimize it, knock yourselves out. I appreciate everyone here.

This is all of the code merged together. This will tell you if a number is Evil Odious Happy ,and/or Unhappy


Code:

Disp "Enter Number"
Input "To Sort",A
A->Z
If A>0
Then
   16->D
   0->dim(L1
   While A>0
      A/2->B
      If int(B)=B
      0->C
      If int(B)!=B
      1->C
      iPart(B)->A
      C->L1(dim(L1)+1)
   End
   sum(L1)->E
   If remainder(E,2)=0
   Then
      Disp "Evil"
   Else
      Disp "Odious"
   End
End
0->dim(L1
1+int(log(Z->1
For(J,1,I
   remainder(Z,10)->L1(dim(L1)+1 
   int(Z/10
End
1->dim(L2
0->L2(dim(L2)+1)
Repeat max(L2=1 or max(L3=1
   ClrList L3
   sum(L1^2)->F
   1+int(log(F)->G
   0->dim(L1
   F=L2
   Ans->L3
   F->L2(dim(L2)+1)
   For(H,1,G)
      remainder(F,10)->L1(dim(L1)+1)
      int(F/10)->F
   End
End
If max(L2=1
Then
   Disp "Happy"
If max(L3=1
Then
   Disp "Unhappy"
End
Here you go, although it only says HAPPY/UNHAPPY:


Code:
{0->L1
Input A:A
Repeat max(L1=Ans) or Ans=1
   Ans->L1(1+dim(L1
   int(10fPart(Ans10^(~seq(A,A,1,14
   sum(Ans^^2
End
If Ans=1
Disp "HAPPY
If Ans!=1
"UNHAPPY


Probably could be optimized more, but meh
I had much the same! For a speed boost, one could prepare

Code:
:10^(-cumSum(binomcdf(13,0→L₂

before the input, then smartly place L₂ within the loop.

For the reveal, I used:

Code:
:sub("UNHAPPY  ",1+2(Ans=1),7

Note that there are two spaces at the end of the string.

If that's somehow undesirable, here's a short fix:

Code:
:1+2(Ans=1
:sub("UNHAPPY",Ans,8-Ans


EDIT: Was able to shrink 1+2(Ans=1) down to int(3^Ans⁻¹) in both cases thanks to lirtosiast's light prodding, but I will leave the former up as a case study.
Thanks for all of the help, but where did the evil and odious part of the program go?

A quick explanation of evil and odious:

Odious:
When changed to binary, there is an odd number of ones.

Evil:
When changed to binary, there is an even number of ones.
Quote:
Thanks for all of the help, but where did the evil and odious part of the program go?

Alright, then. First approach:

Code:
:"EVIL
:If fPart(sum(fPart(.5int(A/2^seq(A,A,0,round(log(A)/log(2
:"ODIOUS
:Ans


Which is to say:
  1. Assume one answer
    1. Divide by sufficiently many powers of 2 while taking only integer parts
    2. Add together the parities of these (as all/most of their one-halves will cancel out)
    3. Ask whether there was a 0.5 left over
  2. If so, switch answers
  3. Show answer

Next up is:

Code:
:A
:While Ans
:.5Ans→B
:iPart(Anscos(2πfPart(Ans
:End
:"EVIL
:If B>0
:"ODIOUS
:Ans

This keeps on dividing by 2, switching the sign with each change of parity, then asks in the end whether it's positive or negative.

These two programs have equal footing both space-wise and speed-wise! The only issue is with the former not handling 0 correctly, which can be amended with:

Code:
…round(log(2)⁻¹log(A+not(A
I just stumbled across this Omnimaga thread which has an even more optimized TI-BASIC version of HAPPY/UNHAPPY (thanks to calc84maniac!). I'm just putting it here for your information Smile
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement